2014-01-29 05:08:55 +00:00
|
|
|
/*
|
|
|
|
******************************************************************************
|
2016-01-07 00:20:53 +00:00
|
|
|
* Copyright (C) 2014-2016, International Business Machines
|
2014-01-29 05:08:55 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
******************************************************************************
|
2014-01-29 22:57:06 +00:00
|
|
|
* simplepatternformatter.h
|
2014-01-29 05:08:55 +00:00
|
|
|
*/
|
|
|
|
|
2014-03-17 18:16:22 +00:00
|
|
|
#ifndef __SIMPLEPATTERNFORMATTER_H__
|
|
|
|
#define __SIMPLEPATTERNFORMATTER_H__
|
2014-01-29 05:08:55 +00:00
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* 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 throw exceptions for syntax errors
|
|
|
|
* and for too few or too many arguments/placeholders.
|
|
|
|
*
|
|
|
|
* SimplePatternFormatter objects are immutable and can be safely cached like strings.
|
|
|
|
*
|
2014-01-29 05:08:55 +00:00
|
|
|
* Example:
|
|
|
|
* <pre>
|
2016-01-07 00:20:53 +00:00
|
|
|
* UErrorCode errorCode = U_ZERO_ERROR;
|
|
|
|
* SimplePatternFormatter fmt("{1} '{born}' in {0}", errorCode);
|
2014-01-29 05:08:55 +00:00
|
|
|
* UnicodeString result;
|
2016-01-07 00:20:53 +00:00
|
|
|
*
|
|
|
|
* // Output: "paul {born} in england"
|
|
|
|
* fmt.format("england", "paul", result, errorCode);
|
2014-01-29 05:08:55 +00:00
|
|
|
* </pre>
|
2016-01-07 00:20:53 +00:00
|
|
|
*
|
|
|
|
* @see MessageFormat
|
|
|
|
* @see UMessagePatternApostropheMode
|
2014-01-29 05:08:55 +00:00
|
|
|
*/
|
2014-01-29 22:57:06 +00:00
|
|
|
class U_COMMON_API SimplePatternFormatter : public UMemory {
|
2014-01-29 05:08:55 +00:00
|
|
|
public:
|
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* Default constructor.
|
2014-01-29 05:08:55 +00:00
|
|
|
*/
|
2016-01-07 00:20:53 +00:00
|
|
|
SimplePatternFormatter() : compiledPattern((UChar)0) {}
|
2014-01-29 05:08:55 +00:00
|
|
|
|
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* Constructs a formatter from the pattern string.
|
|
|
|
*
|
|
|
|
* @param pattern The pattern string.
|
2014-01-29 05:08:55 +00:00
|
|
|
*/
|
2016-01-07 00:20:53 +00:00
|
|
|
explicit SimplePatternFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
|
|
|
|
compile(pattern, errorCode);
|
|
|
|
}
|
2014-01-29 05:08:55 +00:00
|
|
|
|
2015-12-16 04:41:33 +00:00
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* Constructs a formatter from the pattern string.
|
2015-12-16 04:41:33 +00:00
|
|
|
*
|
2016-01-07 00:20:53 +00:00
|
|
|
* @param pattern The pattern string.
|
2015-12-16 04:41:33 +00:00
|
|
|
* @param min The pattern must have at least this many placeholders.
|
|
|
|
* @param max The pattern must have at most this many placeholders.
|
|
|
|
*/
|
|
|
|
SimplePatternFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
|
2016-01-07 00:20:53 +00:00
|
|
|
UErrorCode &errorCode) {
|
|
|
|
compileMinMaxPlaceholders(pattern, min, max, errorCode);
|
|
|
|
}
|
2015-12-16 04:41:33 +00:00
|
|
|
|
2014-01-29 05:08:55 +00:00
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
2016-01-07 00:20:53 +00:00
|
|
|
SimplePatternFormatter(const SimplePatternFormatter& other)
|
|
|
|
: compiledPattern(other.compiledPattern) {}
|
2014-01-29 05:08:55 +00:00
|
|
|
|
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* Assignment operator.
|
2014-01-29 05:08:55 +00:00
|
|
|
*/
|
2014-01-29 22:57:06 +00:00
|
|
|
SimplePatternFormatter &operator=(const SimplePatternFormatter& other);
|
2014-01-29 05:08:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2014-01-29 22:57:06 +00:00
|
|
|
~SimplePatternFormatter();
|
2014-01-29 05:08:55 +00:00
|
|
|
|
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* Changes this object according to the new pattern.
|
2014-01-29 05:08:55 +00:00
|
|
|
*
|
2016-01-07 00:20:53 +00:00
|
|
|
* @param pattern The pattern string.
|
|
|
|
* @return TRUE if U_SUCCESS(errorCode).
|
2014-01-29 05:08:55 +00:00
|
|
|
*/
|
2016-01-07 00:20:53 +00:00
|
|
|
UBool compile(const UnicodeString &pattern, UErrorCode &errorCode) {
|
|
|
|
return compileMinMaxPlaceholders(pattern, 0, INT32_MAX, errorCode);
|
2015-12-16 04:41:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* Changes this object according to the new pattern.
|
2015-12-16 04:41:33 +00:00
|
|
|
*
|
2016-01-07 00:20:53 +00:00
|
|
|
* @param pattern The pattern string.
|
2015-12-16 04:41:33 +00:00
|
|
|
* @param min The pattern must have at least this many placeholders.
|
|
|
|
* @param max The pattern must have at most this many placeholders.
|
2016-01-07 00:20:53 +00:00
|
|
|
* @return TRUE if U_SUCCESS(errorCode).
|
2015-12-16 04:41:33 +00:00
|
|
|
*/
|
|
|
|
UBool compileMinMaxPlaceholders(const UnicodeString &pattern,
|
2016-01-07 00:20:53 +00:00
|
|
|
int32_t min, int32_t max, UErrorCode &errorCode);
|
2014-01-29 05:08:55 +00:00
|
|
|
|
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* @return The max argument number/placeholder ID + 1.
|
2014-01-29 05:08:55 +00:00
|
|
|
*/
|
|
|
|
int32_t getPlaceholderCount() const {
|
2016-01-07 00:20:53 +00:00
|
|
|
return getPlaceholderCount(compiledPattern.getBuffer(), compiledPattern.length());
|
2014-01-29 22:57:06 +00:00
|
|
|
}
|
2014-01-29 05:08:55 +00:00
|
|
|
|
2014-09-10 20:06:52 +00:00
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* Formats the given value, appending to the appendTo builder.
|
|
|
|
* The placeholder value must not be the same object as appendTo.
|
|
|
|
* getPlaceholderCount() must be at most 1.
|
|
|
|
*
|
|
|
|
* @param value0 Value for argument {0}.
|
|
|
|
* @param appendTo Gets the formatted pattern and value appended.
|
|
|
|
* @param errorCode ICU error code in/out parameter.
|
|
|
|
* Must fulfill U_SUCCESS before the function call.
|
|
|
|
* @return appendTo
|
2014-01-29 05:08:55 +00:00
|
|
|
*/
|
2014-01-29 22:57:06 +00:00
|
|
|
UnicodeString &format(
|
2016-01-07 00:20:53 +00:00
|
|
|
const UnicodeString &value0,
|
|
|
|
UnicodeString &appendTo, UErrorCode &errorCode) const;
|
|
|
|
|
2014-01-29 22:57:06 +00:00
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* Formats the given values, appending to the appendTo builder.
|
|
|
|
* A placeholder value must not be the same object as appendTo.
|
|
|
|
* getPlaceholderCount() must be at most 2.
|
|
|
|
*
|
|
|
|
* @param value0 Value for argument {0}.
|
|
|
|
* @param value1 Value for argument {1}.
|
|
|
|
* @param appendTo Gets the formatted pattern and values appended.
|
|
|
|
* @param errorCode ICU error code in/out parameter.
|
|
|
|
* Must fulfill U_SUCCESS before the function call.
|
|
|
|
* @return appendTo
|
2014-01-29 22:57:06 +00:00
|
|
|
*/
|
|
|
|
UnicodeString &format(
|
2016-01-07 00:20:53 +00:00
|
|
|
const UnicodeString &value0,
|
|
|
|
const UnicodeString &value1,
|
|
|
|
UnicodeString &appendTo, UErrorCode &errorCode) const;
|
|
|
|
|
2014-01-29 22:57:06 +00:00
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* Formats the given values, appending to the appendTo builder.
|
|
|
|
* A placeholder value must not be the same object as appendTo.
|
|
|
|
* getPlaceholderCount() must be at most 3.
|
|
|
|
*
|
|
|
|
* @param value0 Value for argument {0}.
|
|
|
|
* @param value1 Value for argument {1}.
|
|
|
|
* @param value2 Value for argument {2}.
|
|
|
|
* @param appendTo Gets the formatted pattern and values appended.
|
|
|
|
* @param errorCode ICU error code in/out parameter.
|
|
|
|
* Must fulfill U_SUCCESS before the function call.
|
|
|
|
* @return appendTo
|
2014-01-29 22:57:06 +00:00
|
|
|
*/
|
|
|
|
UnicodeString &format(
|
2016-01-07 00:20:53 +00:00
|
|
|
const UnicodeString &value0,
|
|
|
|
const UnicodeString &value1,
|
|
|
|
const UnicodeString &value2,
|
|
|
|
UnicodeString &appendTo, UErrorCode &errorCode) const;
|
|
|
|
|
2014-01-29 05:08:55 +00:00
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* Formats the given values, appending to the appendTo string.
|
2014-01-29 05:08:55 +00:00
|
|
|
*
|
2016-01-07 00:20:53 +00:00
|
|
|
* @param values The placeholder values.
|
|
|
|
* A placeholder value must not be the same object as appendTo.
|
|
|
|
* Can be NULL if valuesLength==getPlaceholderCount()==0.
|
|
|
|
* @param valuesLength The length of the values array.
|
|
|
|
* Must be at least getPlaceholderCount().
|
|
|
|
* @param appendTo Gets the formatted pattern and values appended.
|
|
|
|
* @param 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.
|
|
|
|
* @param offsetsLength The length of the offsets array.
|
|
|
|
* @param errorCode ICU error code in/out parameter.
|
|
|
|
* Must fulfill U_SUCCESS before the function call.
|
|
|
|
* @return appendTo
|
2014-01-29 05:08:55 +00:00
|
|
|
*/
|
2014-12-05 20:52:28 +00:00
|
|
|
UnicodeString &formatAndAppend(
|
2016-01-07 00:20:53 +00:00
|
|
|
const UnicodeString *const *values, int32_t valuesLength,
|
2014-01-29 05:08:55 +00:00
|
|
|
UnicodeString &appendTo,
|
2016-01-07 00:20:53 +00:00
|
|
|
int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
|
2014-12-05 20:52:28 +00:00
|
|
|
|
|
|
|
/**
|
2016-01-07 00:20:53 +00:00
|
|
|
* 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 initial argument's corresponding value.
|
2014-12-05 20:52:28 +00:00
|
|
|
*
|
2016-01-07 00:20:53 +00:00
|
|
|
* @param values The placeholder values.
|
|
|
|
* A placeholder value may be the same object as result.
|
|
|
|
* Can be NULL if valuesLength==getPlaceholderCount()==0.
|
|
|
|
* @param valuesLength The length of the values array.
|
|
|
|
* Must be at least getPlaceholderCount().
|
|
|
|
* @param result Gets its contents replaced by the formatted pattern and values.
|
|
|
|
* @param 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.
|
|
|
|
* @param offsetsLength The length of the offsets array.
|
|
|
|
* @param errorCode ICU error code in/out parameter.
|
|
|
|
* Must fulfill U_SUCCESS before the function call.
|
|
|
|
* @return result
|
2014-12-05 20:52:28 +00:00
|
|
|
*/
|
|
|
|
UnicodeString &formatAndReplace(
|
2016-01-07 00:20:53 +00:00
|
|
|
const UnicodeString *const *values, int32_t valuesLength,
|
2014-12-05 20:52:28 +00:00
|
|
|
UnicodeString &result,
|
2016-01-07 00:20:53 +00:00
|
|
|
int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the pattern text with none of the placeholders.
|
|
|
|
* Like formatting with all-empty string values.
|
|
|
|
*/
|
|
|
|
UnicodeString getTextWithNoPlaceholders() const {
|
|
|
|
return getTextWithNoPlaceholders(compiledPattern.getBuffer(), compiledPattern.length());
|
|
|
|
}
|
|
|
|
|
2014-01-29 05:08:55 +00:00
|
|
|
private:
|
2016-01-07 00:20:53 +00:00
|
|
|
/**
|
|
|
|
* Binary representation of the compiled pattern.
|
|
|
|
* Index 0: One more than the highest argument number.
|
|
|
|
* Followed by zero or more arguments or literal-text segments.
|
|
|
|
*
|
|
|
|
* An argument is stored as its number, less than ARG_NUM_LIMIT.
|
|
|
|
* A literal-text segment is stored as its length (at least 1) offset by ARG_NUM_LIMIT,
|
|
|
|
* followed by that many chars.
|
|
|
|
*/
|
|
|
|
UnicodeString compiledPattern;
|
2014-12-05 20:52:28 +00:00
|
|
|
|
2016-01-07 00:20:53 +00:00
|
|
|
static inline int32_t getPlaceholderCount(const UChar *compiledPattern,
|
|
|
|
int32_t compiledPatternLength) {
|
|
|
|
return compiledPatternLength == 0 ? 0 : compiledPattern[0];
|
|
|
|
}
|
2014-12-05 20:52:28 +00:00
|
|
|
|
2016-01-07 00:20:53 +00:00
|
|
|
static UnicodeString getTextWithNoPlaceholders(const UChar *compiledPattern, int32_t compiledPatternLength);
|
2014-04-02 21:43:11 +00:00
|
|
|
|
2016-01-07 00:20:53 +00:00
|
|
|
static UnicodeString &format(
|
|
|
|
const UChar *compiledPattern, int32_t compiledPatternLength,
|
|
|
|
const UnicodeString *const *values,
|
|
|
|
UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
|
|
|
|
int32_t *offsets, int32_t offsetsLength,
|
|
|
|
UErrorCode &errorCode);
|
2014-01-29 05:08:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|