ICU 66.0.1  66.0.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 
21 #if U_SHOW_CPLUSPLUS_API
22 
23 #include "unicode/unistr.h"
24 
25 U_NAMESPACE_BEGIN
26 
27 // Forward declaration:
28 namespace number {
29 namespace impl {
30 class SimpleModifier;
31 }
32 }
33 
63 public:
68  SimpleFormatter() : compiledPattern((char16_t)0) {}
69 
79  SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
80  applyPattern(pattern, errorCode);
81  }
82 
97  SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
98  UErrorCode &errorCode) {
99  applyPatternMinMaxArguments(pattern, min, max, errorCode);
100  }
101 
107  : compiledPattern(other.compiledPattern) {}
108 
113  SimpleFormatter &operator=(const SimpleFormatter& other);
114 
119  ~SimpleFormatter();
120 
131  UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
132  return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
133  }
134 
150  UBool applyPatternMinMaxArguments(const UnicodeString &pattern,
151  int32_t min, int32_t max, UErrorCode &errorCode);
152 
157  int32_t getArgumentLimit() const {
158  return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
159  }
160 
173  UnicodeString &format(
174  const UnicodeString &value0,
175  UnicodeString &appendTo, UErrorCode &errorCode) const;
176 
190  UnicodeString &format(
191  const UnicodeString &value0,
192  const UnicodeString &value1,
193  UnicodeString &appendTo, UErrorCode &errorCode) const;
194 
209  UnicodeString &format(
210  const UnicodeString &value0,
211  const UnicodeString &value1,
212  const UnicodeString &value2,
213  UnicodeString &appendTo, UErrorCode &errorCode) const;
214 
234  UnicodeString &formatAndAppend(
235  const UnicodeString *const *values, int32_t valuesLength,
236  UnicodeString &appendTo,
237  int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
238 
260  UnicodeString &formatAndReplace(
261  const UnicodeString *const *values, int32_t valuesLength,
262  UnicodeString &result,
263  int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
264 
271  return getTextWithNoArguments(
272  compiledPattern.getBuffer(),
273  compiledPattern.length(),
274  nullptr,
275  0);
276  }
277 
278 #ifndef U_HIDE_INTERNAL_API
279 
294  UnicodeString getTextWithNoArguments(int32_t *offsets, int32_t offsetsLength) const {
295  return getTextWithNoArguments(
296  compiledPattern.getBuffer(),
297  compiledPattern.length(),
298  offsets,
299  offsetsLength);
300  }
301 #endif // U_HIDE_INTERNAL_API
302 
303 private:
313  UnicodeString compiledPattern;
314 
315  static inline int32_t getArgumentLimit(const char16_t *compiledPattern,
316  int32_t compiledPatternLength) {
317  return compiledPatternLength == 0 ? 0 : compiledPattern[0];
318  }
319 
320  static UnicodeString getTextWithNoArguments(
321  const char16_t *compiledPattern,
322  int32_t compiledPatternLength,
323  int32_t *offsets,
324  int32_t offsetsLength);
325 
326  static UnicodeString &format(
327  const char16_t *compiledPattern, int32_t compiledPatternLength,
328  const UnicodeString *const *values,
329  UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
330  int32_t *offsets, int32_t offsetsLength,
331  UErrorCode &errorCode);
332 
333  // Give access to internals to SimpleModifier for number formatting
334  friend class number::impl::SimpleModifier;
335 };
336 
337 U_NAMESPACE_END
338 
339 #endif /* U_SHOW_CPLUSPLUS_API */
340 
341 #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.
SimpleFormatter(const SimpleFormatter &other)
Copy constructor.
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:207
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
#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:294
UMemory is the common ICU base class.
Definition: uobject.h:115
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261