ICU 57.1 57.1
simpleformatter.h
Go to the documentation of this file.
1/*
2******************************************************************************
3* Copyright (C) 2014-2016, International Business Machines
4* Corporation and others. All Rights Reserved.
5******************************************************************************
6* simpleformatter.h
7*/
8
9#ifndef __SIMPLEFORMATTER_H__
10#define __SIMPLEFORMATTER_H__
11
17#include "unicode/utypes.h"
18#include "unicode/unistr.h"
19
20#ifndef U_HIDE_DRAFT_API
21
23
52class U_COMMON_API SimpleFormatter U_FINAL : public UMemory {
53public:
58 SimpleFormatter() : compiledPattern((UChar)0) {}
59
69 SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
70 applyPattern(pattern, errorCode);
71 }
72
87 SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
88 UErrorCode &errorCode) {
89 applyPatternMinMaxArguments(pattern, min, max, errorCode);
90 }
91
97 : compiledPattern(other.compiledPattern) {}
98
104
110
121 UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
122 return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
123 }
124
141 int32_t min, int32_t max, UErrorCode &errorCode);
142
147 int32_t getArgumentLimit() const {
148 return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
149 }
150
164 const UnicodeString &value0,
165 UnicodeString &appendTo, UErrorCode &errorCode) const;
166
181 const UnicodeString &value0,
182 const UnicodeString &value1,
183 UnicodeString &appendTo, UErrorCode &errorCode) const;
184
200 const UnicodeString &value0,
201 const UnicodeString &value1,
202 const UnicodeString &value2,
203 UnicodeString &appendTo, UErrorCode &errorCode) const;
204
225 const UnicodeString *const *values, int32_t valuesLength,
226 UnicodeString &appendTo,
227 int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
228
251 const UnicodeString *const *values, int32_t valuesLength,
252 UnicodeString &result,
253 int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
254
261 return getTextWithNoArguments(compiledPattern.getBuffer(), compiledPattern.length());
262 }
263
264private:
274 UnicodeString compiledPattern;
275
276 static inline int32_t getArgumentLimit(const UChar *compiledPattern,
277 int32_t compiledPatternLength) {
278 return compiledPatternLength == 0 ? 0 : compiledPattern[0];
279 }
280
281 static UnicodeString getTextWithNoArguments(const UChar *compiledPattern, int32_t compiledPatternLength);
282
283 static UnicodeString &format(
284 const UChar *compiledPattern, int32_t compiledPatternLength,
285 const UnicodeString *const *values,
286 UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
287 int32_t *offsets, int32_t offsetsLength,
288 UErrorCode &errorCode);
289};
290
292
293#endif /* U_HIDE_DRAFT_API */
294
295#endif // __SIMPLEFORMATTER_H__
Formats simple patterns like "{1} was born in {0}".
UnicodeString getTextWithNoArguments() const
Returns the pattern text with none of the arguments.
UnicodeString & format(const UnicodeString &value0, UnicodeString &appendTo, UErrorCode &errorCode) const
Formats the given value, appending to the appendTo builder.
SimpleFormatter()
Default constructor.
SimpleFormatter(const UnicodeString &pattern, int32_t min, int32_t max, UErrorCode &errorCode)
Constructs a formatter from the pattern string.
~SimpleFormatter()
Destructor.
SimpleFormatter(const UnicodeString &pattern, UErrorCode &errorCode)
Constructs a formatter from the pattern string.
UBool applyPatternMinMaxArguments(const UnicodeString &pattern, int32_t min, int32_t max, UErrorCode &errorCode)
Changes this object according to the new pattern.
SimpleFormatter(const SimpleFormatter &other)
Copy constructor.
int32_t getArgumentLimit() const
UnicodeString & format(const UnicodeString &value0, const UnicodeString &value1, UnicodeString &appendTo, UErrorCode &errorCode) const
Formats the given values, appending to the appendTo builder.
UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode)
Changes this object according to the new pattern.
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.
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.
SimpleFormatter & operator=(const SimpleFormatter &other)
Assignment operator.
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.
UMemory is the common ICU base class.
Definition: uobject.h:110
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:294
#define INT32_MAX
The largest value a 32 bit signed integer can hold.
Definition: umachine.h:180
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234
uint16_t UChar
Define UChar to be UCHAR_TYPE, if that is #defined (for example, to char16_t), or wchar_t if that is ...
Definition: umachine.h:312
C++ API: Unicode String.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition: utypes.h:476
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition: utypes.h:357
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129