Coverage Report

Created: 2026-10-31 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/common/unicode/rep.h
Line
Count
Source
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
**************************************************************************
5
* Copyright (C) 1999-2012, International Business Machines Corporation and
6
* others. All Rights Reserved.
7
**************************************************************************
8
*   Date        Name        Description
9
*   11/17/99    aliu        Creation.  Ported from java.  Modified to
10
*                           match current UnicodeString API.  Forced
11
*                           to use name "handleReplaceBetween" because
12
*                           of existing methods in UnicodeString.
13
**************************************************************************
14
*/
15
16
#ifndef REP_H
17
#define REP_H
18
19
#include "unicode/utypes.h"
20
21
#if U_SHOW_CPLUSPLUS_API
22
23
#include "unicode/uobject.h"
24
25
/**
26
 * \file 
27
 * \brief C++ API: Replaceable String
28
 */
29
 
30
U_NAMESPACE_BEGIN
31
32
class UnicodeString;
33
34
/**
35
 * <code>Replaceable</code> is an abstract base class representing a
36
 * string of characters that supports the replacement of a range of
37
 * itself with a new string of characters.  It is used by APIs that
38
 * change a piece of text while retaining metadata.  Metadata is data
39
 * other than the Unicode characters returned by char32At().  One
40
 * example of metadata is style attributes; another is an edit
41
 * history, marking each character with an author and revision number.
42
 *
43
 * <p>An implicit aspect of the <code>Replaceable</code> API is that
44
 * during a replace operation, new characters take on the metadata of
45
 * the old characters.  For example, if the string "the <b>bold</b>
46
 * font" has range (4, 8) replaced with "strong", then it becomes "the
47
 * <b>strong</b> font".
48
 *
49
 * <p><code>Replaceable</code> specifies ranges using a start
50
 * offset and a limit offset.  The range of characters thus specified
51
 * includes the characters at offset start..limit-1.  That is, the
52
 * start offset is inclusive, and the limit offset is exclusive.
53
 *
54
 * <p><code>Replaceable</code> also includes API to access characters
55
 * in the string: <code>length()</code>, <code>charAt()</code>,
56
 * <code>char32At()</code>, and <code>extractBetween()</code>.
57
 *
58
 * <p>For a subclass to support metadata, typical behavior of
59
 * <code>replace()</code> is the following:
60
 * <ul>
61
 *   <li>Set the metadata of the new text to the metadata of the first
62
 *   character replaced</li>
63
 *   <li>If no characters are replaced, use the metadata of the
64
 *   previous character</li>
65
 *   <li>If there is no previous character (i.e. start == 0), use the
66
 *   following character</li>
67
 *   <li>If there is no following character (i.e. the replaceable was
68
 *   empty), use default metadata.<br>
69
 *   <li>If the code point U+FFFF is seen, it should be interpreted as
70
 *   a special marker having no metadata<li>
71
 *   </li>
72
 * </ul>
73
 * If this is not the behavior, the subclass should document any differences.
74
 * @author Alan Liu
75
 * @stable ICU 2.0
76
 */
77
class U_COMMON_API Replaceable : public UObject {
78
79
public:
80
    /**
81
     * Destructor.
82
     * @stable ICU 2.0
83
     */
84
    virtual ~Replaceable();
85
86
    /**
87
     * Returns the number of 16-bit code units in the text.
88
     * @return number of 16-bit code units in text
89
     * @stable ICU 1.8
90
     */ 
91
    inline int32_t length() const;
92
93
    /**
94
     * Returns the 16-bit code unit at the given offset into the text.
95
     * @param offset an integer between 0 and <code>length()</code>-1
96
     * inclusive
97
     * @return 16-bit code unit of text at given offset
98
     * @stable ICU 1.8
99
     */
100
    inline char16_t charAt(int32_t offset) const;
101
102
    /**
103
     * Returns the 32-bit code point at the given 16-bit offset into
104
     * the text.  This assumes the text is stored as 16-bit code units
105
     * with surrogate pairs intermixed.  If the offset of a leading or
106
     * trailing code unit of a surrogate pair is given, return the
107
     * code point of the surrogate pair.
108
     *
109
     * @param offset an integer between 0 and <code>length()</code>-1
110
     * inclusive
111
     * @return 32-bit code point of text at given offset
112
     * @stable ICU 1.8
113
     */
114
    inline UChar32 char32At(int32_t offset) const;
115
116
    /**
117
     * Copies characters in the range [<tt>start</tt>, <tt>limit</tt>) 
118
     * into the UnicodeString <tt>target</tt>.
119
     * @param start offset of first character which will be copied
120
     * @param limit offset immediately following the last character to
121
     * be copied
122
     * @param target UnicodeString into which to copy characters.
123
     * @stable ICU 2.1
124
     */
125
    virtual void extractBetween(int32_t start,
126
                                int32_t limit,
127
                                UnicodeString& target) const = 0;
128
129
    /**
130
     * Replaces a substring of this object with the given text.  If the
131
     * characters being replaced have metadata, the new characters
132
     * that replace them should be given the same metadata.
133
     *
134
     * <p>Subclasses must ensure that if the text between start and
135
     * limit is equal to the replacement text, that replace has no
136
     * effect. That is, any metadata
137
     * should be unaffected. In addition, subclasses are encouraged to
138
     * check for initial and trailing identical characters, and make a
139
     * smaller replacement if possible. This will preserve as much
140
     * metadata as possible.
141
     * @param start the beginning index, inclusive; <code>0 <= start
142
     * <= limit</code>.
143
     * @param limit the ending index, exclusive; <code>start <= limit
144
     * <= length()</code>.
145
     * @param text the text to replace characters <code>start</code>
146
     * to <code>limit - 1</code> 
147
     * @stable ICU 2.0
148
     */
149
    virtual void handleReplaceBetween(int32_t start,
150
                                      int32_t limit,
151
                                      const UnicodeString& text) = 0;
152
    // Note: All other methods in this class take the names of
153
    // existing UnicodeString methods.  This method is the exception.
154
    // It is named differently because all replace methods of
155
    // UnicodeString return a UnicodeString&.  The 'between' is
156
    // required in order to conform to the UnicodeString naming
157
    // convention; API taking start/length are named <operation>, and
158
    // those taking start/limit are named <operationBetween>.  The
159
    // 'handle' is added because 'replaceBetween' and
160
    // 'doReplaceBetween' are already taken.
161
162
    /**
163
     * Copies a substring of this object, retaining metadata.
164
     * This method is used to duplicate or reorder substrings.
165
     * The destination index must not overlap the source range.
166
     * 
167
     * @param start the beginning index, inclusive; <code>0 <= start <=
168
     * limit</code>.
169
     * @param limit the ending index, exclusive; <code>start <= limit <=
170
     * length()</code>.
171
     * @param dest the destination index.  The characters from
172
     * <code>start..limit-1</code> will be copied to <code>dest</code>.
173
     * Implementations of this method may assume that <code>dest <= start ||
174
     * dest >= limit</code>.
175
     * @stable ICU 2.0
176
     */
177
    virtual void copy(int32_t start, int32_t limit, int32_t dest) = 0;
178
179
    /**
180
     * Returns true if this object contains metadata.  If a
181
     * Replaceable object has metadata, calls to the Replaceable API
182
     * must be made so as to preserve metadata.  If it does not, calls
183
     * to the Replaceable API may be optimized to improve performance.
184
     * The default implementation returns true.
185
     * @return true if this object contains metadata
186
     * @stable ICU 2.2
187
     */
188
    virtual UBool hasMetaData() const;
189
190
    /**
191
     * Clone this object, an instance of a subclass of Replaceable.
192
     * Clones can be used concurrently in multiple threads.
193
     * If a subclass does not implement clone(), or if an error occurs,
194
     * then nullptr is returned.
195
     * The caller must delete the clone.
196
     *
197
     * @return a clone of this object
198
     *
199
     * @see getDynamicClassID
200
     * @stable ICU 2.6
201
     */
202
    virtual Replaceable *clone() const;
203
204
protected:
205
206
    /**
207
     * Default constructor.
208
     * @stable ICU 2.4
209
     */
210
    inline Replaceable();
211
212
    /*
213
     * Assignment operator not declared. The compiler will provide one
214
     * which does nothing since this class does not contain any data members.
215
     * API/code coverage may show the assignment operator as present and
216
     * untested - ignore.
217
     * Subclasses need this assignment operator if they use compiler-provided
218
     * assignment operators of their own. An alternative to not declaring one
219
     * here would be to declare and empty-implement a protected or public one.
220
    Replaceable &Replaceable::operator=(const Replaceable &);
221
     */
222
223
    /**
224
     * Virtual version of length().
225
     * @stable ICU 2.4
226
     */ 
227
    virtual int32_t getLength() const = 0;
228
229
    /**
230
     * Virtual version of charAt().
231
     * @stable ICU 2.4
232
     */
233
    virtual char16_t getCharAt(int32_t offset) const = 0;
234
235
    /**
236
     * Virtual version of char32At().
237
     * @stable ICU 2.4
238
     */
239
    virtual UChar32 getChar32At(int32_t offset) const = 0;
240
};
241
242
1.96G
inline Replaceable::Replaceable() {}
243
244
inline int32_t
245
0
Replaceable::length() const {
246
0
    return getLength();
247
0
}
248
249
inline char16_t
250
0
Replaceable::charAt(int32_t offset) const {
251
0
    return getCharAt(offset);
252
0
}
253
254
inline UChar32
255
0
Replaceable::char32At(int32_t offset) const {
256
0
    return getChar32At(offset);
257
0
}
258
259
// There is no rep.cpp, see unistr.cpp for Replaceable function implementations.
260
261
U_NAMESPACE_END
262
263
#endif /* U_SHOW_CPLUSPLUS_API */
264
265
#endif