rep.h 9.3 KB

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