1 // Copyright (C) 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"
33 struct ListFormatInternal;
35 /* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
37 struct ListFormatData : public UMemory {
38 UnicodeString twoPattern;
39 UnicodeString startPattern;
40 UnicodeString middlePattern;
41 UnicodeString endPattern;
43 ListFormatData(const UnicodeString& two, const UnicodeString& start, const UnicodeString& middle, const UnicodeString& end) :
44 twoPattern(two), startPattern(start), middlePattern(middle), endPattern(end) {}
50 * \brief C++ API: API for formatting a list.
55 * An immutable class for formatting a list, using data from CLDR (or supplied
58 * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted
59 * as "Alice, Bob, Charlie and Delta" in English.
61 * The ListFormatter class is not intended for public subclassing.
64 class U_COMMON_API ListFormatter : public UObject{
72 ListFormatter(const ListFormatter&);
75 * Assignment operator.
78 ListFormatter& operator=(const ListFormatter& other);
81 * Creates a ListFormatter appropriate for the default locale.
83 * @param errorCode ICU error code, set if no data available for default locale.
84 * @return Pointer to a ListFormatter object for the default locale,
85 * created from internal data derived from CLDR data.
88 static ListFormatter* createInstance(UErrorCode& errorCode);
91 * Creates a ListFormatter appropriate for a locale.
93 * @param locale The locale.
94 * @param errorCode ICU error code, set if no data available for the given locale.
95 * @return A ListFormatter object created from internal data derived from
99 static ListFormatter* createInstance(const Locale& locale, UErrorCode& errorCode);
101 #ifndef U_HIDE_INTERNAL_API
103 * Creates a ListFormatter appropriate for a locale and style.
105 * @param locale The locale.
106 * @param style the style, either "standard", "duration", or "duration-short"
107 * @param errorCode ICU error code, set if no data available for the given locale.
108 * @return A ListFormatter object created from internal data derived from
112 static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode);
113 #endif /* U_HIDE_INTERNAL_API */
120 virtual ~ListFormatter();
124 * Formats a list of strings.
126 * @param items An array of strings to be combined and formatted.
127 * @param n_items Length of the array items.
128 * @param appendTo The string to which the result should be appended to.
129 * @param errorCode ICU error code, set if there is an error.
130 * @return Formatted string combining the elements of items, appended to appendTo.
133 UnicodeString& format(const UnicodeString items[], int32_t n_items,
134 UnicodeString& appendTo, UErrorCode& errorCode) const;
136 #ifndef U_HIDE_INTERNAL_API
138 @internal for MeasureFormat
140 UnicodeString& format(
141 const UnicodeString items[],
143 UnicodeString& appendTo,
146 UErrorCode& errorCode) const;
148 * @internal constructor made public for testing.
150 ListFormatter(const ListFormatData &data, UErrorCode &errorCode);
152 * @internal constructor made public for testing.
154 ListFormatter(const ListFormatInternal* listFormatterInternal);
155 #endif /* U_HIDE_INTERNAL_API */
158 static void initializeHash(UErrorCode& errorCode);
159 static const ListFormatInternal* getListFormatInternal(const Locale& locale, const char *style, UErrorCode& errorCode);
163 ListFormatInternal* owned;
164 const ListFormatInternal* data;