]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/listformatter.h
ICU-66108.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / listformatter.h
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
51004dcb
A
3/*
4*******************************************************************************
5*
2ca993e8 6* Copyright (C) 2012-2016, International Business Machines
51004dcb
A
7* Corporation and others. All Rights Reserved.
8*
9*******************************************************************************
10* file name: listformatter.h
f3c0d7a5 11* encoding: UTF-8
51004dcb
A
12* tab size: 8 (not used)
13* indentation:4
14*
15* created on: 20120426
16* created by: Umesh P. Nair
17*/
18
19#ifndef __LISTFORMATTER_H__
20#define __LISTFORMATTER_H__
21
22#include "unicode/utypes.h"
23
340931cb
A
24#if U_SHOW_CPLUSPLUS_API
25
51004dcb
A
26#include "unicode/unistr.h"
27#include "unicode/locid.h"
3d1f044b 28#include "unicode/formattedvalue.h"
340931cb 29#include "unicode/ulistformatter.h"
51004dcb
A
30
31U_NAMESPACE_BEGIN
32
3d1f044b
A
33class FieldPositionIterator;
34class FieldPositionHandler;
35class FormattedListData;
36class ListFormatter;
37
51004dcb
A
38/** @internal */
39class Hashtable;
40
57a6839d
A
41/** @internal */
42struct ListFormatInternal;
43
44/* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
3d1f044b
A
45/**
46 * @internal
47 * \cond
48 */
51004dcb
A
49struct ListFormatData : public UMemory {
50 UnicodeString twoPattern;
51 UnicodeString startPattern;
52 UnicodeString middlePattern;
53 UnicodeString endPattern;
340931cb 54 Locale locale;
51004dcb 55
340931cb
A
56 ListFormatData(const UnicodeString& two, const UnicodeString& start, const UnicodeString& middle, const UnicodeString& end,
57 const Locale& loc) :
58 twoPattern(two), startPattern(start), middlePattern(middle), endPattern(end), locale(loc) {}
51004dcb 59};
3d1f044b 60/** \endcond */
51004dcb
A
61
62
63/**
64 * \file
65 * \brief C++ API: API for formatting a list.
66 */
67
68
3d1f044b
A
69#if !UCONFIG_NO_FORMATTING
70#ifndef U_HIDE_DRAFT_API
71/**
72 * An immutable class containing the result of a list formatting operation.
73 *
74 * Instances of this class are immutable and thread-safe.
75 *
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.
82 *
83 * Not intended for public subclassing.
84 *
85 * @draft ICU 64
86 */
87class U_I18N_API FormattedList : public UMemory, public FormattedValue {
88 public:
89 /**
90 * Default constructor; makes an empty FormattedList.
91 * @draft ICU 64
92 */
93 FormattedList() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
94
95 /**
96 * Move constructor: Leaves the source FormattedList in an undefined state.
97 * @draft ICU 64
98 */
99 FormattedList(FormattedList&& src) U_NOEXCEPT;
100
101 /**
102 * Destruct an instance of FormattedList.
103 * @draft ICU 64
104 */
105 virtual ~FormattedList() U_OVERRIDE;
106
107 /** Copying not supported; use move constructor instead. */
108 FormattedList(const FormattedList&) = delete;
109
110 /** Copying not supported; use move assignment instead. */
111 FormattedList& operator=(const FormattedList&) = delete;
112
113 /**
114 * Move assignment: Leaves the source FormattedList in an undefined state.
115 * @draft ICU 64
116 */
117 FormattedList& operator=(FormattedList&& src) U_NOEXCEPT;
118
119 /** @copydoc FormattedValue::toString() */
120 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
121
122 /** @copydoc FormattedValue::toTempString() */
123 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
124
125 /** @copydoc FormattedValue::appendTo() */
126 Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
127
128 /** @copydoc FormattedValue::nextPosition() */
129 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
130
131 private:
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;
139};
140#endif /* U_HIDE_DRAFT_API */
141#endif // !UCONFIG_NO_FORMATTING
142
143
51004dcb
A
144/**
145 * An immutable class for formatting a list, using data from CLDR (or supplied
146 * separately).
147 *
148 * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted
149 * as "Alice, Bob, Charlie and Delta" in English.
150 *
151 * The ListFormatter class is not intended for public subclassing.
57a6839d 152 * @stable ICU 50
51004dcb 153 */
3d1f044b 154class U_I18N_API ListFormatter : public UObject{
51004dcb
A
155
156 public:
57a6839d
A
157
158 /**
159 * Copy constructor.
b331163b 160 * @stable ICU 52
57a6839d
A
161 */
162 ListFormatter(const ListFormatter&);
163
164 /**
165 * Assignment operator.
b331163b 166 * @stable ICU 52
57a6839d
A
167 */
168 ListFormatter& operator=(const ListFormatter& other);
169
51004dcb
A
170 /**
171 * Creates a ListFormatter appropriate for the default locale.
172 *
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.
57a6839d 176 * @stable ICU 50
51004dcb
A
177 */
178 static ListFormatter* createInstance(UErrorCode& errorCode);
179
180 /**
181 * Creates a ListFormatter appropriate for a locale.
182 *
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
186 * CLDR data.
57a6839d 187 * @stable ICU 50
51004dcb
A
188 */
189 static ListFormatter* createInstance(const Locale& locale, UErrorCode& errorCode);
190
340931cb
A
191#ifndef U_HIDE_DRAFT_API
192#if !UCONFIG_NO_FORMATTING
193 /**
194 * Creates a ListFormatter for the given locale, list type, and style.
195 *
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.
201 * @draft ICU 67
202 */
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 */
207
57a6839d
A
208#ifndef U_HIDE_INTERNAL_API
209 /**
210 * Creates a ListFormatter appropriate for a locale and style.
211 *
340931cb
A
212 * TODO(ICU-20888): Remove this in ICU 68.
213 *
57a6839d 214 * @param locale The locale.
3d1f044b 215 * @param style the style, either "standard", "or", "unit", "unit-narrow", or "unit-short"
57a6839d
A
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
218 * CLDR data.
219 * @internal
220 */
221 static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode);
222#endif /* U_HIDE_INTERNAL_API */
51004dcb
A
223
224 /**
225 * Destructor.
226 *
57a6839d 227 * @stable ICU 50
51004dcb
A
228 */
229 virtual ~ListFormatter();
230
231
232 /**
233 * Formats a list of strings.
234 *
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.
57a6839d 240 * @stable ICU 50
51004dcb
A
241 */
242 UnicodeString& format(const UnicodeString items[], int32_t n_items,
243 UnicodeString& appendTo, UErrorCode& errorCode) const;
244
3d1f044b
A
245#ifndef U_HIDE_DRAFT_API
246 /**
247 * Format a list of strings.
248 *
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
252 * appended.
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.
259 * @draft ICU 63
260 */
261 UnicodeString& format(const UnicodeString items[], int32_t n_items,
262 UnicodeString & appendTo, FieldPositionIterator* posIter,
263 UErrorCode& errorCode) const;
340931cb 264#endif // U_HIDE_DRAFT_API
3d1f044b
A
265
266#if !UCONFIG_NO_FORMATTING
267#ifndef U_HIDE_DRAFT_API
268 /**
269 * Formats a list of strings to a FormattedList, which exposes field
270 * position information. The FormattedList contains more information than
271 * a FieldPositionIterator.
272 *
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.
277 * @draft ICU 64
278 */
279 FormattedList formatStringsToValue(
280 const UnicodeString items[],
281 int32_t n_items,
282 UErrorCode& errorCode) const;
283#endif /* U_HIDE_DRAFT_API */
284#endif // !UCONFIG_NO_FORMATTING
285
57a6839d
A
286#ifndef U_HIDE_INTERNAL_API
287 /**
288 @internal for MeasureFormat
289 */
290 UnicodeString& format(
291 const UnicodeString items[],
292 int32_t n_items,
293 UnicodeString& appendTo,
294 int32_t index,
295 int32_t &offset,
296 UErrorCode& errorCode) const;
297 /**
298 * @internal constructor made public for testing.
299 */
2ca993e8 300 ListFormatter(const ListFormatData &data, UErrorCode &errorCode);
51004dcb
A
301 /**
302 * @internal constructor made public for testing.
303 */
57a6839d
A
304 ListFormatter(const ListFormatInternal* listFormatterInternal);
305#endif /* U_HIDE_INTERNAL_API */
51004dcb
A
306
307 private:
308 static void initializeHash(UErrorCode& errorCode);
57a6839d 309 static const ListFormatInternal* getListFormatInternal(const Locale& locale, const char *style, UErrorCode& errorCode);
f3c0d7a5
A
310 struct ListPatternsSink;
311 static ListFormatInternal* loadListFormatInternal(const Locale& locale, const char* style, UErrorCode& errorCode);
51004dcb 312
3d1f044b
A
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;
316
51004dcb 317 ListFormatter();
51004dcb 318
57a6839d
A
319 ListFormatInternal* owned;
320 const ListFormatInternal* data;
51004dcb
A
321};
322
323U_NAMESPACE_END
340931cb
A
324
325#endif /* U_SHOW_CPLUSPLUS_API */
51004dcb 326
3d1f044b 327#endif // __LISTFORMATTER_H__