ICU 74.1 74.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
25U_NAMESPACE_BEGIN
26
27// Forward declaration:
28namespace number {
29namespace impl {
30class SimpleModifier;
31}
32}
33
62class U_COMMON_API SimpleFormatter final : public UMemory {
63public:
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
114
120
131 UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
132 return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
133 }
134
151 int32_t min, int32_t max, UErrorCode &errorCode);
152
157 int32_t getArgumentLimit() const {
158 return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
159 }
160
174 const UnicodeString &value0,
175 UnicodeString &appendTo, UErrorCode &errorCode) const;
176
191 const UnicodeString &value0,
192 const UnicodeString &value1,
193 UnicodeString &appendTo, UErrorCode &errorCode) const;
194
210 const UnicodeString &value0,
211 const UnicodeString &value1,
212 const UnicodeString &value2,
213 UnicodeString &appendTo, UErrorCode &errorCode) const;
214
235 const UnicodeString *const *values, int32_t valuesLength,
236 UnicodeString &appendTo,
237 int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
238
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
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
303private:
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
337U_NAMESPACE_END
338
339#endif /* U_SHOW_CPLUSPLUS_API */
340
341#endif // __SIMPLEFORMATTER_H__
Formats simple patterns like "{1} was born in {0}".
UnicodeString getTextWithNoArguments() const
Returns the pattern text with none of the arguments.
UnicodeString & format(const UnicodeString &value0, UnicodeString &appendTo, UErrorCode &errorCode) const
Formats the given value, appending to the appendTo builder.
SimpleFormatter & operator=(const SimpleFormatter &other)
Assignment operator.
UBool applyPatternMinMaxArguments(const UnicodeString &pattern, int32_t min, int32_t max, UErrorCode &errorCode)
Changes this object according to the new pattern.
SimpleFormatter(const UnicodeString &pattern, int32_t min, int32_t max, UErrorCode &errorCode)
Constructs a formatter from the pattern string.
int32_t getArgumentLimit() const
~SimpleFormatter()
Destructor.
SimpleFormatter()
Default constructor.
UnicodeString & formatAndAppend(const UnicodeString *const *values, int32_t valuesLength, UnicodeString &appendTo, int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const
Formats the given values, appending to the appendTo string.
UnicodeString & formatAndReplace(const UnicodeString *const *values, int32_t valuesLength, UnicodeString &result, int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const
Formats the given values, replacing the contents of the result string.
UnicodeString & format(const UnicodeString &value0, const UnicodeString &value1, const UnicodeString &value2, UnicodeString &appendTo, UErrorCode &errorCode) const
Formats the given values, appending to the appendTo builder.
UnicodeString & format(const UnicodeString &value0, const UnicodeString &value1, UnicodeString &appendTo, UErrorCode &errorCode) const
Formats the given values, appending to the appendTo builder.
UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode)
Changes this object according to the new pattern.
SimpleFormatter(const SimpleFormatter &other)
Copy constructor.
SimpleFormatter(const UnicodeString &pattern, UErrorCode &errorCode)
Constructs a formatter from the pattern string.
UnicodeString getTextWithNoArguments(int32_t *offsets, int32_t offsetsLength) const
Returns the pattern text with none of the arguments.
UMemory is the common ICU base class.
Definition: uobject.h:115
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
#define INT32_MAX
The largest value a 32 bit signed integer can hold.
Definition: umachine.h:186
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
C++ API: Unicode String.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition: utypes.h:300