ICU 78.2 78.2

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::impl {
29 class SimpleModifier;
30 }
31
60 class U_COMMON_API SimpleFormatter final : public UMemory {
61 public:
66 SimpleFormatter() : compiledPattern(static_cast<char16_t>(0)) {}
67
77 SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
78 applyPattern(pattern, errorCode);
79 }
80
95 SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
96 UErrorCode &errorCode) {
97 applyPatternMinMaxArguments(pattern, min, max, errorCode);
98 }
99
104 SimpleFormatter(const SimpleFormatter& other)
105 : compiledPattern(other.compiledPattern) {}
106
111 SimpleFormatter &operator=(const SimpleFormatter& other);
112
117 ~SimpleFormatter();
118
129 UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
130 return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
131 }
132
148 UBool applyPatternMinMaxArguments(const UnicodeString &pattern,
149 int32_t min, int32_t max, UErrorCode &errorCode);
150
155 int32_t getArgumentLimit() const {
156 return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
157 }
158
171 UnicodeString &format(
172 const UnicodeString &value0,
173 UnicodeString &appendTo, UErrorCode &errorCode) const;
174
188 UnicodeString &format(
189 const UnicodeString &value0,
190 const UnicodeString &value1,
191 UnicodeString &appendTo, UErrorCode &errorCode) const;
192
207 UnicodeString &format(
208 const UnicodeString &value0,
209 const UnicodeString &value1,
210 const UnicodeString &value2,
211 UnicodeString &appendTo, UErrorCode &errorCode) const;
212
232 UnicodeString &formatAndAppend(
233 const UnicodeString *const *values, int32_t valuesLength,
234 UnicodeString &appendTo,
235 int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
236
258 UnicodeString &formatAndReplace(
259 const UnicodeString *const *values, int32_t valuesLength,
260 UnicodeString &result,
261 int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
262
268 UnicodeString getTextWithNoArguments() const {
269 return getTextWithNoArguments(
270 compiledPattern.getBuffer(),
271 compiledPattern.length(),
272 nullptr,
273 0);
274 }
275
276 #ifndef U_HIDE_INTERNAL_API
292 UnicodeString getTextWithNoArguments(int32_t *offsets, int32_t offsetsLength) const {
293 return getTextWithNoArguments(
294 compiledPattern.getBuffer(),
295 compiledPattern.length(),
296 offsets,
297 offsetsLength);
298 }
299 #endif // U_HIDE_INTERNAL_API
300
301 private:
311 UnicodeString compiledPattern;
312
313 static inline int32_t getArgumentLimit(const char16_t *compiledPattern,
314 int32_t compiledPatternLength) {
315 return compiledPatternLength == 0 ? 0 : compiledPattern[0];
316 }
317
318 static UnicodeString getTextWithNoArguments(
319 const char16_t *compiledPattern,
320 int32_t compiledPatternLength,
321 int32_t *offsets,
322 int32_t offsetsLength);
323
324 static UnicodeString &format(
325 const char16_t *compiledPattern, int32_t compiledPatternLength,
326 const UnicodeString *const *values,
327 UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
328 int32_t *offsets, int32_t offsetsLength,
329 UErrorCode &errorCode);
330
331 // Give access to internals to SimpleModifier for number formatting
332 friend class number::impl::SimpleModifier;
333 };
334
335 U_NAMESPACE_END
336
337 #endif /* U_SHOW_CPLUSPLUS_API */
338
339 #endif // __SIMPLEFORMATTER_H__
icu::SimpleFormatter
Formats simple patterns like "{1} was born in {0}".
Definition: simpleformatter.h:60
icu::SimpleFormatter::getTextWithNoArguments
UnicodeString getTextWithNoArguments() const
Returns the pattern text with none of the arguments.
Definition: simpleformatter.h:268
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:95
icu::SimpleFormatter::getArgumentLimit
int32_t getArgumentLimit() const
Definition: simpleformatter.h:155
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:66
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:129
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:104
icu::SimpleFormatter::SimpleFormatter
SimpleFormatter(const UnicodeString &pattern, UErrorCode &errorCode)
Constructs a formatter from the pattern string.
Definition: simpleformatter.h:77
icu::SimpleFormatter::getTextWithNoArguments
UnicodeString getTextWithNoArguments(int32_t *offsets, int32_t offsetsLength) const
Returns the pattern text with none of the arguments.
Definition: simpleformatter.h:292
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:303
INT32_MAX
#define INT32_MAX
The largest value a 32 bit signed integer can hold.
Definition: umachine.h:208
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
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:509
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:315