ICU 62.1 62.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#include "unicode/unistr.h"
21
23
24// Forward declaration:
25namespace number {
26namespace impl {
27class SimpleModifier;
28}
29}
30
60public:
65 SimpleFormatter() : compiledPattern((char16_t)0) {}
66
76 SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
77 applyPattern(pattern, errorCode);
78 }
79
94 SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
95 UErrorCode &errorCode) {
96 applyPatternMinMaxArguments(pattern, min, max, errorCode);
97 }
98
104 : compiledPattern(other.compiledPattern) {}
105
111
117
128 UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
129 return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
130 }
131
148 int32_t min, int32_t max, UErrorCode &errorCode);
149
154 int32_t getArgumentLimit() const {
155 return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
156 }
157
171 const UnicodeString &value0,
172 UnicodeString &appendTo, UErrorCode &errorCode) const;
173
188 const UnicodeString &value0,
189 const UnicodeString &value1,
190 UnicodeString &appendTo, UErrorCode &errorCode) const;
191
207 const UnicodeString &value0,
208 const UnicodeString &value1,
209 const UnicodeString &value2,
210 UnicodeString &appendTo, UErrorCode &errorCode) const;
211
232 const UnicodeString *const *values, int32_t valuesLength,
233 UnicodeString &appendTo,
234 int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
235
258 const UnicodeString *const *values, int32_t valuesLength,
260 int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
261
268 return getTextWithNoArguments(compiledPattern.getBuffer(), compiledPattern.length());
269 }
270
271private:
281 UnicodeString compiledPattern;
282
283 static inline int32_t getArgumentLimit(const char16_t *compiledPattern,
284 int32_t compiledPatternLength) {
285 return compiledPatternLength == 0 ? 0 : compiledPattern[0];
286 }
287
288 static UnicodeString getTextWithNoArguments(const char16_t *compiledPattern, int32_t compiledPatternLength);
289
290 static UnicodeString &format(
291 const char16_t *compiledPattern, int32_t compiledPatternLength,
292 const UnicodeString *const *values,
293 UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
294 int32_t *offsets, int32_t offsetsLength,
295 UErrorCode &errorCode);
296
297 // Give access to internals to SimpleModifier for number formatting
298 friend class number::impl::SimpleModifier;
299};
300
302
303#endif // __SIMPLEFORMATTER_H__
"Smart pointer" base class; do not use directly: use LocalPointer etc.
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 & operator=(const SimpleFormatter &other)
Assignment operator.
UBool applyPatternMinMaxArguments(const UnicodeString &pattern, int32_t min, int32_t max, UErrorCode &errorCode)
Changes this object according to the new pattern.
SimpleFormatter(const UnicodeString &pattern, int32_t min, int32_t max, UErrorCode &errorCode)
Constructs a formatter from the pattern string.
int32_t getArgumentLimit() const
~SimpleFormatter()
Destructor.
SimpleFormatter()
Default constructor.
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.
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.
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 & 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.
SimpleFormatter(const SimpleFormatter &other)
Copy constructor.
SimpleFormatter(const UnicodeString &pattern, UErrorCode &errorCode)
Constructs a formatter from the pattern string.
UMemory is the common ICU base class.
Definition uobject.h:112
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:287
#define INT32_MAX
The largest value a 32 bit signed integer can hold.
Definition umachine.h:182
int8_t UBool
The ICU boolean type.
Definition umachine.h:236
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:396
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition utypes.h:359
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition uversion.h:138
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition uversion.h:137