uformattedvalue.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #ifndef __UFORMATTEDVALUE_H__
  4. #define __UFORMATTEDVALUE_H__
  5. #include "unicode/utypes.h"
  6. #if !UCONFIG_NO_FORMATTING
  7. #include "unicode/ufieldpositer.h"
  8. /**
  9. * \file
  10. * \brief C API: Abstract operations for localized strings.
  11. *
  12. * This file contains declarations for classes that deal with formatted strings. A number
  13. * of APIs throughout ICU use these classes for expressing their localized output.
  14. */
  15. /**
  16. * All possible field categories in ICU. Every entry in this enum corresponds
  17. * to another enum that exists in ICU.
  18. *
  19. * In the APIs that take a UFieldCategory, an int32_t type is used. Field
  20. * categories having any of the top four bits turned on are reserved as
  21. * private-use for external APIs implementing FormattedValue. This means that
  22. * categories 2^28 and higher or below zero (with the highest bit turned on)
  23. * are private-use and will not be used by ICU in the future.
  24. *
  25. * @stable ICU 64
  26. */
  27. typedef enum UFieldCategory {
  28. /**
  29. * For an undefined field category.
  30. *
  31. * @stable ICU 64
  32. */
  33. UFIELD_CATEGORY_UNDEFINED = 0,
  34. /**
  35. * For fields in UDateFormatField (udat.h), from ICU 3.0.
  36. *
  37. * @stable ICU 64
  38. */
  39. UFIELD_CATEGORY_DATE,
  40. /**
  41. * For fields in UNumberFormatFields (unum.h), from ICU 49.
  42. *
  43. * @stable ICU 64
  44. */
  45. UFIELD_CATEGORY_NUMBER,
  46. /**
  47. * For fields in UListFormatterField (ulistformatter.h), from ICU 63.
  48. *
  49. * @stable ICU 64
  50. */
  51. UFIELD_CATEGORY_LIST,
  52. /**
  53. * For fields in URelativeDateTimeFormatterField (ureldatefmt.h), from ICU 64.
  54. *
  55. * @stable ICU 64
  56. */
  57. UFIELD_CATEGORY_RELATIVE_DATETIME,
  58. /**
  59. * Reserved for possible future fields in UDateIntervalFormatField.
  60. *
  61. * @internal
  62. */
  63. UFIELD_CATEGORY_DATE_INTERVAL,
  64. #ifndef U_HIDE_INTERNAL_API
  65. /** @internal */
  66. UFIELD_CATEGORY_COUNT,
  67. #endif /* U_HIDE_INTERNAL_API */
  68. /**
  69. * Category for spans in a list.
  70. *
  71. * @stable ICU 64
  72. */
  73. UFIELD_CATEGORY_LIST_SPAN = 0x1000 + UFIELD_CATEGORY_LIST,
  74. /**
  75. * Category for spans in a date interval.
  76. *
  77. * @stable ICU 64
  78. */
  79. UFIELD_CATEGORY_DATE_INTERVAL_SPAN = 0x1000 + UFIELD_CATEGORY_DATE_INTERVAL,
  80. /**
  81. * Category for spans in a number range.
  82. *
  83. * @stable ICU 69
  84. */
  85. UFIELD_CATEGORY_NUMBER_RANGE_SPAN = 0x1000 + UFIELD_CATEGORY_NUMBER,
  86. } UFieldCategory;
  87. struct UConstrainedFieldPosition;
  88. /**
  89. * Represents a span of a string containing a given field.
  90. *
  91. * This struct differs from UFieldPosition in the following ways:
  92. *
  93. * 1. It has information on the field category.
  94. * 2. It allows you to set constraints to use when iterating over field positions.
  95. * 3. It is used for the newer FormattedValue APIs.
  96. *
  97. * @stable ICU 64
  98. */
  99. typedef struct UConstrainedFieldPosition UConstrainedFieldPosition;
  100. /**
  101. * Creates a new UConstrainedFieldPosition.
  102. *
  103. * By default, the UConstrainedFieldPosition has no iteration constraints.
  104. *
  105. * @param ec Set if an error occurs.
  106. * @return The new object, or NULL if an error occurs.
  107. * @stable ICU 64
  108. */
  109. U_CAPI UConstrainedFieldPosition* U_EXPORT2
  110. ucfpos_open(UErrorCode* ec);
  111. /**
  112. * Resets a UConstrainedFieldPosition to its initial state, as if it were newly created.
  113. *
  114. * Removes any constraints that may have been set on the instance.
  115. *
  116. * @param ucfpos The instance of UConstrainedFieldPosition.
  117. * @param ec Set if an error occurs.
  118. * @stable ICU 64
  119. */
  120. U_CAPI void U_EXPORT2
  121. ucfpos_reset(
  122. UConstrainedFieldPosition* ucfpos,
  123. UErrorCode* ec);
  124. /**
  125. * Destroys a UConstrainedFieldPosition and releases its memory.
  126. *
  127. * @param ucfpos The instance of UConstrainedFieldPosition.
  128. * @stable ICU 64
  129. */
  130. U_CAPI void U_EXPORT2
  131. ucfpos_close(UConstrainedFieldPosition* ucfpos);
  132. /**
  133. * Sets a constraint on the field category.
  134. *
  135. * When this instance of UConstrainedFieldPosition is passed to ufmtval_nextPosition,
  136. * positions are skipped unless they have the given category.
  137. *
  138. * Any previously set constraints are cleared.
  139. *
  140. * For example, to loop over only the number-related fields:
  141. *
  142. * UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
  143. * ucfpos_constrainCategory(ucfpos, UFIELDCATEGORY_NUMBER_FORMAT, ec);
  144. * while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
  145. * // handle the number-related field position
  146. * }
  147. * ucfpos_close(ucfpos);
  148. *
  149. * Changing the constraint while in the middle of iterating over a FormattedValue
  150. * does not generally have well-defined behavior.
  151. *
  152. * @param ucfpos The instance of UConstrainedFieldPosition.
  153. * @param category The field category to fix when iterating.
  154. * @param ec Set if an error occurs.
  155. * @stable ICU 64
  156. */
  157. U_CAPI void U_EXPORT2
  158. ucfpos_constrainCategory(
  159. UConstrainedFieldPosition* ucfpos,
  160. int32_t category,
  161. UErrorCode* ec);
  162. /**
  163. * Sets a constraint on the category and field.
  164. *
  165. * When this instance of UConstrainedFieldPosition is passed to ufmtval_nextPosition,
  166. * positions are skipped unless they have the given category and field.
  167. *
  168. * Any previously set constraints are cleared.
  169. *
  170. * For example, to loop over all grouping separators:
  171. *
  172. * UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
  173. * ucfpos_constrainField(ucfpos, UFIELDCATEGORY_NUMBER_FORMAT, UNUM_GROUPING_SEPARATOR_FIELD, ec);
  174. * while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
  175. * // handle the grouping separator position
  176. * }
  177. * ucfpos_close(ucfpos);
  178. *
  179. * Changing the constraint while in the middle of iterating over a FormattedValue
  180. * does not generally have well-defined behavior.
  181. *
  182. * @param ucfpos The instance of UConstrainedFieldPosition.
  183. * @param category The field category to fix when iterating.
  184. * @param field The field to fix when iterating.
  185. * @param ec Set if an error occurs.
  186. * @stable ICU 64
  187. */
  188. U_CAPI void U_EXPORT2
  189. ucfpos_constrainField(
  190. UConstrainedFieldPosition* ucfpos,
  191. int32_t category,
  192. int32_t field,
  193. UErrorCode* ec);
  194. /**
  195. * Gets the field category for the current position.
  196. *
  197. * If a category or field constraint was set, this function returns the constrained
  198. * category. Otherwise, the return value is well-defined only after
  199. * ufmtval_nextPosition returns true.
  200. *
  201. * @param ucfpos The instance of UConstrainedFieldPosition.
  202. * @param ec Set if an error occurs.
  203. * @return The field category saved in the instance.
  204. * @stable ICU 64
  205. */
  206. U_CAPI int32_t U_EXPORT2
  207. ucfpos_getCategory(
  208. const UConstrainedFieldPosition* ucfpos,
  209. UErrorCode* ec);
  210. /**
  211. * Gets the field for the current position.
  212. *
  213. * If a field constraint was set, this function returns the constrained
  214. * field. Otherwise, the return value is well-defined only after
  215. * ufmtval_nextPosition returns true.
  216. *
  217. * @param ucfpos The instance of UConstrainedFieldPosition.
  218. * @param ec Set if an error occurs.
  219. * @return The field saved in the instance.
  220. * @stable ICU 64
  221. */
  222. U_CAPI int32_t U_EXPORT2
  223. ucfpos_getField(
  224. const UConstrainedFieldPosition* ucfpos,
  225. UErrorCode* ec);
  226. /**
  227. * Gets the INCLUSIVE start and EXCLUSIVE end index stored for the current position.
  228. *
  229. * The output values are well-defined only after ufmtval_nextPosition returns true.
  230. *
  231. * @param ucfpos The instance of UConstrainedFieldPosition.
  232. * @param pStart Set to the start index saved in the instance. Ignored if nullptr.
  233. * @param pLimit Set to the end index saved in the instance. Ignored if nullptr.
  234. * @param ec Set if an error occurs.
  235. * @stable ICU 64
  236. */
  237. U_CAPI void U_EXPORT2
  238. ucfpos_getIndexes(
  239. const UConstrainedFieldPosition* ucfpos,
  240. int32_t* pStart,
  241. int32_t* pLimit,
  242. UErrorCode* ec);
  243. /**
  244. * Gets an int64 that FormattedValue implementations may use for storage.
  245. *
  246. * The initial value is zero.
  247. *
  248. * Users of FormattedValue should not need to call this method.
  249. *
  250. * @param ucfpos The instance of UConstrainedFieldPosition.
  251. * @param ec Set if an error occurs.
  252. * @return The current iteration context from ucfpos_setInt64IterationContext.
  253. * @stable ICU 64
  254. */
  255. U_CAPI int64_t U_EXPORT2
  256. ucfpos_getInt64IterationContext(
  257. const UConstrainedFieldPosition* ucfpos,
  258. UErrorCode* ec);
  259. /**
  260. * Sets an int64 that FormattedValue implementations may use for storage.
  261. *
  262. * Intended to be used by FormattedValue implementations.
  263. *
  264. * @param ucfpos The instance of UConstrainedFieldPosition.
  265. * @param context The new iteration context.
  266. * @param ec Set if an error occurs.
  267. * @stable ICU 64
  268. */
  269. U_CAPI void U_EXPORT2
  270. ucfpos_setInt64IterationContext(
  271. UConstrainedFieldPosition* ucfpos,
  272. int64_t context,
  273. UErrorCode* ec);
  274. /**
  275. * Determines whether a given field should be included given the
  276. * constraints.
  277. *
  278. * Intended to be used by FormattedValue implementations.
  279. *
  280. * @param ucfpos The instance of UConstrainedFieldPosition.
  281. * @param category The category to test.
  282. * @param field The field to test.
  283. * @param ec Set if an error occurs.
  284. * @stable ICU 64
  285. */
  286. U_CAPI UBool U_EXPORT2
  287. ucfpos_matchesField(
  288. const UConstrainedFieldPosition* ucfpos,
  289. int32_t category,
  290. int32_t field,
  291. UErrorCode* ec);
  292. /**
  293. * Sets new values for the primary public getters.
  294. *
  295. * Intended to be used by FormattedValue implementations.
  296. *
  297. * It is up to the implementation to ensure that the user-requested
  298. * constraints are satisfied. This method does not check!
  299. *
  300. * @param ucfpos The instance of UConstrainedFieldPosition.
  301. * @param category The new field category.
  302. * @param field The new field.
  303. * @param start The new inclusive start index.
  304. * @param limit The new exclusive end index.
  305. * @param ec Set if an error occurs.
  306. * @stable ICU 64
  307. */
  308. U_CAPI void U_EXPORT2
  309. ucfpos_setState(
  310. UConstrainedFieldPosition* ucfpos,
  311. int32_t category,
  312. int32_t field,
  313. int32_t start,
  314. int32_t limit,
  315. UErrorCode* ec);
  316. struct UFormattedValue;
  317. /**
  318. * An abstract formatted value: a string with associated field attributes.
  319. * Many formatters format to types compatible with UFormattedValue.
  320. *
  321. * @stable ICU 64
  322. */
  323. typedef struct UFormattedValue UFormattedValue;
  324. /**
  325. * Returns a pointer to the formatted string. The pointer is owned by the UFormattedValue. The
  326. * return value is valid only as long as the UFormattedValue is present and unchanged in memory.
  327. *
  328. * The return value is NUL-terminated but could contain internal NULs.
  329. *
  330. * @param ufmtval
  331. * The object containing the formatted string and attributes.
  332. * @param pLength Output variable for the length of the string. Ignored if NULL.
  333. * @param ec Set if an error occurs.
  334. * @return A NUL-terminated char16 string owned by the UFormattedValue.
  335. * @stable ICU 64
  336. */
  337. U_CAPI const UChar* U_EXPORT2
  338. ufmtval_getString(
  339. const UFormattedValue* ufmtval,
  340. int32_t* pLength,
  341. UErrorCode* ec);
  342. /**
  343. * Iterates over field positions in the UFormattedValue. This lets you determine the position
  344. * of specific types of substrings, like a month or a decimal separator.
  345. *
  346. * To loop over all field positions:
  347. *
  348. * UConstrainedFieldPosition* ucfpos = ucfpos_open(ec);
  349. * while (ufmtval_nextPosition(ufmtval, ucfpos, ec)) {
  350. * // handle the field position; get information from ucfpos
  351. * }
  352. * ucfpos_close(ucfpos);
  353. *
  354. * @param ufmtval
  355. * The object containing the formatted string and attributes.
  356. * @param ucfpos
  357. * The object used for iteration state; can provide constraints to iterate over only
  358. * one specific category or field;
  359. * see ucfpos_constrainCategory
  360. * and ucfpos_constrainField.
  361. * @param ec Set if an error occurs.
  362. * @return true if another position was found; false otherwise.
  363. * @stable ICU 64
  364. */
  365. U_CAPI UBool U_EXPORT2
  366. ufmtval_nextPosition(
  367. const UFormattedValue* ufmtval,
  368. UConstrainedFieldPosition* ucfpos,
  369. UErrorCode* ec);
  370. #if U_SHOW_CPLUSPLUS_API
  371. U_NAMESPACE_BEGIN
  372. /**
  373. * \class LocalUConstrainedFieldPositionPointer
  374. * "Smart pointer" class; closes a UConstrainedFieldPosition via ucfpos_close().
  375. * For most methods see the LocalPointerBase base class.
  376. *
  377. * Usage:
  378. *
  379. * LocalUConstrainedFieldPositionPointer ucfpos(ucfpos_open(ec));
  380. * // no need to explicitly call ucfpos_close()
  381. *
  382. * @stable ICU 64
  383. */
  384. U_DEFINE_LOCAL_OPEN_POINTER(LocalUConstrainedFieldPositionPointer,
  385. UConstrainedFieldPosition,
  386. ucfpos_close);
  387. U_NAMESPACE_END
  388. #endif // U_SHOW_CPLUSPLUS_API
  389. #endif /* #if !UCONFIG_NO_FORMATTING */
  390. #endif // __UFORMATTEDVALUE_H__