rep.h 9.5 KB

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