- * 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. *
- * 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. - *
* Example: *
- * 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); **/ -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 - *
Template("{0} {2}").getPlaceholderCount() evaluates to 3.
- * Callers use this function to find out how many values are needed
- * to evaluate this template.
+ * 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,
diff --git a/icu4c/source/i18n/quantityformatter.cpp b/icu4c/source/i18n/quantityformatter.cpp
index f28e0bc95dc..2a26fb5053e 100644
--- a/icu4c/source/i18n/quantityformatter.cpp
+++ b/icu4c/source/i18n/quantityformatter.cpp
@@ -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
diff --git a/icu4c/source/i18n/quantityformatter.h b/icu4c/source/i18n/quantityformatter.h
index d5a2f200b15..a1576160335 100644
--- a/icu4c/source/i18n/quantityformatter.h
+++ b/icu4c/source/i18n/quantityformatter.h
@@ -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.
*
* 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
diff --git a/icu4c/source/test/intltest/Makefile.in b/icu4c/source/test/intltest/Makefile.in
index be7fe6fd387..da33dc38441 100644
--- a/icu4c/source/test/intltest/Makefile.in
+++ b/icu4c/source/test/intltest/Makefile.in
@@ -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)
diff --git a/icu4c/source/test/intltest/intltest.vcxproj b/icu4c/source/test/intltest/intltest.vcxproj
index efd2b6012d8..7c45d363d49 100644
--- a/icu4c/source/test/intltest/intltest.vcxproj
+++ b/icu4c/source/test/intltest/intltest.vcxproj
@@ -323,7 +323,7 @@
-
+
diff --git a/icu4c/source/test/intltest/intltest.vcxproj.filters b/icu4c/source/test/intltest/intltest.vcxproj.filters
index b7bf661707a..884ef5f5b93 100644
--- a/icu4c/source/test/intltest/intltest.vcxproj.filters
+++ b/icu4c/source/test/intltest/intltest.vcxproj.filters
@@ -277,6 +277,9 @@
formatting
+
+ formatting
+
formatting
diff --git a/icu4c/source/test/intltest/itutil.cpp b/icu4c/source/test/intltest/itutil.cpp
index 742019bc073..8557ff92beb 100644
--- a/icu4c/source/test/intltest/itutil.cpp
+++ b/icu4c/source/test/intltest/itutil.cpp
@@ -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 test(createTemplateTest());
+ logln("TestSuite SimplePatternFormatterTest---"); logln();
+ LocalPointer test(createSimplePatternFormatterTest());
callTest(*test, par);
}
break;
diff --git a/icu4c/source/test/intltest/templatetest.cpp b/icu4c/source/test/intltest/simplepatternformattertest.cpp
similarity index 72%
rename from icu4c/source/test/intltest/templatetest.cpp
rename to icu4c/source/test/intltest/simplepatternformattertest.cpp
index 6eb0e506dbc..a3a00e4dc04 100644
--- a/icu4c/source/test/intltest/templatetest.cpp
+++ b/icu4c/source/test/intltest/simplepatternformattertest.cpp
@@ -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();
}