rep.h 9.4 KB

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