listformatter.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. *
  6. * Copyright (C) 2012-2016, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: listformatter.h
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 20120426
  16. * created by: Umesh P. Nair
  17. */
  18. #ifndef __LISTFORMATTER_H__
  19. #define __LISTFORMATTER_H__
  20. #include "unicode/utypes.h"
  21. #if U_SHOW_CPLUSPLUS_API
  22. #if !UCONFIG_NO_FORMATTING
  23. #include "unicode/unistr.h"
  24. #include "unicode/locid.h"
  25. #include "unicode/formattedvalue.h"
  26. #include "unicode/ulistformatter.h"
  27. U_NAMESPACE_BEGIN
  28. class FieldPositionHandler;
  29. class FormattedListData;
  30. class ListFormatter;
  31. /** @internal */
  32. class Hashtable;
  33. /** @internal */
  34. struct ListFormatInternal;
  35. /* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
  36. /**
  37. * @internal
  38. * \cond
  39. */
  40. struct ListFormatData : public UMemory {
  41. UnicodeString twoPattern;
  42. UnicodeString startPattern;
  43. UnicodeString middlePattern;
  44. UnicodeString endPattern;
  45. Locale locale;
  46. ListFormatData(const UnicodeString& two, const UnicodeString& start, const UnicodeString& middle, const UnicodeString& end,
  47. const Locale& loc) :
  48. twoPattern(two), startPattern(start), middlePattern(middle), endPattern(end), locale(loc) {}
  49. };
  50. /** \endcond */
  51. /**
  52. * \file
  53. * \brief C++ API: API for formatting a list.
  54. */
  55. /**
  56. * An immutable class containing the result of a list formatting operation.
  57. *
  58. * Instances of this class are immutable and thread-safe.
  59. *
  60. * When calling nextPosition():
  61. * The fields are returned from start to end. The special field category
  62. * UFIELD_CATEGORY_LIST_SPAN is used to indicate which argument
  63. * was inserted at the given position. The span category will
  64. * always occur before the corresponding instance of UFIELD_CATEGORY_LIST
  65. * in the nextPosition() iterator.
  66. *
  67. * Not intended for public subclassing.
  68. *
  69. * @stable ICU 64
  70. */
  71. class U_I18N_API FormattedList : public UMemory, public FormattedValue {
  72. public:
  73. /**
  74. * Default constructor; makes an empty FormattedList.
  75. * @stable ICU 64
  76. */
  77. FormattedList() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
  78. /**
  79. * Move constructor: Leaves the source FormattedList in an undefined state.
  80. * @stable ICU 64
  81. */
  82. FormattedList(FormattedList&& src) noexcept;
  83. /**
  84. * Destruct an instance of FormattedList.
  85. * @stable ICU 64
  86. */
  87. virtual ~FormattedList() override;
  88. /** Copying not supported; use move constructor instead. */
  89. FormattedList(const FormattedList&) = delete;
  90. /** Copying not supported; use move assignment instead. */
  91. FormattedList& operator=(const FormattedList&) = delete;
  92. /**
  93. * Move assignment: Leaves the source FormattedList in an undefined state.
  94. * @stable ICU 64
  95. */
  96. FormattedList& operator=(FormattedList&& src) noexcept;
  97. /** @copydoc FormattedValue::toString() */
  98. UnicodeString toString(UErrorCode& status) const override;
  99. /** @copydoc FormattedValue::toTempString() */
  100. UnicodeString toTempString(UErrorCode& status) const override;
  101. /** @copydoc FormattedValue::appendTo() */
  102. Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override;
  103. /** @copydoc FormattedValue::nextPosition() */
  104. UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
  105. private:
  106. FormattedListData *fData;
  107. UErrorCode fErrorCode;
  108. explicit FormattedList(FormattedListData *results)
  109. : fData(results), fErrorCode(U_ZERO_ERROR) {}
  110. explicit FormattedList(UErrorCode errorCode)
  111. : fData(nullptr), fErrorCode(errorCode) {}
  112. friend class ListFormatter;
  113. };
  114. /**
  115. * An immutable class for formatting a list, using data from CLDR (or supplied
  116. * separately).
  117. *
  118. * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted
  119. * as "Alice, Bob, Charlie and Delta" in English.
  120. *
  121. * The ListFormatter class is not intended for public subclassing.
  122. * @stable ICU 50
  123. */
  124. class U_I18N_API ListFormatter : public UObject{
  125. public:
  126. /**
  127. * Copy constructor.
  128. * @stable ICU 52
  129. */
  130. ListFormatter(const ListFormatter&);
  131. /**
  132. * Assignment operator.
  133. * @stable ICU 52
  134. */
  135. ListFormatter& operator=(const ListFormatter& other);
  136. /**
  137. * Creates a ListFormatter appropriate for the default locale.
  138. *
  139. * @param errorCode ICU error code, set if no data available for default locale.
  140. * @return Pointer to a ListFormatter object for the default locale,
  141. * created from internal data derived from CLDR data.
  142. * @stable ICU 50
  143. */
  144. static ListFormatter* createInstance(UErrorCode& errorCode);
  145. /**
  146. * Creates a ListFormatter appropriate for a locale.
  147. *
  148. * @param locale The locale.
  149. * @param errorCode ICU error code, set if no data available for the given locale.
  150. * @return A ListFormatter object created from internal data derived from
  151. * CLDR data.
  152. * @stable ICU 50
  153. */
  154. static ListFormatter* createInstance(const Locale& locale, UErrorCode& errorCode);
  155. /**
  156. * Creates a ListFormatter for the given locale, list type, and style.
  157. *
  158. * @param locale The locale.
  159. * @param type The type of list formatting to use.
  160. * @param width The width of formatting to use.
  161. * @param errorCode ICU error code, set if no data available for the given locale.
  162. * @return A ListFormatter object created from internal data derived from CLDR data.
  163. * @stable ICU 67
  164. */
  165. static ListFormatter* createInstance(
  166. const Locale& locale, UListFormatterType type, UListFormatterWidth width, UErrorCode& errorCode);
  167. /**
  168. * Destructor.
  169. *
  170. * @stable ICU 50
  171. */
  172. virtual ~ListFormatter();
  173. /**
  174. * Formats a list of strings.
  175. *
  176. * @param items An array of strings to be combined and formatted.
  177. * @param n_items Length of the array items.
  178. * @param appendTo The string to which the result should be appended to.
  179. * @param errorCode ICU error code, set if there is an error.
  180. * @return Formatted string combining the elements of items, appended to appendTo.
  181. * @stable ICU 50
  182. */
  183. UnicodeString& format(const UnicodeString items[], int32_t n_items,
  184. UnicodeString& appendTo, UErrorCode& errorCode) const;
  185. /**
  186. * Formats a list of strings to a FormattedList, which exposes field
  187. * position information. The FormattedList contains more information than
  188. * a FieldPositionIterator.
  189. *
  190. * @param items An array of strings to be combined and formatted.
  191. * @param n_items Length of the array items.
  192. * @param errorCode ICU error code returned here.
  193. * @return A FormattedList containing field information.
  194. * @stable ICU 64
  195. */
  196. FormattedList formatStringsToValue(
  197. const UnicodeString items[],
  198. int32_t n_items,
  199. UErrorCode& errorCode) const;
  200. #ifndef U_HIDE_INTERNAL_API
  201. /**
  202. @internal for MeasureFormat
  203. */
  204. UnicodeString& format(
  205. const UnicodeString items[],
  206. int32_t n_items,
  207. UnicodeString& appendTo,
  208. int32_t index,
  209. int32_t &offset,
  210. UErrorCode& errorCode) const;
  211. /**
  212. * @internal constructor made public for testing.
  213. */
  214. ListFormatter(const ListFormatData &data, UErrorCode &errorCode);
  215. /**
  216. * @internal constructor made public for testing.
  217. */
  218. ListFormatter(const ListFormatInternal* listFormatterInternal);
  219. #endif /* U_HIDE_INTERNAL_API */
  220. private:
  221. /**
  222. * Creates a ListFormatter appropriate for a locale and style.
  223. *
  224. * @param locale The locale.
  225. * @param style the style, either "standard", "or", "unit", "unit-narrow", or "unit-short"
  226. */
  227. static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode);
  228. static void initializeHash(UErrorCode& errorCode);
  229. static const ListFormatInternal* getListFormatInternal(const Locale& locale, const char *style, UErrorCode& errorCode);
  230. struct U_HIDDEN ListPatternsSink;
  231. static ListFormatInternal* loadListFormatInternal(const Locale& locale, const char* style, UErrorCode& errorCode);
  232. ListFormatter() = delete;
  233. ListFormatInternal* owned;
  234. const ListFormatInternal* data;
  235. };
  236. U_NAMESPACE_END
  237. #endif /* #if !UCONFIG_NO_FORMATTING */
  238. #endif /* U_SHOW_CPLUSPLUS_API */
  239. #endif // __LISTFORMATTER_H__