ICU 58.2  58.2
simpleformatter.h
Go to the documentation of this file.
1 // Copyright (C) 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 
22 #ifndef U_HIDE_DRAFT_API
23 
25 
54 class U_COMMON_API SimpleFormatter U_FINAL : public UMemory {
55 public:
60  SimpleFormatter() : compiledPattern((UChar)0) {}
61 
71  SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
72  applyPattern(pattern, errorCode);
73  }
74 
89  SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
90  UErrorCode &errorCode) {
91  applyPatternMinMaxArguments(pattern, min, max, errorCode);
92  }
93 
99  : compiledPattern(other.compiledPattern) {}
100 
105  SimpleFormatter &operator=(const SimpleFormatter& other);
106 
111  ~SimpleFormatter();
112 
123  UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
124  return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
125  }
126 
142  UBool applyPatternMinMaxArguments(const UnicodeString &pattern,
143  int32_t min, int32_t max, UErrorCode &errorCode);
144 
149  int32_t getArgumentLimit() const {
150  return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
151  }
152 
165  UnicodeString &format(
166  const UnicodeString &value0,
167  UnicodeString &appendTo, UErrorCode &errorCode) const;
168 
182  UnicodeString &format(
183  const UnicodeString &value0,
184  const UnicodeString &value1,
185  UnicodeString &appendTo, UErrorCode &errorCode) const;
186 
201  UnicodeString &format(
202  const UnicodeString &value0,
203  const UnicodeString &value1,
204  const UnicodeString &value2,
205  UnicodeString &appendTo, UErrorCode &errorCode) const;
206 
226  UnicodeString &formatAndAppend(
227  const UnicodeString *const *values, int32_t valuesLength,
228  UnicodeString &appendTo,
229  int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
230 
252  UnicodeString &formatAndReplace(
253  const UnicodeString *const *values, int32_t valuesLength,
254  UnicodeString &result,
255  int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
256 
263  return getTextWithNoArguments(compiledPattern.getBuffer(), compiledPattern.length());
264  }
265 
266 private:
276  UnicodeString compiledPattern;
277 
278  static inline int32_t getArgumentLimit(const UChar *compiledPattern,
279  int32_t compiledPatternLength) {
280  return compiledPatternLength == 0 ? 0 : compiledPattern[0];
281  }
282 
283  static UnicodeString getTextWithNoArguments(const UChar *compiledPattern, int32_t compiledPatternLength);
284 
285  static UnicodeString &format(
286  const UChar *compiledPattern, int32_t compiledPatternLength,
287  const UnicodeString *const *values,
288  UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
289  int32_t *offsets, int32_t offsetsLength,
290  UErrorCode &errorCode);
291 };
292 
294 
295 #endif /* U_HIDE_DRAFT_API */
296 
297 #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.
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:337
#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:205
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:295
UMemory is the common ICU base class.
Definition: uobject.h:112
int8_t UBool
The ICU boolean type.
Definition: umachine.h:259