ICU 74.1 74.1

simpleformatter.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 * Copyright (C) 2014-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * simpleformatter.h
9 */
10
11 #ifndef __SIMPLEFORMATTER_H__
12 #define __SIMPLEFORMATTER_H__
13
19 #include "unicode/utypes.h"
20
21 #if U_SHOW_CPLUSPLUS_API
22
23 #include "unicode/unistr.h"
24
25 U_NAMESPACE_BEGIN
26
27 // Forward declaration:
28 namespace number {
29 namespace impl {
30 class SimpleModifier;
31 }
32 }
33
62 class U_COMMON_API SimpleFormatter final : public UMemory {
63 public:
68 SimpleFormatter() : compiledPattern((char16_t)0) {}
69
79 SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
80 applyPattern(pattern, errorCode);
81 }
82
97 SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
98 UErrorCode &errorCode) {
99 applyPatternMinMaxArguments(pattern, min, max, errorCode);
100 }
101
106 SimpleFormatter(const SimpleFormatter& other)
107 : compiledPattern(other.compiledPattern) {}
108
113 SimpleFormatter &operator=(const SimpleFormatter& other);
114
119 ~SimpleFormatter();
120
131 UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
132 return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
133 }
134
150 UBool applyPatternMinMaxArguments(const UnicodeString &pattern,
151 int32_t min, int32_t max, UErrorCode &errorCode);
152
157 int32_t getArgumentLimit() const {
158 return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
159 }
160
173 UnicodeString &format(
174 const UnicodeString &value0,
175 UnicodeString &appendTo, UErrorCode &errorCode) const;
176
190 UnicodeString &format(
191 const UnicodeString &value0,
192 const UnicodeString &value1,
193 UnicodeString &appendTo, UErrorCode &errorCode) const;
194
209 UnicodeString &format(
210 const UnicodeString &value0,
211 const UnicodeString &value1,
212 const UnicodeString &value2,
213 UnicodeString &appendTo, UErrorCode &errorCode) const;
214
234 UnicodeString &formatAndAppend(
235 const UnicodeString *const *values, int32_t valuesLength,
236 UnicodeString &appendTo,
237 int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
238
260 UnicodeString &formatAndReplace(
261 const UnicodeString *const *values, int32_t valuesLength,
262 UnicodeString &result,
263 int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
264
270 UnicodeString getTextWithNoArguments() const {
271 return getTextWithNoArguments(
272 compiledPattern.getBuffer(),
273 compiledPattern.length(),
274 nullptr,
275 0);
276 }
277
278 #ifndef U_HIDE_INTERNAL_API
294 UnicodeString getTextWithNoArguments(int32_t *offsets, int32_t offsetsLength) const {
295 return getTextWithNoArguments(
296 compiledPattern.getBuffer(),
297 compiledPattern.length(),
298 offsets,
299 offsetsLength);
300 }
301 #endif // U_HIDE_INTERNAL_API
302
303 private:
313 UnicodeString compiledPattern;
314
315 static inline int32_t getArgumentLimit(const char16_t *compiledPattern,
316 int32_t compiledPatternLength) {
317 return compiledPatternLength == 0 ? 0 : compiledPattern[0];
318 }
319
320 static UnicodeString getTextWithNoArguments(
321 const char16_t *compiledPattern,
322 int32_t compiledPatternLength,
323 int32_t *offsets,
324 int32_t offsetsLength);
325
326 static UnicodeString &format(
327 const char16_t *compiledPattern, int32_t compiledPatternLength,
328 const UnicodeString *const *values,
329 UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
330 int32_t *offsets, int32_t offsetsLength,
331 UErrorCode &errorCode);
332
333 // Give access to internals to SimpleModifier for number formatting
334 friend class number::impl::SimpleModifier;
335 };
336
337 U_NAMESPACE_END
338
339 #endif /* U_SHOW_CPLUSPLUS_API */
340
341 #endif // __SIMPLEFORMATTER_H__
icu::SimpleFormatter
Formats simple patterns like "{1} was born in {0}".
Definition: simpleformatter.h:62
icu::SimpleFormatter::getTextWithNoArguments
UnicodeString getTextWithNoArguments() const
Returns the pattern text with none of the arguments.
Definition: simpleformatter.h:270
icu::SimpleFormatter::formatAndAppend
UnicodeString & formatAndAppend(const UnicodeString *const *values, int32_t valuesLength, UnicodeString &appendTo, int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const
Formats the given values, appending to the appendTo string.
icu::SimpleFormatter::applyPatternMinMaxArguments
UBool applyPatternMinMaxArguments(const UnicodeString &pattern, int32_t min, int32_t max, UErrorCode &errorCode)
Changes this object according to the new pattern.
icu::SimpleFormatter::SimpleFormatter
SimpleFormatter(const UnicodeString &pattern, int32_t min, int32_t max, UErrorCode &errorCode)
Constructs a formatter from the pattern string.
Definition: simpleformatter.h:97
icu::SimpleFormatter::getArgumentLimit
int32_t getArgumentLimit() const
Definition: simpleformatter.h:157
icu::SimpleFormatter::format
UnicodeString & format(const UnicodeString &value0, const UnicodeString &value1, UnicodeString &appendTo, UErrorCode &errorCode) const
Formats the given values, appending to the appendTo builder.
icu::SimpleFormatter::~SimpleFormatter
~SimpleFormatter()
Destructor.
icu::SimpleFormatter::format
UnicodeString & format(const UnicodeString &value0, UnicodeString &appendTo, UErrorCode &errorCode) const
Formats the given value, appending to the appendTo builder.
icu::SimpleFormatter::SimpleFormatter
SimpleFormatter()
Default constructor.
Definition: simpleformatter.h:68
icu::SimpleFormatter::operator=
SimpleFormatter & operator=(const SimpleFormatter &other)
Assignment operator.
icu::SimpleFormatter::formatAndReplace
UnicodeString & formatAndReplace(const UnicodeString *const *values, int32_t valuesLength, UnicodeString &result, int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const
Formats the given values, replacing the contents of the result string.
icu::SimpleFormatter::applyPattern
UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode)
Changes this object according to the new pattern.
Definition: simpleformatter.h:131
icu::SimpleFormatter::format
UnicodeString & format(const UnicodeString &value0, const UnicodeString &value1, const UnicodeString &value2, UnicodeString &appendTo, UErrorCode &errorCode) const
Formats the given values, appending to the appendTo builder.
icu::SimpleFormatter::SimpleFormatter
SimpleFormatter(const SimpleFormatter &other)
Copy constructor.
Definition: simpleformatter.h:106
icu::SimpleFormatter::SimpleFormatter
SimpleFormatter(const UnicodeString &pattern, UErrorCode &errorCode)
Constructs a formatter from the pattern string.
Definition: simpleformatter.h:79
icu::SimpleFormatter::getTextWithNoArguments
UnicodeString getTextWithNoArguments(int32_t *offsets, int32_t offsetsLength) const
Returns the pattern text with none of the arguments.
Definition: simpleformatter.h:294
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
INT32_MAX
#define INT32_MAX
The largest value a 32 bit signed integer can hold.
Definition: umachine.h:186
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
unistr.h
C++ API: Unicode String.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
U_COMMON_API
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition: utypes.h:300