ICU 66.1 66.1
listformatter.h
Go to the documentation of this file.
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"
29
30U_NAMESPACE_BEGIN
31
32class FieldPositionIterator;
33class FieldPositionHandler;
34class FormattedListData;
35class ListFormatter;
36
38class Hashtable;
39
41struct ListFormatInternal;
42
43/* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
48struct ListFormatData : public UMemory {
49 UnicodeString twoPattern;
50 UnicodeString startPattern;
51 UnicodeString middlePattern;
52 UnicodeString endPattern;
53
54 ListFormatData(const UnicodeString& two, const UnicodeString& start, const UnicodeString& middle, const UnicodeString& end) :
55 twoPattern(two), startPattern(start), middlePattern(middle), endPattern(end) {}
56};
66#if !UCONFIG_NO_FORMATTING
67#ifndef U_HIDE_DRAFT_API
85 public:
90 FormattedList() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
91
97
103
105 FormattedList(const FormattedList&) = delete;
106
108 FormattedList& operator=(const FormattedList&) = delete;
109
115
117 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
118
120 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
121
123 Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
124
126 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
127
128 private:
129 FormattedListData *fData;
130 UErrorCode fErrorCode;
131 explicit FormattedList(FormattedListData *results)
132 : fData(results), fErrorCode(U_ZERO_ERROR) {}
133 explicit FormattedList(UErrorCode errorCode)
134 : fData(nullptr), fErrorCode(errorCode) {}
135 friend class ListFormatter;
136};
137#endif /* U_HIDE_DRAFT_API */
138#endif // !UCONFIG_NO_FORMATTING
139
140
152
153 public:
154
160
166
176
186 static ListFormatter* createInstance(const Locale& locale, UErrorCode& errorCode);
187
188#ifndef U_HIDE_INTERNAL_API
199 static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode);
200#endif /* U_HIDE_INTERNAL_API */
201
207 virtual ~ListFormatter();
208
209
220 UnicodeString& format(const UnicodeString items[], int32_t n_items,
221 UnicodeString& appendTo, UErrorCode& errorCode) const;
222
223#ifndef U_HIDE_DRAFT_API
239 UnicodeString& format(const UnicodeString items[], int32_t n_items,
240 UnicodeString & appendTo, FieldPositionIterator* posIter,
241 UErrorCode& errorCode) const;
242#endif // U_HIDE_DRAFT_API
243
244#if !UCONFIG_NO_FORMATTING
245#ifndef U_HIDE_DRAFT_API
258 const UnicodeString items[],
259 int32_t n_items,
260 UErrorCode& errorCode) const;
261#endif /* U_HIDE_DRAFT_API */
262#endif // !UCONFIG_NO_FORMATTING
263
264#ifndef U_HIDE_INTERNAL_API
269 const UnicodeString items[],
270 int32_t n_items,
271 UnicodeString& appendTo,
272 int32_t index,
273 int32_t &offset,
274 UErrorCode& errorCode) const;
278 ListFormatter(const ListFormatData &data, UErrorCode &errorCode);
282 ListFormatter(const ListFormatInternal* listFormatterInternal);
283#endif /* U_HIDE_INTERNAL_API */
284
285 private:
286 static void initializeHash(UErrorCode& errorCode);
287 static const ListFormatInternal* getListFormatInternal(const Locale& locale, const char *style, UErrorCode& errorCode);
288 struct ListPatternsSink;
289 static ListFormatInternal* loadListFormatInternal(const Locale& locale, const char* style, UErrorCode& errorCode);
290
291 UnicodeString& format_(
292 const UnicodeString items[], int32_t n_items, UnicodeString& appendTo,
293 int32_t index, int32_t &offset, FieldPositionHandler* handler, UErrorCode& errorCode) const;
294
296
297 ListFormatInternal* owned;
298 const ListFormatInternal* data;
299};
300
301U_NAMESPACE_END
302
303#endif /* U_SHOW_CPLUSPLUS_API */
304
305#endif // __LISTFORMATTER_H__
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
Represents a span of a string containing a given field.
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
An immutable class containing the result of a list formatting operation.
Definition: listformatter.h:84
FormattedList(FormattedList &&src) U_NOEXCEPT
Move constructor: Leaves the source FormattedList in an undefined state.
virtual ~FormattedList() U_OVERRIDE
Destruct an instance of FormattedList.
FormattedList()
Default constructor; makes an empty FormattedList.
Definition: listformatter.h:90
An abstract formatted value: a string with associated field attributes.
An immutable class for formatting a list, using data from CLDR (or supplied separately).
static ListFormatter * createInstance(const Locale &locale, UErrorCode &errorCode)
Creates a ListFormatter appropriate for a locale.
ListFormatter(const ListFormatInternal *listFormatterInternal)
virtual ~ListFormatter()
Destructor.
static ListFormatter * createInstance(UErrorCode &errorCode)
Creates a ListFormatter appropriate for the default locale.
UnicodeString & format(const UnicodeString items[], int32_t n_items, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &errorCode) const
Format a list of strings.
UnicodeString & format(const UnicodeString items[], int32_t n_items, UnicodeString &appendTo, UErrorCode &errorCode) const
Formats a list of strings.
static ListFormatter * createInstance(const Locale &locale, const char *style, UErrorCode &errorCode)
Creates a ListFormatter appropriate for a locale and style.
ListFormatter & operator=(const ListFormatter &other)
Assignment operator.
FormattedList formatStringsToValue(const UnicodeString items[], int32_t n_items, UErrorCode &errorCode) const
Formats a list of strings to a FormattedList, which exposes field position information.
ListFormatter(const ListFormatter &)
Copy constructor.
UnicodeString & format(const UnicodeString items[], int32_t n_items, UnicodeString &appendTo, int32_t index, int32_t &offset, UErrorCode &errorCode) const
ListFormatter(const ListFormatData &data, UErrorCode &errorCode)
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
UMemory is the common ICU base class.
Definition: uobject.h:115
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:295
C++ API: Abstract operations for localized strings.
C++ API: Locale ID object.
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:129
C++ API: Unicode String.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:478
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:449
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301