ICU 64.2  64.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 
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(
269  compiledPattern.getBuffer(),
270  compiledPattern.length(),
271  nullptr,
272  0);
273  }
274 
275 #ifndef U_HIDE_INTERNAL_API
276 
291  UnicodeString getTextWithNoArguments(int32_t *offsets, int32_t offsetsLength) const {
292  return getTextWithNoArguments(
293  compiledPattern.getBuffer(),
294  compiledPattern.length(),
295  offsets,
296  offsetsLength);
297  }
298 #endif // U_HIDE_INTERNAL_API
299 
300 private:
310  UnicodeString compiledPattern;
311 
312  static inline int32_t getArgumentLimit(const char16_t *compiledPattern,
313  int32_t compiledPatternLength) {
314  return compiledPatternLength == 0 ? 0 : compiledPattern[0];
315  }
316 
317  static UnicodeString getTextWithNoArguments(
318  const char16_t *compiledPattern,
319  int32_t compiledPatternLength,
320  int32_t *offsets,
321  int32_t offsetsLength);
322 
323  static UnicodeString &format(
324  const char16_t *compiledPattern, int32_t compiledPatternLength,
325  const UnicodeString *const *values,
326  UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
327  int32_t *offsets, int32_t offsetsLength,
328  UErrorCode &errorCode);
329 
330  // Give access to internals to SimpleModifier for number formatting
331  friend class number::impl::SimpleModifier;
332 };
333 
335 
336 #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:137
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:138
UnicodeString getTextWithNoArguments(int32_t *offsets, int32_t offsetsLength) const
Returns the pattern text with none of the arguments.
#define INT32_MAX
The largest value a 32 bit signed integer can hold.
Definition: umachine.h:171
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
#define U_FINAL
Defined to the C++11 "final" keyword if available.
Definition: umachine.h:140
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:300
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:289
UMemory is the common ICU base class.
Definition: uobject.h:112
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225