ICU4C 79
Loading...
Searching...
No Matches
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
18
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::impl {
29class SimpleModifier;
30}
31
60class U_COMMON_API SimpleFormatter final : public UMemory {
61public:
66 SimpleFormatter() : compiledPattern(static_cast<char16_t>(0)) {}
67
77 SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
78 applyPattern(pattern, errorCode);
79 }
80
95 SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
96 UErrorCode &errorCode) {
97 applyPatternMinMaxArguments(pattern, min, max, errorCode);
98 }
99
105 : compiledPattern(other.compiledPattern) {}
106
111 SimpleFormatter& operator=(const SimpleFormatter& other) U_LIFETIME_BOUND;
112
118
129 UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
130 return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, errorCode);
131 }
132
149 int32_t min, int32_t max, UErrorCode &errorCode);
150
155 int32_t getArgumentLimit() const {
156 return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
157 }
158
172 const UnicodeString& value0,
173 UnicodeString& appendTo U_LIFETIME_BOUND,
174 UErrorCode& errorCode) const;
175
190 const UnicodeString& value0,
191 const UnicodeString& value1,
192 UnicodeString& appendTo U_LIFETIME_BOUND,
193 UErrorCode& errorCode) const;
194
210 const UnicodeString& value0,
211 const UnicodeString& value1,
212 const UnicodeString& value2,
213 UnicodeString& appendTo U_LIFETIME_BOUND,
214 UErrorCode& errorCode) const;
215
236 const UnicodeString* const* values,
237 int32_t valuesLength,
238 UnicodeString& appendTo U_LIFETIME_BOUND,
239 int32_t* offsets,
240 int32_t offsetsLength,
241 UErrorCode& errorCode) const;
242
265 const UnicodeString* const* values,
266 int32_t valuesLength,
267 UnicodeString& result U_LIFETIME_BOUND,
268 int32_t* offsets,
269 int32_t offsetsLength,
270 UErrorCode& errorCode) const;
271
279 compiledPattern.getBuffer(),
280 compiledPattern.length(),
281 nullptr,
282 0);
283 }
284
285#ifndef U_HIDE_INTERNAL_API
301 UnicodeString getTextWithNoArguments(int32_t *offsets, int32_t offsetsLength) const {
303 compiledPattern.getBuffer(),
304 compiledPattern.length(),
305 offsets,
306 offsetsLength);
307 }
308#endif // U_HIDE_INTERNAL_API
309
310private:
320 UnicodeString compiledPattern;
321
322 static inline int32_t getArgumentLimit(const char16_t *compiledPattern,
323 int32_t compiledPatternLength) {
324 return compiledPatternLength == 0 ? 0 : compiledPattern[0];
325 }
326
327 static UnicodeString getTextWithNoArguments(
328 const char16_t *compiledPattern,
329 int32_t compiledPatternLength,
330 int32_t *offsets,
331 int32_t offsetsLength);
332
333 static UnicodeString& format(
334 const char16_t* compiledPattern,
335 int32_t compiledPatternLength,
336 const UnicodeString* const* values,
337 UnicodeString& result U_LIFETIME_BOUND,
338 const UnicodeString* resultCopy,
339 UBool forbidResultAsValue,
340 int32_t* offsets,
341 int32_t offsetsLength,
342 UErrorCode& errorCode);
343
344 // Give access to internals to SimpleModifier for number formatting
345 friend class number::impl::SimpleModifier;
346};
347
348U_NAMESPACE_END
349
350#endif /* U_SHOW_CPLUSPLUS_API */
351
352#endif // __SIMPLEFORMATTER_H__
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:303
#define INT32_MAX
The largest value a 32 bit signed integer can hold.
Definition umachine.h:208
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:269
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:509
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition utypes.h:315