ICU-10896 turn SimplePatternFormatter into public SimpleFormatter

X-SVN-Rev: 38195
This commit is contained in:
Markus Scherer 2016-01-23 05:20:23 +00:00
parent 00122edbf6
commit 57f01a8756
17 changed files with 277 additions and 225 deletions

View file

@ -1,6 +1,6 @@
#******************************************************************************
#
# Copyright (C) 1999-2015, International Business Machines
# Copyright (C) 1999-2016, International Business Machines
# Corporation and others. All Rights Reserved.
#
#******************************************************************************
@ -105,7 +105,7 @@ serv.o servnotf.o servls.o servlk.o servlkf.o servrbf.o servslkf.o \
uidna.o usprep.o uts46.o punycode.o \
util.o util_props.o parsepos.o locbased.o cwchar.o wintz.o dtintrv.o ucnvsel.o propsvec.o \
ulist.o uloc_tag.o icudataver.o icuplug.o listformatter.o ulistformatter.o \
sharedobject.o simplepatternformatter.o unifiedcache.o uloc_keytype.o \
sharedobject.o simpleformatter.o unifiedcache.o uloc_keytype.o \
pluralmap.o
## Header files to install

View file

@ -446,7 +446,7 @@
<ClCompile Include="schriter.cpp" />
<ClCompile Include="stringpiece.cpp" />
<ClCompile Include="stringtriebuilder.cpp" />
<ClCompile Include="simplepatternformatter.cpp" />
<ClCompile Include="simpleformatter.cpp" />
<ClCompile Include="ucasemap.cpp" />
<ClCompile Include="ucasemap_titlecase_brkiter.cpp" />
<ClCompile Include="ucharstrie.cpp" />
@ -1089,7 +1089,20 @@
</CustomBuild>
<ClInclude Include="sharedobject.h" />
<ClCompile Include="sharedobject.cpp" />
<ClInclude Include="simplepatternformatter.h" />
<CustomBuild Include="unicode\simpleformatter.h">
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">copy "%(FullPath)" ..\..\include\unicode
</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">copy "%(FullPath)" ..\..\include\unicode
</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">copy "%(FullPath)" ..\..\include\unicode
</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">copy "%(FullPath)" ..\..\include\unicode
</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs>
</CustomBuild>
<CustomBuild Include="unicode\ucat.h">
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">copy "%(FullPath)" ..\..\include\unicode
</Command>

View file

@ -280,7 +280,7 @@
<ClCompile Include="parsepos.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="simplepatternformatter.cpp">
<ClCompile Include="simpleformatter.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="ustrfmt.c">
@ -751,9 +751,9 @@
<ClInclude Include="umapfile.h">
<Filter>data &amp; memory</Filter>
</ClInclude>
<ClInclude Include="simplepatternformatter.h">
<CustomBuild Include="unicode\simpleformatter.h">
<Filter>formatting</Filter>
</ClInclude>
</CustomBuild>
<ClInclude Include="ustrfmt.h">
<Filter>formatting</Filter>
</ClInclude>

View file

@ -15,7 +15,7 @@
*/
#include "unicode/listformatter.h"
#include "simplepatternformatter.h"
#include "unicode/simpleformatter.h"
#include "mutex.h"
#include "hash.h"
#include "cstring.h"
@ -27,10 +27,10 @@
U_NAMESPACE_BEGIN
struct ListFormatInternal : public UMemory {
SimplePatternFormatter twoPattern;
SimplePatternFormatter startPattern;
SimplePatternFormatter middlePattern;
SimplePatternFormatter endPattern;
SimpleFormatter twoPattern;
SimpleFormatter startPattern;
SimpleFormatter middlePattern;
SimpleFormatter endPattern;
ListFormatInternal(
const UnicodeString& two,
@ -257,7 +257,7 @@ ListFormatter::~ListFormatter() {
* according to pat. Any previous value of result gets replaced.
*/
static void joinStringsAndReplace(
const SimplePatternFormatter& pat,
const SimpleFormatter& pat,
const UnicodeString& first,
const UnicodeString& second,
UnicodeString &result,

View file

@ -3,12 +3,12 @@
* Copyright (C) 2014-2016, International Business Machines
* Corporation and others. All Rights Reserved.
******************************************************************************
* simplepatternformatter.cpp
* simpleformatter.cpp
*/
#include "unicode/utypes.h"
#include "unicode/simpleformatter.h"
#include "unicode/unistr.h"
#include "simplepatternformatter.h"
#include "uassert.h"
U_NAMESPACE_BEGIN
@ -48,8 +48,7 @@ inline UBool isInvalidArray(const void *array, int32_t length) {
} // namespace
SimplePatternFormatter &SimplePatternFormatter::operator=(
const SimplePatternFormatter& other) {
SimpleFormatter &SimpleFormatter::operator=(const SimpleFormatter& other) {
if (this == &other) {
return *this;
}
@ -57,9 +56,9 @@ SimplePatternFormatter &SimplePatternFormatter::operator=(
return *this;
}
SimplePatternFormatter::~SimplePatternFormatter() {}
SimpleFormatter::~SimpleFormatter() {}
UBool SimplePatternFormatter::compileMinMaxPlaceholders(
UBool SimpleFormatter::applyPatternMinMaxArguments(
const UnicodeString &pattern,
int32_t min, int32_t max,
UErrorCode &errorCode) {
@ -154,14 +153,14 @@ UBool SimplePatternFormatter::compileMinMaxPlaceholders(
return TRUE;
}
UnicodeString& SimplePatternFormatter::format(
UnicodeString& SimpleFormatter::format(
const UnicodeString &value0,
UnicodeString &appendTo, UErrorCode &errorCode) const {
const UnicodeString *values[] = { &value0 };
return formatAndAppend(values, 1, appendTo, NULL, 0, errorCode);
}
UnicodeString& SimplePatternFormatter::format(
UnicodeString& SimpleFormatter::format(
const UnicodeString &value0,
const UnicodeString &value1,
UnicodeString &appendTo, UErrorCode &errorCode) const {
@ -169,7 +168,7 @@ UnicodeString& SimplePatternFormatter::format(
return formatAndAppend(values, 2, appendTo, NULL, 0, errorCode);
}
UnicodeString& SimplePatternFormatter::format(
UnicodeString& SimpleFormatter::format(
const UnicodeString &value0,
const UnicodeString &value1,
const UnicodeString &value2,
@ -178,7 +177,7 @@ UnicodeString& SimplePatternFormatter::format(
return formatAndAppend(values, 3, appendTo, NULL, 0, errorCode);
}
UnicodeString& SimplePatternFormatter::formatAndAppend(
UnicodeString& SimpleFormatter::formatAndAppend(
const UnicodeString *const *values, int32_t valuesLength,
UnicodeString &appendTo,
int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const {
@ -186,7 +185,7 @@ UnicodeString& SimplePatternFormatter::formatAndAppend(
return appendTo;
}
if (isInvalidArray(values, valuesLength) || isInvalidArray(offsets, offsetsLength) ||
valuesLength < getPlaceholderCount()) {
valuesLength < getArgumentLimit()) {
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
return appendTo;
}
@ -195,7 +194,7 @@ UnicodeString& SimplePatternFormatter::formatAndAppend(
offsets, offsetsLength, errorCode);
}
UnicodeString &SimplePatternFormatter::formatAndReplace(
UnicodeString &SimpleFormatter::formatAndReplace(
const UnicodeString *const *values, int32_t valuesLength,
UnicodeString &result,
int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const {
@ -208,7 +207,7 @@ UnicodeString &SimplePatternFormatter::formatAndReplace(
}
const UChar *cp = compiledPattern.getBuffer();
int32_t cpLength = compiledPattern.length();
if (valuesLength < getPlaceholderCount(cp, cpLength)) {
if (valuesLength < getArgumentLimit(cp, cpLength)) {
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
return result;
}
@ -220,7 +219,7 @@ UnicodeString &SimplePatternFormatter::formatAndReplace(
// If any non-initial argument value is the same object as the result,
// then we first copy its contents and use that instead while formatting.
UnicodeString resultCopy;
if (getPlaceholderCount(cp, cpLength) > 0) {
if (getArgumentLimit(cp, cpLength) > 0) {
for (int32_t i = 1; i < cpLength;) {
int32_t n = cp[i++];
if (n < ARG_NUM_LIMIT) {
@ -244,10 +243,10 @@ UnicodeString &SimplePatternFormatter::formatAndReplace(
offsets, offsetsLength, errorCode);
}
UnicodeString SimplePatternFormatter::getTextWithNoPlaceholders(
UnicodeString SimpleFormatter::getTextWithNoArguments(
const UChar *compiledPattern, int32_t compiledPatternLength) {
int32_t capacity = compiledPatternLength - 1 -
getPlaceholderCount(compiledPattern, compiledPatternLength);
getArgumentLimit(compiledPattern, compiledPatternLength);
UnicodeString sb(capacity, 0, 0); // Java: StringBuilder
for (int32_t i = 1; i < compiledPatternLength;) {
int32_t segmentLength = compiledPattern[i++] - ARG_NUM_LIMIT;
@ -259,7 +258,7 @@ UnicodeString SimplePatternFormatter::getTextWithNoPlaceholders(
return sb;
}
UnicodeString &SimplePatternFormatter::format(
UnicodeString &SimpleFormatter::format(
const UChar *compiledPattern, int32_t compiledPatternLength,
const UnicodeString *const *values,
UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,

View file

@ -3,11 +3,16 @@
* Copyright (C) 2014-2016, International Business Machines
* Corporation and others. All Rights Reserved.
******************************************************************************
* simplepatternformatter.h
* simpleformatter.h
*/
#ifndef __SIMPLEPATTERNFORMATTER_H__
#define __SIMPLEPATTERNFORMATTER_H__
#ifndef __SIMPLEFORMATTER_H__
#define __SIMPLEFORMATTER_H__
/**
* \file
* \brief C++ API: Simple formatter, minimal subset of MessageFormat.
*/
#include "unicode/utypes.h"
#include "unicode/unistr.h"
@ -21,106 +26,136 @@ U_NAMESPACE_BEGIN
* and formats only string values.
* Quoting via ASCII apostrophe compatible with ICU MessageFormat default behavior.
*
* Factory methods throw exceptions for syntax errors
* Factory methods set error codes for syntax errors
* and for too few or too many arguments/placeholders.
*
* SimplePatternFormatter objects are immutable and can be safely cached like strings.
* SimpleFormatter objects are thread-safe except for assignment and applying new patterns.
*
* Example:
* <pre>
* UErrorCode errorCode = U_ZERO_ERROR;
* SimplePatternFormatter fmt("{1} '{born}' in {0}", errorCode);
* SimpleFormatter fmt("{1} '{born}' in {0}", errorCode);
* UnicodeString result;
*
* // Output: "paul {born} in england"
* fmt.format("england", "paul", result, errorCode);
* </pre>
*
* This class is not intended for public subclassing.
*
* @see MessageFormat
* @see UMessagePatternApostropheMode
* @draft ICU 57
*/
class U_COMMON_API SimplePatternFormatter : public UMemory {
class U_COMMON_API SimpleFormatter U_FINAL : public UMemory {
public:
/**
* Default constructor.
*/
SimplePatternFormatter() : compiledPattern((UChar)0) {}
SimpleFormatter() : compiledPattern((UChar)0) {}
/**
* Constructs a formatter from the pattern string.
*
* @param pattern The pattern string.
* @param 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 ICU 57
*/
explicit SimplePatternFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
compile(pattern, errorCode);
SimpleFormatter(const UnicodeString& pattern, UErrorCode &errorCode) {
applyPattern(pattern, errorCode);
}
/**
* 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.
*
* @param pattern The pattern string.
* @param min The pattern must have at least this many placeholders.
* @param max The pattern must have at most this many placeholders.
* @param min The pattern must have at least this many arguments.
* @param max The pattern must have at most this many arguments.
* @param 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 ICU 57
*/
SimplePatternFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
UErrorCode &errorCode) {
compileMinMaxPlaceholders(pattern, min, max, errorCode);
SimpleFormatter(const UnicodeString& pattern, int32_t min, int32_t max,
UErrorCode &errorCode) {
applyPatternMinMaxArguments(pattern, min, max, errorCode);
}
/**
* Copy constructor.
* @draft ICU 57
*/
SimplePatternFormatter(const SimplePatternFormatter& other)
SimpleFormatter(const SimpleFormatter& other)
: compiledPattern(other.compiledPattern) {}
/**
* Assignment operator.
* @draft ICU 57
*/
SimplePatternFormatter &operator=(const SimplePatternFormatter& other);
SimpleFormatter &operator=(const SimpleFormatter& other);
/**
* Destructor.
* @draft ICU 57
*/
~SimplePatternFormatter();
~SimpleFormatter();
/**
* Changes this object according to the new pattern.
*
* @param pattern The pattern string.
* @param 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.
* @return TRUE if U_SUCCESS(errorCode).
* @draft ICU 57
*/
UBool compile(const UnicodeString &pattern, UErrorCode &errorCode) {
return compileMinMaxPlaceholders(pattern, 0, INT32_MAX, errorCode);
UBool applyPattern(const UnicodeString &pattern, UErrorCode &errorCode) {
return applyPatternMinMaxArguments(pattern, 0, INT32_MAX, 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.
*
* @param pattern The pattern string.
* @param min The pattern must have at least this many placeholders.
* @param max The pattern must have at most this many placeholders.
* @param min The pattern must have at least this many arguments.
* @param max The pattern must have at most this many arguments.
* @param 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.
* @return TRUE if U_SUCCESS(errorCode).
* @draft ICU 57
*/
UBool compileMinMaxPlaceholders(const UnicodeString &pattern,
int32_t min, int32_t max, UErrorCode &errorCode);
UBool applyPatternMinMaxArguments(const UnicodeString &pattern,
int32_t min, int32_t max, UErrorCode &errorCode);
/**
* @return The max argument number/placeholder ID + 1.
* @return The max argument number + 1.
* @draft ICU 57
*/
int32_t getPlaceholderCount() const {
return getPlaceholderCount(compiledPattern.getBuffer(), compiledPattern.length());
int32_t getArgumentLimit() const {
return getArgumentLimit(compiledPattern.getBuffer(), compiledPattern.length());
}
/**
* 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.
* The argument value must not be the same object as appendTo.
* getArgumentLimit() 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
* @draft ICU 57
*/
UnicodeString &format(
const UnicodeString &value0,
@ -128,8 +163,8 @@ public:
/**
* 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.
* An argument value must not be the same object as appendTo.
* getArgumentLimit() must be at most 2.
*
* @param value0 Value for argument {0}.
* @param value1 Value for argument {1}.
@ -137,6 +172,7 @@ public:
* @param errorCode ICU error code in/out parameter.
* Must fulfill U_SUCCESS before the function call.
* @return appendTo
* @draft ICU 57
*/
UnicodeString &format(
const UnicodeString &value0,
@ -145,8 +181,8 @@ public:
/**
* 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.
* An argument value must not be the same object as appendTo.
* getArgumentLimit() must be at most 3.
*
* @param value0 Value for argument {0}.
* @param value1 Value for argument {1}.
@ -155,6 +191,7 @@ public:
* @param errorCode ICU error code in/out parameter.
* Must fulfill U_SUCCESS before the function call.
* @return appendTo
* @draft ICU 57
*/
UnicodeString &format(
const UnicodeString &value0,
@ -165,11 +202,11 @@ public:
/**
* Formats the given values, appending to the appendTo string.
*
* @param values The placeholder values.
* A placeholder value must not be the same object as appendTo.
* Can be NULL if valuesLength==getPlaceholderCount()==0.
* @param values The argument values.
* An argument value must not be the same object as appendTo.
* Can be NULL if valuesLength==getArgumentLimit()==0.
* @param valuesLength The length of the values array.
* Must be at least getPlaceholderCount().
* Must be at least getArgumentLimit().
* @param appendTo Gets the formatted pattern and values appended.
* @param offsets offsets[i] receives the offset of where
* values[i] replaced pattern argument {i}.
@ -179,6 +216,7 @@ public:
* @param errorCode ICU error code in/out parameter.
* Must fulfill U_SUCCESS before the function call.
* @return appendTo
* @draft ICU 57
*/
UnicodeString &formatAndAppend(
const UnicodeString *const *values, int32_t valuesLength,
@ -188,13 +226,13 @@ public:
/**
* 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.
* as the value corresponding to the initial argument in the pattern.
*
* @param values The placeholder values.
* A placeholder value may be the same object as result.
* Can be NULL if valuesLength==getPlaceholderCount()==0.
* @param values The argument values.
* An argument value may be the same object as result.
* Can be NULL if valuesLength==getArgumentLimit()==0.
* @param valuesLength The length of the values array.
* Must be at least getPlaceholderCount().
* Must be at least getArgumentLimit().
* @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}.
@ -204,6 +242,7 @@ public:
* @param errorCode ICU error code in/out parameter.
* Must fulfill U_SUCCESS before the function call.
* @return result
* @draft ICU 57
*/
UnicodeString &formatAndReplace(
const UnicodeString *const *values, int32_t valuesLength,
@ -211,11 +250,12 @@ public:
int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const;
/**
* Returns the pattern text with none of the placeholders.
* Returns the pattern text with none of the arguments.
* Like formatting with all-empty string values.
* @draft ICU 57
*/
UnicodeString getTextWithNoPlaceholders() const {
return getTextWithNoPlaceholders(compiledPattern.getBuffer(), compiledPattern.length());
UnicodeString getTextWithNoArguments() const {
return getTextWithNoArguments(compiledPattern.getBuffer(), compiledPattern.length());
}
private:
@ -230,12 +270,12 @@ private:
*/
UnicodeString compiledPattern;
static inline int32_t getPlaceholderCount(const UChar *compiledPattern,
static inline int32_t getArgumentLimit(const UChar *compiledPattern,
int32_t compiledPatternLength) {
return compiledPatternLength == 0 ? 0 : compiledPattern[0];
}
static UnicodeString getTextWithNoPlaceholders(const UChar *compiledPattern, int32_t compiledPatternLength);
static UnicodeString getTextWithNoArguments(const UChar *compiledPattern, int32_t compiledPatternLength);
static UnicodeString &format(
const UChar *compiledPattern, int32_t compiledPatternLength,
@ -247,4 +287,4 @@ private:
U_NAMESPACE_END
#endif
#endif // __SIMPLEFORMATTER_H__

View file

@ -18,7 +18,7 @@
#include "currfmt.h"
#include "unicode/localpointer.h"
#include "resource.h"
#include "simplepatternformatter.h"
#include "unicode/simpleformatter.h"
#include "quantityformatter.h"
#include "unicode/plurrule.h"
#include "unicode/decimfmt.h"
@ -107,8 +107,8 @@ public:
*/
UMeasureFormatWidth widthFallback[WIDTH_INDEX_COUNT];
/** Measure unit -> format width -> array of patterns ("{0} meters") (plurals + PER_UNIT_INDEX) */
SimplePatternFormatter *patterns[MEAS_UNIT_COUNT][WIDTH_INDEX_COUNT][PATTERN_COUNT];
SimplePatternFormatter perFormatters[WIDTH_INDEX_COUNT];
SimpleFormatter *patterns[MEAS_UNIT_COUNT][WIDTH_INDEX_COUNT][PATTERN_COUNT];
SimpleFormatter perFormatters[WIDTH_INDEX_COUNT];
MeasureFormatCacheData();
virtual ~MeasureFormatCacheData();
@ -116,7 +116,7 @@ public:
UBool hasPerFormatter(int32_t width) const {
// TODO: Create a more obvious way to test if the per-formatter has been set?
// Use pointers, check for NULL? Or add an isValid() method?
return perFormatters[width].getPlaceholderCount() == 2;
return perFormatters[width].getArgumentLimit() == 2;
}
void adoptCurrencyFormat(int32_t widthIndex, NumberFormat *nfToAdopt) {
@ -224,10 +224,10 @@ struct UnitDataSink : public ResourceTableSink {
void setFormatterIfAbsent(int32_t index, const ResourceValue &value,
int32_t minPlaceholders, UErrorCode &errorCode) {
SimplePatternFormatter **patterns =
SimpleFormatter **patterns =
&outer.cacheData.patterns[outer.unitIndex][outer.width][0];
if (U_SUCCESS(errorCode) && patterns[index] == NULL) {
patterns[index] = new SimplePatternFormatter(
patterns[index] = new SimpleFormatter(
value.getUnicodeString(errorCode), minPlaceholders, 1, errorCode);
if (U_SUCCESS(errorCode) && patterns[index] == NULL) {
errorCode = U_MEMORY_ALLOCATION_ERROR;
@ -282,7 +282,7 @@ struct UnitDataSink : public ResourceTableSink {
virtual void put(const char *key, const ResourceValue &value, UErrorCode &errorCode) {
if (U_SUCCESS(errorCode) && uprv_strcmp(key, "per") == 0) {
outer.cacheData.perFormatters[outer.width].
compileMinMaxPlaceholders(value.getUnicodeString(errorCode), 2, 2, errorCode);
applyPatternMinMaxArguments(value.getUnicodeString(errorCode), 2, 2, errorCode);
}
}
UnitDataSink &outer;
@ -904,7 +904,7 @@ UnicodeString &MeasureFormat::formatMeasure(
UnicodeString formattedNumber;
StandardPlural::Form pluralForm = QuantityFormatter::selectPlural(
amtNumber, nf, **pluralRules, formattedNumber, pos, status);
const SimplePatternFormatter *formatter = getPluralFormatter(amtUnit, width, pluralForm, status);
const SimpleFormatter *formatter = getPluralFormatter(amtUnit, width, pluralForm, status);
return QuantityFormatter::format(*formatter, formattedNumber, appendTo, pos, status);
}
@ -1037,10 +1037,10 @@ UnicodeString &MeasureFormat::formatNumeric(
return appendTo;
}
const SimplePatternFormatter *MeasureFormat::getFormatterOrNull(
const SimpleFormatter *MeasureFormat::getFormatterOrNull(
const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index) const {
width = getRegularWidth(width);
SimplePatternFormatter *const (*unitPatterns)[MeasureFormatCacheData::PATTERN_COUNT] =
SimpleFormatter *const (*unitPatterns)[MeasureFormatCacheData::PATTERN_COUNT] =
&cache->patterns[unit.getIndex()][0];
if (unitPatterns[width][index] != NULL) {
return unitPatterns[width][index];
@ -1052,27 +1052,27 @@ const SimplePatternFormatter *MeasureFormat::getFormatterOrNull(
return NULL;
}
const SimplePatternFormatter *MeasureFormat::getFormatter(
const SimpleFormatter *MeasureFormat::getFormatter(
const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
UErrorCode &errorCode) const {
if (U_FAILURE(errorCode)) {
return NULL;
}
const SimplePatternFormatter *pattern = getFormatterOrNull(unit, width, index);
const SimpleFormatter *pattern = getFormatterOrNull(unit, width, index);
if (pattern == NULL) {
errorCode = U_MISSING_RESOURCE_ERROR;
}
return pattern;
}
const SimplePatternFormatter *MeasureFormat::getPluralFormatter(
const SimpleFormatter *MeasureFormat::getPluralFormatter(
const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
UErrorCode &errorCode) const {
if (U_FAILURE(errorCode)) {
return NULL;
}
if (index != StandardPlural::OTHER) {
const SimplePatternFormatter *pattern = getFormatterOrNull(unit, width, index);
const SimpleFormatter *pattern = getFormatterOrNull(unit, width, index);
if (pattern != NULL) {
return pattern;
}
@ -1080,20 +1080,20 @@ const SimplePatternFormatter *MeasureFormat::getPluralFormatter(
return getFormatter(unit, width, StandardPlural::OTHER, errorCode);
}
const SimplePatternFormatter *MeasureFormat::getPerFormatter(
const SimpleFormatter *MeasureFormat::getPerFormatter(
UMeasureFormatWidth width,
UErrorCode &status) const {
if (U_FAILURE(status)) {
return NULL;
}
width = getRegularWidth(width);
const SimplePatternFormatter * perFormatters = cache->perFormatters;
if (perFormatters[width].getPlaceholderCount() == 2) {
const SimpleFormatter * perFormatters = cache->perFormatters;
if (perFormatters[width].getArgumentLimit() == 2) {
return &perFormatters[width];
}
int32_t fallbackWidth = cache->widthFallback[width];
if (fallbackWidth != UMEASFMT_WIDTH_COUNT &&
perFormatters[fallbackWidth].getPlaceholderCount() == 2) {
perFormatters[fallbackWidth].getArgumentLimit() == 2) {
return &perFormatters[fallbackWidth];
}
status = U_MISSING_RESOURCE_ERROR;
@ -1109,7 +1109,7 @@ int32_t MeasureFormat::withPerUnitAndAppend(
if (U_FAILURE(status)) {
return offset;
}
const SimplePatternFormatter *perUnitFormatter =
const SimpleFormatter *perUnitFormatter =
getFormatterOrNull(perUnit, width, MeasureFormatCacheData::PER_UNIT_INDEX);
if (perUnitFormatter != NULL) {
const UnicodeString *params[] = {&formatted};
@ -1122,13 +1122,13 @@ int32_t MeasureFormat::withPerUnitAndAppend(
status);
return offset;
}
const SimplePatternFormatter *perFormatter = getPerFormatter(width, status);
const SimplePatternFormatter *pattern =
const SimpleFormatter *perFormatter = getPerFormatter(width, status);
const SimpleFormatter *pattern =
getPluralFormatter(perUnit, width, StandardPlural::ONE, status);
if (U_FAILURE(status)) {
return offset;
}
UnicodeString perUnitString = pattern->getTextWithNoPlaceholders();
UnicodeString perUnitString = pattern->getTextWithNoArguments();
perUnitString.trim();
const UnicodeString *params[] = {&formatted, &perUnitString};
perFormatter->formatAndAppend(

View file

@ -1,6 +1,6 @@
/*
******************************************************************************
* Copyright (C) 2014-2015, International Business Machines
* Copyright (C) 2014-2016, International Business Machines
* Corporation and others. All Rights Reserved.
******************************************************************************
* quantityformatter.cpp
@ -10,8 +10,8 @@
#if !UCONFIG_NO_FORMATTING
#include "unicode/simpleformatter.h"
#include "quantityformatter.h"
#include "simplepatternformatter.h"
#include "uassert.h"
#include "unicode/unistr.h"
#include "unicode/decimfmt.h"
@ -37,7 +37,7 @@ QuantityFormatter::QuantityFormatter(const QuantityFormatter &other) {
if (other.formatters[i] == NULL) {
formatters[i] = NULL;
} else {
formatters[i] = new SimplePatternFormatter(*other.formatters[i]);
formatters[i] = new SimpleFormatter(*other.formatters[i]);
}
}
}
@ -52,7 +52,7 @@ QuantityFormatter &QuantityFormatter::operator=(
if (other.formatters[i] == NULL) {
formatters[i] = NULL;
} else {
formatters[i] = new SimplePatternFormatter(*other.formatters[i]);
formatters[i] = new SimpleFormatter(*other.formatters[i]);
}
}
return *this;
@ -82,7 +82,7 @@ UBool QuantityFormatter::addIfAbsent(
if (formatters[pluralIndex] != NULL) {
return TRUE;
}
SimplePatternFormatter *newFmt = new SimplePatternFormatter(rawPattern, 0, 1, status);
SimpleFormatter *newFmt = new SimpleFormatter(rawPattern, 0, 1, status);
if (newFmt == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return FALSE;
@ -99,11 +99,11 @@ UBool QuantityFormatter::isValid() const {
return formatters[StandardPlural::OTHER] != NULL;
}
const SimplePatternFormatter *QuantityFormatter::getByVariant(
const SimpleFormatter *QuantityFormatter::getByVariant(
const char *variant) const {
U_ASSERT(isValid());
int32_t pluralIndex = StandardPlural::indexOrOtherIndexFromString(variant);
const SimplePatternFormatter *pattern = formatters[pluralIndex];
const SimpleFormatter *pattern = formatters[pluralIndex];
if (pattern == NULL) {
pattern = formatters[StandardPlural::OTHER];
}
@ -122,7 +122,7 @@ UnicodeString &QuantityFormatter::format(
if (U_FAILURE(status)) {
return appendTo;
}
const SimplePatternFormatter *pattern = formatters[p];
const SimpleFormatter *pattern = formatters[p];
if (pattern == NULL) {
pattern = formatters[StandardPlural::OTHER];
if (pattern == NULL) {
@ -134,7 +134,7 @@ UnicodeString &QuantityFormatter::format(
}
// The following methods live here so that class PluralRules does not depend on number formatting,
// and the SimplePatternFormatter does not depend on FieldPosition.
// and the SimpleFormatter does not depend on FieldPosition.
StandardPlural::Form QuantityFormatter::selectPlural(
const Formattable &number,
@ -173,7 +173,7 @@ StandardPlural::Form QuantityFormatter::selectPlural(
}
UnicodeString &QuantityFormatter::format(
const SimplePatternFormatter &pattern,
const SimpleFormatter &pattern,
const UnicodeString &value,
UnicodeString &appendTo,
FieldPosition &pos,

View file

@ -1,6 +1,6 @@
/*
******************************************************************************
* Copyright (C) 2014-2015, International Business Machines
* Copyright (C) 2014-2016, International Business Machines
* Corporation and others. All Rights Reserved.
******************************************************************************
* quantityformatter.h
@ -18,7 +18,7 @@
U_NAMESPACE_BEGIN
class SimplePatternFormatter;
class SimpleFormatter;
class UnicodeString;
class PluralRules;
class NumberFormat;
@ -85,7 +85,7 @@ public:
* If isValid() returns TRUE, this method is guaranteed to return a
* non-NULL value.
*/
const SimplePatternFormatter *getByVariant(const char *variant) const;
const SimpleFormatter *getByVariant(const char *variant) const;
/**
* Formats a number with this object appending the result to appendTo.
@ -122,14 +122,14 @@ public:
* Formats the pattern with the value and adjusts the FieldPosition.
*/
static UnicodeString &format(
const SimplePatternFormatter &pattern,
const SimpleFormatter &pattern,
const UnicodeString &value,
UnicodeString &appendTo,
FieldPosition &pos,
UErrorCode &status);
private:
SimplePatternFormatter *formatters[StandardPlural::COUNT];
SimpleFormatter *formatters[StandardPlural::COUNT];
};
U_NAMESPACE_END

View file

@ -1,6 +1,6 @@
/*
**********************************************************************
* Copyright (c) 2004-2015, International Business Machines
* Copyright (c) 2004-2016, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Author: Alan Liu
@ -78,7 +78,7 @@ class MeasureFormatCacheData;
class SharedNumberFormat;
class SharedPluralRules;
class QuantityFormatter;
class SimplePatternFormatter;
class SimpleFormatter;
class ListFormatter;
class DateFormat;
@ -327,18 +327,18 @@ class U_I18N_API MeasureFormat : public Format {
// shared across instances.
ListFormatter *listFormatter;
const SimplePatternFormatter *getFormatterOrNull(
const SimpleFormatter *getFormatterOrNull(
const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index) const;
const SimplePatternFormatter *getFormatter(
const SimpleFormatter *getFormatter(
const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
UErrorCode &errorCode) const;
const SimplePatternFormatter *getPluralFormatter(
const SimpleFormatter *getPluralFormatter(
const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
UErrorCode &errorCode) const;
const SimplePatternFormatter *getPerFormatter(
const SimpleFormatter *getPerFormatter(
UMeasureFormatWidth width,
UErrorCode &status) const;

View file

@ -1,5 +1,5 @@
# -*- icu-dependencies -*-
# Copyright (C) 2011-2015, International Business Machines
# Copyright (C) 2011-2016, International Business Machines
# Corporation and others. All Rights Reserved.
#
# file name: dependencies.txt
@ -150,7 +150,7 @@ library: common
resourcebundle service_registration resbund_cnv ures_cnv icudataver ucat
loclikely
conversion converter_selector ucnv_set ucnvdisp
messagepattern simplepatternformatter
messagepattern simpleformatter
icu_utility icu_utility_with_props
ustr_wcs
unifiedcache
@ -400,8 +400,8 @@ group: messagepattern # for MessageFormat and tools
deps
patternprops platform
group: simplepatternformatter
simplepatternformatter.o
group: simpleformatter
simpleformatter.o
deps
platform
@ -525,7 +525,7 @@ group: service_registration
group: listformatter
listformatter.o ulistformatter.o
deps
resourcebundle simplepatternformatter
resourcebundle simpleformatter
group: ucat # message-catalog-like API
ucat.o

View file

@ -1,6 +1,6 @@
#******************************************************************************
#
# Copyright (C) 1999-2015, International Business Machines
# Copyright (C) 1999-2016, International Business Machines
# Corporation and others. All Rights Reserved.
#
#******************************************************************************
@ -57,7 +57,7 @@ uobjtest.o idnaref.o idnaconf.o nptrans.o punyref.o testidn.o testidna.o uts46te
incaltst.o calcasts.o v32test.o uvectest.o textfile.o tokiter.o utxttest.o \
windttst.o winnmtst.o winutil.o csdetest.o tzrulets.o tzoffloc.o tzfmttst.o ssearch.o dtifmtts.o \
tufmtts.o itspoof.o simplethread.o bidiconf.o locnmtst.o dcfmtest.o alphaindextst.o listformattertest.o genderinfotest.o compactdecimalformattest.o regiontst.o \
reldatefmttest.o simplepatternformattertest.o measfmttest.o numfmtspectest.o unifiedcachetest.o quantityformattertest.o \
reldatefmttest.o simpleformattertest.o measfmttest.o numfmtspectest.o unifiedcachetest.o quantityformattertest.o \
scientificnumberformattertest.o datadrivennumberformattestsuite.o \
numberformattesttuple.o numberformat2test.o pluralmaptest.o

View file

@ -329,7 +329,7 @@
<ClCompile Include="sdtfmtts.cpp" />
<ClCompile Include="selfmts.cpp" />
<ClCompile Include="tchcfmt.cpp" />
<ClCompile Include="simplepatternformattertest.cpp" />
<ClCompile Include="simpleformattertest.cpp" />
<ClCompile Include="tfsmalls.cpp" />
<ClCompile Include="tmsgfmt.cpp" />
<ClCompile Include="tsdate.cpp" />

View file

@ -292,7 +292,7 @@
<ClCompile Include="selfmts.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="simplepatternformattertest.cpp">
<ClCompile Include="simpleformattertest.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="tchcfmt.cpp">

View file

@ -1,6 +1,6 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2015, International Business Machines Corporation and
* Copyright (c) 1997-2016, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
@ -34,7 +34,7 @@ extern IntlTest *createBytesTrieTest();
static IntlTest *createLocalPointerTest();
extern IntlTest *createUCharsTrieTest();
static IntlTest *createEnumSetTest();
extern IntlTest *createSimplePatternFormatterTest();
extern IntlTest *createSimpleFormatterTest();
extern IntlTest *createUnifiedCacheTest();
extern IntlTest *createQuantityFormatterTest();
extern IntlTest *createPluralMapTest();
@ -102,10 +102,10 @@ void IntlTestUtilities::runIndexedTest( int32_t index, UBool exec, const char* &
}
break;
case 20:
name = "SimplePatternFormatterTest";
name = "SimpleFormatterTest";
if (exec) {
logln("TestSuite SimplePatternFormatterTest---"); logln();
LocalPointer<IntlTest> test(createSimplePatternFormatterTest());
logln("TestSuite SimpleFormatterTest---"); logln();
LocalPointer<IntlTest> test(createSimpleFormatterTest());
callTest(*test, par);
}
break;

View file

@ -11,7 +11,7 @@
#include "cstring.h"
#include "intltest.h"
#include "quantityformatter.h"
#include "simplepatternformatter.h"
#include "unicode/simpleformatter.h"
#include "unicode/numfmt.h"
#include "unicode/plurrule.h"
@ -59,19 +59,19 @@ void QuantityFormatterTest::TestBasic() {
assertEquals(
"getByVariant",
fmt.getByVariant("bad variant")->getTextWithNoPlaceholders(),
fmt.getByVariant("bad variant")->getTextWithNoArguments(),
" pounds");
assertEquals(
"getByVariant",
fmt.getByVariant("other")->getTextWithNoPlaceholders(),
fmt.getByVariant("other")->getTextWithNoArguments(),
" pounds");
assertEquals(
"getByVariant",
fmt.getByVariant("one")->getTextWithNoPlaceholders(),
fmt.getByVariant("one")->getTextWithNoArguments(),
" pound");
assertEquals(
"getByVariant",
fmt.getByVariant("few")->getTextWithNoPlaceholders(),
fmt.getByVariant("few")->getTextWithNoArguments(),
" pounds");
// Test copy constructor
@ -79,15 +79,15 @@ void QuantityFormatterTest::TestBasic() {
QuantityFormatter copied(fmt);
assertEquals(
"copied getByVariant",
copied.getByVariant("other")->getTextWithNoPlaceholders(),
copied.getByVariant("other")->getTextWithNoArguments(),
" pounds");
assertEquals(
"copied getByVariant",
copied.getByVariant("one")->getTextWithNoPlaceholders(),
copied.getByVariant("one")->getTextWithNoArguments(),
" pound");
assertEquals(
"copied getByVariant",
copied.getByVariant("few")->getTextWithNoPlaceholders(),
copied.getByVariant("few")->getTextWithNoArguments(),
" pounds");
}
@ -97,15 +97,15 @@ void QuantityFormatterTest::TestBasic() {
assigned = fmt;
assertEquals(
"assigned getByVariant",
assigned.getByVariant("other")->getTextWithNoPlaceholders(),
assigned.getByVariant("other")->getTextWithNoArguments(),
" pounds");
assertEquals(
"assigned getByVariant",
assigned.getByVariant("one")->getTextWithNoPlaceholders(),
assigned.getByVariant("one")->getTextWithNoArguments(),
" pound");
assertEquals(
"assigned getByVariant",
assigned.getByVariant("few")->getTextWithNoPlaceholders(),
assigned.getByVariant("few")->getTextWithNoArguments(),
" pounds");
}

View file

@ -4,7 +4,7 @@
* others. All Rights Reserved.
*******************************************************************************
*
* File SIMPLEPATTERNFORMATTERTEST.CPP
* simpleformattertest.cpp
*
********************************************************************************
*/
@ -13,24 +13,24 @@
#include "unicode/unistr.h"
#include "cstring.h"
#include "intltest.h"
#include "simplepatternformatter.h"
#include "unicode/simpleformatter.h"
class SimplePatternFormatterTest : public IntlTest {
class SimpleFormatterTest : public IntlTest {
public:
SimplePatternFormatterTest() {
SimpleFormatterTest() {
}
void TestNoPlaceholders();
void TestNoArguments();
void TestSyntaxErrors();
void TestOnePlaceholder();
void TestBigPlaceholder();
void TestManyPlaceholders();
void TestTooFewPlaceholderValues();
void TestOneArgument();
void TestBigArgument();
void TestManyArguments();
void TestTooFewArgumentValues();
void TestBadArguments();
void TestTextWithNoPlaceholders();
void TestTextWithNoArguments();
void TestFormatReplaceNoOptimization();
void TestFormatReplaceNoOptimizationLeadingText();
void TestFormatReplaceOptimization();
void TestFormatReplaceNoOptimizationLeadingPlaceholderUsedTwice();
void TestFormatReplaceNoOptimizationLeadingArgumentUsedTwice();
void TestFormatReplaceOptimizationNoOffsets();
void TestFormatReplaceNoOptimizationNoOffsets();
void TestQuotingLikeMessageFormat();
@ -42,30 +42,30 @@ private:
int32_t count);
};
void SimplePatternFormatterTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
void SimpleFormatterTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(TestNoPlaceholders);
TESTCASE_AUTO(TestNoArguments);
TESTCASE_AUTO(TestSyntaxErrors);
TESTCASE_AUTO(TestOnePlaceholder);
TESTCASE_AUTO(TestBigPlaceholder);
TESTCASE_AUTO(TestManyPlaceholders);
TESTCASE_AUTO(TestTooFewPlaceholderValues);
TESTCASE_AUTO(TestOneArgument);
TESTCASE_AUTO(TestBigArgument);
TESTCASE_AUTO(TestManyArguments);
TESTCASE_AUTO(TestTooFewArgumentValues);
TESTCASE_AUTO(TestBadArguments);
TESTCASE_AUTO(TestTextWithNoPlaceholders);
TESTCASE_AUTO(TestTextWithNoArguments);
TESTCASE_AUTO(TestFormatReplaceNoOptimization);
TESTCASE_AUTO(TestFormatReplaceNoOptimizationLeadingText);
TESTCASE_AUTO(TestFormatReplaceOptimization);
TESTCASE_AUTO(TestFormatReplaceNoOptimizationLeadingPlaceholderUsedTwice);
TESTCASE_AUTO(TestFormatReplaceNoOptimizationLeadingArgumentUsedTwice);
TESTCASE_AUTO(TestFormatReplaceOptimizationNoOffsets);
TESTCASE_AUTO(TestFormatReplaceNoOptimizationNoOffsets);
TESTCASE_AUTO(TestQuotingLikeMessageFormat);
TESTCASE_AUTO_END;
}
void SimplePatternFormatterTest::TestNoPlaceholders() {
void SimpleFormatterTest::TestNoArguments() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt("This doesn''t have templates '{0}", status);
assertEquals("getPlaceholderCount", 0, fmt.getPlaceholderCount());
SimpleFormatter fmt("This doesn''t have templates '{0}", status);
assertEquals("getArgumentLimit", 0, fmt.getArgumentLimit());
UnicodeString appendTo;
assertEquals(
"format",
@ -85,23 +85,23 @@ void SimplePatternFormatterTest::TestNoPlaceholders() {
assertSuccess("Status", status);
}
void SimplePatternFormatterTest::TestSyntaxErrors() {
void SimpleFormatterTest::TestSyntaxErrors() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt("{}", status);
SimpleFormatter fmt("{}", status);
assertEquals("syntax error {}", U_ILLEGAL_ARGUMENT_ERROR, status);
status = U_ZERO_ERROR;
fmt.compile("{12d", status);
fmt.applyPattern("{12d", status);
assertEquals("syntax error {12d", U_ILLEGAL_ARGUMENT_ERROR, status);
}
void SimplePatternFormatterTest::TestOnePlaceholder() {
void SimpleFormatterTest::TestOneArgument() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt;
fmt.compile("{0} meter", status);
SimpleFormatter fmt;
fmt.applyPattern("{0} meter", status);
if (!assertSuccess("Status", status)) {
return;
}
assertEquals("PlaceholderCount", 1, fmt.getPlaceholderCount());
assertEquals("getArgumentLimit", 1, fmt.getArgumentLimit());
UnicodeString appendTo;
assertEquals(
"format",
@ -109,7 +109,7 @@ void SimplePatternFormatterTest::TestOnePlaceholder() {
fmt.format("1", appendTo, status));
// assignment
SimplePatternFormatter s;
SimpleFormatter s;
s = fmt;
appendTo.remove();
assertEquals(
@ -118,7 +118,7 @@ void SimplePatternFormatterTest::TestOnePlaceholder() {
s.format("1", appendTo, status));
// Copy constructor
SimplePatternFormatter r(fmt);
SimpleFormatter r(fmt);
appendTo.remove();
assertEquals(
"Copy constructor",
@ -127,13 +127,13 @@ void SimplePatternFormatterTest::TestOnePlaceholder() {
assertSuccess("Status", status);
}
void SimplePatternFormatterTest::TestBigPlaceholder() {
void SimpleFormatterTest::TestBigArgument() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt("a{20}c", status);
SimpleFormatter fmt("a{20}c", status);
if (!assertSuccess("Status", status)) {
return;
}
assertEquals("{20} count", 21, fmt.getPlaceholderCount());
assertEquals("{20} count", 21, fmt.getArgumentLimit());
UnicodeString b("b");
UnicodeString *values[] = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
@ -145,15 +145,15 @@ void SimplePatternFormatterTest::TestBigPlaceholder() {
assertSuccess("Status", status);
}
void SimplePatternFormatterTest::TestManyPlaceholders() {
void SimpleFormatterTest::TestManyArguments() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt;
fmt.compile(
SimpleFormatter fmt;
fmt.applyPattern(
"Templates {2}{1}{5} and {4} are out of order.", status);
if (!assertSuccess("Status", status)) {
return;
}
assertEquals("PlaceholderCount", 6, fmt.getPlaceholderCount());
assertEquals("getArgumentLimit", 6, fmt.getArgumentLimit());
UnicodeString values[] = {
"freddy", "tommy", "frog", "billy", "leg", "{0}"};
UnicodeString *params[] = {
@ -191,7 +191,7 @@ void SimplePatternFormatterTest::TestManyPlaceholders() {
assertEquals("Offsets buffer length", 289, offsets[UPRV_LENGTHOF(offsets) - 1]);
// Test assignment
SimplePatternFormatter s;
SimpleFormatter s;
s = fmt;
appendTo.remove();
assertEquals(
@ -206,7 +206,7 @@ void SimplePatternFormatterTest::TestManyPlaceholders() {
status));
// Copy constructor
SimplePatternFormatter r(fmt);
SimpleFormatter r(fmt);
appendTo.remove();
assertEquals(
"Copy constructor",
@ -218,22 +218,22 @@ void SimplePatternFormatterTest::TestManyPlaceholders() {
NULL,
0,
status));
r.compile("{0} meter", status);
assertEquals("PlaceholderCount", 1, r.getPlaceholderCount());
r.applyPattern("{0} meter", status);
assertEquals("getArgumentLimit", 1, r.getArgumentLimit());
appendTo.remove();
assertEquals(
"Replace with new compile",
"Replace with new applyPattern",
"freddy meter",
r.format("freddy", appendTo, status));
r.compile("{0}, {1}", status);
assertEquals("PlaceholderCount", 2, r.getPlaceholderCount());
r.applyPattern("{0}, {1}", status);
assertEquals("getArgumentLimit", 2, r.getArgumentLimit());
appendTo.remove();
assertEquals(
"2 arg",
"foo, bar",
r.format("foo", "bar", appendTo, status));
r.compile("{0}, {1} and {2}", status);
assertEquals("PlaceholderCount", 3, r.getPlaceholderCount());
r.applyPattern("{0}, {1} and {2}", status);
assertEquals("getArgumentLimit", 3, r.getArgumentLimit());
appendTo.remove();
assertEquals(
"3 arg",
@ -242,9 +242,9 @@ void SimplePatternFormatterTest::TestManyPlaceholders() {
assertSuccess("Status", status);
}
void SimplePatternFormatterTest::TestTooFewPlaceholderValues() {
void SimpleFormatterTest::TestTooFewArgumentValues() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt("{0} and {1}", status);
SimpleFormatter fmt("{0} and {1}", status);
UnicodeString appendTo;
UnicodeString firstValue;
UnicodeString *params[] = {&firstValue};
@ -270,9 +270,9 @@ void SimplePatternFormatterTest::TestTooFewPlaceholderValues() {
}
}
void SimplePatternFormatterTest::TestBadArguments() {
void SimpleFormatterTest::TestBadArguments() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt("pickle", status);
SimpleFormatter fmt("pickle", status);
UnicodeString appendTo;
// These succeed
@ -300,7 +300,7 @@ void SimplePatternFormatterTest::TestBadArguments() {
status = U_ZERO_ERROR;
// fails because appendTo used as a parameter value
SimplePatternFormatter fmt2("Placeholders {0} and {1}", status);
SimpleFormatter fmt2("Arguments {0} and {1}", status);
UnicodeString frog("frog");
const UnicodeString *params[] = { &appendTo, &frog };
fmt2.formatAndAppend(params, 2, appendTo, NULL, 0, status);
@ -326,17 +326,17 @@ void SimplePatternFormatterTest::TestBadArguments() {
}
}
void SimplePatternFormatterTest::TestTextWithNoPlaceholders() {
void SimpleFormatterTest::TestTextWithNoArguments() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt("{0} has no {1} placeholders.", status);
SimpleFormatter fmt("{0} has no {1} arguments.", status);
assertEquals(
"", " has no placeholders.", fmt.getTextWithNoPlaceholders());
"", " has no arguments.", fmt.getTextWithNoArguments());
}
void SimplePatternFormatterTest::TestFormatReplaceNoOptimization() {
void SimpleFormatterTest::TestFormatReplaceNoOptimization() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt;
fmt.compile("{2}, {0}, {1} and {3}", status);
SimpleFormatter fmt;
fmt.applyPattern("{2}, {0}, {1} and {3}", status);
if (!assertSuccess("Status", status)) {
return;
}
@ -363,10 +363,10 @@ void SimplePatternFormatterTest::TestFormatReplaceNoOptimization() {
verifyOffsets(expectedOffsets, offsets, UPRV_LENGTHOF(expectedOffsets));
}
void SimplePatternFormatterTest::TestFormatReplaceNoOptimizationLeadingText() {
void SimpleFormatterTest::TestFormatReplaceNoOptimizationLeadingText() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt;
fmt.compile("boo {2}, {0}, {1} and {3}", status);
SimpleFormatter fmt;
fmt.applyPattern("boo {2}, {0}, {1} and {3}", status);
if (!assertSuccess("Status", status)) {
return;
}
@ -393,10 +393,10 @@ void SimplePatternFormatterTest::TestFormatReplaceNoOptimizationLeadingText() {
verifyOffsets(expectedOffsets, offsets, UPRV_LENGTHOF(expectedOffsets));
}
void SimplePatternFormatterTest::TestFormatReplaceOptimization() {
void SimpleFormatterTest::TestFormatReplaceOptimization() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt;
fmt.compile("{2}, {0}, {1} and {3}", status);
SimpleFormatter fmt;
fmt.applyPattern("{2}, {0}, {1} and {3}", status);
if (!assertSuccess("Status", status)) {
return;
}
@ -423,10 +423,10 @@ void SimplePatternFormatterTest::TestFormatReplaceOptimization() {
verifyOffsets(expectedOffsets, offsets, UPRV_LENGTHOF(expectedOffsets));
}
void SimplePatternFormatterTest::TestFormatReplaceNoOptimizationLeadingPlaceholderUsedTwice() {
void SimpleFormatterTest::TestFormatReplaceNoOptimizationLeadingArgumentUsedTwice() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt;
fmt.compile("{2}, {0}, {1} and {3} {2}", status);
SimpleFormatter fmt;
fmt.applyPattern("{2}, {0}, {1} and {3} {2}", status);
if (!assertSuccess("Status", status)) {
return;
}
@ -453,10 +453,10 @@ void SimplePatternFormatterTest::TestFormatReplaceNoOptimizationLeadingPlacehold
verifyOffsets(expectedOffsets, offsets, UPRV_LENGTHOF(expectedOffsets));
}
void SimplePatternFormatterTest::TestFormatReplaceOptimizationNoOffsets() {
void SimpleFormatterTest::TestFormatReplaceOptimizationNoOffsets() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt;
fmt.compile("{2}, {0}, {1} and {3}", status);
SimpleFormatter fmt;
fmt.applyPattern("{2}, {0}, {1} and {3}", status);
if (!assertSuccess("Status", status)) {
return;
}
@ -478,15 +478,15 @@ void SimplePatternFormatterTest::TestFormatReplaceOptimizationNoOffsets() {
assertSuccess("Status", status);
}
void SimplePatternFormatterTest::TestFormatReplaceNoOptimizationNoOffsets() {
void SimpleFormatterTest::TestFormatReplaceNoOptimizationNoOffsets() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt("Placeholders {0} and {1}", status);
SimpleFormatter fmt("Arguments {0} and {1}", status);
UnicodeString result("previous:");
UnicodeString frog("frog");
const UnicodeString *params[] = {&result, &frog};
assertEquals(
"",
"Placeholders previous: and frog",
"Arguments previous: and frog",
fmt.formatAndReplace(
params,
UPRV_LENGTHOF(params),
@ -497,10 +497,10 @@ void SimplePatternFormatterTest::TestFormatReplaceNoOptimizationNoOffsets() {
assertSuccess("Status", status);
}
void SimplePatternFormatterTest::TestQuotingLikeMessageFormat() {
void SimpleFormatterTest::TestQuotingLikeMessageFormat() {
UErrorCode status = U_ZERO_ERROR;
UnicodeString pattern = "{0} don't can''t '{5}''}{a' again '}'{1} to the '{end";
SimplePatternFormatter spf(pattern, status);
SimpleFormatter spf(pattern, status);
MessageFormat mf(pattern, Locale::getRoot(), status);
UnicodeString expected = "X don't can't {5}'}{a again }Y to the {end";
UnicodeString x("X"), y("Y");
@ -508,10 +508,10 @@ void SimplePatternFormatterTest::TestQuotingLikeMessageFormat() {
UnicodeString result;
FieldPosition ignore(FieldPosition::DONT_CARE);
assertEquals("MessageFormat", expected, mf.format(values, 2, result, ignore, status));
assertEquals("SimplePatternFormatter", expected, spf.format(x, y, result.remove(), status));
assertEquals("SimpleFormatter", expected, spf.format(x, y, result.remove(), status));
}
void SimplePatternFormatterTest::verifyOffsets(
void SimpleFormatterTest::verifyOffsets(
const int32_t *expected, const int32_t *actual, int32_t count) {
for (int32_t i = 0; i < count; ++i) {
if (expected[i] != actual[i]) {
@ -520,6 +520,6 @@ void SimplePatternFormatterTest::verifyOffsets(
}
}
extern IntlTest *createSimplePatternFormatterTest() {
return new SimplePatternFormatterTest();
extern IntlTest *createSimpleFormatterTest() {
return new SimpleFormatterTest();
}