Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes16kdownloads
listformatter.h302 linesDownload Raw Back to unicode
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4*******************************************************************************
5*
6*   Copyright (C) 2012-2016, International Business Machines
7*   Corporation and others.  All Rights Reserved.
8*
9*******************************************************************************
10*   file name:  listformatter.h
11*   encoding:   UTF-8
12*   tab size:   8 (not used)
13*   indentation:4
14*
15*   created on: 20120426
16*   created by: Umesh P. Nair
17*/
18
19#ifndef __LISTFORMATTER_H__
20#define __LISTFORMATTER_H__
21
22#include "unicode/utypes.h"
23
24#if U_SHOW_CPLUSPLUS_API
25
26#include "unicode/unistr.h"
27#include "unicode/locid.h"
28#include "unicode/formattedvalue.h"
29#include "unicode/ulistformatter.h"
30
31U_NAMESPACE_BEGIN
32
33class FieldPositionHandler;
34class FormattedListData;
35class ListFormatter;
36
37/** @internal */
38class Hashtable;
39
40/** @internal */
41struct ListFormatInternal;
42
43/* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
44/**
45 * @internal
46 * \cond
47 */
48struct ListFormatData : public UMemory {
49    UnicodeString twoPattern;
50    UnicodeString startPattern;
51    UnicodeString middlePattern;
52    UnicodeString endPattern;
53    Locale locale;
54
55  ListFormatData(const UnicodeString& two, const UnicodeString& start, const UnicodeString& middle, const UnicodeString& end,
56                 const Locale& loc) :
57      twoPattern(two), startPattern(start), middlePattern(middle), endPattern(end), locale(loc) {}
58};
59/** \endcond */
60
61
62/**
63 * \file
64 * \brief C++ API: API for formatting a list.
65 */
66
67
68#if !UCONFIG_NO_FORMATTING
69/**
70 * An immutable class containing the result of a list formatting operation.
71 *
72 * Instances of this class are immutable and thread-safe.
73 *
74 * When calling nextPosition():
75 * The fields are returned from start to end. The special field category
76 * UFIELD_CATEGORY_LIST_SPAN is used to indicate which argument
77 * was inserted at the given position. The span category will
78 * always occur before the corresponding instance of UFIELD_CATEGORY_LIST
79 * in the nextPosition() iterator.
80 *
81 * Not intended for public subclassing.
82 *
83 * @stable ICU 64
84 */
85class U_I18N_API FormattedList : public UMemory, public FormattedValue {
86  public:
87    /**
88     * Default constructor; makes an empty FormattedList.
89     * @stable ICU 64
90     */
91    FormattedList() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
92
93    /**
94     * Move constructor: Leaves the source FormattedList in an undefined state.
95     * @stable ICU 64
96     */
97    FormattedList(FormattedList&& src) U_NOEXCEPT;
98
99    /**
100     * Destruct an instance of FormattedList.
101     * @stable ICU 64
102     */
103    virtual ~FormattedList() U_OVERRIDE;
104
105    /** Copying not supported; use move constructor instead. */
106    FormattedList(const FormattedList&) = delete;
107
108    /** Copying not supported; use move assignment instead. */
109    FormattedList& operator=(const FormattedList&) = delete;
110
111    /**
112     * Move assignment: Leaves the source FormattedList in an undefined state.
113     * @stable ICU 64
114     */
115    FormattedList& operator=(FormattedList&& src) U_NOEXCEPT;
116
117    /** @copydoc FormattedValue::toString() */
118    UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
119
120    /** @copydoc FormattedValue::toTempString() */
121    UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
122
123    /** @copydoc FormattedValue::appendTo() */
124    Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
125
126    /** @copydoc FormattedValue::nextPosition() */
127    UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
128
129  private:
130    FormattedListData *fData;
131    UErrorCode fErrorCode;
132    explicit FormattedList(FormattedListData *results)
133        : fData(results), fErrorCode(U_ZERO_ERROR) {}
134    explicit FormattedList(UErrorCode errorCode)
135        : fData(nullptr), fErrorCode(errorCode) {}
136    friend class ListFormatter;
137};
138#endif // !UCONFIG_NO_FORMATTING
139
140
141/**
142 * An immutable class for formatting a list, using data from CLDR (or supplied
143 * separately).
144 *
145 * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted
146 * as "Alice, Bob, Charlie and Delta" in English.
147 *
148 * The ListFormatter class is not intended for public subclassing.
149 * @stable ICU 50
150 */
151class U_I18N_API ListFormatter : public UObject{
152
153  public:
154
155    /**
156     * Copy constructor.
157     * @stable ICU 52
158     */
159    ListFormatter(const ListFormatter&);
160
161    /**
162     * Assignment operator.
163     * @stable ICU 52
164     */
165    ListFormatter& operator=(const ListFormatter& other);
166
167    /**
168     * Creates a ListFormatter appropriate for the default locale.
169     *
170     * @param errorCode ICU error code, set if no data available for default locale.
171     * @return Pointer to a ListFormatter object for the default locale,
172     *     created from internal data derived from CLDR data.
173     * @stable ICU 50
174     */
175    static ListFormatter* createInstance(UErrorCode& errorCode);
176
177    /**
178     * Creates a ListFormatter appropriate for a locale.
179     *
180     * @param locale The locale.
181     * @param errorCode ICU error code, set if no data available for the given locale.
182     * @return A ListFormatter object created from internal data derived from
183     *     CLDR data.
184     * @stable ICU 50
185     */
186    static ListFormatter* createInstance(const Locale& locale, UErrorCode& errorCode);
187
188#ifndef U_HIDE_DRAFT_API
189#if !UCONFIG_NO_FORMATTING
190    /**
191     * Creates a ListFormatter for the given locale, list type, and style.
192     *
193     * @param locale The locale.
194     * @param type The type of list formatting to use.
195     * @param width The width of formatting to use.
196     * @param errorCode ICU error code, set if no data available for the given locale.
197     * @return A ListFormatter object created from internal data derived from CLDR data.
198     * @draft ICU 67
199     */
200    static ListFormatter* createInstance(
201      const Locale& locale, UListFormatterType type, UListFormatterWidth width, UErrorCode& errorCode);
202#endif  /* !UCONFIG_NO_FORMATTING */
203#endif  /* U_HIDE_DRAFT_API */
204  
205#ifndef U_HIDE_INTERNAL_API
206    /**
207     * Creates a ListFormatter appropriate for a locale and style.
208     *
209     * TODO(ICU-20888): Remove this in ICU 68.
210     *
211     * @param locale The locale.
212     * @param style the style, either "standard", "or", "unit", "unit-narrow", or "unit-short"
213     * @param errorCode ICU error code, set if no data available for the given locale.
214     * @return A ListFormatter object created from internal data derived from
215     *     CLDR data.
216     * @internal
217     */
218    static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode);
219#endif  /* U_HIDE_INTERNAL_API */
220
221    /**
222     * Destructor.
223     *
224     * @stable ICU 50
225     */
226    virtual ~ListFormatter();
227
228
229    /**
230     * Formats a list of strings.
231     *
232     * @param items An array of strings to be combined and formatted.
233     * @param n_items Length of the array items.
234     * @param appendTo The string to which the result should be appended to.
235     * @param errorCode ICU error code, set if there is an error.
236     * @return Formatted string combining the elements of items, appended to appendTo.
237     * @stable ICU 50
238     */
239    UnicodeString& format(const UnicodeString items[], int32_t n_items,
240        UnicodeString& appendTo, UErrorCode& errorCode) const;
241
242#if !UCONFIG_NO_FORMATTING
243    /**
244     * Formats a list of strings to a FormattedList, which exposes field
245     * position information. The FormattedList contains more information than
246     * a FieldPositionIterator.
247     *
248     * @param items     An array of strings to be combined and formatted.
249     * @param n_items   Length of the array items.
250     * @param errorCode ICU error code returned here.
251     * @return          A FormattedList containing field information.
252     * @stable ICU 64
253     */
254    FormattedList formatStringsToValue(
255        const UnicodeString items[],
256        int32_t n_items,
257        UErrorCode& errorCode) const;
258#endif // !UCONFIG_NO_FORMATTING
259
260#ifndef U_HIDE_INTERNAL_API
261    /**
262      @internal for MeasureFormat
263    */
264    UnicodeString& format(
265            const UnicodeString items[],
266            int32_t n_items,
267            UnicodeString& appendTo,
268            int32_t index,
269            int32_t &offset,
270            UErrorCode& errorCode) const;
271    /**
272     * @internal constructor made public for testing.
273     */
274    ListFormatter(const ListFormatData &data, UErrorCode &errorCode);
275    /**
276     * @internal constructor made public for testing.
277     */
278    ListFormatter(const ListFormatInternal* listFormatterInternal);
279#endif  /* U_HIDE_INTERNAL_API */
280
281  private:
282    static void initializeHash(UErrorCode& errorCode);
283    static const ListFormatInternal* getListFormatInternal(const Locale& locale, const char *style, UErrorCode& errorCode);
284    struct ListPatternsSink;
285    static ListFormatInternal* loadListFormatInternal(const Locale& locale, const char* style, UErrorCode& errorCode);
286
287    UnicodeString& format_(
288        const UnicodeString items[], int32_t n_items, UnicodeString& appendTo,
289        int32_t index, int32_t &offset, FieldPositionHandler* handler, UErrorCode& errorCode) const;
290
291    ListFormatter();
292
293    ListFormatInternal* owned;
294    const ListFormatInternal* data;
295};
296
297U_NAMESPACE_END
298
299#endif /* U_SHOW_CPLUSPLUS_API */
300
301#endif // __LISTFORMATTER_H__
302 
codekingpro/portable-devtools · Team Ai