michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2012-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: listformatter.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 20120426 michael@0: * created by: Umesh P. Nair michael@0: */ michael@0: michael@0: #ifndef __LISTFORMATTER_H__ michael@0: #define __LISTFORMATTER_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #ifndef U_HIDE_DRAFT_API michael@0: michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/locid.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /** @internal */ michael@0: class Hashtable; michael@0: michael@0: #ifndef U_HIDE_INTERNAL_API michael@0: /** @internal */ michael@0: struct ListFormatData : public UMemory { michael@0: UnicodeString twoPattern; michael@0: UnicodeString startPattern; michael@0: UnicodeString middlePattern; michael@0: UnicodeString endPattern; michael@0: michael@0: ListFormatData(const UnicodeString& two, const UnicodeString& start, const UnicodeString& middle, const UnicodeString& end) : michael@0: twoPattern(two), startPattern(start), middlePattern(middle), endPattern(end) {} michael@0: }; michael@0: #endif /* U_HIDE_INTERNAL_API */ michael@0: michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: API for formatting a list. michael@0: */ michael@0: michael@0: michael@0: /** michael@0: * An immutable class for formatting a list, using data from CLDR (or supplied michael@0: * separately). michael@0: * michael@0: * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted michael@0: * as "Alice, Bob, Charlie and Delta" in English. michael@0: * michael@0: * The ListFormatter class is not intended for public subclassing. michael@0: * @draft ICU 50 michael@0: */ michael@0: class U_COMMON_API ListFormatter : public UObject{ michael@0: michael@0: public: michael@0: michael@0: /** michael@0: * Copy constructor. michael@0: * @draft ICU 52 michael@0: */ michael@0: ListFormatter(const ListFormatter&); michael@0: michael@0: /** michael@0: * Assignment operator. michael@0: * @draft ICU 52 michael@0: */ michael@0: ListFormatter& operator=(const ListFormatter& other); michael@0: michael@0: /** michael@0: * Creates a ListFormatter appropriate for the default locale. michael@0: * michael@0: * @param errorCode ICU error code, set if no data available for default locale. michael@0: * @return Pointer to a ListFormatter object for the default locale, michael@0: * created from internal data derived from CLDR data. michael@0: * @draft ICU 50 michael@0: */ michael@0: static ListFormatter* createInstance(UErrorCode& errorCode); michael@0: michael@0: /** michael@0: * Creates a ListFormatter appropriate for a locale. michael@0: * michael@0: * @param locale The locale. michael@0: * @param errorCode ICU error code, set if no data available for the given locale. michael@0: * @return A ListFormatter object created from internal data derived from michael@0: * CLDR data. michael@0: * @draft ICU 50 michael@0: */ michael@0: static ListFormatter* createInstance(const Locale& locale, UErrorCode& errorCode); michael@0: michael@0: #ifndef U_HIDE_INTERNAL_API michael@0: /** michael@0: * Creates a ListFormatter appropriate for a locale and style. michael@0: * michael@0: * @param locale The locale. michael@0: * @param style the style, either "standard", "duration", or "duration-short" michael@0: * @param errorCode ICU error code, set if no data available for the given locale. michael@0: * @return A ListFormatter object created from internal data derived from michael@0: * CLDR data. michael@0: * @internal michael@0: */ michael@0: static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode); michael@0: #endif /* U_HIDE_INTERNAL_API */ michael@0: michael@0: /** michael@0: * Destructor. michael@0: * michael@0: * @draft ICU 50 michael@0: */ michael@0: virtual ~ListFormatter(); michael@0: michael@0: michael@0: /** michael@0: * Formats a list of strings. michael@0: * michael@0: * @param items An array of strings to be combined and formatted. michael@0: * @param n_items Length of the array items. michael@0: * @param appendTo The string to which the result should be appended to. michael@0: * @param errorCode ICU error code, set if there is an error. michael@0: * @return Formatted string combining the elements of items, appended to appendTo. michael@0: * @draft ICU 50 michael@0: */ michael@0: UnicodeString& format(const UnicodeString items[], int32_t n_items, michael@0: UnicodeString& appendTo, UErrorCode& errorCode) const; michael@0: michael@0: #ifndef U_HIDE_INTERNAL_API michael@0: /** michael@0: * @internal constructor made public for testing. michael@0: */ michael@0: ListFormatter(const ListFormatData* listFormatterData); michael@0: #endif /* U_HIDE_INTERNAL_API */ michael@0: michael@0: private: michael@0: static void initializeHash(UErrorCode& errorCode); michael@0: static const ListFormatData* getListFormatData(const Locale& locale, const char *style, UErrorCode& errorCode); michael@0: michael@0: ListFormatter(); michael@0: void addNewString(const UnicodeString& pattern, UnicodeString& originalString, michael@0: const UnicodeString& newString, UErrorCode& errorCode) const; michael@0: michael@0: const ListFormatData* data; michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* U_HIDE_DRAFT_API */ michael@0: #endif