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 #include "unicode/unistr.h"
25 #include "unicode/locid.h"
26 #include "unicode/formattedvalue.h"
28 #if U_SHOW_CPLUSPLUS_API
31 class FieldPositionIterator
;
32 class FieldPositionHandler
;
33 class FormattedListData
;
40 struct ListFormatInternal
;
42 /* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
47 struct ListFormatData
: public UMemory
{
48 UnicodeString twoPattern
;
49 UnicodeString startPattern
;
50 UnicodeString middlePattern
;
51 UnicodeString endPattern
;
53 ListFormatData(const UnicodeString
& two
, const UnicodeString
& start
, const UnicodeString
& middle
, const UnicodeString
& end
) :
54 twoPattern(two
), startPattern(start
), middlePattern(middle
), endPattern(end
) {}
61 * \brief C++ API: API for formatting a list.
65 #if !UCONFIG_NO_FORMATTING
66 #ifndef U_HIDE_DRAFT_API
68 * An immutable class containing the result of a list formatting operation.
70 * Instances of this class are immutable and thread-safe.
72 * When calling nextPosition():
73 * The fields are returned from start to end. The special field category
74 * UFIELD_CATEGORY_LIST_SPAN is used to indicate which argument
75 * was inserted at the given position. The span category will
76 * always occur before the corresponding instance of UFIELD_CATEGORY_LIST
77 * in the nextPosition() iterator.
79 * Not intended for public subclassing.
83 class U_I18N_API FormattedList
: public UMemory
, public FormattedValue
{
86 * Default constructor; makes an empty FormattedList.
89 FormattedList() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR
) {}
92 * Move constructor: Leaves the source FormattedList in an undefined state.
95 FormattedList(FormattedList
&& src
) U_NOEXCEPT
;
98 * Destruct an instance of FormattedList.
101 virtual ~FormattedList() U_OVERRIDE
;
103 /** Copying not supported; use move constructor instead. */
104 FormattedList(const FormattedList
&) = delete;
106 /** Copying not supported; use move assignment instead. */
107 FormattedList
& operator=(const FormattedList
&) = delete;
110 * Move assignment: Leaves the source FormattedList in an undefined state.
113 FormattedList
& operator=(FormattedList
&& src
) U_NOEXCEPT
;
115 /** @copydoc FormattedValue::toString() */
116 UnicodeString
toString(UErrorCode
& status
) const U_OVERRIDE
;
118 /** @copydoc FormattedValue::toTempString() */
119 UnicodeString
toTempString(UErrorCode
& status
) const U_OVERRIDE
;
121 /** @copydoc FormattedValue::appendTo() */
122 Appendable
&appendTo(Appendable
& appendable
, UErrorCode
& status
) const U_OVERRIDE
;
124 /** @copydoc FormattedValue::nextPosition() */
125 UBool
nextPosition(ConstrainedFieldPosition
& cfpos
, UErrorCode
& status
) const U_OVERRIDE
;
128 FormattedListData
*fData
;
129 UErrorCode fErrorCode
;
130 explicit FormattedList(FormattedListData
*results
)
131 : fData(results
), fErrorCode(U_ZERO_ERROR
) {}
132 explicit FormattedList(UErrorCode errorCode
)
133 : fData(nullptr), fErrorCode(errorCode
) {}
134 friend class ListFormatter
;
136 #endif /* U_HIDE_DRAFT_API */
137 #endif // !UCONFIG_NO_FORMATTING
141 * An immutable class for formatting a list, using data from CLDR (or supplied
144 * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted
145 * as "Alice, Bob, Charlie and Delta" in English.
147 * The ListFormatter class is not intended for public subclassing.
150 class U_I18N_API ListFormatter
: public UObject
{
158 ListFormatter(const ListFormatter
&);
161 * Assignment operator.
164 ListFormatter
& operator=(const ListFormatter
& other
);
167 * Creates a ListFormatter appropriate for the default locale.
169 * @param errorCode ICU error code, set if no data available for default locale.
170 * @return Pointer to a ListFormatter object for the default locale,
171 * created from internal data derived from CLDR data.
174 static ListFormatter
* createInstance(UErrorCode
& errorCode
);
177 * Creates a ListFormatter appropriate for a locale.
179 * @param locale The locale.
180 * @param errorCode ICU error code, set if no data available for the given locale.
181 * @return A ListFormatter object created from internal data derived from
185 static ListFormatter
* createInstance(const Locale
& locale
, UErrorCode
& errorCode
);
187 #ifndef U_HIDE_INTERNAL_API
189 * Creates a ListFormatter appropriate for a locale and style.
191 * @param locale The locale.
192 * @param style the style, either "standard", "or", "unit", "unit-narrow", or "unit-short"
193 * @param errorCode ICU error code, set if no data available for the given locale.
194 * @return A ListFormatter object created from internal data derived from
198 static ListFormatter
* createInstance(const Locale
& locale
, const char* style
, UErrorCode
& errorCode
);
199 #endif /* U_HIDE_INTERNAL_API */
206 virtual ~ListFormatter();
210 * Formats a list of strings.
212 * @param items An array of strings to be combined and formatted.
213 * @param n_items Length of the array items.
214 * @param appendTo The string to which the result should be appended to.
215 * @param errorCode ICU error code, set if there is an error.
216 * @return Formatted string combining the elements of items, appended to appendTo.
219 UnicodeString
& format(const UnicodeString items
[], int32_t n_items
,
220 UnicodeString
& appendTo
, UErrorCode
& errorCode
) const;
222 #ifndef U_HIDE_DRAFT_API
224 * Format a list of strings.
226 * @param items An array of strings to be combined and formatted.
227 * @param n_items Length of the array items.
228 * @param appendTo The string to which the formatted result will be
230 * @param posIter On return, can be used to iterate over positions of
231 * fields generated by this format call. Field values are
232 * defined in UListFormatterField. Can be NULL.
233 * @param errorCode ICU error code returned here.
234 * @return Formatted string combining the elements of items,
235 * appended to appendTo.
238 UnicodeString
& format(const UnicodeString items
[], int32_t n_items
,
239 UnicodeString
& appendTo
, FieldPositionIterator
* posIter
,
240 UErrorCode
& errorCode
) const;
241 #endif /* U_HIDE_DRAFT_API */
243 #if !UCONFIG_NO_FORMATTING
244 #ifndef U_HIDE_DRAFT_API
246 * Formats a list of strings to a FormattedList, which exposes field
247 * position information. The FormattedList contains more information than
248 * a FieldPositionIterator.
250 * @param items An array of strings to be combined and formatted.
251 * @param n_items Length of the array items.
252 * @param errorCode ICU error code returned here.
253 * @return A FormattedList containing field information.
256 FormattedList
formatStringsToValue(
257 const UnicodeString items
[],
259 UErrorCode
& errorCode
) const;
260 #endif /* U_HIDE_DRAFT_API */
261 #endif // !UCONFIG_NO_FORMATTING
263 #ifndef U_HIDE_INTERNAL_API
265 @internal for MeasureFormat
267 UnicodeString
& format(
268 const UnicodeString items
[],
270 UnicodeString
& appendTo
,
273 UErrorCode
& errorCode
) const;
275 * @internal constructor made public for testing.
277 ListFormatter(const ListFormatData
&data
, UErrorCode
&errorCode
);
279 * @internal constructor made public for testing.
281 ListFormatter(const ListFormatInternal
* listFormatterInternal
);
282 #endif /* U_HIDE_INTERNAL_API */
285 static void initializeHash(UErrorCode
& errorCode
);
286 static const ListFormatInternal
* getListFormatInternal(const Locale
& locale
, const char *style
, UErrorCode
& errorCode
);
287 struct ListPatternsSink
;
288 static ListFormatInternal
* loadListFormatInternal(const Locale
& locale
, const char* style
, UErrorCode
& errorCode
);
290 UnicodeString
& format_(
291 const UnicodeString items
[], int32_t n_items
, UnicodeString
& appendTo
,
292 int32_t index
, int32_t &offset
, FieldPositionHandler
* handler
, UErrorCode
& errorCode
) const;
296 ListFormatInternal
* owned
;
297 const ListFormatInternal
* data
;
301 #endif // U_SHOW_CPLUSPLUS_API
303 #endif // __LISTFORMATTER_H__