ICU 60.2  60.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 #include "unicode/unistr.h"
21 
23 
24 // Forward declaration:
25 namespace number {
26 namespace impl {
27 class SimpleModifier;
28 }
29 }
30 
59 class U_COMMON_API SimpleFormatter U_FINAL : public UMemory {
60 public:
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 
110  SimpleFormatter &operator=(const SimpleFormatter& other);
111 
116  ~SimpleFormatter();
117 
128  UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
129  return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
130  }
131 
147  UBool applyPatternMinMaxArguments(const UnicodeString &pattern,
148  int32_t min, int32_t max, UErrorCode &errorCode);
149 
154  int32_t getArgumentLimit() const {
155  return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
156  }
157 
170  UnicodeString &format(
171  const UnicodeString &value0,
172  UnicodeString &appendTo, UErrorCode &errorCode) const;
173 
187  UnicodeString &format(
188  const UnicodeString &value0,
189  const UnicodeString &value1,
190  UnicodeString &appendTo, UErrorCode &errorCode) const;
191 
206  UnicodeString &format(
207  const UnicodeString &value0,
208  const UnicodeString &value1,
209  const UnicodeString &value2,
210  UnicodeString &appendTo, UErrorCode &errorCode) const;
211 
231  UnicodeString &formatAndAppend(
232  const UnicodeString *const *values, int32_t valuesLength,
233  UnicodeString &appendTo,
234  int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
235 
257  UnicodeString &formatAndReplace(
258  const UnicodeString *const *values, int32_t valuesLength,
259  UnicodeString &result,
260  int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
261 
268  return getTextWithNoArguments(compiledPattern.getBuffer(), compiledPattern.length());
269  }
270 
271 private:
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__
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
UnicodeString getTextWithNoArguments() const
Returns the pattern text with none of the arguments.
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
int32_t getArgumentLimit() const
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:286
UMemory is the common ICU base class.
Definition: uobject.h:112
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236