ICU-10646 Tempalte->SimplePatternFormatter. evaluate -> format

X-SVN-Rev: 35027
This commit is contained in:
Travis Keep 2014-01-29 22:57:06 +00:00
parent 6f61caff18
commit 844d4e860b
14 changed files with 211 additions and 170 deletions

6
.gitattributes vendored
View file

@ -53,8 +53,8 @@ icu4c/source/aclocal.m4 -text
icu4c/source/allinone/icucheck.bat -text
icu4c/source/common/common.vcxproj -text
icu4c/source/common/common.vcxproj.filters -text
icu4c/source/common/template.cpp -text
icu4c/source/common/template.h -text
icu4c/source/common/simplepatternformatter.cpp -text
icu4c/source/common/simplepatternformatter.h -text
icu4c/source/data/curr/pool.res -text
icu4c/source/data/in/coll/invuca.icu -text
icu4c/source/data/in/coll/ucadata.icu -text
@ -146,7 +146,7 @@ icu4c/source/test/cintltst/cintltst.vcxproj -text
icu4c/source/test/cintltst/cintltst.vcxproj.filters -text
icu4c/source/test/intltest/intltest.vcxproj -text
icu4c/source/test/intltest/intltest.vcxproj.filters -text
icu4c/source/test/intltest/templatetest.cpp -text
icu4c/source/test/intltest/simplepatternformattertest.cpp -text
icu4c/source/test/iotest/iotest.vcxproj -text
icu4c/source/test/iotest/iotest.vcxproj.filters -text
icu4c/source/test/letest/cletest.vcxproj -text

View file

@ -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 lrucache.o \
sharedobject.o template.o
sharedobject.o simplepatternformatter.o
## Header files to install
HEADERS = $(srcdir)/unicode/*.h

View file

@ -446,7 +446,7 @@
<ClCompile Include="schriter.cpp" />
<ClCompile Include="stringpiece.cpp" />
<ClCompile Include="stringtriebuilder.cpp" />
<ClCompile Include="template.cpp" />
<ClCompile Include="simplepatternformatter.cpp" />
<ClCompile Include="ucasemap.cpp" />
<ClCompile Include="ucasemap_titlecase_brkiter.cpp" />
<ClCompile Include="ucharstrie.cpp" />
@ -1088,7 +1088,7 @@
<ClInclude Include="sharedobject.h" />
<ClCompile Include="sharedobject.cpp" />
<ClInclude Include="sharedptr.h" />
<ClInclude Include="template.h" />
<ClInclude Include="simplepatternformatter.h" />
<CustomBuild Include="unicode\ucat.h">
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">copy "%(FullPath)" ..\..\include\unicode
</Command>

View file

@ -277,7 +277,7 @@
<ClCompile Include="parsepos.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="template.cpp">
<ClCompile Include="simplepatternformatter.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="ustrfmt.c">
@ -735,7 +735,7 @@
<ClInclude Include="umapfile.h">
<Filter>data &amp; memory</Filter>
</ClInclude>
<ClInclude Include="template.h">
<ClInclude Include="simplepatternformatter.h">
<Filter>formatting</Filter>
</ClInclude>
<ClInclude Include="ustrfmt.h">

View file

@ -15,7 +15,7 @@
*/
#include "unicode/listformatter.h"
#include "template.h"
#include "simplepatternformatter.h"
#include "mutex.h"
#include "hash.h"
#include "cstring.h"
@ -29,10 +29,10 @@
U_NAMESPACE_BEGIN
struct ListFormatInternal : public UMemory {
Template twoPattern;
Template startPattern;
Template middlePattern;
Template endPattern;
SimplePatternFormatter twoPattern;
SimplePatternFormatter startPattern;
SimplePatternFormatter middlePattern;
SimplePatternFormatter endPattern;
ListFormatInternal(
const UnicodeString& two,
@ -255,7 +255,7 @@ ListFormatter::~ListFormatter() {
* Joins first and second using the pattern pat.
*/
static void joinStrings(
const Template& pat,
const SimplePatternFormatter& pat,
const UnicodeString& first,
const UnicodeString& second,
UnicodeString &result,
@ -267,7 +267,7 @@ static void joinStrings(
}
const UnicodeString *params[2] = {&first, &second};
int32_t offsets[2];
pat.evaluate(
pat.format(
params,
LENGTHOF(params),
result,

View file

@ -3,24 +3,26 @@
* Copyright (C) 2014, International Business Machines
* Corporation and others. All Rights Reserved.
******************************************************************************
* template.cpp
* simplepatternformatter.cpp
*/
#include "template.h"
#include "simplepatternformatter.h"
#include "cstring.h"
#include "uassert.h"
#define LENGTHOF(array) (int32_t)(sizeof(array) / sizeof((array)[0]))
U_NAMESPACE_BEGIN
typedef enum TemplateCompileState {
typedef enum SimplePatternFormatterCompileState {
INIT,
APOSTROPHE,
PLACEHOLDER
} TemplateCompileState;
} SimplePatternFormatterCompileState;
class TemplateIdBuilder {
class SimplePatternFormatterIdBuilder {
public:
TemplateIdBuilder() : id(0), idLen(0) { }
~TemplateIdBuilder() { }
SimplePatternFormatterIdBuilder() : id(0), idLen(0) { }
~SimplePatternFormatterIdBuilder() { }
void reset() { id = 0; idLen = 0; }
int32_t getId() const { return id; }
void appendTo(UChar *buffer, int32_t *len) const;
@ -29,11 +31,14 @@ public:
private:
int32_t id;
int32_t idLen;
TemplateIdBuilder(const TemplateIdBuilder &other);
TemplateIdBuilder &operator=(const TemplateIdBuilder &other);
SimplePatternFormatterIdBuilder(
const SimplePatternFormatterIdBuilder &other);
SimplePatternFormatterIdBuilder &operator=(
const SimplePatternFormatterIdBuilder &other);
};
void TemplateIdBuilder::appendTo(UChar *buffer, int32_t *len) const {
void SimplePatternFormatterIdBuilder::appendTo(
UChar *buffer, int32_t *len) const {
int32_t origLen = *len;
int32_t kId = id;
for (int32_t i = origLen + idLen - 1; i >= origLen; i--) {
@ -44,12 +49,12 @@ void TemplateIdBuilder::appendTo(UChar *buffer, int32_t *len) const {
*len = origLen + idLen;
}
void TemplateIdBuilder::add(UChar ch) {
void SimplePatternFormatterIdBuilder::add(UChar ch) {
id = id * 10 + (ch - 0x30);
idLen++;
}
Template::Template() :
SimplePatternFormatter::SimplePatternFormatter() :
noPlaceholders(),
placeholdersByOffset(placeholderBuffer),
placeholderSize(0),
@ -57,7 +62,7 @@ Template::Template() :
placeholderCount(0) {
}
Template::Template(const UnicodeString &pattern) :
SimplePatternFormatter::SimplePatternFormatter(const UnicodeString &pattern) :
noPlaceholders(),
placeholdersByOffset(placeholderBuffer),
placeholderSize(0),
@ -67,7 +72,8 @@ Template::Template(const UnicodeString &pattern) :
compile(pattern, status);
}
Template::Template(const Template &other) :
SimplePatternFormatter::SimplePatternFormatter(
const SimplePatternFormatter &other) :
noPlaceholders(other.noPlaceholders),
placeholdersByOffset(placeholderBuffer),
placeholderSize(0),
@ -80,7 +86,8 @@ Template::Template(const Template &other) :
placeholderSize * 2 * sizeof(int32_t));
}
Template &Template::operator=(const Template& other) {
SimplePatternFormatter &SimplePatternFormatter::operator=(
const SimplePatternFormatter& other) {
if (this == &other) {
return *this;
}
@ -94,13 +101,14 @@ Template &Template::operator=(const Template& other) {
return *this;
}
Template::~Template() {
SimplePatternFormatter::~SimplePatternFormatter() {
if (placeholdersByOffset != placeholderBuffer) {
uprv_free(placeholdersByOffset);
}
}
UBool Template::compile(const UnicodeString &pattern, UErrorCode &status) {
UBool SimplePatternFormatter::compile(
const UnicodeString &pattern, UErrorCode &status) {
if (U_FAILURE(status)) {
return FALSE;
}
@ -110,8 +118,8 @@ UBool Template::compile(const UnicodeString &pattern, UErrorCode &status) {
int32_t len = 0;
placeholderSize = 0;
placeholderCount = 0;
TemplateCompileState state = INIT;
TemplateIdBuilder idBuilder;
SimplePatternFormatterCompileState state = INIT;
SimplePatternFormatterIdBuilder idBuilder;
for (int32_t i = 0; i < patternLength; ++i) {
UChar ch = patternBuffer[i];
switch (state) {
@ -175,14 +183,45 @@ UBool Template::compile(const UnicodeString &pattern, UErrorCode &status) {
return TRUE;
}
UnicodeString& Template::evaluate(
const UnicodeString * const *placeholderValues,
int32_t placeholderValueCount,
UnicodeString& SimplePatternFormatter::format(
const UnicodeString &arg0,
UnicodeString &appendTo,
UErrorCode &status) const {
return evaluate(
placeholderValues,
placeholderValueCount,
const UnicodeString *params[] = {&arg0};
return format(
params,
LENGTHOF(params),
appendTo,
NULL,
0,
status);
}
UnicodeString& SimplePatternFormatter::format(
const UnicodeString &arg0,
const UnicodeString &arg1,
UnicodeString &appendTo,
UErrorCode &status) const {
const UnicodeString *params[] = {&arg0, &arg1};
return format(
params,
LENGTHOF(params),
appendTo,
NULL,
0,
status);
}
UnicodeString& SimplePatternFormatter::format(
const UnicodeString &arg0,
const UnicodeString &arg1,
const UnicodeString &arg2,
UnicodeString &appendTo,
UErrorCode &status) const {
const UnicodeString *params[] = {&arg0, &arg1, &arg2};
return format(
params,
LENGTHOF(params),
appendTo,
NULL,
0,
@ -207,7 +246,7 @@ static void appendRange(
dest.append(src, start, end - start);
}
UnicodeString& Template::evaluate(
UnicodeString& SimplePatternFormatter::format(
const UnicodeString * const *placeholderValues,
int32_t placeholderValueCount,
UnicodeString &appendTo,
@ -260,7 +299,7 @@ UnicodeString& Template::evaluate(
return appendTo;
}
int32_t Template::ensureCapacity(int32_t atLeast) {
int32_t SimplePatternFormatter::ensureCapacity(int32_t atLeast) {
if (atLeast <= placeholderCapacity) {
return atLeast;
}
@ -287,7 +326,7 @@ int32_t Template::ensureCapacity(int32_t atLeast) {
return atLeast;
}
UBool Template::addPlaceholder(int32_t id, int32_t offset) {
UBool SimplePatternFormatter::addPlaceholder(int32_t id, int32_t offset) {
if (ensureCapacity(placeholderSize + 1) < placeholderSize + 1) {
return FALSE;
}

View file

@ -3,7 +3,7 @@
* Copyright (C) 2014, International Business Machines
* Corporation and others. All Rights Reserved.
******************************************************************************
* template.h
* simplepatternformatter.h
*/
#ifndef __TEMPLATE_H__
@ -17,9 +17,9 @@
U_NAMESPACE_BEGIN
/**
* Compiled version of a template such as "{1} was born in {0}".
* Compiled version of a pattern string such as "{1} was born in {0}".
* <p>
* Using Template objects is both faster and safer than adhoc replacement.
* Using SimplePatternFormatter is both faster and safer than adhoc replacement.
* They are faster because they are precompiled; they are safer because they
* account for curly braces escaped by apostrophe (').
*
@ -27,49 +27,42 @@ U_NAMESPACE_BEGIN
* by a single quote, it becomes a curly brace instead of the start of a
* placeholder. Two single quotes resolve to one single quote.
* <p>
* Concurrent calls only to const methods on a Template object are safe,
* but concurrent const and non-const method calls on a Template object
* are not safe and require synchronization.
* <p>
* Example:
* <pre>
* Template template("{1} '{born} in {0}");
* UnicodeString england("england");
* UnicodeString paul("paul");
* UnicodeString *params[] = {&england, &paul};
* SimplePatternFormatter fmt("{1} '{born} in {0}");
* UnicodeString result;
* UErrorCode status = U_ZERO_ERROR;
* // Evaluates to: "paul {born} in england"
* template.evaluate(params, 2, result, status);
* fmt.format("englad", "paul", result, status);
* </pre>
*/
class U_COMMON_API Template : public UMemory {
class U_COMMON_API SimplePatternFormatter : public UMemory {
public:
/**
* Default constructor
*/
Template();
SimplePatternFormatter();
/**
* Construct from a pattern. Will never fail if pattern has three or
* fewer placeholders in it.
*/
explicit Template(const UnicodeString& pattern);
explicit SimplePatternFormatter(const UnicodeString& pattern);
/**
* Copy constructor.
*/
Template(const Template& other);
SimplePatternFormatter(const SimplePatternFormatter& other);
/**
* Assignment operator
*/
Template &operator=(const Template& other);
SimplePatternFormatter &operator=(const SimplePatternFormatter& other);
/**
* Destructor.
*/
~Template();
~SimplePatternFormatter();
/**
* Compiles pattern and makes this object represent pattern.
@ -82,34 +75,44 @@ public:
/**
* Returns (maxPlaceholderId + 1). For example
* <code>Template("{0} {2}").getPlaceholderCount() evaluates to 3.
* Callers use this function to find out how many values are needed
* to evaluate this template.
* <code>SimplePatternFormatter("{0} {2}").getPlaceholderCount()
* evaluates to 3.
* Callers use this function to find out how many values this object
* expects when formatting.
*/
int32_t getPlaceholderCount() const {
return placeholderCount;
}
}
/**
* Evaluates this template according to the given placeholder values.
*
* The caller retains ownership of all pointers.
* @param placeholderValues 1st one corresponds to {0}; 2nd to {1};
* 3rd to {2} etc.
* @param placeholderValueCount the number of placeholder values
* must be at least large enough to provide values for all placeholders
* in this object. Otherwise status set to U_ILLEGAL_ARGUMENT_ERROR.
* @param appendTo resulting string appended here.
* @param status any error stored here.
* Formats given value.
*/
UnicodeString &evaluate(
const UnicodeString * const *placeholderValues,
int32_t placeholderValueCount,
UnicodeString &format(
const UnicodeString &args0,
UnicodeString &appendTo,
UErrorCode &status) const;
/**
* Evaluates this template according to the given placeholder values.
* Formats given values.
*/
UnicodeString &format(
const UnicodeString &args0,
const UnicodeString &args1,
UnicodeString &appendTo,
UErrorCode &status) const;
/**
* Formats given values.
*/
UnicodeString &format(
const UnicodeString &args0,
const UnicodeString &args1,
const UnicodeString &args2,
UnicodeString &appendTo,
UErrorCode &status) const;
/**
* Formats given values.
*
* The caller retains ownership of all pointers.
* @param placeholderValues 1st one corresponds to {0}; 2nd to {1};
@ -127,7 +130,7 @@ public:
* placeholderValueCount.
* @param status any error stored here.
*/
UnicodeString &evaluate(
UnicodeString &format(
const UnicodeString * const *placeholderValues,
int32_t placeholderValueCount,
UnicodeString &appendTo,

View file

@ -6,7 +6,7 @@
* quantityformatter.cpp
*/
#include "quantityformatter.h"
#include "template.h"
#include "simplepatternformatter.h"
#include "uassert.h"
#include "unicode/unistr.h"
#include "unicode/decimfmt.h"
@ -35,17 +35,17 @@ static int32_t getPluralIndex(const char *pluralForm) {
}
QuantityFormatter::QuantityFormatter() {
for (int32_t i = 0; i < LENGTHOF(templates); ++i) {
templates[i] = NULL;
for (int32_t i = 0; i < LENGTHOF(formatters); ++i) {
formatters[i] = NULL;
}
}
QuantityFormatter::QuantityFormatter(const QuantityFormatter &other) {
for (int32_t i = 0; i < LENGTHOF(templates); ++i) {
if (other.templates[i] == NULL) {
templates[i] = NULL;
for (int32_t i = 0; i < LENGTHOF(formatters); ++i) {
if (other.formatters[i] == NULL) {
formatters[i] = NULL;
} else {
templates[i] = new Template(*other.templates[i]);
formatters[i] = new SimplePatternFormatter(*other.formatters[i]);
}
}
}
@ -55,27 +55,27 @@ QuantityFormatter &QuantityFormatter::operator=(
if (this == &other) {
return *this;
}
for (int32_t i = 0; i < LENGTHOF(templates); ++i) {
delete templates[i];
if (other.templates[i] == NULL) {
templates[i] = NULL;
for (int32_t i = 0; i < LENGTHOF(formatters); ++i) {
delete formatters[i];
if (other.formatters[i] == NULL) {
formatters[i] = NULL;
} else {
templates[i] = new Template(*other.templates[i]);
formatters[i] = new SimplePatternFormatter(*other.formatters[i]);
}
}
return *this;
}
QuantityFormatter::~QuantityFormatter() {
for (int32_t i = 0; i < LENGTHOF(templates); ++i) {
delete templates[i];
for (int32_t i = 0; i < LENGTHOF(formatters); ++i) {
delete formatters[i];
}
}
void QuantityFormatter::reset() {
for (int32_t i = 0; i < LENGTHOF(templates); ++i) {
delete templates[i];
templates[i] = NULL;
for (int32_t i = 0; i < LENGTHOF(formatters); ++i) {
delete formatters[i];
formatters[i] = NULL;
}
}
@ -91,18 +91,19 @@ UBool QuantityFormatter::add(
status = U_ILLEGAL_ARGUMENT_ERROR;
return FALSE;
}
Template *newTemplate = new Template(rawPattern);
if (newTemplate == NULL) {
SimplePatternFormatter *newFmt =
new SimplePatternFormatter(rawPattern);
if (newFmt == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return FALSE;
}
if (newTemplate->getPlaceholderCount() > 1) {
delete newTemplate;
if (newFmt->getPlaceholderCount() > 1) {
delete newFmt;
status = U_ILLEGAL_ARGUMENT_ERROR;
return FALSE;
}
delete templates[pluralIndex];
templates[pluralIndex] = newTemplate;
delete formatters[pluralIndex];
formatters[pluralIndex] = newFmt;
return TRUE;
}
@ -144,9 +145,9 @@ UnicodeString &QuantityFormatter::format(
if (pluralIndex == -1) {
pluralIndex = 0;
}
const Template *pattern = templates[pluralIndex];
const SimplePatternFormatter *pattern = formatters[pluralIndex];
if (pattern == NULL) {
pattern = templates[0];
pattern = formatters[0];
}
if (pattern == NULL) {
status = U_INVALID_STATE_ERROR;
@ -155,8 +156,7 @@ UnicodeString &QuantityFormatter::format(
UnicodeString formattedNumber;
FieldPosition pos(0);
fmt.format(quantity, formattedNumber, pos, status);
UnicodeString *params[] = {&formattedNumber};
return pattern->evaluate(params, LENGTHOF(params), appendTo, status);
return pattern->format(formattedNumber, appendTo, status);
}
U_NAMESPACE_END

View file

@ -14,14 +14,14 @@
U_NAMESPACE_BEGIN
class Template;
class SimplePatternFormatter;
class UnicodeString;
class PluralRules;
class NumberFormat;
class Formattable;
/**
* A plural aware template that is good for expressing a single quantity and
* A plural aware formatter that is good for expressing a single quantity and
* a unit.
* <p>
* First use the add() methods to add a pattern for each plural variant.
@ -94,7 +94,7 @@ public:
UErrorCode &status) const;
private:
Template *templates[6];
SimplePatternFormatter *formatters[6];
};
U_NAMESPACE_END

View file

@ -56,7 +56,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 lrucachetest.o templatetest.o
reldatefmttest.o lrucachetest.o simplepatternformattertest.o
DEPS = $(OBJECTS:.o=.d)

View file

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

View file

@ -277,6 +277,9 @@
<ClCompile Include="selfmts.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="simplepatternformattertest.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="tchcfmt.cpp">
<Filter>formatting</Filter>
</ClCompile>

View file

@ -34,7 +34,7 @@ static IntlTest *createLocalPointerTest();
extern IntlTest *createUCharsTrieTest();
static IntlTest *createEnumSetTest();
extern IntlTest *createLRUCacheTest();
extern IntlTest *createTemplateTest();
extern IntlTest *createSimplePatternFormatterTest();
#define CASE(id, test) case id: \
name = #test; \
@ -106,10 +106,10 @@ void IntlTestUtilities::runIndexedTest( int32_t index, UBool exec, const char* &
}
break;
case 21:
name = "TemplateTest";
name = "SimplePatternFormatterTest";
if (exec) {
logln("TestSuite TemplateTest---"); logln();
LocalPointer<IntlTest> test(createTemplateTest());
logln("TestSuite SimplePatternFormatterTest---"); logln();
LocalPointer<IntlTest> test(createSimplePatternFormatterTest());
callTest(*test, par);
}
break;

View file

@ -4,19 +4,19 @@
* others. All Rights Reserved. *
*******************************************************************************
*
* File TEMPLATETEST.CPP
* File SIMPLEPATTERNFORMATTERTEST.CPP
*
********************************************************************************
*/
#include "cstring.h"
#include "intltest.h"
#include "template.h"
#include "simplepatternformatter.h"
#define LENGTHOF(array) (int32_t)(sizeof(array) / sizeof((array)[0]))
class TemplateTest : public IntlTest {
class SimplePatternFormatterTest : public IntlTest {
public:
TemplateTest() {
SimplePatternFormatterTest() {
}
void TestNoPlaceholders();
void TestOnePlaceholder();
@ -25,7 +25,7 @@ public:
private:
};
void TemplateTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
void SimplePatternFormatterTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(TestNoPlaceholders);
TESTCASE_AUTO(TestOnePlaceholder);
@ -33,86 +33,79 @@ void TemplateTest::runIndexedTest(int32_t index, UBool exec, const char* &name,
TESTCASE_AUTO_END;
}
void TemplateTest::TestNoPlaceholders() {
void SimplePatternFormatterTest::TestNoPlaceholders() {
UErrorCode status = U_ZERO_ERROR;
Template t("This doesn''t have templates '{0}");
assertEquals("PlaceholderCount", 0, t.getPlaceholderCount());
SimplePatternFormatter fmt("This doesn''t have templates '{0}");
assertEquals("PlaceholderCount", 0, fmt.getPlaceholderCount());
UnicodeString appendTo;
assertEquals(
"Evaluate",
"This doesn't have templates {0}",
t.evaluate(
NULL,
0,
fmt.format(
"unused",
appendTo,
status));
appendTo.remove();
t.compile("This has {} bad {012d placeholders", status);
assertEquals("PlaceholderCount", 0, t.getPlaceholderCount());
fmt.compile("This has {} bad {012d placeholders", status);
assertEquals("PlaceholderCount", 0, fmt.getPlaceholderCount());
assertEquals(
"Evaluate",
"This has {} bad {012d placeholders",
t.evaluate(
NULL,
0,
fmt.format(
"unused",
appendTo,
status));
appendTo.remove();
assertSuccess("Status", status);
}
void TemplateTest::TestOnePlaceholder() {
void SimplePatternFormatterTest::TestOnePlaceholder() {
UErrorCode status = U_ZERO_ERROR;
Template t;
t.compile("{0} meter", status);
assertEquals("PlaceholderCount", 1, t.getPlaceholderCount());
UnicodeString one("1");
UnicodeString *params[] = {&one};
SimplePatternFormatter fmt;
fmt.compile("{0} meter", status);
assertEquals("PlaceholderCount", 1, fmt.getPlaceholderCount());
UnicodeString appendTo;
assertEquals(
"Evaluate",
"1 meter",
t.evaluate(
params,
LENGTHOF(params),
fmt.format(
"1",
appendTo,
status));
appendTo.remove();
assertSuccess("Status", status);
// assignment
Template s;
s = t;
SimplePatternFormatter s;
s = fmt;
assertEquals(
"Assignment",
"1 meter",
s.evaluate(
params,
LENGTHOF(params),
s.format(
"1",
appendTo,
status));
appendTo.remove();
// Copy constructor
Template r(t);
SimplePatternFormatter r(fmt);
assertEquals(
"Copy constructor",
"1 meter",
r.evaluate(
params,
LENGTHOF(params),
r.format(
"1",
appendTo,
status));
appendTo.remove();
assertSuccess("Status", status);
}
void TemplateTest::TestManyPlaceholders() {
void SimplePatternFormatterTest::TestManyPlaceholders() {
UErrorCode status = U_ZERO_ERROR;
Template t;
t.compile(
SimplePatternFormatter fmt;
fmt.compile(
"Templates {2}{1}{5} and {4} are out of order.", status);
assertEquals("PlaceholderCount", 6, t.getPlaceholderCount());
assertEquals("PlaceholderCount", 6, fmt.getPlaceholderCount());
UnicodeString values[] = {
"freddy", "tommy", "frog", "billy", "leg", "{0}"};
UnicodeString *params[] = {
@ -123,7 +116,7 @@ void TemplateTest::TestManyPlaceholders() {
assertEquals(
"Evaluate",
"Templates frogtommy{0} and leg are out of order.",
t.evaluate(
fmt.format(
params,
LENGTHOF(params),
appendTo,
@ -137,7 +130,7 @@ void TemplateTest::TestManyPlaceholders() {
errln("Expected %d, got %d", expectedOffsets[i], offsets[i]);
}
}
t.evaluate(
fmt.format(
params,
LENGTHOF(params) - 1,
appendTo,
@ -149,7 +142,7 @@ void TemplateTest::TestManyPlaceholders() {
}
status = U_ZERO_ERROR;
offsets[LENGTHOF(offsets) - 1] = 289;
t.evaluate(
fmt.format(
params,
LENGTHOF(params),
appendTo,
@ -160,43 +153,46 @@ void TemplateTest::TestManyPlaceholders() {
assertEquals("Offsets buffer length", 289, offsets[LENGTHOF(offsets) - 1]);
// Test assignment
Template s;
s = t;
SimplePatternFormatter s;
s = fmt;
assertEquals(
"Assignment",
"Templates frogtommy{0} and leg are out of order.",
s.evaluate(
s.format(
params,
LENGTHOF(params),
appendTo,
NULL,
0,
status));
appendTo.remove();
// Copy constructor
Template r(t);
SimplePatternFormatter r(fmt);
assertEquals(
"Assignment",
"Copy constructor",
"Templates frogtommy{0} and leg are out of order.",
r.evaluate(
r.format(
params,
LENGTHOF(params),
appendTo,
NULL,
0,
status));
appendTo.remove();
r.compile("{0} meter", status);
assertEquals("PlaceholderCount", 1, r.getPlaceholderCount());
assertEquals(
"Assignment",
"Replace with new compile",
"freddy meter",
r.evaluate(
params,
1,
r.format(
"freddy",
appendTo,
status));
appendTo.remove();
assertSuccess("Status", status);
}
extern IntlTest *createTemplateTest() {
return new TemplateTest();
extern IntlTest *createSimplePatternFormatterTest() {
return new SimplePatternFormatterTest();
}