formattedvalue.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #ifndef __FORMATTEDVALUE_H__
  4. #define __FORMATTEDVALUE_H__
  5. #include "unicode/utypes.h"
  6. #if U_SHOW_CPLUSPLUS_API
  7. #if !UCONFIG_NO_FORMATTING
  8. #include "unicode/appendable.h"
  9. #include "unicode/fpositer.h"
  10. #include "unicode/unistr.h"
  11. #include "unicode/uformattedvalue.h"
  12. U_NAMESPACE_BEGIN
  13. /**
  14. * \file
  15. * \brief C++ API: Abstract operations for localized strings.
  16. *
  17. * This file contains declarations for classes that deal with formatted strings. A number
  18. * of APIs throughout ICU use these classes for expressing their localized output.
  19. */
  20. /**
  21. * Represents a span of a string containing a given field.
  22. *
  23. * This class differs from FieldPosition in the following ways:
  24. *
  25. * 1. It has information on the field category.
  26. * 2. It allows you to set constraints to use when iterating over field positions.
  27. * 3. It is used for the newer FormattedValue APIs.
  28. *
  29. * This class is not intended for public subclassing.
  30. *
  31. * @stable ICU 64
  32. */
  33. class U_I18N_API ConstrainedFieldPosition : public UMemory {
  34. public:
  35. /**
  36. * Initializes a ConstrainedFieldPosition.
  37. *
  38. * By default, the ConstrainedFieldPosition has no iteration constraints.
  39. *
  40. * @stable ICU 64
  41. */
  42. ConstrainedFieldPosition();
  43. /** @stable ICU 64 */
  44. ~ConstrainedFieldPosition();
  45. /**
  46. * Resets this ConstrainedFieldPosition to its initial state, as if it were newly created:
  47. *
  48. * - Removes any constraints that may have been set on the instance.
  49. * - Resets the iteration position.
  50. *
  51. * @stable ICU 64
  52. */
  53. void reset();
  54. /**
  55. * Sets a constraint on the field category.
  56. *
  57. * When this instance of ConstrainedFieldPosition is passed to FormattedValue#nextPosition,
  58. * positions are skipped unless they have the given category.
  59. *
  60. * Any previously set constraints are cleared.
  61. *
  62. * For example, to loop over only the number-related fields:
  63. *
  64. * ConstrainedFieldPosition cfpos;
  65. * cfpos.constrainCategory(UFIELDCATEGORY_NUMBER_FORMAT);
  66. * while (fmtval.nextPosition(cfpos, status)) {
  67. * // handle the number-related field position
  68. * }
  69. *
  70. * Changing the constraint while in the middle of iterating over a FormattedValue
  71. * does not generally have well-defined behavior.
  72. *
  73. * @param category The field category to fix when iterating.
  74. * @stable ICU 64
  75. */
  76. void constrainCategory(int32_t category);
  77. /**
  78. * Sets a constraint on the category and field.
  79. *
  80. * When this instance of ConstrainedFieldPosition is passed to FormattedValue#nextPosition,
  81. * positions are skipped unless they have the given category and field.
  82. *
  83. * Any previously set constraints are cleared.
  84. *
  85. * For example, to loop over all grouping separators:
  86. *
  87. * ConstrainedFieldPosition cfpos;
  88. * cfpos.constrainField(UFIELDCATEGORY_NUMBER_FORMAT, UNUM_GROUPING_SEPARATOR_FIELD);
  89. * while (fmtval.nextPosition(cfpos, status)) {
  90. * // handle the grouping separator position
  91. * }
  92. *
  93. * Changing the constraint while in the middle of iterating over a FormattedValue
  94. * does not generally have well-defined behavior.
  95. *
  96. * @param category The field category to fix when iterating.
  97. * @param field The field to fix when iterating.
  98. * @stable ICU 64
  99. */
  100. void constrainField(int32_t category, int32_t field);
  101. /**
  102. * Gets the field category for the current position.
  103. *
  104. * The return value is well-defined only after
  105. * FormattedValue#nextPosition returns true.
  106. *
  107. * @return The field category saved in the instance.
  108. * @stable ICU 64
  109. */
  110. inline int32_t getCategory() const {
  111. return fCategory;
  112. }
  113. /**
  114. * Gets the field for the current position.
  115. *
  116. * The return value is well-defined only after
  117. * FormattedValue#nextPosition returns true.
  118. *
  119. * @return The field saved in the instance.
  120. * @stable ICU 64
  121. */
  122. inline int32_t getField() const {
  123. return fField;
  124. }
  125. /**
  126. * Gets the INCLUSIVE start index for the current position.
  127. *
  128. * The return value is well-defined only after FormattedValue#nextPosition returns true.
  129. *
  130. * @return The start index saved in the instance.
  131. * @stable ICU 64
  132. */
  133. inline int32_t getStart() const {
  134. return fStart;
  135. }
  136. /**
  137. * Gets the EXCLUSIVE end index stored for the current position.
  138. *
  139. * The return value is well-defined only after FormattedValue#nextPosition returns true.
  140. *
  141. * @return The end index saved in the instance.
  142. * @stable ICU 64
  143. */
  144. inline int32_t getLimit() const {
  145. return fLimit;
  146. }
  147. ////////////////////////////////////////////////////////////////////
  148. //// The following methods are for FormattedValue implementers; ////
  149. //// most users can ignore them. ////
  150. ////////////////////////////////////////////////////////////////////
  151. /**
  152. * Gets an int64 that FormattedValue implementations may use for storage.
  153. *
  154. * The initial value is zero.
  155. *
  156. * Users of FormattedValue should not need to call this method.
  157. *
  158. * @return The current iteration context from {@link #setInt64IterationContext}.
  159. * @stable ICU 64
  160. */
  161. inline int64_t getInt64IterationContext() const {
  162. return fContext;
  163. }
  164. /**
  165. * Sets an int64 that FormattedValue implementations may use for storage.
  166. *
  167. * Intended to be used by FormattedValue implementations.
  168. *
  169. * @param context The new iteration context.
  170. * @stable ICU 64
  171. */
  172. void setInt64IterationContext(int64_t context);
  173. /**
  174. * Determines whether a given field should be included given the
  175. * constraints.
  176. *
  177. * Intended to be used by FormattedValue implementations.
  178. *
  179. * @param category The category to test.
  180. * @param field The field to test.
  181. * @stable ICU 64
  182. */
  183. UBool matchesField(int32_t category, int32_t field) const;
  184. /**
  185. * Sets new values for the primary public getters.
  186. *
  187. * Intended to be used by FormattedValue implementations.
  188. *
  189. * It is up to the implementation to ensure that the user-requested
  190. * constraints are satisfied. This method does not check!
  191. *
  192. * @param category The new field category.
  193. * @param field The new field.
  194. * @param start The new inclusive start index.
  195. * @param limit The new exclusive end index.
  196. * @stable ICU 64
  197. */
  198. void setState(
  199. int32_t category,
  200. int32_t field,
  201. int32_t start,
  202. int32_t limit);
  203. private:
  204. int64_t fContext = 0LL;
  205. int32_t fField = 0;
  206. int32_t fStart = 0;
  207. int32_t fLimit = 0;
  208. int32_t fCategory = UFIELD_CATEGORY_UNDEFINED;
  209. int8_t fConstraint = 0;
  210. };
  211. /**
  212. * An abstract formatted value: a string with associated field attributes.
  213. * Many formatters format to classes implementing FormattedValue.
  214. *
  215. * @stable ICU 64
  216. */
  217. class U_I18N_API FormattedValue /* not : public UObject because this is an interface/mixin class */ {
  218. public:
  219. /** @stable ICU 64 */
  220. virtual ~FormattedValue();
  221. /**
  222. * Returns the formatted string as a self-contained UnicodeString.
  223. *
  224. * If you need the string within the current scope only, consider #toTempString.
  225. *
  226. * @param status Set if an error occurs.
  227. * @return a UnicodeString containing the formatted string.
  228. *
  229. * @stable ICU 64
  230. */
  231. virtual UnicodeString toString(UErrorCode& status) const = 0;
  232. /**
  233. * Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
  234. *
  235. * The return value is valid only as long as this FormattedValue is present and unchanged in
  236. * memory. If you need the string outside the current scope, consider #toString.
  237. *
  238. * The buffer returned by calling UnicodeString#getBuffer() on the return value is
  239. * guaranteed to be NUL-terminated.
  240. *
  241. * @param status Set if an error occurs.
  242. * @return a temporary UnicodeString containing the formatted string.
  243. *
  244. * @stable ICU 64
  245. */
  246. virtual UnicodeString toTempString(UErrorCode& status) const = 0;
  247. /**
  248. * Appends the formatted string to an Appendable.
  249. *
  250. * @param appendable
  251. * The Appendable to which to append the string output.
  252. * @param status Set if an error occurs.
  253. * @return The same Appendable, for chaining.
  254. *
  255. * @stable ICU 64
  256. * @see Appendable
  257. */
  258. virtual Appendable& appendTo(Appendable& appendable, UErrorCode& status) const = 0;
  259. /**
  260. * Iterates over field positions in the FormattedValue. This lets you determine the position
  261. * of specific types of substrings, like a month or a decimal separator.
  262. *
  263. * To loop over all field positions:
  264. *
  265. * ConstrainedFieldPosition cfpos;
  266. * while (fmtval.nextPosition(cfpos, status)) {
  267. * // handle the field position; get information from cfpos
  268. * }
  269. *
  270. * @param cfpos
  271. * The object used for iteration state. This can provide constraints to iterate over
  272. * only one specific category or field;
  273. * see ConstrainedFieldPosition#constrainCategory
  274. * and ConstrainedFieldPosition#constrainField.
  275. * @param status Set if an error occurs.
  276. * @return true if a new occurrence of the field was found;
  277. * false otherwise or if an error was set.
  278. *
  279. * @stable ICU 64
  280. */
  281. virtual UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const = 0;
  282. };
  283. U_NAMESPACE_END
  284. #endif /* #if !UCONFIG_NO_FORMATTING */
  285. #endif /* U_SHOW_CPLUSPLUS_API */
  286. #endif // __FORMATTEDVALUE_H__