ICU-1008 Do less static initialization, and use the default converter less

X-SVN-Rev: 5073
This commit is contained in:
George Rhoten 2001-06-25 21:15:56 +00:00
parent 744dff7002
commit 6f068c37a1

View File

@ -25,11 +25,18 @@
#include "unicode/smpdtfmt.h"
#include "unicode/choicfmt.h"
#include "mutex.h"
#include "unicode/ustring.h"
#include "umsg_imp.h"
// *****************************************************************************
// class MessageFormat
// *****************************************************************************
#define COMMA ((UChar)0x002C)
#define SINGLE_QUOTE ((UChar)0x0027)
#define LEFT_CURLY_BRACE ((UChar)0x007B)
#define RIGHT_CURLY_BRACE ((UChar)0x007D)
// -------------------------------------
char MessageFormat::fgClassID = 0; // Value is irrelevant
@ -221,37 +228,37 @@ MessageFormat::applyPattern(const UnicodeString& newPattern,
for (int i = 0; i < newPattern.length(); ++i) {
UChar ch = newPattern[i];
if (part == 0) {
if (ch == 0x0027 /*'\''*/) {
if (ch == SINGLE_QUOTE) {
if (i + 1 < newPattern.length()
&& newPattern[i+1] == 0x0027 /*'\''*/) {
&& newPattern[i+1] == SINGLE_QUOTE) {
segments[part] += ch; // handle doubles
++i;
} else {
inQuote = !inQuote;
}
} else if (ch == 0x007B /*'{'*/ && !inQuote) {
} else if (ch == LEFT_CURLY_BRACE && !inQuote) {
part = 1;
} else {
segments[part] += ch;
}
} else if (inQuote) { // just copy quotes in parts
segments[part] += ch;
if (ch == 0x0027 /*'\''*/) {
if (ch == SINGLE_QUOTE) {
inQuote = FALSE;
}
} else {
switch (ch) {
case 0x002C /*','*/:
case COMMA:
if (part < 3)
part += 1;
else
segments[part] += ch;
break;
case 0x007B /*'{'*/:
case LEFT_CURLY_BRACE:
++braceStack;
segments[part] += ch;
break;
case 0x007D /*'}'*/:
case RIGHT_CURLY_BRACE:
if (braceStack == 0) {
part = 0;
makeFormat(/*i,*/ formatNumber, segments, success);
@ -263,7 +270,7 @@ MessageFormat::applyPattern(const UnicodeString& newPattern,
segments[part] += ch;
}
break;
case 0x0027 /*'\''*/:
case SINGLE_QUOTE:
inQuote = TRUE;
// fall through, so we keep quotes in other parts
default:
@ -291,7 +298,7 @@ MessageFormat::toPattern(UnicodeString& result) const
for (int i = 0; i <= fMaxOffset; ++i) {
copyAndFixQuotes(fPattern, lastOffset, fOffsets[i], result);
lastOffset = fOffsets[i];
result += (UChar)0x007B /*'{'*/;
result += LEFT_CURLY_BRACE;
// {sfb} check this later
//result += (UChar) (fArgumentNumbers[i] + '0');
UnicodeString temp;
@ -308,23 +315,24 @@ MessageFormat::toPattern(UnicodeString& result) const
NumberFormat *percentTemplate = NumberFormat::createPercentInstance(fLocale, status);
NumberFormat *integerTemplate = createIntegerFormat(fLocale, status);
if (formatAlias == *numberTemplate) {
result += ",number";
}
else if (formatAlias == *currencyTemplate) {
result += ",number,currency";
result += COMMA;
result += g_umsg_number;
if (formatAlias != *numberTemplate) {
result += COMMA;
if (formatAlias == *currencyTemplate) {
result += g_umsg_currency;
}
else if (formatAlias == *percentTemplate) {
result += ",number,percent";
result += g_umsg_percent;
}
else if (formatAlias == *integerTemplate) {
result += ",number,integer";
result += g_umsg_integer;
}
else {
UnicodeString buffer;
result += ",number,";
result += ((DecimalFormat*)fFormats[i])->toPattern(buffer);
}
}
delete numberTemplate;
delete currencyTemplate;
@ -343,39 +351,57 @@ MessageFormat::toPattern(UnicodeString& result) const
DateFormat *fullTimeTemplate = DateFormat::createTimeInstance(DateFormat::kFull, fLocale);
result += COMMA;
if (formatAlias == *defaultDateTemplate) {
result += ",date";
result += g_umsg_date;
}
else if (formatAlias == *shortDateTemplate) {
result += ",date,short";
result += g_umsg_date;
result += COMMA;
result += g_umsg_short;
}
else if (formatAlias == *defaultDateTemplate) {
result += ",date,medium";
result += g_umsg_date;
result += COMMA;
result += g_umsg_medium;
}
else if (formatAlias == *longDateTemplate) {
result += ",date,long";
result += g_umsg_date;
result += COMMA;
result += g_umsg_long;
}
else if (formatAlias == *fullDateTemplate) {
result += ",date,full";
result += g_umsg_date;
result += COMMA;
result += g_umsg_full;
}
else if (formatAlias == *defaultTimeTemplate) {
result += ",time";
result += g_umsg_time;
}
else if (formatAlias == *shortTimeTemplate) {
result += ",time,short";
result += g_umsg_time;
result += COMMA;
result += g_umsg_short;
}
else if (formatAlias == *defaultTimeTemplate) {
result += ",time,medium";
result += g_umsg_time;
result += COMMA;
result += g_umsg_medium;
}
else if (formatAlias == *longTimeTemplate) {
result += ",time,long";
result += g_umsg_time;
result += COMMA;
result += g_umsg_long;
}
else if (formatAlias == *fullTimeTemplate) {
result += ",time,full";
result += g_umsg_time;
result += COMMA;
result += g_umsg_full;
}
else {
UnicodeString buffer;
result += ",date,";
result += g_umsg_date;
result += COMMA;
result += ((SimpleDateFormat*)fFormats[i])->toPattern(buffer);
}
@ -391,13 +417,15 @@ MessageFormat::toPattern(UnicodeString& result) const
}
else if (fFormats[i]->getDynamicClassID() == ChoiceFormat::getStaticClassID()) {
UnicodeString buffer;
result += ",choice,";
result += COMMA;
result += g_umsg_choice;
result += COMMA;
result += ((ChoiceFormat*)fFormats[i])->toPattern(buffer);
}
else {
//result += ", unknown";
}
result += (UChar)0x007D /*'}'*/;
result += RIGHT_CURLY_BRACE;
}
copyAndFixQuotes(fPattern, lastOffset, fPattern.length(), result);
return result;
@ -437,16 +465,18 @@ MessageFormat::setFormats(const Format** newFormats,
int32_t i;
// Cleans up first.
for (i = 0; i < fCount; i++)
for (i = 0; i < fCount; i++) {
delete fFormats[i];
}
fCount = (cnt > kMaxFormat) ? kMaxFormat : cnt;
for (i = 0; i < fCount; i++)
for (i = 0; i < fCount; i++) {
if (newFormats[i] == NULL) {
fFormats[i] = NULL;
}
else{
fFormats[i] = newFormats[i]->clone();
}
}
}
// -------------------------------------
@ -591,10 +621,10 @@ MessageFormat::format(const Formattable* arguments,
if (argumentNumber >= cnt) {
/*success = U_ILLEGAL_ARGUMENT_ERROR;
return result;*/
result += "{";
result += LEFT_CURLY_BRACE;
UnicodeString temp;
result += itos(argumentNumber, temp);
result += "}";
result += RIGHT_CURLY_BRACE;
continue;
}
@ -640,7 +670,7 @@ MessageFormat::format(const Formattable* arguments,
}
// Needs to reprocess the ChoiceFormat option by using the MessageFormat
// pattern application.
if (tryRecursion && arg.indexOf("{") >= 0) {
if (tryRecursion && arg.indexOf(LEFT_CURLY_BRACE) >= 0) {
MessageFormat *temp = NULL;
temp = new MessageFormat(arg, fLocale, success);
if (U_FAILURE(success))
@ -663,28 +693,64 @@ MessageFormat::format(const Formattable* arguments,
return result;
}
extern const UChar g_umsg_number[] = {
0x6E, 0x75, 0x6D, 0x62, 0x65, 0x72, 0 /* "number" */
};
extern const UChar g_umsg_date[] = {
0x64, 0x61, 0x74, 0x65, 0 /* "date" */
};
extern const UChar g_umsg_time[] = {
0x74, 0x69, 0x6D, 0x65, 0 /* "time" */
};
extern const UChar g_umsg_choice[] = {
0x63, 0x68, 0x6F, 0x69, 0x63, 0x65, 0 /* "choice" */
};
// MessageFormat Type List Number, Date, Time or Choice
const UnicodeString MessageFormat::fgTypeList[] = {
UnicodeString(), UnicodeString(), UNICODE_STRING("number", 6), UnicodeString(),
UNICODE_STRING("date", 4), UnicodeString(), UNICODE_STRING("time", 4), UnicodeString(),
UNICODE_STRING("choice", 6)
extern const UChar *g_umsgTypeList[] = {
NULL, NULL, g_umsg_number,
NULL, g_umsg_date, NULL,
g_umsg_time, NULL, g_umsg_choice
};
extern const UChar g_umsg_currency[] = {
0x63, 0x75, 0x72, 0x72, 0x65, 0x6E, 0x63, 0x79, 0 /* "currency" */
};
extern const UChar g_umsg_percent[] = {
0x70, 0x65, 0x72, 0x63, 0x65, 0x6E, 0x74, 0 /* "percent" */
};
extern const UChar g_umsg_integer[] = {
0x69, 0x6E, 0x74, 0x65, 0x67, 0x65, 0x72, 0 /* "integer" */
};
// NumberFormat modifier list, default, currency, percent or integer
const UnicodeString MessageFormat::fgModifierList[] = {
UnicodeString(), UnicodeString(), UNICODE_STRING("currency", 8), UnicodeString(),
UNICODE_STRING("percent", 7), UnicodeString(), UNICODE_STRING("integer", 7), UnicodeString(),
UnicodeString()
extern const UChar *g_umsgModifierList[] = {
NULL, NULL, g_umsg_currency,
NULL, g_umsg_percent, NULL,
g_umsg_integer, NULL, NULL
};
extern const UChar g_umsg_short[] = {
0x73, 0x68, 0x6F, 0x72, 0x74, 0 /* "short" */
};
extern const UChar g_umsg_medium[] = {
0x6D, 0x65, 0x64, 0x69, 0x75, 0x6D, 0 /* "medium" */
};
extern const UChar g_umsg_long[] = {
0x6C, 0x6F, 0x6E, 0x67, 0 /* "long" */
};
extern const UChar g_umsg_full[] = {
0x66, 0x75, 0x6C, 0x6C, 0 /* "full" */
};
// DateFormat modifier list, default, short, medium, long or full
const UnicodeString MessageFormat::fgDateModifierList[] = {
UnicodeString(), UnicodeString(), UNICODE_STRING("short", 5), UnicodeString(),
UNICODE_STRING("medium", 6), UnicodeString(), UNICODE_STRING("long", 4), UnicodeString(),
UNICODE_STRING("full", 4)
extern const UChar *g_umsgDateModifierList[] = {
NULL, NULL, g_umsg_short,
NULL, g_umsg_medium, NULL,
g_umsg_long, NULL, g_umsg_full
};
const int32_t MessageFormat::fgListLength= 9;
extern const int32_t g_umsgListLength = 9;
// -------------------------------------
// Parses the source pattern and returns the Formattable objects array,
@ -743,18 +809,19 @@ MessageFormat::parse(const UnicodeString& source,
UnicodeString buffer;
source.extract(sourceOffset,next - sourceOffset, buffer);
UnicodeString strValue = buffer;
UnicodeString temp("{");
UnicodeString temp(LEFT_CURLY_BRACE);
// {sfb} check this later
UnicodeString temp1;
temp += itos(fArgumentNumbers[i], temp1);
temp += "}";
temp += RIGHT_CURLY_BRACE;
if (strValue != temp) {
source.extract(sourceOffset,next - sourceOffset, buffer);
resultArray[fArgumentNumbers[i]].setString(buffer);
// {sfb} not sure about this
if ((fArgumentNumbers[i] + 1) > count)
if ((fArgumentNumbers[i] + 1) > count) {
count = (fArgumentNumbers[i] + 1);
}
}
sourceOffset = next;
}
}
@ -914,8 +981,12 @@ MessageFormat::itos(int32_t i,
UErrorCode status = U_ZERO_ERROR;
NumberFormat *myFormat = getNumberFormat(status);
if(U_FAILURE(status))
return (string = "<ERROR>"); // _REVISIT_ maybe toPattern should take an errorcode.
if(U_FAILURE(status)) {
/* "<ERROR>" */
static const UChar ERROR[] = {0x3C, 0x45, 0x52, 0x52, 0x4F, 0x52, 0x3E, 0};
return string = ERROR; // TODO: maybe toPattern should take an errorcode.
}
UnicodeString &retval = myFormat->format(i, string);
@ -950,11 +1021,11 @@ MessageFormat::makeFormat(/*int32_t position, */
// now get the format
Format *newFormat = NULL;
switch (findKeyword(segments[2], fgTypeList)) {
switch (findKeyword(segments[2], g_umsgTypeList)) {
case 0:
break;
case 1: case 2:// number
switch (findKeyword(segments[3], fgModifierList)) {
switch (findKeyword(segments[3], g_umsgModifierList)) {
case 0: // default;
newFormat = NumberFormat::createInstance(fLocale, success);
break;
@ -985,7 +1056,7 @@ MessageFormat::makeFormat(/*int32_t position, */
break;
case 3: case 4: // date
switch (findKeyword(segments[3], fgDateModifierList)) {
switch (findKeyword(segments[3], g_umsgDateModifierList)) {
case 0: // default
newFormat = DateFormat::createDateInstance(DateFormat::kDefault, fLocale);
break;
@ -1014,7 +1085,7 @@ MessageFormat::makeFormat(/*int32_t position, */
}
break;
case 5: case 6:// time
switch (findKeyword(segments[3], fgDateModifierList)) {
switch (findKeyword(segments[3], g_umsgDateModifierList)) {
case 0: // default
newFormat = DateFormat::createTimeInstance(DateFormat::kDefault, fLocale);
break;
@ -1069,17 +1140,20 @@ MessageFormat::makeFormat(/*int32_t position, */
// -------------------------------------
// Finds the string, s, in the string array, list.
int32_t MessageFormat::findKeyword(const UnicodeString& s,
const UnicodeString* list)
const UChar **list)
{
if (s.length() == 0)
return 0;
UnicodeString buffer = s;
// Trims the space characters and turns all characters
// in s to lower case.
buffer.trim().toLower();
for (int32_t i = 0; i < fgListLength; ++i) {
if (buffer == list[i])
for (int32_t i = 0; i < g_umsgListLength; ++i) {
if (list[i] && !buffer.compare(list[i], u_strlen(list[i])))
return i;
}
return - 1;
return -1;
}
// -------------------------------------
@ -1096,21 +1170,27 @@ MessageFormat::copyAndFixQuotes(const UnicodeString& source,
for (UTextOffset i = start; i < end; ++i) {
UChar ch = source[i];
if (ch == 0x007B /*'{'*/) {
target += "'{'";
if (ch == LEFT_CURLY_BRACE) {
target += SINGLE_QUOTE;
target += LEFT_CURLY_BRACE;
target += SINGLE_QUOTE;
gotLB = TRUE;
}
else if (ch == 0x007D /*'}'*/) {
else if (ch == RIGHT_CURLY_BRACE) {
if(gotLB) {
target += "}";
target += RIGHT_CURLY_BRACE;
gotLB = FALSE;
}
else
else {
// orig code.
target += "'}'";
target += SINGLE_QUOTE;
target += RIGHT_CURLY_BRACE;
target += SINGLE_QUOTE;
}
else if (ch == 0x0027 /*'\''*/) {
target += "''";
}
else if (ch == SINGLE_QUOTE) {
target += SINGLE_QUOTE;
target += SINGLE_QUOTE;
}
else {
target += ch;