2 *******************************************************************************
4 * Copyright (C) 2012-2013, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: listformatter.h
10 * tab size: 8 (not used)
13 * created on: 20120426
14 * created by: Umesh P. Nair
17 #ifndef __LISTFORMATTER_H__
18 #define __LISTFORMATTER_H__
20 #include "unicode/utypes.h"
22 #ifndef U_HIDE_DRAFT_API
24 #include "unicode/unistr.h"
25 #include "unicode/locid.h"
33 struct ListFormatData
: public UMemory
{
34 UnicodeString twoPattern
;
35 UnicodeString startPattern
;
36 UnicodeString middlePattern
;
37 UnicodeString endPattern
;
39 ListFormatData(const UnicodeString
& two
, const UnicodeString
& start
, const UnicodeString
& middle
, const UnicodeString
& end
) :
40 twoPattern(two
), startPattern(start
), middlePattern(middle
), endPattern(end
) {}
46 * \brief C++ API: API for formatting a list.
51 * An immutable class for formatting a list, using data from CLDR (or supplied
54 * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted
55 * as "Alice, Bob, Charlie and Delta" in English.
57 * The ListFormatter class is not intended for public subclassing.
60 class U_COMMON_API ListFormatter
: public UObject
{
64 * Creates a ListFormatter appropriate for the default locale.
66 * @param errorCode ICU error code, set if no data available for default locale.
67 * @return Pointer to a ListFormatter object for the default locale,
68 * created from internal data derived from CLDR data.
71 static ListFormatter
* createInstance(UErrorCode
& errorCode
);
74 * Creates a ListFormatter appropriate for a locale.
76 * @param locale The locale.
77 * @param errorCode ICU error code, set if no data available for the given locale.
78 * @return A ListFormatter object created from internal data derived from
82 static ListFormatter
* createInstance(const Locale
& locale
, UErrorCode
& errorCode
);
90 virtual ~ListFormatter();
94 * Formats a list of strings.
96 * @param items An array of strings to be combined and formatted.
97 * @param n_items Length of the array items.
98 * @param appendTo The string to which the result should be appended to.
99 * @param errorCode ICU error code, set if there is an error.
100 * @return Formatted string combining the elements of items, appended to appendTo.
103 UnicodeString
& format(const UnicodeString items
[], int32_t n_items
,
104 UnicodeString
& appendTo
, UErrorCode
& errorCode
) const;
107 * @internal constructor made public for testing.
109 ListFormatter(const ListFormatData
& listFormatterData
);
112 static void initializeHash(UErrorCode
& errorCode
);
113 static const ListFormatData
* getListFormatData(const Locale
& locale
, UErrorCode
& errorCode
);
116 ListFormatter(const ListFormatter
&);
118 ListFormatter
& operator = (const ListFormatter
&);
119 void addNewString(const UnicodeString
& pattern
, UnicodeString
& originalString
,
120 const UnicodeString
& newString
, UErrorCode
& errorCode
) const;
122 const ListFormatData
& data
;
127 #endif /* U_HIDE_DRAFT_API */