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"
27 #if U_SHOW_CPLUSPLUS_API
34 struct ListFormatInternal
;
36 /* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
38 struct ListFormatData
: public UMemory
{
39 UnicodeString twoPattern
;
40 UnicodeString startPattern
;
41 UnicodeString middlePattern
;
42 UnicodeString endPattern
;
44 ListFormatData(const UnicodeString
& two
, const UnicodeString
& start
, const UnicodeString
& middle
, const UnicodeString
& end
) :
45 twoPattern(two
), startPattern(start
), middlePattern(middle
), endPattern(end
) {}
51 * \brief C++ API: API for formatting a list.
56 * An immutable class for formatting a list, using data from CLDR (or supplied
59 * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted
60 * as "Alice, Bob, Charlie and Delta" in English.
62 * The ListFormatter class is not intended for public subclassing.
65 class U_COMMON_API ListFormatter
: public UObject
{
73 ListFormatter(const ListFormatter
&);
76 * Assignment operator.
79 ListFormatter
& operator=(const ListFormatter
& other
);
82 * Creates a ListFormatter appropriate for the default locale.
84 * @param errorCode ICU error code, set if no data available for default locale.
85 * @return Pointer to a ListFormatter object for the default locale,
86 * created from internal data derived from CLDR data.
89 static ListFormatter
* createInstance(UErrorCode
& errorCode
);
92 * Creates a ListFormatter appropriate for a locale.
94 * @param locale The locale.
95 * @param errorCode ICU error code, set if no data available for the given locale.
96 * @return A ListFormatter object created from internal data derived from
100 static ListFormatter
* createInstance(const Locale
& locale
, UErrorCode
& errorCode
);
102 #ifndef U_HIDE_INTERNAL_API
104 * Creates a ListFormatter appropriate for a locale and style.
106 * @param locale The locale.
107 * @param style the style, either "standard", "duration", or "duration-short"
108 * @param errorCode ICU error code, set if no data available for the given locale.
109 * @return A ListFormatter object created from internal data derived from
113 static ListFormatter
* createInstance(const Locale
& locale
, const char* style
, UErrorCode
& errorCode
);
114 #endif /* U_HIDE_INTERNAL_API */
121 virtual ~ListFormatter();
125 * Formats a list of strings.
127 * @param items An array of strings to be combined and formatted.
128 * @param n_items Length of the array items.
129 * @param appendTo The string to which the result should be appended to.
130 * @param errorCode ICU error code, set if there is an error.
131 * @return Formatted string combining the elements of items, appended to appendTo.
134 UnicodeString
& format(const UnicodeString items
[], int32_t n_items
,
135 UnicodeString
& appendTo
, UErrorCode
& errorCode
) const;
137 #ifndef U_HIDE_INTERNAL_API
139 @internal for MeasureFormat
141 UnicodeString
& format(
142 const UnicodeString items
[],
144 UnicodeString
& appendTo
,
147 UErrorCode
& errorCode
) const;
149 * @internal constructor made public for testing.
151 ListFormatter(const ListFormatData
&data
, UErrorCode
&errorCode
);
153 * @internal constructor made public for testing.
155 ListFormatter(const ListFormatInternal
* listFormatterInternal
);
156 #endif /* U_HIDE_INTERNAL_API */
159 static void initializeHash(UErrorCode
& errorCode
);
160 static const ListFormatInternal
* getListFormatInternal(const Locale
& locale
, const char *style
, UErrorCode
& errorCode
);
161 struct ListPatternsSink
;
162 static ListFormatInternal
* loadListFormatInternal(const Locale
& locale
, const char* style
, UErrorCode
& errorCode
);
166 ListFormatInternal
* owned
;
167 const ListFormatInternal
* data
;
171 #endif // U_SHOW_CPLUSPLUS_API