1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2012-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: listformatter.h
12 * tab size: 8 (not used)
15 * created on: 20120426
16 * created by: Umesh P. Nair
19 #ifndef __LISTFORMATTER_H__
20 #define __LISTFORMATTER_H__
22 #include "unicode/utypes.h"
24 #if U_SHOW_CPLUSPLUS_API
26 #include "unicode/unistr.h"
27 #include "unicode/locid.h"
28 #include "unicode/formattedvalue.h"
29 #include "unicode/ulistformatter.h"
33 class FieldPositionIterator
;
34 class FieldPositionHandler
;
35 class FormattedListData
;
42 struct ListFormatInternal
;
44 /* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
49 struct ListFormatData
: public UMemory
{
50 UnicodeString twoPattern
;
51 UnicodeString startPattern
;
52 UnicodeString middlePattern
;
53 UnicodeString endPattern
;
56 ListFormatData(const UnicodeString
& two
, const UnicodeString
& start
, const UnicodeString
& middle
, const UnicodeString
& end
,
58 twoPattern(two
), startPattern(start
), middlePattern(middle
), endPattern(end
), locale(loc
) {}
65 * \brief C++ API: API for formatting a list.
69 #if !UCONFIG_NO_FORMATTING
70 #ifndef U_HIDE_DRAFT_API
72 * An immutable class containing the result of a list formatting operation.
74 * Instances of this class are immutable and thread-safe.
76 * When calling nextPosition():
77 * The fields are returned from start to end. The special field category
78 * UFIELD_CATEGORY_LIST_SPAN is used to indicate which argument
79 * was inserted at the given position. The span category will
80 * always occur before the corresponding instance of UFIELD_CATEGORY_LIST
81 * in the nextPosition() iterator.
83 * Not intended for public subclassing.
87 class U_I18N_API FormattedList
: public UMemory
, public FormattedValue
{
90 * Default constructor; makes an empty FormattedList.
93 FormattedList() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR
) {}
96 * Move constructor: Leaves the source FormattedList in an undefined state.
99 FormattedList(FormattedList
&& src
) U_NOEXCEPT
;
102 * Destruct an instance of FormattedList.
105 virtual ~FormattedList() U_OVERRIDE
;
107 /** Copying not supported; use move constructor instead. */
108 FormattedList(const FormattedList
&) = delete;
110 /** Copying not supported; use move assignment instead. */
111 FormattedList
& operator=(const FormattedList
&) = delete;
114 * Move assignment: Leaves the source FormattedList in an undefined state.
117 FormattedList
& operator=(FormattedList
&& src
) U_NOEXCEPT
;
119 /** @copydoc FormattedValue::toString() */
120 UnicodeString
toString(UErrorCode
& status
) const U_OVERRIDE
;
122 /** @copydoc FormattedValue::toTempString() */
123 UnicodeString
toTempString(UErrorCode
& status
) const U_OVERRIDE
;
125 /** @copydoc FormattedValue::appendTo() */
126 Appendable
&appendTo(Appendable
& appendable
, UErrorCode
& status
) const U_OVERRIDE
;
128 /** @copydoc FormattedValue::nextPosition() */
129 UBool
nextPosition(ConstrainedFieldPosition
& cfpos
, UErrorCode
& status
) const U_OVERRIDE
;
132 FormattedListData
*fData
;
133 UErrorCode fErrorCode
;
134 explicit FormattedList(FormattedListData
*results
)
135 : fData(results
), fErrorCode(U_ZERO_ERROR
) {}
136 explicit FormattedList(UErrorCode errorCode
)
137 : fData(nullptr), fErrorCode(errorCode
) {}
138 friend class ListFormatter
;
140 #endif /* U_HIDE_DRAFT_API */
141 #endif // !UCONFIG_NO_FORMATTING
145 * An immutable class for formatting a list, using data from CLDR (or supplied
148 * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted
149 * as "Alice, Bob, Charlie and Delta" in English.
151 * The ListFormatter class is not intended for public subclassing.
154 class U_I18N_API ListFormatter
: public UObject
{
162 ListFormatter(const ListFormatter
&);
165 * Assignment operator.
168 ListFormatter
& operator=(const ListFormatter
& other
);
171 * Creates a ListFormatter appropriate for the default locale.
173 * @param errorCode ICU error code, set if no data available for default locale.
174 * @return Pointer to a ListFormatter object for the default locale,
175 * created from internal data derived from CLDR data.
178 static ListFormatter
* createInstance(UErrorCode
& errorCode
);
181 * Creates a ListFormatter appropriate for a locale.
183 * @param locale The locale.
184 * @param errorCode ICU error code, set if no data available for the given locale.
185 * @return A ListFormatter object created from internal data derived from
189 static ListFormatter
* createInstance(const Locale
& locale
, UErrorCode
& errorCode
);
191 #ifndef U_HIDE_DRAFT_API
192 #if !UCONFIG_NO_FORMATTING
194 * Creates a ListFormatter for the given locale, list type, and style.
196 * @param locale The locale.
197 * @param type The type of list formatting to use.
198 * @param width The width of formatting to use.
199 * @param errorCode ICU error code, set if no data available for the given locale.
200 * @return A ListFormatter object created from internal data derived from CLDR data.
203 static ListFormatter
* createInstance(
204 const Locale
& locale
, UListFormatterType type
, UListFormatterWidth width
, UErrorCode
& errorCode
);
205 #endif /* !UCONFIG_NO_FORMATTING */
206 #endif /* U_HIDE_DRAFT_API */
208 #ifndef U_HIDE_INTERNAL_API
210 * Creates a ListFormatter appropriate for a locale and style.
212 * TODO(ICU-20888): Remove this in ICU 68.
214 * @param locale The locale.
215 * @param style the style, either "standard", "or", "unit", "unit-narrow", or "unit-short"
216 * @param errorCode ICU error code, set if no data available for the given locale.
217 * @return A ListFormatter object created from internal data derived from
221 static ListFormatter
* createInstance(const Locale
& locale
, const char* style
, UErrorCode
& errorCode
);
222 #endif /* U_HIDE_INTERNAL_API */
229 virtual ~ListFormatter();
233 * Formats a list of strings.
235 * @param items An array of strings to be combined and formatted.
236 * @param n_items Length of the array items.
237 * @param appendTo The string to which the result should be appended to.
238 * @param errorCode ICU error code, set if there is an error.
239 * @return Formatted string combining the elements of items, appended to appendTo.
242 UnicodeString
& format(const UnicodeString items
[], int32_t n_items
,
243 UnicodeString
& appendTo
, UErrorCode
& errorCode
) const;
245 #ifndef U_HIDE_DRAFT_API
247 * Format a list of strings.
249 * @param items An array of strings to be combined and formatted.
250 * @param n_items Length of the array items.
251 * @param appendTo The string to which the formatted result will be
253 * @param posIter On return, can be used to iterate over positions of
254 * fields generated by this format call. Field values are
255 * defined in UListFormatterField. Can be NULL.
256 * @param errorCode ICU error code returned here.
257 * @return Formatted string combining the elements of items,
258 * appended to appendTo.
261 UnicodeString
& format(const UnicodeString items
[], int32_t n_items
,
262 UnicodeString
& appendTo
, FieldPositionIterator
* posIter
,
263 UErrorCode
& errorCode
) const;
264 #endif // U_HIDE_DRAFT_API
266 #if !UCONFIG_NO_FORMATTING
267 #ifndef U_HIDE_DRAFT_API
269 * Formats a list of strings to a FormattedList, which exposes field
270 * position information. The FormattedList contains more information than
271 * a FieldPositionIterator.
273 * @param items An array of strings to be combined and formatted.
274 * @param n_items Length of the array items.
275 * @param errorCode ICU error code returned here.
276 * @return A FormattedList containing field information.
279 FormattedList
formatStringsToValue(
280 const UnicodeString items
[],
282 UErrorCode
& errorCode
) const;
283 #endif /* U_HIDE_DRAFT_API */
284 #endif // !UCONFIG_NO_FORMATTING
286 #ifndef U_HIDE_INTERNAL_API
288 @internal for MeasureFormat
290 UnicodeString
& format(
291 const UnicodeString items
[],
293 UnicodeString
& appendTo
,
296 UErrorCode
& errorCode
) const;
298 * @internal constructor made public for testing.
300 ListFormatter(const ListFormatData
&data
, UErrorCode
&errorCode
);
302 * @internal constructor made public for testing.
304 ListFormatter(const ListFormatInternal
* listFormatterInternal
);
305 #endif /* U_HIDE_INTERNAL_API */
308 static void initializeHash(UErrorCode
& errorCode
);
309 static const ListFormatInternal
* getListFormatInternal(const Locale
& locale
, const char *style
, UErrorCode
& errorCode
);
310 struct ListPatternsSink
;
311 static ListFormatInternal
* loadListFormatInternal(const Locale
& locale
, const char* style
, UErrorCode
& errorCode
);
313 UnicodeString
& format_(
314 const UnicodeString items
[], int32_t n_items
, UnicodeString
& appendTo
,
315 int32_t index
, int32_t &offset
, FieldPositionHandler
* handler
, UErrorCode
& errorCode
) const;
319 ListFormatInternal
* owned
;
320 const ListFormatInternal
* data
;
325 #endif /* U_SHOW_CPLUSPLUS_API */
327 #endif // __LISTFORMATTER_H__