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