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 
52 class U_COMMON_API SimpleFormatter U_FINAL : public UMemory {
53 public:
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 
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 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__
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:129
UnicodeString getTextWithNoArguments() const
Returns the pattern text with none of the arguments.
SimpleFormatter(const SimpleFormatter &other)
Copy constructor.
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
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
#define INT32_MAX
The largest value a 32 bit signed integer can hold.
Definition: umachine.h:180
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:476
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:357
int32_t getArgumentLimit() const
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:293
UMemory is the common ICU base class.
Definition: uobject.h:110
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234