ICU 59.1  59.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 
52 class U_COMMON_API SimpleFormatter U_FINAL : public UMemory {
53 public:
58  SimpleFormatter() : compiledPattern((char16_t)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 
103  SimpleFormatter &operator=(const SimpleFormatter& other);
104 
109  ~SimpleFormatter();
110 
121  UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
122  return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
123  }
124 
140  UBool applyPatternMinMaxArguments(const UnicodeString &pattern,
141  int32_t min, int32_t max, UErrorCode &errorCode);
142 
147  int32_t getArgumentLimit() const {
148  return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
149  }
150 
163  UnicodeString &format(
164  const UnicodeString &value0,
165  UnicodeString &appendTo, UErrorCode &errorCode) const;
166 
180  UnicodeString &format(
181  const UnicodeString &value0,
182  const UnicodeString &value1,
183  UnicodeString &appendTo, UErrorCode &errorCode) const;
184 
199  UnicodeString &format(
200  const UnicodeString &value0,
201  const UnicodeString &value1,
202  const UnicodeString &value2,
203  UnicodeString &appendTo, UErrorCode &errorCode) const;
204 
224  UnicodeString &formatAndAppend(
225  const UnicodeString *const *values, int32_t valuesLength,
226  UnicodeString &appendTo,
227  int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
228 
250  UnicodeString &formatAndReplace(
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 
264 private:
274  UnicodeString compiledPattern;
275 
276  static inline int32_t getArgumentLimit(const char16_t *compiledPattern,
277  int32_t compiledPatternLength) {
278  return compiledPatternLength == 0 ? 0 : compiledPattern[0];
279  }
280 
281  static UnicodeString getTextWithNoArguments(const char16_t *compiledPattern, int32_t compiledPatternLength);
282 
283  static UnicodeString &format(
284  const char16_t *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 // __SIMPLEFORMATTER_H__
int32_t getArgumentLimit() const
SimpleFormatter(const UnicodeString &pattern, UErrorCode &errorCode)
Constructs a formatter from the pattern string.
SimpleFormatter()
Default constructor.
C++ API: Unicode String.
Formats simple patterns like "{1} was born in {0}".
SimpleFormatter(const UnicodeString &pattern, int32_t min, int32_t max, UErrorCode &errorCode)
Constructs a formatter from the pattern string.
UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode)
Changes this object according to the new pattern.
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
SimpleFormatter(const SimpleFormatter &other)
Copy constructor.
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
#define INT32_MAX
The largest value a 32 bit signed integer can hold.
Definition: umachine.h:182
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:396
Basic definitions for ICU, for both C and C++ APIs.
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:359
UnicodeString getTextWithNoArguments() const
Returns the pattern text with none of the arguments.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
UMemory is the common ICU base class.
Definition: uobject.h:112
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236