ICU 67.1 67.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"
30
31U_NAMESPACE_BEGIN
32
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 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};
60
61
66
67
68#if !UCONFIG_NO_FORMATTING
86 public:
91 FormattedList() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
92
98
104
106 FormattedList(const FormattedList&) = delete;
107
109 FormattedList& operator=(const FormattedList&) = delete;
110
116
119
122
124 Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
125
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
152
153 public:
154
160
166
176
186 static ListFormatter* createInstance(const Locale& locale, UErrorCode& errorCode);
187
188#ifndef U_HIDE_DRAFT_API
189#if !UCONFIG_NO_FORMATTING
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
218 static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode);
219#endif /* U_HIDE_INTERNAL_API */
220
226 virtual ~ListFormatter();
227
228
239 UnicodeString& format(const UnicodeString items[], int32_t n_items,
240 UnicodeString& appendTo, UErrorCode& errorCode) const;
241
242#if !UCONFIG_NO_FORMATTING
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
265 const UnicodeString items[],
266 int32_t n_items,
267 UnicodeString& appendTo,
268 int32_t index,
269 int32_t &offset,
270 UErrorCode& errorCode) const;
274 ListFormatter(const ListFormatData &data, UErrorCode &errorCode);
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
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__
Represents a span of a string containing a given field.
An immutable class containing the result of a list formatting operation.
FormattedList(FormattedList &&src) U_NOEXCEPT
Move constructor: Leaves the source FormattedList in an undefined state.
UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const U_OVERRIDE
Iterates over field positions in the FormattedValue.
virtual ~FormattedList() U_OVERRIDE
Destruct an instance of FormattedList.
FormattedList()
Default constructor; makes an empty FormattedList.
Appendable & appendTo(Appendable &appendable, UErrorCode &status) const U_OVERRIDE
Appends the formatted string to an Appendable.
UnicodeString toTempString(UErrorCode &status) const U_OVERRIDE
Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
UnicodeString toString(UErrorCode &status) const U_OVERRIDE
Returns the formatted string as a self-contained UnicodeString.
An abstract formatted value: a string with associated field attributes.
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, 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.
static ListFormatter * createInstance(const Locale &locale, UListFormatterType type, UListFormatterWidth width, UErrorCode &errorCode)
Creates a ListFormatter for the given locale, list type, and style.
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
C API: Format a list in a locale-appropriate way.
UListFormatterWidth
Verbosity level of the list patterns.
UListFormatterType
Type of meaning expressed by the list.
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