ulistformatter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *****************************************************************************************
  5. * Copyright (C) 2015-2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *****************************************************************************************
  8. */
  9. #ifndef ULISTFORMATTER_H
  10. #define ULISTFORMATTER_H
  11. #include "unicode/utypes.h"
  12. #if !UCONFIG_NO_FORMATTING
  13. #include "unicode/uformattedvalue.h"
  14. #if U_SHOW_CPLUSPLUS_API
  15. #include "unicode/localpointer.h"
  16. #endif // U_SHOW_CPLUSPLUS_API
  17. /**
  18. * \file
  19. * \brief C API: Format a list in a locale-appropriate way.
  20. *
  21. * A UListFormatter is used to format a list of items in a locale-appropriate way,
  22. * using data from CLDR.
  23. * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted
  24. * as "Alice, Bob, Charlie, and Delta" in English.
  25. */
  26. /**
  27. * Opaque UListFormatter object for use in C
  28. * @stable ICU 55
  29. */
  30. struct UListFormatter;
  31. typedef struct UListFormatter UListFormatter; /**< C typedef for struct UListFormatter. @stable ICU 55 */
  32. struct UFormattedList;
  33. /**
  34. * Opaque struct to contain the results of a UListFormatter operation.
  35. * @stable ICU 64
  36. */
  37. typedef struct UFormattedList UFormattedList;
  38. /**
  39. * FieldPosition and UFieldPosition selectors for format fields
  40. * defined by ListFormatter.
  41. * @stable ICU 63
  42. */
  43. typedef enum UListFormatterField {
  44. /**
  45. * The literal text in the result which came from the resources.
  46. * @stable ICU 63
  47. */
  48. ULISTFMT_LITERAL_FIELD,
  49. /**
  50. * The element text in the result which came from the input strings.
  51. * @stable ICU 63
  52. */
  53. ULISTFMT_ELEMENT_FIELD
  54. } UListFormatterField;
  55. /**
  56. * Type of meaning expressed by the list.
  57. *
  58. * @stable ICU 67
  59. */
  60. typedef enum UListFormatterType {
  61. /**
  62. * Conjunction formatting, e.g. "Alice, Bob, Charlie, and Delta".
  63. *
  64. * @stable ICU 67
  65. */
  66. ULISTFMT_TYPE_AND,
  67. /**
  68. * Disjunction (or alternative, or simply one of) formatting, e.g.
  69. * "Alice, Bob, Charlie, or Delta".
  70. *
  71. * @stable ICU 67
  72. */
  73. ULISTFMT_TYPE_OR,
  74. /**
  75. * Formatting of a list of values with units, e.g. "5 pounds, 12 ounces".
  76. *
  77. * @stable ICU 67
  78. */
  79. ULISTFMT_TYPE_UNITS
  80. } UListFormatterType;
  81. /**
  82. * Verbosity level of the list patterns.
  83. *
  84. * @stable ICU 67
  85. */
  86. typedef enum UListFormatterWidth {
  87. /**
  88. * Use list formatting with full words (no abbreviations) when possible.
  89. *
  90. * @stable ICU 67
  91. */
  92. ULISTFMT_WIDTH_WIDE,
  93. /**
  94. * Use list formatting of typical length.
  95. * @stable ICU 67
  96. */
  97. ULISTFMT_WIDTH_SHORT,
  98. /**
  99. * Use list formatting of the shortest possible length.
  100. * @stable ICU 67
  101. */
  102. ULISTFMT_WIDTH_NARROW,
  103. } UListFormatterWidth;
  104. /**
  105. * Open a new UListFormatter object using the rules for a given locale.
  106. * The object will be initialized with AND type and WIDE width.
  107. *
  108. * @param locale
  109. * The locale whose rules should be used; may be NULL for
  110. * default locale.
  111. * @param status
  112. * A pointer to a standard ICU UErrorCode (input/output parameter).
  113. * Its input value must pass the U_SUCCESS() test, or else the
  114. * function returns immediately. The caller should check its output
  115. * value with U_FAILURE(), or use with function chaining (see User
  116. * Guide for details).
  117. * @return
  118. * A pointer to a UListFormatter object for the specified locale,
  119. * or NULL if an error occurred.
  120. * @stable ICU 55
  121. */
  122. U_CAPI UListFormatter* U_EXPORT2
  123. ulistfmt_open(const char* locale,
  124. UErrorCode* status);
  125. /**
  126. * Open a new UListFormatter object appropriate for the given locale, list type,
  127. * and style.
  128. *
  129. * @param locale
  130. * The locale whose rules should be used; may be NULL for
  131. * default locale.
  132. * @param type
  133. * The type of list formatting to use.
  134. * @param width
  135. * The width of formatting to use.
  136. * @param status
  137. * A pointer to a standard ICU UErrorCode (input/output parameter).
  138. * Its input value must pass the U_SUCCESS() test, or else the
  139. * function returns immediately. The caller should check its output
  140. * value with U_FAILURE(), or use with function chaining (see User
  141. * Guide for details).
  142. * @return
  143. * A pointer to a UListFormatter object for the specified locale,
  144. * or NULL if an error occurred.
  145. * @stable ICU 67
  146. */
  147. U_CAPI UListFormatter* U_EXPORT2
  148. ulistfmt_openForType(const char* locale, UListFormatterType type,
  149. UListFormatterWidth width, UErrorCode* status);
  150. /**
  151. * Close a UListFormatter object. Once closed it may no longer be used.
  152. * @param listfmt
  153. * The UListFormatter object to close.
  154. * @stable ICU 55
  155. */
  156. U_CAPI void U_EXPORT2
  157. ulistfmt_close(UListFormatter *listfmt);
  158. /**
  159. * Creates an object to hold the result of a UListFormatter
  160. * operation. The object can be used repeatedly; it is cleared whenever
  161. * passed to a format function.
  162. *
  163. * @param ec Set if an error occurs.
  164. * @return A pointer needing ownership.
  165. * @stable ICU 64
  166. */
  167. U_CAPI UFormattedList* U_EXPORT2
  168. ulistfmt_openResult(UErrorCode* ec);
  169. /**
  170. * Returns a representation of a UFormattedList as a UFormattedValue,
  171. * which can be subsequently passed to any API requiring that type.
  172. *
  173. * The returned object is owned by the UFormattedList and is valid
  174. * only as long as the UFormattedList is present and unchanged in memory.
  175. *
  176. * You can think of this method as a cast between types.
  177. *
  178. * When calling ufmtval_nextPosition():
  179. * The fields are returned from start to end. The special field category
  180. * UFIELD_CATEGORY_LIST_SPAN is used to indicate which argument
  181. * was inserted at the given position. The span category will
  182. * always occur before the corresponding instance of UFIELD_CATEGORY_LIST
  183. * in the ufmtval_nextPosition() iterator.
  184. *
  185. * @param uresult The object containing the formatted string.
  186. * @param ec Set if an error occurs.
  187. * @return A UFormattedValue owned by the input object.
  188. * @stable ICU 64
  189. */
  190. U_CAPI const UFormattedValue* U_EXPORT2
  191. ulistfmt_resultAsValue(const UFormattedList* uresult, UErrorCode* ec);
  192. /**
  193. * Releases the UFormattedList created by ulistfmt_openResult().
  194. *
  195. * @param uresult The object to release.
  196. * @stable ICU 64
  197. */
  198. U_CAPI void U_EXPORT2
  199. ulistfmt_closeResult(UFormattedList* uresult);
  200. #if U_SHOW_CPLUSPLUS_API
  201. U_NAMESPACE_BEGIN
  202. /**
  203. * \class LocalUListFormatterPointer
  204. * "Smart pointer" class, closes a UListFormatter via ulistfmt_close().
  205. * For most methods see the LocalPointerBase base class.
  206. *
  207. * @see LocalPointerBase
  208. * @see LocalPointer
  209. * @stable ICU 55
  210. */
  211. U_DEFINE_LOCAL_OPEN_POINTER(LocalUListFormatterPointer, UListFormatter, ulistfmt_close);
  212. /**
  213. * \class LocalUFormattedListPointer
  214. * "Smart pointer" class, closes a UFormattedList via ulistfmt_closeResult().
  215. * For most methods see the LocalPointerBase base class.
  216. *
  217. * @see LocalPointerBase
  218. * @see LocalPointer
  219. * @stable ICU 64
  220. */
  221. U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedListPointer, UFormattedList, ulistfmt_closeResult);
  222. U_NAMESPACE_END
  223. #endif
  224. /**
  225. * Formats a list of strings using the conventions established for the
  226. * UListFormatter object.
  227. * @param listfmt
  228. * The UListFormatter object specifying the list conventions.
  229. * @param strings
  230. * An array of pointers to UChar strings; the array length is
  231. * specified by stringCount. Must be non-NULL if stringCount > 0.
  232. * @param stringLengths
  233. * An array of string lengths corresponding to the strings[]
  234. * parameter; any individual length value may be negative to indicate
  235. * that the corresponding strings[] entry is 0-terminated, or
  236. * stringLengths itself may be NULL if all of the strings are
  237. * 0-terminated. If non-NULL, the stringLengths array must have
  238. * stringCount entries.
  239. * @param stringCount
  240. * the number of entries in strings[], and the number of entries
  241. * in the stringLengths array if it is not NULL. Must be >= 0.
  242. * @param result
  243. * A pointer to a buffer to receive the formatted list.
  244. * @param resultCapacity
  245. * The maximum size of result.
  246. * @param status
  247. * A pointer to a standard ICU UErrorCode (input/output parameter).
  248. * Its input value must pass the U_SUCCESS() test, or else the
  249. * function returns immediately. The caller should check its output
  250. * value with U_FAILURE(), or use with function chaining (see User
  251. * Guide for details).
  252. * @return
  253. * The total buffer size needed; if greater than resultLength, the
  254. * output was truncated. May be <=0 if unable to determine the
  255. * total buffer size needed (e.g. for illegal arguments).
  256. * @stable ICU 55
  257. */
  258. U_CAPI int32_t U_EXPORT2
  259. ulistfmt_format(const UListFormatter* listfmt,
  260. const UChar* const strings[],
  261. const int32_t * stringLengths,
  262. int32_t stringCount,
  263. UChar* result,
  264. int32_t resultCapacity,
  265. UErrorCode* status);
  266. /**
  267. * Formats a list of strings to a UFormattedList, which exposes more
  268. * information than the string exported by ulistfmt_format().
  269. *
  270. * @param listfmt
  271. * The UListFormatter object specifying the list conventions.
  272. * @param strings
  273. * An array of pointers to UChar strings; the array length is
  274. * specified by stringCount. Must be non-NULL if stringCount > 0.
  275. * @param stringLengths
  276. * An array of string lengths corresponding to the strings[]
  277. * parameter; any individual length value may be negative to indicate
  278. * that the corresponding strings[] entry is 0-terminated, or
  279. * stringLengths itself may be NULL if all of the strings are
  280. * 0-terminated. If non-NULL, the stringLengths array must have
  281. * stringCount entries.
  282. * @param stringCount
  283. * the number of entries in strings[], and the number of entries
  284. * in the stringLengths array if it is not NULL. Must be >= 0.
  285. * @param uresult
  286. * The object in which to store the result of the list formatting
  287. * operation. See ulistfmt_openResult().
  288. * @param status
  289. * Error code set if an error occurred during formatting.
  290. * @stable ICU 64
  291. */
  292. U_CAPI void U_EXPORT2
  293. ulistfmt_formatStringsToResult(
  294. const UListFormatter* listfmt,
  295. const UChar* const strings[],
  296. const int32_t * stringLengths,
  297. int32_t stringCount,
  298. UFormattedList* uresult,
  299. UErrorCode* status);
  300. #endif /* #if !UCONFIG_NO_FORMATTING */
  301. #endif