ICU 58.2 58.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- Data Structures
- Data Structure Index
- Class Hierarchy
- Data Fields

Public Member Functions
icu::SimpleFormatter Class Reference

Formats simple patterns like "{1} was born in {0}". More...

`#include <simpleformatter.h>`

Inheritance diagram for icu::SimpleFormatter:

## 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...

## Detailed Description

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.

See also
  MessageFormat
  UMessagePatternApostropheMode

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

Definition at line 54 of file simpleformatter.h.

## Constructor & Destructor Documentation

icu::SimpleFormatter::SimpleFormatter | ( |  | ) |
| inline

Default constructor.

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

Definition at line 60 of file simpleformatter.h.

icu::SimpleFormatter::SimpleFormatter | ( | const UnicodeString & | pattern,
|  | UErrorCode & | errorCode
| ) |  |
| inline

Constructs a formatter from the pattern string.

Parameters

  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.

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

Definition at line 71 of file simpleformatter.h.

icu::SimpleFormatter::SimpleFormatter | ( | const UnicodeString & | pattern,
|  | int32_t | min,
|  | int32_t | max,
|  | UErrorCode & | errorCode
| ) |  |
| 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.

Parameters

  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.

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

Definition at line 89 of file simpleformatter.h.

icu::SimpleFormatter::SimpleFormatter | ( | const SimpleFormatter & | other | ) |
| inline

Copy constructor.

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

Definition at line 98 of file simpleformatter.h.

icu::SimpleFormatter::~SimpleFormatter | ( |  | ) |

Destructor.

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

## Member Function Documentation

UBool icu::SimpleFormatter::applyPattern | ( | const UnicodeString & | pattern,
|  | UErrorCode & | errorCode
| ) |  |
| inline

Changes this object according to the new pattern.

Parameters

  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.

Returns
  TRUE if U_SUCCESS(errorCode).

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

Definition at line 123 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.

Parameters

  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.

Returns
  TRUE if U_SUCCESS(errorCode).

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

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.

Parameters

  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.

Returns
  appendTo

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

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.

Parameters

  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.

Returns
  appendTo

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

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.

Parameters

  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.

Returns
  appendTo

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

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.

Parameters

  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.

Returns
  appendTo

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

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.

Parameters

  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.

Returns
  result

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

int32_t icu::SimpleFormatter::getArgumentLimit | ( |  | ) | const
| inline

Returns
  The max argument number + 1.

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

Definition at line 149 of file simpleformatter.h.

UnicodeString icu::SimpleFormatter::getTextWithNoArguments | ( |  | ) | const
| inline

Returns the pattern text with none of the arguments.

Like formatting with all-empty string values.

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

Definition at line 262 of file simpleformatter.h.

References U_NAMESPACE_END.

SimpleFormatter& icu::SimpleFormatter::operator= | ( | const SimpleFormatter & | other | ) |

Assignment operator.

Draft:
  This API may be changed in the future versions and was introduced in ICU 57

The documentation for this class was generated from the following file:

- simpleformatter.h