2010-01-16 03:07:18 +00:00
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
|
|
|
* Copyright (c) 1997-2010, International Business Machines Corporation and
|
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************
|
|
|
|
*
|
|
|
|
* File MSGFMT.CPP
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 02/19/97 aliu Converted from java.
|
|
|
|
* 03/20/97 helena Finished first cut of implementation.
|
|
|
|
* 04/10/97 aliu Made to work on AIX. Added stoi to replace wtoi.
|
|
|
|
* 06/11/97 helena Fixed addPattern to take the pattern correctly.
|
|
|
|
* 06/17/97 helena Fixed the getPattern to return the correct pattern.
|
|
|
|
* 07/09/97 helena Made ParsePosition into a class.
|
|
|
|
* 02/22/99 stephen Removed character literals for EBCDIC safety
|
|
|
|
* 11/01/09 kirtig Added SelectFormat
|
|
|
|
********************************************************************/
|
2000-08-23 23:48:04 +00:00
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/msgfmt.h"
|
|
|
|
#include "unicode/decimfmt.h"
|
|
|
|
#include "unicode/datefmt.h"
|
|
|
|
#include "unicode/smpdtfmt.h"
|
|
|
|
#include "unicode/choicfmt.h"
|
2007-12-17 01:39:55 +00:00
|
|
|
#include "unicode/plurfmt.h"
|
2010-01-16 03:07:18 +00:00
|
|
|
#include "unicode/selfmt.h"
|
2001-06-25 21:15:56 +00:00
|
|
|
#include "unicode/ustring.h"
|
2001-08-16 00:55:16 +00:00
|
|
|
#include "unicode/ucnv_err.h"
|
2001-11-30 00:06:13 +00:00
|
|
|
#include "unicode/uchar.h"
|
2005-05-27 22:07:16 +00:00
|
|
|
#include "unicode/umsg.h"
|
2004-08-04 05:57:45 +00:00
|
|
|
#include "unicode/rbnf.h"
|
2002-02-28 01:42:40 +00:00
|
|
|
#include "cmemory.h"
|
2007-12-17 01:39:55 +00:00
|
|
|
#include "msgfmt_impl.h"
|
2004-10-15 22:56:26 +00:00
|
|
|
#include "util.h"
|
2003-01-27 17:27:40 +00:00
|
|
|
#include "uassert.h"
|
2007-12-17 01:39:55 +00:00
|
|
|
#include "ustrfmt.h"
|
|
|
|
#include "uvector.h"
|
2000-08-23 23:48:04 +00:00
|
|
|
|
2010-01-16 03:07:18 +00:00
|
|
|
//Todo:remove stdio
|
|
|
|
#include "stdio.h"
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// *****************************************************************************
|
|
|
|
// class MessageFormat
|
|
|
|
// *****************************************************************************
|
2000-08-23 23:48:04 +00:00
|
|
|
|
2001-06-25 21:15:56 +00:00
|
|
|
#define COMMA ((UChar)0x002C)
|
|
|
|
#define SINGLE_QUOTE ((UChar)0x0027)
|
|
|
|
#define LEFT_CURLY_BRACE ((UChar)0x007B)
|
|
|
|
#define RIGHT_CURLY_BRACE ((UChar)0x007D)
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
//---------------------------------------
|
|
|
|
// static data
|
|
|
|
|
2004-08-04 05:57:45 +00:00
|
|
|
static const UChar ID_EMPTY[] = {
|
|
|
|
0 /* empty string, used for default so that null can mark end of list */
|
|
|
|
};
|
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar ID_NUMBER[] = {
|
2001-08-16 00:55:16 +00:00
|
|
|
0x6E, 0x75, 0x6D, 0x62, 0x65, 0x72, 0 /* "number" */
|
|
|
|
};
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar ID_DATE[] = {
|
|
|
|
0x64, 0x61, 0x74, 0x65, 0 /* "date" */
|
2001-08-16 00:55:16 +00:00
|
|
|
};
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar ID_TIME[] = {
|
|
|
|
0x74, 0x69, 0x6D, 0x65, 0 /* "time" */
|
2001-08-16 00:55:16 +00:00
|
|
|
};
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar ID_CHOICE[] = {
|
2001-08-16 00:55:16 +00:00
|
|
|
0x63, 0x68, 0x6F, 0x69, 0x63, 0x65, 0 /* "choice" */
|
|
|
|
};
|
2004-08-04 05:57:45 +00:00
|
|
|
static const UChar ID_SPELLOUT[] = {
|
|
|
|
0x73, 0x70, 0x65, 0x6c, 0x6c, 0x6f, 0x75, 0x74, 0 /* "spellout" */
|
|
|
|
};
|
|
|
|
static const UChar ID_ORDINAL[] = {
|
|
|
|
0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x6c, 0 /* "ordinal" */
|
|
|
|
};
|
|
|
|
static const UChar ID_DURATION[] = {
|
|
|
|
0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0 /* "duration" */
|
|
|
|
};
|
2007-12-17 01:39:55 +00:00
|
|
|
static const UChar ID_PLURAL[] = {
|
|
|
|
0x70, 0x6c, 0x75, 0x72, 0x61, 0x6c, 0 /* "plural" */
|
|
|
|
};
|
2010-01-16 03:07:18 +00:00
|
|
|
static const UChar ID_SELECT[] = {
|
|
|
|
0x73, 0x65, 0x6C, 0x65, 0x63, 0x74, 0 /* "select" */
|
|
|
|
};
|
2001-08-16 00:55:16 +00:00
|
|
|
|
|
|
|
// MessageFormat Type List Number, Date, Time or Choice
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar * const TYPE_IDS[] = {
|
2004-08-04 05:57:45 +00:00
|
|
|
ID_EMPTY,
|
2003-01-27 17:27:40 +00:00
|
|
|
ID_NUMBER,
|
|
|
|
ID_DATE,
|
|
|
|
ID_TIME,
|
2004-08-04 05:57:45 +00:00
|
|
|
ID_CHOICE,
|
|
|
|
ID_SPELLOUT,
|
|
|
|
ID_ORDINAL,
|
|
|
|
ID_DURATION,
|
2007-12-17 01:39:55 +00:00
|
|
|
ID_PLURAL,
|
2010-01-16 03:07:18 +00:00
|
|
|
ID_SELECT,
|
2004-08-04 05:57:45 +00:00
|
|
|
NULL,
|
2001-08-16 00:55:16 +00:00
|
|
|
};
|
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar ID_CURRENCY[] = {
|
2001-08-16 00:55:16 +00:00
|
|
|
0x63, 0x75, 0x72, 0x72, 0x65, 0x6E, 0x63, 0x79, 0 /* "currency" */
|
|
|
|
};
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar ID_PERCENT[] = {
|
|
|
|
0x70, 0x65, 0x72, 0x63, 0x65, 0x6E, 0x74, 0 /* "percent" */
|
2001-08-16 00:55:16 +00:00
|
|
|
};
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar ID_INTEGER[] = {
|
|
|
|
0x69, 0x6E, 0x74, 0x65, 0x67, 0x65, 0x72, 0 /* "integer" */
|
2001-08-16 00:55:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// NumberFormat modifier list, default, currency, percent or integer
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar * const NUMBER_STYLE_IDS[] = {
|
2004-08-04 05:57:45 +00:00
|
|
|
ID_EMPTY,
|
2003-01-27 17:27:40 +00:00
|
|
|
ID_CURRENCY,
|
|
|
|
ID_PERCENT,
|
|
|
|
ID_INTEGER,
|
|
|
|
NULL,
|
2001-08-16 00:55:16 +00:00
|
|
|
};
|
2004-08-04 05:57:45 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar ID_SHORT[] = {
|
|
|
|
0x73, 0x68, 0x6F, 0x72, 0x74, 0 /* "short" */
|
2001-08-16 00:55:16 +00:00
|
|
|
};
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar ID_MEDIUM[] = {
|
2001-08-16 00:55:16 +00:00
|
|
|
0x6D, 0x65, 0x64, 0x69, 0x75, 0x6D, 0 /* "medium" */
|
|
|
|
};
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar ID_LONG[] = {
|
|
|
|
0x6C, 0x6F, 0x6E, 0x67, 0 /* "long" */
|
2001-08-16 00:55:16 +00:00
|
|
|
};
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar ID_FULL[] = {
|
|
|
|
0x66, 0x75, 0x6C, 0x6C, 0 /* "full" */
|
2001-08-16 00:55:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// DateFormat modifier list, default, short, medium, long or full
|
2003-01-27 17:27:40 +00:00
|
|
|
static const UChar * const DATE_STYLE_IDS[] = {
|
2004-08-04 05:57:45 +00:00
|
|
|
ID_EMPTY,
|
2003-01-27 17:27:40 +00:00
|
|
|
ID_SHORT,
|
|
|
|
ID_MEDIUM,
|
|
|
|
ID_LONG,
|
2004-08-04 05:57:45 +00:00
|
|
|
ID_FULL,
|
|
|
|
NULL,
|
2001-08-16 00:55:16 +00:00
|
|
|
};
|
|
|
|
|
2006-09-04 16:36:21 +00:00
|
|
|
static const U_NAMESPACE_QUALIFIER DateFormat::EStyle DATE_STYLES[] = {
|
|
|
|
U_NAMESPACE_QUALIFIER DateFormat::kDefault,
|
|
|
|
U_NAMESPACE_QUALIFIER DateFormat::kShort,
|
|
|
|
U_NAMESPACE_QUALIFIER DateFormat::kMedium,
|
|
|
|
U_NAMESPACE_QUALIFIER DateFormat::kLong,
|
|
|
|
U_NAMESPACE_QUALIFIER DateFormat::kFull,
|
2003-01-27 17:27:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const int32_t DEFAULT_INITIAL_CAPACITY = 10;
|
2001-10-08 23:26:58 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
// -------------------------------------
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MessageFormat)
|
2007-12-17 01:39:55 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FormatNameEnumeration)
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a string to an unsigned decimal, ignoring rule whitespace.
|
|
|
|
* @return a non-negative number if successful, or a negative number
|
|
|
|
* upon failure.
|
|
|
|
*/
|
|
|
|
static int32_t stou(const UnicodeString& string) {
|
|
|
|
int32_t n = 0;
|
|
|
|
int32_t count = 0;
|
|
|
|
UChar32 c;
|
|
|
|
for (int32_t i=0; i<string.length(); i+=U16_LENGTH(c)) {
|
|
|
|
c = string.char32At(i);
|
|
|
|
if (uprv_isRuleWhiteSpace(c)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int32_t d = u_digit(c, 10);
|
|
|
|
if (d < 0 || ++count > 10) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
n = 10*n + d;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an integer value to a string and append the result to
|
|
|
|
* the given UnicodeString.
|
|
|
|
*/
|
|
|
|
static UnicodeString& itos(int32_t i, UnicodeString& appendTo) {
|
|
|
|
UChar temp[16];
|
|
|
|
uprv_itou(temp,16,i,10,0); // 10 == radix
|
|
|
|
appendTo.append(temp);
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
2008-01-22 21:15:14 +00:00
|
|
|
/*
|
|
|
|
* A structure representing one subformat of this MessageFormat.
|
|
|
|
* Each subformat has a Format object, an offset into the plain
|
|
|
|
* pattern text fPattern, and an argument number. The argument
|
|
|
|
* number corresponds to the array of arguments to be formatted.
|
|
|
|
* @internal
|
|
|
|
*/
|
2008-01-24 03:51:26 +00:00
|
|
|
class MessageFormat::Subformat : public UMemory {
|
2008-01-22 21:15:14 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
Format* format; // formatter
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
int32_t offset; // offset into fPattern
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
// TODO (claireho) or save the number to argName and use itos to convert to number.=> we need this number
|
|
|
|
int32_t argNum; // 0-based argument number
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
UnicodeString* argName; // argument name or number
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone that.format and assign it to this.format
|
|
|
|
* Do NOT delete this.format
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
Subformat& operator=(const Subformat& that) {
|
|
|
|
if (this != &that) {
|
|
|
|
format = that.format ? that.format->clone() : NULL;
|
|
|
|
offset = that.offset;
|
|
|
|
argNum = that.argNum;
|
|
|
|
argName = (that.argNum==-1) ? new UnicodeString(*that.argName): NULL;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
UBool operator==(const Subformat& that) const {
|
|
|
|
// Do cheap comparisons first
|
|
|
|
return offset == that.offset &&
|
|
|
|
argNum == that.argNum &&
|
|
|
|
((argName == that.argName) ||
|
|
|
|
(*argName == *that.argName)) &&
|
|
|
|
((format == that.format) || // handles NULL
|
|
|
|
(*format == *that.format));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
UBool operator!=(const Subformat& that) const {
|
|
|
|
return !operator==(that);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Creates a MessageFormat instance based on the pattern.
|
|
|
|
|
|
|
|
MessageFormat::MessageFormat(const UnicodeString& pattern,
|
|
|
|
UErrorCode& success)
|
2000-08-14 23:23:20 +00:00
|
|
|
: fLocale(Locale::getDefault()), // Uses the default locale
|
2003-02-11 02:45:50 +00:00
|
|
|
formatAliases(NULL),
|
|
|
|
formatAliasesCapacity(0),
|
2007-12-29 05:51:50 +00:00
|
|
|
idStart(UCHAR_ID_START),
|
|
|
|
idContinue(UCHAR_ID_CONTINUE),
|
2003-01-27 17:27:40 +00:00
|
|
|
subformats(NULL),
|
|
|
|
subformatCount(0),
|
|
|
|
subformatCapacity(0),
|
|
|
|
argTypes(NULL),
|
|
|
|
argTypeCount(0),
|
2003-01-28 22:17:40 +00:00
|
|
|
argTypeCapacity(0),
|
2007-12-17 01:39:55 +00:00
|
|
|
isArgNumeric(TRUE),
|
2007-12-29 05:51:50 +00:00
|
|
|
defaultNumberFormat(NULL),
|
|
|
|
defaultDateFormat(NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-01-27 17:27:40 +00:00
|
|
|
if (!allocateSubformats(DEFAULT_INITIAL_CAPACITY) ||
|
|
|
|
!allocateArgTypes(DEFAULT_INITIAL_CAPACITY)) {
|
2002-06-29 09:31:05 +00:00
|
|
|
success = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
applyPattern(pattern, success);
|
2004-01-20 23:06:38 +00:00
|
|
|
setLocaleIDs(fLocale.getName(), fLocale.getName());
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageFormat::MessageFormat(const UnicodeString& pattern,
|
|
|
|
const Locale& newLocale,
|
|
|
|
UErrorCode& success)
|
2003-01-27 17:27:40 +00:00
|
|
|
: fLocale(newLocale),
|
2003-02-11 02:45:50 +00:00
|
|
|
formatAliases(NULL),
|
|
|
|
formatAliasesCapacity(0),
|
2007-12-29 05:51:50 +00:00
|
|
|
idStart(UCHAR_ID_START),
|
|
|
|
idContinue(UCHAR_ID_CONTINUE),
|
2003-01-27 17:27:40 +00:00
|
|
|
subformats(NULL),
|
|
|
|
subformatCount(0),
|
|
|
|
subformatCapacity(0),
|
|
|
|
argTypes(NULL),
|
|
|
|
argTypeCount(0),
|
2003-01-28 22:17:40 +00:00
|
|
|
argTypeCapacity(0),
|
2007-12-17 01:39:55 +00:00
|
|
|
isArgNumeric(TRUE),
|
2007-12-29 05:51:50 +00:00
|
|
|
defaultNumberFormat(NULL),
|
|
|
|
defaultDateFormat(NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-01-27 17:27:40 +00:00
|
|
|
if (!allocateSubformats(DEFAULT_INITIAL_CAPACITY) ||
|
|
|
|
!allocateArgTypes(DEFAULT_INITIAL_CAPACITY)) {
|
2002-06-29 09:31:05 +00:00
|
|
|
success = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
applyPattern(pattern, success);
|
2004-01-20 23:06:38 +00:00
|
|
|
setLocaleIDs(fLocale.getName(), fLocale.getName());
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
MessageFormat::MessageFormat(const UnicodeString& pattern,
|
|
|
|
const Locale& newLocale,
|
|
|
|
UParseError& parseError,
|
|
|
|
UErrorCode& success)
|
2003-01-27 17:27:40 +00:00
|
|
|
: fLocale(newLocale),
|
2003-02-11 02:45:50 +00:00
|
|
|
formatAliases(NULL),
|
|
|
|
formatAliasesCapacity(0),
|
2007-12-29 05:51:50 +00:00
|
|
|
idStart(UCHAR_ID_START),
|
|
|
|
idContinue(UCHAR_ID_CONTINUE),
|
2003-01-27 17:27:40 +00:00
|
|
|
subformats(NULL),
|
|
|
|
subformatCount(0),
|
|
|
|
subformatCapacity(0),
|
|
|
|
argTypes(NULL),
|
|
|
|
argTypeCount(0),
|
2003-01-28 22:17:40 +00:00
|
|
|
argTypeCapacity(0),
|
2007-12-17 01:39:55 +00:00
|
|
|
isArgNumeric(TRUE),
|
2007-12-29 05:51:50 +00:00
|
|
|
defaultNumberFormat(NULL),
|
|
|
|
defaultDateFormat(NULL)
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
2003-01-27 17:27:40 +00:00
|
|
|
if (!allocateSubformats(DEFAULT_INITIAL_CAPACITY) ||
|
|
|
|
!allocateArgTypes(DEFAULT_INITIAL_CAPACITY)) {
|
2002-06-29 09:31:05 +00:00
|
|
|
success = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
applyPattern(pattern, parseError, success);
|
2004-01-20 23:06:38 +00:00
|
|
|
setLocaleIDs(fLocale.getName(), fLocale.getName());
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
2003-01-28 22:17:40 +00:00
|
|
|
MessageFormat::MessageFormat(const MessageFormat& that)
|
|
|
|
: Format(that),
|
2003-02-11 02:45:50 +00:00
|
|
|
formatAliases(NULL),
|
|
|
|
formatAliasesCapacity(0),
|
2007-12-29 05:51:50 +00:00
|
|
|
idStart(UCHAR_ID_START),
|
|
|
|
idContinue(UCHAR_ID_CONTINUE),
|
2003-01-28 22:17:40 +00:00
|
|
|
subformats(NULL),
|
|
|
|
subformatCount(0),
|
|
|
|
subformatCapacity(0),
|
|
|
|
argTypes(NULL),
|
|
|
|
argTypeCount(0),
|
|
|
|
argTypeCapacity(0),
|
2007-12-17 01:39:55 +00:00
|
|
|
isArgNumeric(TRUE),
|
2007-12-29 05:51:50 +00:00
|
|
|
defaultNumberFormat(NULL),
|
|
|
|
defaultDateFormat(NULL)
|
2003-01-28 22:17:40 +00:00
|
|
|
{
|
|
|
|
*this = that;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
MessageFormat::~MessageFormat()
|
|
|
|
{
|
2003-02-25 19:56:49 +00:00
|
|
|
int32_t idx;
|
|
|
|
for (idx = 0; idx < subformatCount; idx++) {
|
|
|
|
delete subformats[idx].format;
|
2007-12-17 01:39:55 +00:00
|
|
|
delete subformats[idx].argName;
|
2003-02-25 19:56:49 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
uprv_free(subformats);
|
|
|
|
subformats = NULL;
|
|
|
|
subformatCount = subformatCapacity = 0;
|
|
|
|
|
|
|
|
uprv_free(argTypes);
|
|
|
|
argTypes = NULL;
|
|
|
|
argTypeCount = argTypeCapacity = 0;
|
2003-01-28 22:17:40 +00:00
|
|
|
|
|
|
|
uprv_free(formatAliases);
|
2003-02-05 00:53:44 +00:00
|
|
|
|
|
|
|
delete defaultNumberFormat;
|
|
|
|
delete defaultDateFormat;
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// Variable-size array management
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate subformats[] to at least the given capacity and return
|
|
|
|
* TRUE if successful. If not, leave subformats[] unchanged.
|
|
|
|
*
|
|
|
|
* If subformats is NULL, allocate it. If it is not NULL, enlarge it
|
|
|
|
* if necessary to be at least as large as specified.
|
|
|
|
*/
|
|
|
|
UBool MessageFormat::allocateSubformats(int32_t capacity) {
|
|
|
|
if (subformats == NULL) {
|
|
|
|
subformats = (Subformat*) uprv_malloc(sizeof(*subformats) * capacity);
|
|
|
|
subformatCapacity = capacity;
|
|
|
|
subformatCount = 0;
|
|
|
|
if (subformats == NULL) {
|
|
|
|
subformatCapacity = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else if (subformatCapacity < capacity) {
|
|
|
|
if (capacity < 2*subformatCapacity) {
|
|
|
|
capacity = 2*subformatCapacity;
|
|
|
|
}
|
|
|
|
Subformat* a = (Subformat*)
|
|
|
|
uprv_realloc(subformats, sizeof(*subformats) * capacity);
|
|
|
|
if (a == NULL) {
|
|
|
|
return FALSE; // request failed
|
2001-08-31 02:11:48 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
subformats = a;
|
|
|
|
subformatCapacity = capacity;
|
2001-08-31 02:11:48 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate argTypes[] to at least the given capacity and return
|
|
|
|
* TRUE if successful. If not, leave argTypes[] unchanged.
|
|
|
|
*
|
|
|
|
* If argTypes is NULL, allocate it. If it is not NULL, enlarge it
|
|
|
|
* if necessary to be at least as large as specified.
|
|
|
|
*/
|
|
|
|
UBool MessageFormat::allocateArgTypes(int32_t capacity) {
|
|
|
|
if (argTypes == NULL) {
|
|
|
|
argTypes = (Formattable::Type*) uprv_malloc(sizeof(*argTypes) * capacity);
|
|
|
|
argTypeCount = 0;
|
|
|
|
argTypeCapacity = capacity;
|
|
|
|
if (argTypes == NULL) {
|
|
|
|
argTypeCapacity = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<capacity; ++i) {
|
|
|
|
argTypes[i] = Formattable::kString;
|
|
|
|
}
|
|
|
|
} else if (argTypeCapacity < capacity) {
|
|
|
|
if (capacity < 2*argTypeCapacity) {
|
|
|
|
capacity = 2*argTypeCapacity;
|
|
|
|
}
|
|
|
|
Formattable::Type* a = (Formattable::Type*)
|
|
|
|
uprv_realloc(argTypes, sizeof(*argTypes) * capacity);
|
|
|
|
if (a == NULL) {
|
|
|
|
return FALSE; // request failed
|
|
|
|
}
|
|
|
|
for (int32_t i=argTypeCapacity; i<capacity; ++i) {
|
|
|
|
a[i] = Formattable::kString;
|
|
|
|
}
|
|
|
|
argTypes = a;
|
|
|
|
argTypeCapacity = capacity;
|
|
|
|
}
|
|
|
|
return TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// assignment operator
|
|
|
|
|
|
|
|
const MessageFormat&
|
|
|
|
MessageFormat::operator=(const MessageFormat& that)
|
|
|
|
{
|
2003-01-27 17:27:40 +00:00
|
|
|
// Reallocate the arrays BEFORE changing this object
|
|
|
|
if (this != &that &&
|
|
|
|
allocateSubformats(that.subformatCount) &&
|
|
|
|
allocateArgTypes(that.argTypeCount)) {
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// Calls the super class for assignment first.
|
|
|
|
Format::operator=(that);
|
2003-01-27 17:27:40 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
fPattern = that.fPattern;
|
2003-02-05 00:53:44 +00:00
|
|
|
setLocale(that.fLocale);
|
2007-12-17 01:39:55 +00:00
|
|
|
isArgNumeric = that.isArgNumeric;
|
2003-01-27 17:27:40 +00:00
|
|
|
int32_t j;
|
|
|
|
for (j=0; j<subformatCount; ++j) {
|
|
|
|
delete subformats[j].format;
|
|
|
|
}
|
|
|
|
subformatCount = 0;
|
|
|
|
|
|
|
|
for (j=0; j<that.subformatCount; ++j) {
|
|
|
|
// Subformat::operator= does NOT delete this.format
|
|
|
|
subformats[j] = that.subformats[j];
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
subformatCount = that.subformatCount;
|
|
|
|
|
|
|
|
for (j=0; j<that.argTypeCount; ++j) {
|
|
|
|
argTypes[j] = that.argTypes[j];
|
|
|
|
}
|
|
|
|
argTypeCount = that.argTypeCount;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
2003-01-27 17:27:40 +00:00
|
|
|
MessageFormat::operator==(const Format& rhs) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-01-27 17:27:40 +00:00
|
|
|
if (this == &rhs) return TRUE;
|
|
|
|
|
|
|
|
MessageFormat& that = (MessageFormat&)rhs;
|
|
|
|
|
|
|
|
// Check class ID before checking MessageFormat members
|
|
|
|
if (!Format::operator==(rhs) ||
|
|
|
|
fPattern != that.fPattern ||
|
2007-12-17 01:39:55 +00:00
|
|
|
fLocale != that.fLocale ||
|
|
|
|
isArgNumeric != that.isArgNumeric) {
|
2003-01-27 17:27:40 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t j;
|
|
|
|
for (j=0; j<subformatCount; ++j) {
|
|
|
|
if (subformats[j] != that.subformats[j]) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return FALSE;
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2007-12-17 01:39:55 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Creates a copy of this MessageFormat, the caller owns the copy.
|
|
|
|
|
|
|
|
Format*
|
|
|
|
MessageFormat::clone() const
|
|
|
|
{
|
2003-01-27 17:27:40 +00:00
|
|
|
return new MessageFormat(*this);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Sets the locale of this MessageFormat object to theLocale.
|
|
|
|
|
|
|
|
void
|
|
|
|
MessageFormat::setLocale(const Locale& theLocale)
|
|
|
|
{
|
2003-02-05 00:53:44 +00:00
|
|
|
if (fLocale != theLocale) {
|
|
|
|
delete defaultNumberFormat;
|
|
|
|
defaultNumberFormat = NULL;
|
|
|
|
delete defaultDateFormat;
|
|
|
|
defaultDateFormat = NULL;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
fLocale = theLocale;
|
2004-01-20 23:06:38 +00:00
|
|
|
setLocaleIDs(fLocale.getName(), fLocale.getName());
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Gets the locale of this MessageFormat object.
|
|
|
|
|
|
|
|
const Locale&
|
|
|
|
MessageFormat::getLocale() const
|
|
|
|
{
|
|
|
|
return fLocale;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
MessageFormat::applyPattern(const UnicodeString& newPattern,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
|
|
|
UParseError parseError;
|
|
|
|
applyPattern(newPattern,parseError,status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Applies the new pattern and returns an error if the pattern
|
|
|
|
// is not correct.
|
2001-08-16 00:55:16 +00:00
|
|
|
void
|
2003-01-27 17:27:40 +00:00
|
|
|
MessageFormat::applyPattern(const UnicodeString& pattern,
|
2001-08-16 00:55:16 +00:00
|
|
|
UParseError& parseError,
|
2003-01-27 17:27:40 +00:00
|
|
|
UErrorCode& ec)
|
|
|
|
{
|
|
|
|
if(U_FAILURE(ec)) {
|
2001-08-16 00:55:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
// The pattern is broken up into segments. Each time a subformat
|
|
|
|
// is encountered, 4 segments are recorded. For example, consider
|
|
|
|
// the pattern:
|
|
|
|
// "There {0,choice,0.0#are no files|1.0#is one file|1.0<are {0, number} files} on disk {1}."
|
|
|
|
// The first set of segments is:
|
|
|
|
// segments[0] = "There "
|
|
|
|
// segments[1] = "0"
|
|
|
|
// segments[2] = "choice"
|
|
|
|
// segments[3] = "0.0#are no files|1.0#is one file|1.0<are {0, number} files"
|
|
|
|
|
|
|
|
// During parsing, the plain text is accumulated into segments[0].
|
|
|
|
// Segments 1..3 are used to parse each subpattern. Each time a
|
|
|
|
// subpattern is parsed, it creates a format object that is stored
|
2003-01-28 22:17:40 +00:00
|
|
|
// in the subformats array, together with an offset and argument
|
|
|
|
// number. The offset into the plain text stored in
|
|
|
|
// segments[0].
|
2003-01-27 17:27:40 +00:00
|
|
|
|
|
|
|
// Quotes in segment 0 are handled normally. They are removed.
|
|
|
|
// Quotes may not occur in segments 1 or 2.
|
|
|
|
// Quotes in segment 3 are parsed and _copied_. This makes
|
|
|
|
// subformat patterns work, e.g., {1,number,'#'.##} passes
|
|
|
|
// the pattern "'#'.##" to DecimalFormat.
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
UnicodeString segments[4];
|
2003-01-27 17:27:40 +00:00
|
|
|
int32_t part = 0; // segment we are in, 0..3
|
|
|
|
// Record the highest argument number in the pattern. (In the
|
|
|
|
// subpattern {3,number} the argument number is 3.)
|
2001-08-16 00:55:16 +00:00
|
|
|
int32_t formatNumber = 0;
|
|
|
|
UBool inQuote = FALSE;
|
|
|
|
int32_t braceStack = 0;
|
|
|
|
// Clear error struct
|
2001-11-15 21:13:37 +00:00
|
|
|
parseError.offset = -1;
|
2001-08-16 00:55:16 +00:00
|
|
|
parseError.preContext[0] = parseError.postContext[0] = (UChar)0;
|
2003-01-27 17:27:40 +00:00
|
|
|
int32_t patLen = pattern.length();
|
|
|
|
int32_t i;
|
2010-01-16 03:07:18 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
for (i=0; i<subformatCount; ++i) {
|
|
|
|
delete subformats[i].format;
|
|
|
|
}
|
|
|
|
subformatCount = 0;
|
|
|
|
argTypeCount = 0;
|
|
|
|
|
|
|
|
for (i=0; i<patLen; ++i) {
|
|
|
|
UChar ch = pattern[i];
|
2001-08-16 00:55:16 +00:00
|
|
|
if (part == 0) {
|
2003-01-27 17:27:40 +00:00
|
|
|
// In segment 0, recognize and remove quotes
|
2001-08-16 00:55:16 +00:00
|
|
|
if (ch == SINGLE_QUOTE) {
|
2003-01-27 17:27:40 +00:00
|
|
|
if (i+1 < patLen && pattern[i+1] == SINGLE_QUOTE) {
|
|
|
|
segments[0] += ch;
|
2001-08-16 00:55:16 +00:00
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
inQuote = !inQuote;
|
|
|
|
}
|
|
|
|
} else if (ch == LEFT_CURLY_BRACE && !inQuote) {
|
2003-01-27 17:27:40 +00:00
|
|
|
// The only way we get from segment 0 to 1 is via an
|
|
|
|
// unquoted '{'.
|
2001-08-16 00:55:16 +00:00
|
|
|
part = 1;
|
|
|
|
} else {
|
2003-01-27 17:27:40 +00:00
|
|
|
segments[0] += ch;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
} else if (inQuote) {
|
|
|
|
// In segments 1..3, recognize quoted matter, and copy it
|
|
|
|
// into the segment, together with the quotes. This takes
|
|
|
|
// care of '' as well.
|
2001-08-16 00:55:16 +00:00
|
|
|
segments[part] += ch;
|
|
|
|
if (ch == SINGLE_QUOTE) {
|
|
|
|
inQuote = FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
2003-01-27 17:27:40 +00:00
|
|
|
// We have an unquoted character in segment 1..3
|
2001-08-16 00:55:16 +00:00
|
|
|
switch (ch) {
|
|
|
|
case COMMA:
|
2003-01-27 17:27:40 +00:00
|
|
|
// Commas bump us to the next segment, except for segment 3,
|
|
|
|
// which can contain commas. See example above.
|
2001-08-16 00:55:16 +00:00
|
|
|
if (part < 3)
|
|
|
|
part += 1;
|
|
|
|
else
|
2003-01-27 17:27:40 +00:00
|
|
|
segments[3] += ch;
|
2001-08-16 00:55:16 +00:00
|
|
|
break;
|
|
|
|
case LEFT_CURLY_BRACE:
|
2003-01-27 17:27:40 +00:00
|
|
|
// Handle '{' within segment 3. The initial '{'
|
|
|
|
// before segment 1 is handled above.
|
|
|
|
if (part != 3) {
|
|
|
|
ec = U_PATTERN_SYNTAX_ERROR;
|
|
|
|
goto SYNTAX_ERROR;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
++braceStack;
|
|
|
|
segments[part] += ch;
|
|
|
|
break;
|
|
|
|
case RIGHT_CURLY_BRACE:
|
|
|
|
if (braceStack == 0) {
|
2003-01-27 17:27:40 +00:00
|
|
|
makeFormat(formatNumber, segments, parseError,ec);
|
|
|
|
if (U_FAILURE(ec)){
|
|
|
|
goto SYNTAX_ERROR;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
formatNumber++;
|
2010-01-16 03:07:18 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
segments[1].remove();
|
|
|
|
segments[2].remove();
|
|
|
|
segments[3].remove();
|
|
|
|
part = 0;
|
2001-08-16 00:55:16 +00:00
|
|
|
} else {
|
|
|
|
--braceStack;
|
|
|
|
segments[part] += ch;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SINGLE_QUOTE:
|
|
|
|
inQuote = TRUE;
|
2003-01-27 17:27:40 +00:00
|
|
|
// fall through (copy quote chars in segments 1..3)
|
2001-08-16 00:55:16 +00:00
|
|
|
default:
|
|
|
|
segments[part] += ch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
if (braceStack != 0 || part != 0) {
|
|
|
|
// Unmatched braces in the pattern
|
|
|
|
ec = U_UNMATCHED_BRACES;
|
|
|
|
goto SYNTAX_ERROR;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
fPattern = segments[0];
|
2003-01-27 17:27:40 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
SYNTAX_ERROR:
|
|
|
|
syntaxError(pattern, i, parseError);
|
|
|
|
for (i=0; i<subformatCount; ++i) {
|
|
|
|
delete subformats[i].format;
|
|
|
|
}
|
|
|
|
argTypeCount = subformatCount = 0;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Converts this MessageFormat instance to a pattern.
|
2003-01-27 17:27:40 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
2003-01-27 17:27:40 +00:00
|
|
|
MessageFormat::toPattern(UnicodeString& appendTo) const {
|
2001-06-25 21:15:56 +00:00
|
|
|
// later, make this more extensible
|
|
|
|
int32_t lastOffset = 0;
|
2003-01-27 17:27:40 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<subformatCount; ++i) {
|
|
|
|
copyAndFixQuotes(fPattern, lastOffset, subformats[i].offset, appendTo);
|
|
|
|
lastOffset = subformats[i].offset;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += LEFT_CURLY_BRACE;
|
2007-12-17 01:39:55 +00:00
|
|
|
if (isArgNumeric) {
|
|
|
|
itos(subformats[i].argNum, appendTo);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
appendTo += *subformats[i].argName;
|
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
Format* fmt = subformats[i].format;
|
|
|
|
if (fmt == NULL) {
|
2001-06-25 21:15:56 +00:00
|
|
|
// do nothing, string format
|
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
else if (fmt->getDynamicClassID() == DecimalFormat::getStaticClassID()) {
|
2001-06-25 21:15:56 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
NumberFormat& formatAlias = *(NumberFormat*)fmt;
|
|
|
|
NumberFormat *defaultTemplate = NumberFormat::createInstance(fLocale, ec);
|
|
|
|
NumberFormat *currencyTemplate = NumberFormat::createCurrencyInstance(fLocale, ec);
|
|
|
|
NumberFormat *percentTemplate = NumberFormat::createPercentInstance(fLocale, ec);
|
|
|
|
NumberFormat *integerTemplate = createIntegerFormat(fLocale, ec);
|
2001-06-25 21:15:56 +00:00
|
|
|
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_NUMBER;
|
|
|
|
if (formatAlias != *defaultTemplate) {
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2001-06-25 21:15:56 +00:00
|
|
|
if (formatAlias == *currencyTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_CURRENCY;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else if (formatAlias == *percentTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_PERCENT;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else if (formatAlias == *integerTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_INTEGER;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
UnicodeString buffer;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ((DecimalFormat*)fmt)->toPattern(buffer);
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
delete defaultTemplate;
|
2001-06-25 21:15:56 +00:00
|
|
|
delete currencyTemplate;
|
|
|
|
delete percentTemplate;
|
|
|
|
delete integerTemplate;
|
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
else if (fmt->getDynamicClassID() == SimpleDateFormat::getStaticClassID()) {
|
|
|
|
DateFormat& formatAlias = *(DateFormat*)fmt;
|
2001-06-25 21:15:56 +00:00
|
|
|
DateFormat *defaultDateTemplate = DateFormat::createDateInstance(DateFormat::kDefault, fLocale);
|
|
|
|
DateFormat *shortDateTemplate = DateFormat::createDateInstance(DateFormat::kShort, fLocale);
|
|
|
|
DateFormat *longDateTemplate = DateFormat::createDateInstance(DateFormat::kLong, fLocale);
|
|
|
|
DateFormat *fullDateTemplate = DateFormat::createDateInstance(DateFormat::kFull, fLocale);
|
|
|
|
DateFormat *defaultTimeTemplate = DateFormat::createTimeInstance(DateFormat::kDefault, fLocale);
|
|
|
|
DateFormat *shortTimeTemplate = DateFormat::createTimeInstance(DateFormat::kShort, fLocale);
|
|
|
|
DateFormat *longTimeTemplate = DateFormat::createTimeInstance(DateFormat::kLong, fLocale);
|
|
|
|
DateFormat *fullTimeTemplate = DateFormat::createTimeInstance(DateFormat::kFull, fLocale);
|
|
|
|
|
|
|
|
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2001-06-25 21:15:56 +00:00
|
|
|
if (formatAlias == *defaultDateTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_DATE;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else if (formatAlias == *shortDateTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_DATE;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_SHORT;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else if (formatAlias == *defaultDateTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_DATE;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_MEDIUM;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else if (formatAlias == *longDateTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_DATE;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_LONG;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else if (formatAlias == *fullDateTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_DATE;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_FULL;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else if (formatAlias == *defaultTimeTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_TIME;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else if (formatAlias == *shortTimeTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_TIME;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_SHORT;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else if (formatAlias == *defaultTimeTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_TIME;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_MEDIUM;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else if (formatAlias == *longTimeTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_TIME;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_LONG;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else if (formatAlias == *fullTimeTemplate) {
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_TIME;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_FULL;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
UnicodeString buffer;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_DATE;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ((SimpleDateFormat*)fmt)->toPattern(buffer);
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete defaultDateTemplate;
|
|
|
|
delete shortDateTemplate;
|
|
|
|
delete longDateTemplate;
|
|
|
|
delete fullDateTemplate;
|
|
|
|
delete defaultTimeTemplate;
|
|
|
|
delete shortTimeTemplate;
|
|
|
|
delete longTimeTemplate;
|
|
|
|
delete fullTimeTemplate;
|
|
|
|
// {sfb} there should be a more efficient way to do this!
|
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
else if (fmt->getDynamicClassID() == ChoiceFormat::getStaticClassID()) {
|
2001-06-25 21:15:56 +00:00
|
|
|
UnicodeString buffer;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ID_CHOICE;
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += COMMA;
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo += ((ChoiceFormat*)fmt)->toPattern(buffer);
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
|
|
|
else if (fmt->getDynamicClassID() == PluralFormat::getStaticClassID()) {
|
|
|
|
UnicodeString buffer;
|
|
|
|
appendTo += ((PluralFormat*)fmt)->toPattern(buffer);
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
2010-01-16 03:07:18 +00:00
|
|
|
else if (fmt->getDynamicClassID() == SelectFormat::getStaticClassID()) {
|
|
|
|
UnicodeString buffer;
|
|
|
|
appendTo += ((SelectFormat*)fmt)->toPattern(buffer);
|
|
|
|
}
|
2001-06-25 21:15:56 +00:00
|
|
|
else {
|
2002-11-23 01:11:50 +00:00
|
|
|
//appendTo += ", unknown";
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += RIGHT_CURLY_BRACE;
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
|
|
|
copyAndFixQuotes(fPattern, lastOffset, fPattern.length(), appendTo);
|
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Adopts the new formats array and updates the array count.
|
|
|
|
// This MessageFormat instance owns the new formats.
|
|
|
|
|
|
|
|
void
|
|
|
|
MessageFormat::adoptFormats(Format** newFormats,
|
2003-01-27 17:27:40 +00:00
|
|
|
int32_t count) {
|
|
|
|
if (newFormats == NULL || count < 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
int32_t i;
|
2003-01-27 17:27:40 +00:00
|
|
|
if (allocateSubformats(count)) {
|
|
|
|
for (i=0; i<subformatCount; ++i) {
|
|
|
|
delete subformats[i].format;
|
|
|
|
}
|
|
|
|
for (i=0; i<count; ++i) {
|
|
|
|
subformats[i].format = newFormats[i];
|
|
|
|
}
|
|
|
|
subformatCount = count;
|
|
|
|
} else {
|
|
|
|
// An adopt method must always take ownership. Delete
|
|
|
|
// the incoming format objects and return unchanged.
|
|
|
|
for (i=0; i<count; ++i) {
|
|
|
|
delete newFormats[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
// TODO: What about the .offset and .argNum fields?
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Sets the new formats array and updates the array count.
|
|
|
|
// This MessageFormat instance maks a copy of the new formats.
|
|
|
|
|
|
|
|
void
|
|
|
|
MessageFormat::setFormats(const Format** newFormats,
|
2003-01-27 17:27:40 +00:00
|
|
|
int32_t count) {
|
|
|
|
if (newFormats == NULL || count < 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
|
|
|
|
if (allocateSubformats(count)) {
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<subformatCount; ++i) {
|
|
|
|
delete subformats[i].format;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
subformatCount = 0;
|
|
|
|
|
|
|
|
for (i=0; i<count; ++i) {
|
|
|
|
subformats[i].format = newFormats[i] ? newFormats[i]->clone() : NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
subformatCount = count;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
|
|
|
|
// TODO: What about the .offset and .arg fields?
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
2007-12-17 01:39:55 +00:00
|
|
|
// Adopt a single format by format number.
|
|
|
|
// Do nothing if the format number is not less than the array count.
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
void
|
2003-01-27 17:27:40 +00:00
|
|
|
MessageFormat::adoptFormat(int32_t n, Format *newFormat) {
|
|
|
|
if (n < 0 || n >= subformatCount) {
|
|
|
|
delete newFormat;
|
|
|
|
} else {
|
|
|
|
delete subformats[n].format;
|
|
|
|
subformats[n].format = newFormat;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Adopt a single format by format name.
|
|
|
|
// Do nothing if there is no match of formatName.
|
|
|
|
void
|
|
|
|
MessageFormat::adoptFormat(const UnicodeString& formatName,
|
|
|
|
Format* formatToAdopt,
|
|
|
|
UErrorCode& status) {
|
|
|
|
if (isArgNumeric ) {
|
|
|
|
int32_t argumentNumber = stou(formatName);
|
|
|
|
if (argumentNumber<0) {
|
|
|
|
status = U_ARGUMENT_TYPE_MISMATCH;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
adoptFormat(argumentNumber, formatToAdopt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<subformatCount; ++i) {
|
|
|
|
if (formatName==*subformats[i].argName) {
|
|
|
|
delete subformats[i].format;
|
|
|
|
if ( formatToAdopt== NULL) {
|
|
|
|
// This should never happen -- but we'll be nice if it does
|
|
|
|
subformats[i].format = NULL;
|
|
|
|
} else {
|
|
|
|
subformats[i].format = formatToAdopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
2003-01-27 17:27:40 +00:00
|
|
|
// Set a single format.
|
2007-12-17 01:39:55 +00:00
|
|
|
// Do nothing if the variable is not less than the array count.
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
void
|
2003-01-27 17:27:40 +00:00
|
|
|
MessageFormat::setFormat(int32_t n, const Format& newFormat) {
|
|
|
|
if (n >= 0 && n < subformatCount) {
|
|
|
|
delete subformats[n].format;
|
|
|
|
if (&newFormat == NULL) {
|
|
|
|
// This should never happen -- but we'll be nice if it does
|
|
|
|
subformats[n].format = NULL;
|
|
|
|
} else {
|
|
|
|
subformats[n].format = newFormat.clone();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-17 01:39:55 +00:00
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Get a single format by format name.
|
|
|
|
// Do nothing if the variable is not less than the array count.
|
|
|
|
Format *
|
|
|
|
MessageFormat::getFormat(const UnicodeString& formatName, UErrorCode& status) {
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) return NULL;
|
|
|
|
|
|
|
|
if (isArgNumeric ) {
|
|
|
|
int32_t argumentNumber = stou(formatName);
|
|
|
|
if (argumentNumber<0) {
|
|
|
|
status = U_ARGUMENT_TYPE_MISMATCH;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (argumentNumber < 0 || argumentNumber >= subformatCount) {
|
|
|
|
return subformats[argumentNumber].format;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i=0; i<subformatCount; ++i) {
|
|
|
|
if (formatName==*subformats[i].argName)
|
|
|
|
{
|
|
|
|
return subformats[i].format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Set a single format by format name
|
|
|
|
// Do nothing if the variable is not less than the array count.
|
|
|
|
void
|
|
|
|
MessageFormat::setFormat(const UnicodeString& formatName,
|
|
|
|
const Format& newFormat,
|
|
|
|
UErrorCode& status) {
|
|
|
|
if (isArgNumeric) {
|
|
|
|
status = U_ARGUMENT_TYPE_MISMATCH;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<subformatCount; ++i) {
|
|
|
|
if (formatName==*subformats[i].argName)
|
|
|
|
{
|
|
|
|
delete subformats[i].format;
|
|
|
|
if (&newFormat == NULL) {
|
|
|
|
// This should never happen -- but we'll be nice if it does
|
|
|
|
subformats[i].format = NULL;
|
|
|
|
} else {
|
|
|
|
subformats[i].format = newFormat.clone();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Gets the format array.
|
|
|
|
|
|
|
|
const Format**
|
|
|
|
MessageFormat::getFormats(int32_t& cnt) const
|
|
|
|
{
|
2003-01-28 22:17:40 +00:00
|
|
|
// This old API returns an array (which we hold) of Format*
|
|
|
|
// pointers. The array is valid up to the next call to any
|
|
|
|
// method on this object. We construct and resize an array
|
|
|
|
// on demand that contains aliases to the subformats[i].format
|
|
|
|
// pointers.
|
|
|
|
MessageFormat* t = (MessageFormat*) this;
|
|
|
|
cnt = 0;
|
|
|
|
if (formatAliases == NULL) {
|
|
|
|
t->formatAliasesCapacity = (subformatCount<10) ? 10 : subformatCount;
|
|
|
|
Format** a = (Format**)
|
|
|
|
uprv_malloc(sizeof(Format*) * formatAliasesCapacity);
|
|
|
|
if (a == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
t->formatAliases = a;
|
|
|
|
} else if (subformatCount > formatAliasesCapacity) {
|
|
|
|
Format** a = (Format**)
|
|
|
|
uprv_realloc(formatAliases, sizeof(Format*) * subformatCount);
|
|
|
|
if (a == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
t->formatAliases = a;
|
|
|
|
t->formatAliasesCapacity = subformatCount;
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
2003-01-28 22:17:40 +00:00
|
|
|
for (int32_t i=0; i<subformatCount; ++i) {
|
|
|
|
t->formatAliases[i] = subformats[i].format;
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
2003-01-28 22:17:40 +00:00
|
|
|
cnt = subformatCount;
|
|
|
|
return (const Format**)formatAliases;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
|
|
|
|
StringEnumeration*
|
|
|
|
MessageFormat::getFormatNames(UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) return NULL;
|
|
|
|
|
|
|
|
if (isArgNumeric) {
|
|
|
|
status = U_ARGUMENT_TYPE_MISMATCH;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
UVector *fFormatNames = new UVector(status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (int32_t i=0; i<subformatCount; ++i) {
|
|
|
|
fFormatNames->addElement(new UnicodeString(*subformats[i].argName), status);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringEnumeration* nameEnumerator = new FormatNameEnumeration(fFormatNames, status);
|
|
|
|
return nameEnumerator;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Formats the source Formattable array and copy into the result buffer.
|
|
|
|
// Ignore the FieldPosition result for error checking.
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
MessageFormat::format(const Formattable* source,
|
|
|
|
int32_t cnt,
|
2002-11-23 01:11:50 +00:00
|
|
|
UnicodeString& appendTo,
|
1999-08-16 21:50:52 +00:00
|
|
|
FieldPosition& ignore,
|
|
|
|
UErrorCode& success) const
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
2002-11-23 01:11:50 +00:00
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-11-23 01:11:50 +00:00
|
|
|
return format(source, cnt, appendTo, ignore, 0, success);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Internally creates a MessageFormat instance based on the
|
|
|
|
// pattern and formats the arguments Formattable array and
|
2002-11-23 01:11:50 +00:00
|
|
|
// copy into the appendTo buffer.
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
MessageFormat::format( const UnicodeString& pattern,
|
|
|
|
const Formattable* arguments,
|
|
|
|
int32_t cnt,
|
2002-11-23 01:11:50 +00:00
|
|
|
UnicodeString& appendTo,
|
1999-08-16 21:50:52 +00:00
|
|
|
UErrorCode& success)
|
|
|
|
{
|
2003-01-27 17:27:40 +00:00
|
|
|
MessageFormat temp(pattern, success);
|
1999-08-16 21:50:52 +00:00
|
|
|
FieldPosition ignore(0);
|
2003-01-27 17:27:40 +00:00
|
|
|
temp.format(arguments, cnt, appendTo, ignore, success);
|
2002-11-23 01:11:50 +00:00
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Formats the source Formattable object and copy into the
|
2002-11-23 01:11:50 +00:00
|
|
|
// appendTo buffer. The Formattable object must be an array
|
1999-08-16 21:50:52 +00:00
|
|
|
// of Formattable instances, returns error otherwise.
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
MessageFormat::format(const Formattable& source,
|
2002-11-23 01:11:50 +00:00
|
|
|
UnicodeString& appendTo,
|
1999-08-16 21:50:52 +00:00
|
|
|
FieldPosition& ignore,
|
|
|
|
UErrorCode& success) const
|
|
|
|
{
|
|
|
|
int32_t cnt;
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
2002-11-23 01:11:50 +00:00
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
if (source.getType() != Formattable::kArray) {
|
1999-10-07 00:07:53 +00:00
|
|
|
success = U_ILLEGAL_ARGUMENT_ERROR;
|
2002-11-23 01:11:50 +00:00
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
const Formattable* tmpPtr = source.getArray(cnt);
|
|
|
|
|
2002-11-23 01:11:50 +00:00
|
|
|
return format(tmpPtr, cnt, appendTo, ignore, 0, success);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2007-12-17 01:39:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
MessageFormat::format(const UnicodeString* argumentNames,
|
|
|
|
const Formattable* arguments,
|
|
|
|
int32_t count,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
UErrorCode& success) const {
|
|
|
|
FieldPosition ignore(0);
|
|
|
|
return format(arguments, argumentNames, count, appendTo, ignore, 0, success);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
MessageFormat::format(const Formattable* arguments,
|
|
|
|
int32_t cnt,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& status,
|
|
|
|
int32_t recursionProtection,
|
|
|
|
UErrorCode& success) const
|
|
|
|
{
|
|
|
|
return format(arguments, NULL, cnt, appendTo, status, recursionProtection, success);
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
2002-11-23 01:11:50 +00:00
|
|
|
// Formats the arguments Formattable array and copy into the appendTo buffer.
|
1999-08-16 21:50:52 +00:00
|
|
|
// Ignore the FieldPosition result for error checking.
|
|
|
|
|
|
|
|
UnicodeString&
|
2007-12-17 01:39:55 +00:00
|
|
|
MessageFormat::format(const Formattable* arguments,
|
|
|
|
const UnicodeString *argumentNames,
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t cnt,
|
2002-11-23 01:11:50 +00:00
|
|
|
UnicodeString& appendTo,
|
1999-08-16 21:50:52 +00:00
|
|
|
FieldPosition& status,
|
|
|
|
int32_t recursionProtection,
|
|
|
|
UErrorCode& success) const
|
2007-12-17 01:39:55 +00:00
|
|
|
{
|
|
|
|
int32_t lastOffset = 0;
|
|
|
|
int32_t argumentNumber=0;
|
2003-01-27 17:27:40 +00:00
|
|
|
if (cnt < 0 || (cnt && arguments == NULL)) {
|
1999-10-07 00:07:53 +00:00
|
|
|
success = U_ILLEGAL_ARGUMENT_ERROR;
|
2002-11-23 01:11:50 +00:00
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2007-12-17 01:39:55 +00:00
|
|
|
|
|
|
|
if ( !isArgNumeric && argumentNames== NULL ) {
|
|
|
|
success = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Formattable *obj=NULL;
|
2003-01-27 17:27:40 +00:00
|
|
|
for (int32_t i=0; i<subformatCount; ++i) {
|
1999-08-16 21:50:52 +00:00
|
|
|
// Append the prefix of current format element.
|
2003-01-27 17:27:40 +00:00
|
|
|
appendTo.append(fPattern, lastOffset, subformats[i].offset - lastOffset);
|
|
|
|
lastOffset = subformats[i].offset;
|
2007-12-17 01:39:55 +00:00
|
|
|
obj = NULL;
|
|
|
|
if (isArgNumeric) {
|
|
|
|
argumentNumber = subformats[i].argNum;
|
|
|
|
|
|
|
|
// Checks the scope of the argument number.
|
|
|
|
if (argumentNumber >= cnt) {
|
|
|
|
appendTo += LEFT_CURLY_BRACE;
|
|
|
|
itos(argumentNumber, appendTo);
|
|
|
|
appendTo += RIGHT_CURLY_BRACE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
obj = arguments+argumentNumber;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (int32_t j=0; j<cnt; ++j) {
|
|
|
|
if (argumentNames[j]== *subformats[i].argName ) {
|
|
|
|
obj = arguments+j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (obj == NULL ) {
|
|
|
|
appendTo += LEFT_CURLY_BRACE;
|
|
|
|
appendTo += *subformats[i].argName;
|
|
|
|
appendTo += RIGHT_CURLY_BRACE;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2002-10-12 01:09:00 +00:00
|
|
|
Formattable::Type type = obj->getType();
|
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
// Recursively calling the format process only if the current
|
2010-01-16 03:07:18 +00:00
|
|
|
// format argument refers to either of the following:
|
|
|
|
// a ChoiceFormat object ,a PluralFormat object, a SelectFormat object.
|
2003-01-27 17:27:40 +00:00
|
|
|
Format* fmt = subformats[i].format;
|
|
|
|
if (fmt != NULL) {
|
2007-12-17 01:39:55 +00:00
|
|
|
UnicodeString argNum;
|
|
|
|
fmt->format(*obj, argNum, success);
|
2002-10-12 01:09:00 +00:00
|
|
|
|
2010-01-16 03:07:18 +00:00
|
|
|
// Needs to reprocess the ChoiceFormat and PluralFormat and SelectFormat option by using the
|
2003-01-27 17:27:40 +00:00
|
|
|
// MessageFormat pattern application.
|
2007-12-17 01:39:55 +00:00
|
|
|
if ((fmt->getDynamicClassID() == ChoiceFormat::getStaticClassID() ||
|
2010-01-16 03:07:18 +00:00
|
|
|
fmt->getDynamicClassID() == PluralFormat::getStaticClassID() ||
|
|
|
|
fmt->getDynamicClassID() == SelectFormat::getStaticClassID()
|
|
|
|
) &&
|
2007-12-17 01:39:55 +00:00
|
|
|
argNum.indexOf(LEFT_CURLY_BRACE) >= 0) {
|
|
|
|
MessageFormat temp(argNum, fLocale, success);
|
2003-01-27 17:27:40 +00:00
|
|
|
// TODO: Implement recursion protection
|
2007-12-17 01:39:55 +00:00
|
|
|
if ( isArgNumeric ) {
|
|
|
|
temp.format(arguments, NULL, cnt, appendTo, status, recursionProtection, success);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
temp.format(arguments, argumentNames, cnt, appendTo, status, recursionProtection, success);
|
|
|
|
}
|
2002-10-12 01:09:00 +00:00
|
|
|
if (U_FAILURE(success)) {
|
2002-11-23 01:11:50 +00:00
|
|
|
return appendTo;
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2007-12-17 01:39:55 +00:00
|
|
|
appendTo += argNum;
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2002-10-12 01:09:00 +00:00
|
|
|
// If the obj data type is a number, use a NumberFormat instance.
|
2003-11-19 04:42:44 +00:00
|
|
|
else if ((type == Formattable::kDouble) ||
|
|
|
|
(type == Formattable::kLong) ||
|
|
|
|
(type == Formattable::kInt64)) {
|
|
|
|
|
2003-02-14 22:16:14 +00:00
|
|
|
const NumberFormat* nf = getDefaultNumberFormat(success);
|
2003-02-05 00:53:44 +00:00
|
|
|
if (nf == NULL) {
|
2002-11-23 01:11:50 +00:00
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2002-10-12 01:09:00 +00:00
|
|
|
if (type == Formattable::kDouble) {
|
2003-02-05 00:53:44 +00:00
|
|
|
nf->format(obj->getDouble(), appendTo);
|
2003-11-19 04:42:44 +00:00
|
|
|
} else if (type == Formattable::kLong) {
|
2003-02-05 00:53:44 +00:00
|
|
|
nf->format(obj->getLong(), appendTo);
|
2003-11-19 04:42:44 +00:00
|
|
|
} else {
|
|
|
|
nf->format(obj->getInt64(), appendTo);
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
// If the obj data type is a Date instance, use a DateFormat instance.
|
2002-10-12 01:09:00 +00:00
|
|
|
else if (type == Formattable::kDate) {
|
2003-02-14 22:16:14 +00:00
|
|
|
const DateFormat* df = getDefaultDateFormat(success);
|
2003-02-05 00:53:44 +00:00
|
|
|
if (df == NULL) {
|
2002-11-23 01:11:50 +00:00
|
|
|
return appendTo;
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
2003-02-05 00:53:44 +00:00
|
|
|
df->format(obj->getDate(), appendTo);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2002-10-12 01:09:00 +00:00
|
|
|
else if (type == Formattable::kString) {
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += obj->getString();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
else {
|
1999-10-07 00:07:53 +00:00
|
|
|
success = U_ILLEGAL_ARGUMENT_ERROR;
|
2002-11-23 01:11:50 +00:00
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Appends the rest of the pattern characters after the real last offset.
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo.append(fPattern, lastOffset, 0x7fffffff);
|
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
1999-12-02 18:34:10 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Parses the source pattern and returns the Formattable objects array,
|
|
|
|
// the array count and the ending parse position. The caller of this method
|
|
|
|
// owns the array.
|
|
|
|
|
|
|
|
Formattable*
|
|
|
|
MessageFormat::parse(const UnicodeString& source,
|
2003-01-27 17:27:40 +00:00
|
|
|
ParsePosition& pos,
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t& count) const
|
|
|
|
{
|
2003-01-27 17:27:40 +00:00
|
|
|
// Allocate at least one element. Allocating an array of length
|
|
|
|
// zero causes problems on some platforms (e.g. Win32).
|
|
|
|
Formattable *resultArray = new Formattable[argTypeCount ? argTypeCount : 1];
|
2001-06-25 21:15:56 +00:00
|
|
|
int32_t patternOffset = 0;
|
2003-01-27 17:27:40 +00:00
|
|
|
int32_t sourceOffset = pos.getIndex();
|
|
|
|
ParsePosition tempPos(0);
|
2001-06-25 21:15:56 +00:00
|
|
|
count = 0; // {sfb} reset to zero
|
2003-01-27 17:27:40 +00:00
|
|
|
int32_t len;
|
2008-01-11 22:17:28 +00:00
|
|
|
// If resultArray could not be created, exit out.
|
|
|
|
// Avoid crossing initialization of variables above.
|
|
|
|
if (resultArray == NULL) {
|
2008-02-23 19:15:18 +00:00
|
|
|
goto PARSE_ERROR;
|
2008-01-11 22:17:28 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
for (int32_t i = 0; i < subformatCount; ++i) {
|
2001-06-25 21:15:56 +00:00
|
|
|
// match up to format
|
2003-01-27 17:27:40 +00:00
|
|
|
len = subformats[i].offset - patternOffset;
|
2001-06-25 21:15:56 +00:00
|
|
|
if (len == 0 ||
|
|
|
|
fPattern.compare(patternOffset, len, source, sourceOffset, len) == 0) {
|
|
|
|
sourceOffset += len;
|
|
|
|
patternOffset += len;
|
|
|
|
}
|
|
|
|
else {
|
2003-01-27 17:27:40 +00:00
|
|
|
goto PARSE_ERROR;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now use format
|
2003-01-27 17:27:40 +00:00
|
|
|
Format* fmt = subformats[i].format;
|
2007-12-17 01:39:55 +00:00
|
|
|
int32_t argNum = subformats[i].argNum;
|
2003-01-27 17:27:40 +00:00
|
|
|
if (fmt == NULL) { // string format
|
2001-06-25 21:15:56 +00:00
|
|
|
// if at end, use longest possible match
|
|
|
|
// otherwise uses first match to intervening string
|
|
|
|
// does NOT recursively try all possibilities
|
2003-01-27 17:27:40 +00:00
|
|
|
int32_t tempLength = (i+1<subformatCount) ?
|
|
|
|
subformats[i+1].offset : fPattern.length();
|
2001-06-25 21:15:56 +00:00
|
|
|
|
|
|
|
int32_t next;
|
|
|
|
if (patternOffset >= tempLength) {
|
|
|
|
next = source.length();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
UnicodeString buffer;
|
|
|
|
fPattern.extract(patternOffset,tempLength - patternOffset, buffer);
|
|
|
|
next = source.indexOf(buffer, sourceOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next < 0) {
|
2003-01-27 17:27:40 +00:00
|
|
|
goto PARSE_ERROR;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
UnicodeString buffer;
|
|
|
|
source.extract(sourceOffset,next - sourceOffset, buffer);
|
|
|
|
UnicodeString strValue = buffer;
|
|
|
|
UnicodeString temp(LEFT_CURLY_BRACE);
|
1999-08-16 21:50:52 +00:00
|
|
|
// {sfb} check this later
|
2007-12-17 01:39:55 +00:00
|
|
|
if (isArgNumeric) {
|
|
|
|
itos(argNum, temp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
temp+=(*subformats[i].argName);
|
|
|
|
}
|
2001-06-25 21:15:56 +00:00
|
|
|
temp += RIGHT_CURLY_BRACE;
|
|
|
|
if (strValue != temp) {
|
|
|
|
source.extract(sourceOffset,next - sourceOffset, buffer);
|
2007-12-17 01:39:55 +00:00
|
|
|
resultArray[argNum].setString(buffer);
|
2001-06-25 21:15:56 +00:00
|
|
|
// {sfb} not sure about this
|
2007-12-17 01:39:55 +00:00
|
|
|
if ((argNum + 1) > count) {
|
|
|
|
count = argNum + 1;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sourceOffset = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2003-01-27 17:27:40 +00:00
|
|
|
tempPos.setIndex(sourceOffset);
|
2007-12-17 01:39:55 +00:00
|
|
|
fmt->parseObject(source, resultArray[argNum], tempPos);
|
2003-01-27 17:27:40 +00:00
|
|
|
if (tempPos.getIndex() == sourceOffset) {
|
|
|
|
goto PARSE_ERROR;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
if ((argNum + 1) > count) {
|
|
|
|
count = argNum + 1;
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
|
|
|
sourceOffset = tempPos.getIndex(); // update
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
len = fPattern.length() - patternOffset;
|
2001-06-25 21:15:56 +00:00
|
|
|
if (len == 0 ||
|
|
|
|
fPattern.compare(patternOffset, len, source, sourceOffset, len) == 0) {
|
2003-01-27 17:27:40 +00:00
|
|
|
pos.setIndex(sourceOffset + len);
|
|
|
|
return resultArray;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
// else fall through...
|
|
|
|
|
|
|
|
PARSE_ERROR:
|
|
|
|
pos.setErrorIndex(sourceOffset);
|
|
|
|
delete [] resultArray;
|
|
|
|
count = 0;
|
|
|
|
return NULL; // leave index as is to signal error
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Parses the source string and returns the array of
|
|
|
|
// Formattable objects and the array count. The caller
|
|
|
|
// owns the returned array.
|
|
|
|
|
|
|
|
Formattable*
|
|
|
|
MessageFormat::parse(const UnicodeString& source,
|
|
|
|
int32_t& cnt,
|
|
|
|
UErrorCode& success) const
|
|
|
|
{
|
2007-12-17 01:39:55 +00:00
|
|
|
if (!isArgNumeric ) {
|
|
|
|
success = U_ARGUMENT_TYPE_MISMATCH;
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
ParsePosition status(0);
|
|
|
|
// Calls the actual implementation method and starts
|
|
|
|
// from zero offset of the source text.
|
|
|
|
Formattable* result = parse(source, status, cnt);
|
|
|
|
if (status.getIndex() == 0) {
|
1999-10-07 00:07:53 +00:00
|
|
|
success = U_MESSAGE_PARSE_ERROR;
|
2003-01-27 17:27:40 +00:00
|
|
|
delete[] result;
|
1999-08-16 21:50:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Parses the source text and copy into the result buffer.
|
|
|
|
|
|
|
|
void
|
|
|
|
MessageFormat::parseObject( const UnicodeString& source,
|
|
|
|
Formattable& result,
|
|
|
|
ParsePosition& status) const
|
|
|
|
{
|
|
|
|
int32_t cnt = 0;
|
|
|
|
Formattable* tmpResult = parse(source, status, cnt);
|
|
|
|
if (tmpResult != NULL)
|
|
|
|
result.adoptArray(tmpResult, cnt);
|
|
|
|
}
|
|
|
|
|
2005-05-27 22:07:16 +00:00
|
|
|
UnicodeString
|
|
|
|
MessageFormat::autoQuoteApostrophe(const UnicodeString& pattern, UErrorCode& status) {
|
|
|
|
UnicodeString result;
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
int32_t plen = pattern.length();
|
|
|
|
const UChar* pat = pattern.getBuffer();
|
|
|
|
int32_t blen = plen * 2 + 1; // space for null termination, convenience
|
|
|
|
UChar* buf = result.getBuffer(blen);
|
|
|
|
if (buf == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
} else {
|
|
|
|
int32_t len = umsg_autoQuoteApostrophe(pat, plen, buf, blen, &status);
|
2005-06-22 02:40:25 +00:00
|
|
|
result.releaseBuffer(U_SUCCESS(status) ? len : 0);
|
2005-05-27 22:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
result.setToBogus();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
2004-08-04 05:57:45 +00:00
|
|
|
|
|
|
|
static Format* makeRBNF(URBNFRuleSetTag tag, const Locale& locale, const UnicodeString& defaultRuleSet, UErrorCode& ec) {
|
|
|
|
RuleBasedNumberFormat* fmt = new RuleBasedNumberFormat(tag, locale, ec);
|
2008-01-11 22:17:28 +00:00
|
|
|
if (fmt == NULL) {
|
2008-02-23 19:15:18 +00:00
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
2008-01-11 22:17:28 +00:00
|
|
|
} else if (U_SUCCESS(ec) && defaultRuleSet.length() > 0) {
|
2008-02-23 19:15:18 +00:00
|
|
|
UErrorCode localStatus = U_ZERO_ERROR; // ignore unrecognized default rule set
|
|
|
|
fmt->setDefaultRuleSet(defaultRuleSet, localStatus);
|
2004-08-04 05:57:45 +00:00
|
|
|
}
|
|
|
|
return fmt;
|
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
2003-01-27 17:27:40 +00:00
|
|
|
* Reads the segments[] array (see applyPattern()) and parses the
|
|
|
|
* segments[1..3] into a Format* object. Stores the format object in
|
|
|
|
* the subformats[] array. Updates the argTypes[] array type
|
|
|
|
* information for the corresponding argument.
|
|
|
|
*
|
|
|
|
* @param formatNumber index into subformats[] for this format
|
|
|
|
* @param segments array of strings with the parsed pattern segments
|
|
|
|
* @param parseError parse error data (output param)
|
|
|
|
* @param ec error code
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2003-01-27 17:27:40 +00:00
|
|
|
void
|
|
|
|
MessageFormat::makeFormat(int32_t formatNumber,
|
|
|
|
UnicodeString* segments,
|
|
|
|
UParseError& parseError,
|
|
|
|
UErrorCode& ec) {
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
// Parse the argument number
|
|
|
|
int32_t argumentNumber = stou(segments[1]); // always unlocalized!
|
2007-12-17 01:39:55 +00:00
|
|
|
UnicodeString argumentName;
|
2003-01-27 17:27:40 +00:00
|
|
|
if (argumentNumber < 0) {
|
2007-12-17 01:39:55 +00:00
|
|
|
if ( (isArgNumeric==TRUE) && (formatNumber !=0) ) {
|
|
|
|
ec = U_INVALID_FORMAT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
isArgNumeric = FALSE;
|
|
|
|
argumentNumber=formatNumber;
|
|
|
|
}
|
|
|
|
if (!isArgNumeric) {
|
|
|
|
if ( !isLegalArgName(segments[1]) ) {
|
|
|
|
ec = U_INVALID_FORMAT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
argumentName = segments[1];
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
// Parse the format, recording the argument type and creating a
|
|
|
|
// new Format object (except for string arguments).
|
|
|
|
Formattable::Type argType;
|
|
|
|
Format *fmt = NULL;
|
|
|
|
int32_t typeID, styleID;
|
|
|
|
DateFormat::EStyle style;
|
2007-12-17 01:39:55 +00:00
|
|
|
UnicodeString unquotedPattern, quotedPattern;
|
|
|
|
UBool inQuote = FALSE;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
switch (typeID = findKeyword(segments[2], TYPE_IDS)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
case 0: // string
|
|
|
|
argType = Formattable::kString;
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
case 1: // number
|
|
|
|
argType = Formattable::kDouble;
|
|
|
|
|
|
|
|
switch (findKeyword(segments[3], NUMBER_STYLE_IDS)) {
|
|
|
|
case 0: // default
|
|
|
|
fmt = NumberFormat::createInstance(fLocale, ec);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
2003-01-27 17:27:40 +00:00
|
|
|
case 1: // currency
|
|
|
|
fmt = NumberFormat::createCurrencyInstance(fLocale, ec);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
2003-01-27 17:27:40 +00:00
|
|
|
case 2: // percent
|
|
|
|
fmt = NumberFormat::createPercentInstance(fLocale, ec);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
2003-01-27 17:27:40 +00:00
|
|
|
case 3: // integer
|
2003-11-20 17:37:05 +00:00
|
|
|
argType = Formattable::kLong;
|
2003-01-27 17:27:40 +00:00
|
|
|
fmt = createIntegerFormat(fLocale, ec);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
default: // pattern
|
2003-01-27 17:27:40 +00:00
|
|
|
fmt = NumberFormat::createInstance(fLocale, ec);
|
|
|
|
if (fmt &&
|
|
|
|
fmt->getDynamicClassID() == DecimalFormat::getStaticClassID()) {
|
|
|
|
((DecimalFormat*)fmt)->applyPattern(segments[3],parseError,ec);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
case 2: // date
|
|
|
|
case 3: // time
|
|
|
|
argType = Formattable::kDate;
|
|
|
|
styleID = findKeyword(segments[3], DATE_STYLE_IDS);
|
|
|
|
style = (styleID >= 0) ? DATE_STYLES[styleID] : DateFormat::kDefault;
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
if (typeID == 2) {
|
|
|
|
fmt = DateFormat::createDateInstance(style, fLocale);
|
|
|
|
} else {
|
|
|
|
fmt = DateFormat::createTimeInstance(style, fLocale);
|
2002-01-30 06:18:26 +00:00
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2003-01-28 22:17:40 +00:00
|
|
|
if (styleID < 0 &&
|
2003-01-27 17:27:40 +00:00
|
|
|
fmt != NULL &&
|
|
|
|
fmt->getDynamicClassID() == SimpleDateFormat::getStaticClassID()) {
|
|
|
|
((SimpleDateFormat*)fmt)->applyPattern(segments[3]);
|
2002-01-30 06:18:26 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
2003-01-27 17:27:40 +00:00
|
|
|
|
|
|
|
case 4: // choice
|
|
|
|
argType = Formattable::kDouble;
|
|
|
|
|
|
|
|
fmt = new ChoiceFormat(segments[3], parseError, ec);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
2003-01-27 17:27:40 +00:00
|
|
|
|
2004-08-04 05:57:45 +00:00
|
|
|
case 5: // spellout
|
|
|
|
argType = Formattable::kDouble;
|
|
|
|
fmt = makeRBNF(URBNF_SPELLOUT, fLocale, segments[3], ec);
|
|
|
|
break;
|
|
|
|
case 6: // ordinal
|
|
|
|
argType = Formattable::kDouble;
|
|
|
|
fmt = makeRBNF(URBNF_ORDINAL, fLocale, segments[3], ec);
|
|
|
|
break;
|
|
|
|
case 7: // duration
|
|
|
|
argType = Formattable::kDouble;
|
|
|
|
fmt = makeRBNF(URBNF_DURATION, fLocale, segments[3], ec);
|
|
|
|
break;
|
2007-12-17 01:39:55 +00:00
|
|
|
case 8: // plural
|
2010-01-16 03:07:18 +00:00
|
|
|
case 9: // Select
|
|
|
|
if(typeID == 8)
|
|
|
|
argType = Formattable::kDouble;
|
|
|
|
else
|
|
|
|
argType = Formattable::kString;
|
2007-12-17 01:39:55 +00:00
|
|
|
quotedPattern = segments[3];
|
|
|
|
for (int32_t i = 0; i < quotedPattern.length(); ++i) {
|
|
|
|
UChar ch = quotedPattern.charAt(i);
|
|
|
|
if (ch == SINGLE_QUOTE) {
|
|
|
|
if (i+1 < quotedPattern.length() && quotedPattern.charAt(i+1)==SINGLE_QUOTE) {
|
|
|
|
unquotedPattern+=ch;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
inQuote = !inQuote;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unquotedPattern += ch;
|
|
|
|
}
|
|
|
|
}
|
2010-01-16 03:07:18 +00:00
|
|
|
if(typeID == 8)
|
|
|
|
fmt = new PluralFormat(fLocale, unquotedPattern, ec);
|
|
|
|
else
|
|
|
|
fmt = new SelectFormat(unquotedPattern, ec);
|
2007-12-17 01:39:55 +00:00
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
default:
|
2003-02-11 02:45:50 +00:00
|
|
|
argType = Formattable::kString;
|
2003-01-27 17:27:40 +00:00
|
|
|
ec = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fmt==NULL && argType!=Formattable::kString && U_SUCCESS(ec)) {
|
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allocateSubformats(formatNumber+1) ||
|
|
|
|
!allocateArgTypes(argumentNumber+1)) {
|
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
delete fmt;
|
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
// Parse succeeded; record results in our arrays
|
|
|
|
subformats[formatNumber].format = fmt;
|
|
|
|
subformats[formatNumber].offset = segments[0].length();
|
2007-12-17 01:39:55 +00:00
|
|
|
if (isArgNumeric) {
|
|
|
|
subformats[formatNumber].argName = NULL;
|
|
|
|
subformats[formatNumber].argNum = argumentNumber;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
subformats[formatNumber].argName = new UnicodeString(argumentName);
|
|
|
|
subformats[formatNumber].argNum = -1;
|
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
subformatCount = formatNumber+1;
|
|
|
|
|
|
|
|
// Careful here: argumentNumber may in general arrive out of
|
|
|
|
// sequence, e.g., "There was {2} on {0,date} (see {1,number})."
|
|
|
|
argTypes[argumentNumber] = argType;
|
|
|
|
if (argumentNumber+1 > argTypeCount) {
|
|
|
|
argTypeCount = argumentNumber+1;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Finds the string, s, in the string array, list.
|
|
|
|
int32_t MessageFormat::findKeyword(const UnicodeString& s,
|
2003-01-27 17:27:40 +00:00
|
|
|
const UChar * const *list)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-06-25 21:15:56 +00:00
|
|
|
if (s.length() == 0)
|
2004-08-04 05:57:45 +00:00
|
|
|
return 0; // default
|
2001-06-25 21:15:56 +00:00
|
|
|
|
|
|
|
UnicodeString buffer = s;
|
|
|
|
// Trims the space characters and turns all characters
|
|
|
|
// in s to lower case.
|
2004-12-22 19:40:16 +00:00
|
|
|
buffer.trim().toLower("");
|
2004-08-04 05:57:45 +00:00
|
|
|
for (int32_t i = 0; list[i]; ++i) {
|
|
|
|
if (!buffer.compare(list[i], u_strlen(list[i]))) {
|
2001-06-25 21:15:56 +00:00
|
|
|
return i;
|
2004-08-04 05:57:45 +00:00
|
|
|
}
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
return -1;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Checks the range of the source text to quote the special
|
|
|
|
// characters, { and ' and copy to target buffer.
|
|
|
|
|
|
|
|
void
|
|
|
|
MessageFormat::copyAndFixQuotes(const UnicodeString& source,
|
|
|
|
int32_t start,
|
|
|
|
int32_t end,
|
2002-11-23 01:11:50 +00:00
|
|
|
UnicodeString& appendTo)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-06-25 21:15:56 +00:00
|
|
|
UBool gotLB = FALSE;
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
for (int32_t i = start; i < end; ++i) {
|
2001-06-25 21:15:56 +00:00
|
|
|
UChar ch = source[i];
|
|
|
|
if (ch == LEFT_CURLY_BRACE) {
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += SINGLE_QUOTE;
|
|
|
|
appendTo += LEFT_CURLY_BRACE;
|
|
|
|
appendTo += SINGLE_QUOTE;
|
2001-06-25 21:15:56 +00:00
|
|
|
gotLB = TRUE;
|
|
|
|
}
|
|
|
|
else if (ch == RIGHT_CURLY_BRACE) {
|
|
|
|
if(gotLB) {
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += RIGHT_CURLY_BRACE;
|
2001-06-25 21:15:56 +00:00
|
|
|
gotLB = FALSE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// orig code.
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += SINGLE_QUOTE;
|
|
|
|
appendTo += RIGHT_CURLY_BRACE;
|
|
|
|
appendTo += SINGLE_QUOTE;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ch == SINGLE_QUOTE) {
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += SINGLE_QUOTE;
|
|
|
|
appendTo += SINGLE_QUOTE;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += ch;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method that ought to be in NumberFormat
|
|
|
|
*/
|
|
|
|
NumberFormat*
|
|
|
|
MessageFormat::createIntegerFormat(const Locale& locale, UErrorCode& status) const {
|
2001-06-25 21:15:56 +00:00
|
|
|
NumberFormat *temp = NumberFormat::createInstance(locale, status);
|
2005-06-27 22:30:32 +00:00
|
|
|
if (temp != NULL && temp->getDynamicClassID() == DecimalFormat::getStaticClassID()) {
|
2001-06-25 21:15:56 +00:00
|
|
|
DecimalFormat *temp2 = (DecimalFormat*) temp;
|
|
|
|
temp2->setMaximumFractionDigits(0);
|
|
|
|
temp2->setDecimalSeparatorAlwaysShown(FALSE);
|
|
|
|
temp2->setParseIntegerOnly(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return temp;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-02-05 00:53:44 +00:00
|
|
|
/**
|
|
|
|
* Return the default number format. Used to format a numeric
|
|
|
|
* argument when subformats[i].format is NULL. Returns NULL
|
|
|
|
* on failure.
|
|
|
|
*
|
|
|
|
* Semantically const but may modify *this.
|
|
|
|
*/
|
2003-02-14 22:16:14 +00:00
|
|
|
const NumberFormat* MessageFormat::getDefaultNumberFormat(UErrorCode& ec) const {
|
2003-02-05 00:53:44 +00:00
|
|
|
if (defaultNumberFormat == NULL) {
|
|
|
|
MessageFormat* t = (MessageFormat*) this;
|
|
|
|
t->defaultNumberFormat = NumberFormat::createInstance(fLocale, ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
delete t->defaultNumberFormat;
|
|
|
|
t->defaultNumberFormat = NULL;
|
2003-02-14 22:16:14 +00:00
|
|
|
} else if (t->defaultNumberFormat == NULL) {
|
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
2003-02-05 00:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultNumberFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the default date format. Used to format a date
|
|
|
|
* argument when subformats[i].format is NULL. Returns NULL
|
|
|
|
* on failure.
|
|
|
|
*
|
|
|
|
* Semantically const but may modify *this.
|
|
|
|
*/
|
2003-02-14 22:16:14 +00:00
|
|
|
const DateFormat* MessageFormat::getDefaultDateFormat(UErrorCode& ec) const {
|
2003-02-05 00:53:44 +00:00
|
|
|
if (defaultDateFormat == NULL) {
|
|
|
|
MessageFormat* t = (MessageFormat*) this;
|
|
|
|
t->defaultDateFormat = DateFormat::createDateTimeInstance(DateFormat::kShort, DateFormat::kShort, fLocale);
|
2003-02-14 22:16:14 +00:00
|
|
|
if (t->defaultDateFormat == NULL) {
|
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
2003-02-05 00:53:44 +00:00
|
|
|
}
|
|
|
|
return defaultDateFormat;
|
|
|
|
}
|
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
UBool
|
|
|
|
MessageFormat::usesNamedArguments() const {
|
|
|
|
return !isArgNumeric;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
MessageFormat::isLegalArgName(const UnicodeString& argName) const {
|
|
|
|
if(!u_hasBinaryProperty(argName.charAt(0), idStart)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
for (int32_t i=1; i<argName.length(); ++i) {
|
|
|
|
if(!u_hasBinaryProperty(argName.charAt(i), idContinue)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-09-19 19:05:37 +00:00
|
|
|
int32_t
|
|
|
|
MessageFormat::getArgTypeCount() const {
|
|
|
|
return argTypeCount;
|
2010-01-16 03:07:18 +00:00
|
|
|
}
|
2008-09-19 19:05:37 +00:00
|
|
|
|
2007-12-29 05:51:50 +00:00
|
|
|
FormatNameEnumeration::FormatNameEnumeration(UVector *fNameList, UErrorCode& /*status*/) {
|
2007-12-17 01:39:55 +00:00
|
|
|
pos=0;
|
|
|
|
fFormatNames = fNameList;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString*
|
|
|
|
FormatNameEnumeration::snext(UErrorCode& status) {
|
|
|
|
if (U_SUCCESS(status) && pos < fFormatNames->size()) {
|
|
|
|
return (const UnicodeString*)fFormatNames->elementAt(pos++);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FormatNameEnumeration::reset(UErrorCode& /*status*/) {
|
|
|
|
pos=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
FormatNameEnumeration::count(UErrorCode& /*status*/) const {
|
|
|
|
return (fFormatNames==NULL) ? 0 : fFormatNames->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatNameEnumeration::~FormatNameEnumeration() {
|
|
|
|
UnicodeString *s;
|
|
|
|
for (int32_t i=0; i<fFormatNames->size(); ++i) {
|
|
|
|
if ((s=(UnicodeString *)fFormatNames->elementAt(i))!=NULL) {
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete fFormatNames;
|
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//eof
|