ICU 66.0.1
66.0.1
|
Formats simple patterns like "{1} was born in {0}". More...
#include <simpleformatter.h>
Public Member Functions | |
SimpleFormatter () | |
Default constructor. More... | |
SimpleFormatter (const UnicodeString &pattern, UErrorCode &errorCode) | |
Constructs a formatter from the pattern string. More... | |
SimpleFormatter (const UnicodeString &pattern, int32_t min, int32_t max, UErrorCode &errorCode) | |
Constructs a formatter from the pattern string. More... | |
SimpleFormatter (const SimpleFormatter &other) | |
Copy constructor. More... | |
SimpleFormatter & | operator= (const SimpleFormatter &other) |
Assignment operator. More... | |
~SimpleFormatter () | |
Destructor. More... | |
UBool | applyPattern (const UnicodeString &pattern, UErrorCode &errorCode) |
Changes this object according to the new pattern. More... | |
UBool | applyPatternMinMaxArguments (const UnicodeString &pattern, int32_t min, int32_t max, UErrorCode &errorCode) |
Changes this object according to the new pattern. More... | |
int32_t | getArgumentLimit () const |
UnicodeString & | format (const UnicodeString &value0, UnicodeString &appendTo, UErrorCode &errorCode) const |
Formats the given value, appending to the appendTo builder. More... | |
UnicodeString & | format (const UnicodeString &value0, const UnicodeString &value1, UnicodeString &appendTo, UErrorCode &errorCode) const |
Formats the given values, appending to the appendTo builder. More... | |
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. More... | |
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. More... | |
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. More... | |
UnicodeString | getTextWithNoArguments () const |
Returns the pattern text with none of the arguments. More... | |
UnicodeString | getTextWithNoArguments (int32_t *offsets, int32_t offsetsLength) const |
Returns the pattern text with none of the arguments. More... | |
Friends | |
class | number::impl::SimpleModifier |
Formats simple patterns like "{1} was born in {0}".
Minimal subset of MessageFormat; fast, simple, minimal dependencies. Supports only numbered arguments with no type nor style parameters, and formats only string values. Quoting via ASCII apostrophe compatible with ICU MessageFormat default behavior.
Factory methods set error codes for syntax errors and for too few or too many arguments/placeholders.
SimpleFormatter objects are thread-safe except for assignment and applying new patterns.
Example:
UErrorCode errorCode = U_ZERO_ERROR; SimpleFormatter fmt("{1} '{born}' in {0}", errorCode); UnicodeString result;
// Output: "paul {born} in england" fmt.format("england", "paul", result, errorCode);
This class is not intended for public subclassing.
Definition at line 62 of file simpleformatter.h.
|
inline |
|
inline |
Constructs a formatter from the pattern string.
pattern | The pattern string. |
errorCode | ICU error code in/out parameter. Must fulfill U_SUCCESS before the function call. Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax. |
Definition at line 79 of file simpleformatter.h.
|
inline |
Constructs a formatter from the pattern string.
The number of arguments checked against the given limits is the highest argument number plus one, not the number of occurrences of arguments.
pattern | The pattern string. |
min | The pattern must have at least this many arguments. |
max | The pattern must have at most this many arguments. |
errorCode | ICU error code in/out parameter. Must fulfill U_SUCCESS before the function call. Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and too few or too many arguments. |
Definition at line 97 of file simpleformatter.h.
|
inline |
icu::SimpleFormatter::~SimpleFormatter | ( | ) |
Destructor.
|
inline |
Changes this object according to the new pattern.
pattern | The pattern string. |
errorCode | ICU error code in/out parameter. Must fulfill U_SUCCESS before the function call. Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax. |
Definition at line 131 of file simpleformatter.h.
References INT32_MAX.
UBool icu::SimpleFormatter::applyPatternMinMaxArguments | ( | const UnicodeString & | pattern, |
int32_t | min, | ||
int32_t | max, | ||
UErrorCode & | errorCode | ||
) |
Changes this object according to the new pattern.
The number of arguments checked against the given limits is the highest argument number plus one, not the number of occurrences of arguments.
pattern | The pattern string. |
min | The pattern must have at least this many arguments. |
max | The pattern must have at most this many arguments. |
errorCode | ICU error code in/out parameter. Must fulfill U_SUCCESS before the function call. Set to U_ILLEGAL_ARGUMENT_ERROR for bad argument syntax and too few or too many arguments. |
UnicodeString& icu::SimpleFormatter::format | ( | const UnicodeString & | value0, |
UnicodeString & | appendTo, | ||
UErrorCode & | errorCode | ||
) | const |
Formats the given value, appending to the appendTo builder.
The argument value must not be the same object as appendTo. getArgumentLimit() must be at most 1.
value0 | Value for argument {0}. |
appendTo | Gets the formatted pattern and value appended. |
errorCode | ICU error code in/out parameter. Must fulfill U_SUCCESS before the function call. |
UnicodeString& icu::SimpleFormatter::format | ( | const UnicodeString & | value0, |
const UnicodeString & | value1, | ||
UnicodeString & | appendTo, | ||
UErrorCode & | errorCode | ||
) | const |
Formats the given values, appending to the appendTo builder.
An argument value must not be the same object as appendTo. getArgumentLimit() must be at most 2.
value0 | Value for argument {0}. |
value1 | Value for argument {1}. |
appendTo | Gets the formatted pattern and values appended. |
errorCode | ICU error code in/out parameter. Must fulfill U_SUCCESS before the function call. |
UnicodeString& icu::SimpleFormatter::format | ( | const UnicodeString & | value0, |
const UnicodeString & | value1, | ||
const UnicodeString & | value2, | ||
UnicodeString & | appendTo, | ||
UErrorCode & | errorCode | ||
) | const |
Formats the given values, appending to the appendTo builder.
An argument value must not be the same object as appendTo. getArgumentLimit() must be at most 3.
value0 | Value for argument {0}. |
value1 | Value for argument {1}. |
value2 | Value for argument {2}. |
appendTo | Gets the formatted pattern and values appended. |
errorCode | ICU error code in/out parameter. Must fulfill U_SUCCESS before the function call. |
UnicodeString& icu::SimpleFormatter::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.
values | The argument values. An argument value must not be the same object as appendTo. Can be NULL if valuesLength==getArgumentLimit()==0. |
valuesLength | The length of the values array. Must be at least getArgumentLimit(). |
appendTo | Gets the formatted pattern and values appended. |
offsets | offsets[i] receives the offset of where values[i] replaced pattern argument {i}. Can be shorter or longer than values. Can be NULL if offsetsLength==0. If there is no {i} in the pattern, then offsets[i] is set to -1. |
offsetsLength | The length of the offsets array. |
errorCode | ICU error code in/out parameter. Must fulfill U_SUCCESS before the function call. |
UnicodeString& icu::SimpleFormatter::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.
May optimize by actually appending to the result if it is the same object as the value corresponding to the initial argument in the pattern.
values | The argument values. An argument value may be the same object as result. Can be NULL if valuesLength==getArgumentLimit()==0. |
valuesLength | The length of the values array. Must be at least getArgumentLimit(). |
result | Gets its contents replaced by the formatted pattern and values. |
offsets | offsets[i] receives the offset of where values[i] replaced pattern argument {i}. Can be shorter or longer than values. Can be NULL if offsetsLength==0. If there is no {i} in the pattern, then offsets[i] is set to -1. |
offsetsLength | The length of the offsets array. |
errorCode | ICU error code in/out parameter. Must fulfill U_SUCCESS before the function call. |
|
inline |
|
inline |
Returns the pattern text with none of the arguments.
Like formatting with all-empty string values.
Definition at line 270 of file simpleformatter.h.
|
inline |
Returns the pattern text with none of the arguments.
Like formatting with all-empty string values.
TODO(ICU-20406): Replace this with an Iterator interface.
offsets | offsets[i] receives the offset of where {i} was located before it was replaced by an empty string. For example, "a{0}b{1}" produces offset 1 for i=0 and 2 for i=1. Can be nullptr if offsetsLength==0. If there is no {i} in the pattern, then offsets[i] is set to -1. |
offsetsLength | The length of the offsets array. |
Definition at line 294 of file simpleformatter.h.
SimpleFormatter& icu::SimpleFormatter::operator= | ( | const SimpleFormatter & | other | ) |
Assignment operator.