2010-01-16 03:07:18 +00:00
|
|
|
/********************************************************************
|
2010-10-20 02:36:31 +00:00
|
|
|
* COPYRIGHT:
|
2012-04-07 04:27:27 +00:00
|
|
|
* Copyright (c) 1997-2012, International Business Machines Corporation and
|
2010-01-16 03:07:18 +00:00
|
|
|
* 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
|
2010-10-20 02:36:31 +00:00
|
|
|
* 11/01/09 kirtig Added SelectFormat
|
2010-01-16 03:07:18 +00:00
|
|
|
********************************************************************/
|
2000-08-23 23:48:04 +00:00
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "unicode/appendable.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/choicfmt.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "unicode/datefmt.h"
|
|
|
|
#include "unicode/decimfmt.h"
|
|
|
|
#include "unicode/localpointer.h"
|
|
|
|
#include "unicode/msgfmt.h"
|
2007-12-17 01:39:55 +00:00
|
|
|
#include "unicode/plurfmt.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "unicode/rbnf.h"
|
2010-01-16 03:07:18 +00:00
|
|
|
#include "unicode/selfmt.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "unicode/smpdtfmt.h"
|
2005-05-27 22:07:16 +00:00
|
|
|
#include "unicode/umsg.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "unicode/ustring.h"
|
2002-02-28 01:42:40 +00:00
|
|
|
#include "cmemory.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "patternprops.h"
|
|
|
|
#include "messageimpl.h"
|
2007-12-17 01:39:55 +00:00
|
|
|
#include "msgfmt_impl.h"
|
2003-01-27 17:27:40 +00:00
|
|
|
#include "uassert.h"
|
2011-07-04 23:51:56 +00:00
|
|
|
#include "uelement.h"
|
|
|
|
#include "uhash.h"
|
2007-12-17 01:39:55 +00:00
|
|
|
#include "ustrfmt.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "util.h"
|
2007-12-17 01:39:55 +00:00
|
|
|
#include "uvector.h"
|
2000-08-23 23:48:04 +00:00
|
|
|
|
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 SINGLE_QUOTE ((UChar)0x0027)
|
2011-04-25 20:47:32 +00:00
|
|
|
#define COMMA ((UChar)0x002C)
|
2001-06-25 21:15:56 +00:00
|
|
|
#define LEFT_CURLY_BRACE ((UChar)0x007B)
|
|
|
|
#define RIGHT_CURLY_BRACE ((UChar)0x007D)
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
//---------------------------------------
|
|
|
|
// static data
|
|
|
|
|
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
|
|
|
};
|
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" */
|
|
|
|
};
|
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[] = {
|
|
|
|
ID_NUMBER,
|
2010-10-20 02:36:31 +00:00
|
|
|
ID_DATE,
|
2003-01-27 17:27:40 +00:00
|
|
|
ID_TIME,
|
2004-08-04 05:57:45 +00:00
|
|
|
ID_SPELLOUT,
|
|
|
|
ID_ORDINAL,
|
|
|
|
ID_DURATION,
|
|
|
|
NULL,
|
2001-08-16 00:55:16 +00:00
|
|
|
};
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2011-04-25 20:47:32 +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_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
|
|
|
};
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2011-07-06 04:03:35 +00:00
|
|
|
static const icu::DateFormat::EStyle DATE_STYLES[] = {
|
|
|
|
icu::DateFormat::kDefault,
|
|
|
|
icu::DateFormat::kShort,
|
|
|
|
icu::DateFormat::kMedium,
|
|
|
|
icu::DateFormat::kLong,
|
|
|
|
icu::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
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
static const UChar NULL_STRING[] = {
|
|
|
|
0x6E, 0x75, 0x6C, 0x6C, 0 // "null"
|
|
|
|
};
|
|
|
|
|
|
|
|
static const UChar OTHER_STRING[] = {
|
|
|
|
0x6F, 0x74, 0x68, 0x65, 0x72, 0 // "other"
|
|
|
|
};
|
|
|
|
|
|
|
|
U_CDECL_BEGIN
|
|
|
|
static UBool U_CALLCONV equalFormatsForHash(const UHashTok key1,
|
|
|
|
const UHashTok key2) {
|
2011-07-06 04:03:35 +00:00
|
|
|
return icu::MessageFormat::equalFormats(key1.pointer, key2.pointer);
|
2011-04-25 20:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CDECL_END
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
// -------------------------------------
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MessageFormat)
|
2011-04-25 20:47:32 +00:00
|
|
|
UOBJECT_DEFINE_NO_RTTI_IMPLEMENTATION(MessageFormat::DummyFormat)
|
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 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
|
2011-07-07 18:46:19 +00:00
|
|
|
appendTo.append(temp, -1);
|
2003-01-27 17:27:40 +00:00
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
|
|
|
|
// AppendableWrapper: encapsulates the result of formatting, keeping track
|
|
|
|
// of the string and its length.
|
|
|
|
class AppendableWrapper : public UMemory {
|
2008-01-22 21:15:14 +00:00
|
|
|
public:
|
2011-04-25 20:47:32 +00:00
|
|
|
AppendableWrapper(Appendable& appendable) : app(appendable), len(0) {
|
2008-01-22 21:15:14 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
void append(const UnicodeString& s) {
|
|
|
|
app.appendString(s.getBuffer(), s.length());
|
|
|
|
len += s.length();
|
2008-01-22 21:15:14 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
void append(const UChar* s, const int32_t sLength) {
|
|
|
|
app.appendString(s, sLength);
|
|
|
|
len += sLength;
|
|
|
|
}
|
|
|
|
void append(const UnicodeString& s, int32_t start, int32_t length) {
|
|
|
|
append(s.tempSubString(start, length));
|
|
|
|
}
|
|
|
|
void formatAndAppend(const Format* formatter, const Formattable& arg, UErrorCode& ec) {
|
|
|
|
UnicodeString s;
|
|
|
|
formatter->format(arg, s, ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
append(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t length() {
|
|
|
|
return len;
|
2008-01-22 21:15:14 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
private:
|
|
|
|
Appendable& app;
|
|
|
|
int32_t len;
|
2008-01-22 21:15:14 +00:00
|
|
|
};
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
|
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
|
2011-04-25 20:47:32 +00:00
|
|
|
msgPattern(success),
|
2003-02-11 02:45:50 +00:00
|
|
|
formatAliases(NULL),
|
|
|
|
formatAliasesCapacity(0),
|
2003-01-27 17:27:40 +00:00
|
|
|
argTypes(NULL),
|
|
|
|
argTypeCount(0),
|
2003-01-28 22:17:40 +00:00
|
|
|
argTypeCapacity(0),
|
2011-04-25 20:47:32 +00:00
|
|
|
hasArgTypeConflicts(FALSE),
|
2007-12-29 05:51:50 +00:00
|
|
|
defaultNumberFormat(NULL),
|
2011-04-25 20:47:32 +00:00
|
|
|
defaultDateFormat(NULL),
|
|
|
|
cachedFormatters(NULL),
|
|
|
|
customFormatArgStarts(NULL),
|
2012-04-07 04:27:27 +00:00
|
|
|
pluralProvider(&fLocale, UPLURAL_TYPE_CARDINAL),
|
|
|
|
ordinalProvider(&fLocale, UPLURAL_TYPE_ORDINAL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2004-01-20 23:06:38 +00:00
|
|
|
setLocaleIDs(fLocale.getName(), fLocale.getName());
|
2011-04-25 20:47:32 +00:00
|
|
|
applyPattern(pattern, success);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
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),
|
2011-04-25 20:47:32 +00:00
|
|
|
msgPattern(success),
|
2003-02-11 02:45:50 +00:00
|
|
|
formatAliases(NULL),
|
|
|
|
formatAliasesCapacity(0),
|
2003-01-27 17:27:40 +00:00
|
|
|
argTypes(NULL),
|
|
|
|
argTypeCount(0),
|
2003-01-28 22:17:40 +00:00
|
|
|
argTypeCapacity(0),
|
2011-04-25 20:47:32 +00:00
|
|
|
hasArgTypeConflicts(FALSE),
|
2007-12-29 05:51:50 +00:00
|
|
|
defaultNumberFormat(NULL),
|
2011-04-25 20:47:32 +00:00
|
|
|
defaultDateFormat(NULL),
|
|
|
|
cachedFormatters(NULL),
|
|
|
|
customFormatArgStarts(NULL),
|
2012-04-07 04:27:27 +00:00
|
|
|
pluralProvider(&fLocale, UPLURAL_TYPE_CARDINAL),
|
|
|
|
ordinalProvider(&fLocale, UPLURAL_TYPE_ORDINAL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2004-01-20 23:06:38 +00:00
|
|
|
setLocaleIDs(fLocale.getName(), fLocale.getName());
|
2011-04-25 20:47:32 +00:00
|
|
|
applyPattern(pattern, success);
|
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),
|
2011-04-25 20:47:32 +00:00
|
|
|
msgPattern(success),
|
2003-02-11 02:45:50 +00:00
|
|
|
formatAliases(NULL),
|
|
|
|
formatAliasesCapacity(0),
|
2003-01-27 17:27:40 +00:00
|
|
|
argTypes(NULL),
|
|
|
|
argTypeCount(0),
|
2003-01-28 22:17:40 +00:00
|
|
|
argTypeCapacity(0),
|
2011-04-25 20:47:32 +00:00
|
|
|
hasArgTypeConflicts(FALSE),
|
2007-12-29 05:51:50 +00:00
|
|
|
defaultNumberFormat(NULL),
|
2011-04-25 20:47:32 +00:00
|
|
|
defaultDateFormat(NULL),
|
|
|
|
cachedFormatters(NULL),
|
|
|
|
customFormatArgStarts(NULL),
|
2012-04-07 04:27:27 +00:00
|
|
|
pluralProvider(&fLocale, UPLURAL_TYPE_CARDINAL),
|
|
|
|
ordinalProvider(&fLocale, UPLURAL_TYPE_ORDINAL)
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
2004-01-20 23:06:38 +00:00
|
|
|
setLocaleIDs(fLocale.getName(), fLocale.getName());
|
2011-04-25 20:47:32 +00:00
|
|
|
applyPattern(pattern, parseError, success);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
2003-01-28 22:17:40 +00:00
|
|
|
MessageFormat::MessageFormat(const MessageFormat& that)
|
2011-04-25 20:47:32 +00:00
|
|
|
:
|
|
|
|
Format(that),
|
|
|
|
fLocale(that.fLocale),
|
|
|
|
msgPattern(that.msgPattern),
|
2003-02-11 02:45:50 +00:00
|
|
|
formatAliases(NULL),
|
|
|
|
formatAliasesCapacity(0),
|
2003-01-28 22:17:40 +00:00
|
|
|
argTypes(NULL),
|
|
|
|
argTypeCount(0),
|
|
|
|
argTypeCapacity(0),
|
2011-04-25 20:47:32 +00:00
|
|
|
hasArgTypeConflicts(that.hasArgTypeConflicts),
|
2007-12-29 05:51:50 +00:00
|
|
|
defaultNumberFormat(NULL),
|
2011-04-25 20:47:32 +00:00
|
|
|
defaultDateFormat(NULL),
|
|
|
|
cachedFormatters(NULL),
|
|
|
|
customFormatArgStarts(NULL),
|
2012-04-07 04:27:27 +00:00
|
|
|
pluralProvider(&fLocale, UPLURAL_TYPE_CARDINAL),
|
|
|
|
ordinalProvider(&fLocale, UPLURAL_TYPE_ORDINAL)
|
2003-01-28 22:17:40 +00:00
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
// This will take care of creating the hash tables (since they are NULL).
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
copyObjects(that, ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
resetPattern();
|
|
|
|
}
|
2003-01-28 22:17:40 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
MessageFormat::~MessageFormat()
|
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
uhash_close(cachedFormatters);
|
|
|
|
uhash_close(customFormatArgStarts);
|
2003-01-27 17:27:40 +00:00
|
|
|
|
|
|
|
uprv_free(argTypes);
|
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 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.
|
|
|
|
*/
|
2011-04-25 20:47:32 +00:00
|
|
|
UBool MessageFormat::allocateArgTypes(int32_t capacity, UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (argTypeCapacity >= capacity) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (capacity < DEFAULT_INITIAL_CAPACITY) {
|
|
|
|
capacity = DEFAULT_INITIAL_CAPACITY;
|
|
|
|
} else if (capacity < 2*argTypeCapacity) {
|
|
|
|
capacity = 2*argTypeCapacity;
|
|
|
|
}
|
|
|
|
Formattable::Type* a = (Formattable::Type*)
|
2003-01-27 17:27:40 +00:00
|
|
|
uprv_realloc(argTypes, sizeof(*argTypes) * capacity);
|
2011-04-25 20:47:32 +00:00
|
|
|
if (a == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return FALSE;
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
argTypes = a;
|
|
|
|
argTypeCapacity = capacity;
|
2003-01-27 17:27:40 +00:00
|
|
|
return TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// assignment operator
|
|
|
|
|
|
|
|
const MessageFormat&
|
|
|
|
MessageFormat::operator=(const MessageFormat& that)
|
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
if (this != &that) {
|
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
|
|
|
|
2003-02-05 00:53:44 +00:00
|
|
|
setLocale(that.fLocale);
|
2011-04-25 20:47:32 +00:00
|
|
|
msgPattern = that.msgPattern;
|
|
|
|
hasArgTypeConflicts = that.hasArgTypeConflicts;
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
copyObjects(that, ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
resetPattern();
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
2010-10-20 02:36:31 +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;
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
MessageFormat& that = (MessageFormat&)rhs;
|
|
|
|
|
|
|
|
// Check class ID before checking MessageFormat members
|
|
|
|
if (!Format::operator==(rhs) ||
|
2011-04-25 20:47:32 +00:00
|
|
|
msgPattern != that.msgPattern ||
|
|
|
|
fLocale != that.fLocale) {
|
2003-01-27 17:27:40 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
// Compare hashtables.
|
|
|
|
if ((customFormatArgStarts == NULL) != (that.customFormatArgStarts == NULL)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (customFormatArgStarts == NULL) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
const int32_t count = uhash_count(customFormatArgStarts);
|
|
|
|
const int32_t rhs_count = uhash_count(that.customFormatArgStarts);
|
|
|
|
if (count != rhs_count) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-09-15 19:34:17 +00:00
|
|
|
int32_t idx = 0, rhs_idx = 0, pos = -1, rhs_pos = -1;
|
2011-04-25 20:47:32 +00:00
|
|
|
for (; idx < count && rhs_idx < rhs_count && U_SUCCESS(ec); ++idx, ++rhs_idx) {
|
|
|
|
const UHashElement* cur = uhash_nextElement(customFormatArgStarts, &pos);
|
|
|
|
const UHashElement* rhs_cur = uhash_nextElement(that.customFormatArgStarts, &rhs_pos);
|
|
|
|
if (cur->key.integer != rhs_cur->key.integer) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
const Format* format = (const Format*)uhash_iget(cachedFormatters, cur->key.integer);
|
|
|
|
const Format* rhs_format = (const Format*)uhash_iget(that.cachedFormatters, rhs_cur->key.integer);
|
|
|
|
if (*format != *rhs_format) {
|
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
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Creates a copy of this MessageFormat, the caller owns the copy.
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
Format*
|
|
|
|
MessageFormat::clone() const
|
|
|
|
{
|
2003-01-27 17:27:40 +00:00
|
|
|
return new MessageFormat(*this);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Sets the locale of this MessageFormat object to theLocale.
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
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;
|
2011-04-25 20:47:32 +00:00
|
|
|
fLocale = theLocale;
|
|
|
|
setLocaleIDs(fLocale.getName(), fLocale.getName());
|
|
|
|
pluralProvider.reset(&fLocale);
|
2012-04-07 04:27:27 +00:00
|
|
|
ordinalProvider.reset(&fLocale);
|
2003-02-05 00:53:44 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Gets the locale of this MessageFormat object.
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
const Locale&
|
|
|
|
MessageFormat::getLocale() const
|
|
|
|
{
|
|
|
|
return fLocale;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
|
|
|
|
void
|
2010-10-20 02:36:31 +00:00
|
|
|
MessageFormat::applyPattern(const UnicodeString& newPattern,
|
2001-08-16 00:55:16 +00:00
|
|
|
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
|
2010-10-20 02:36:31 +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)
|
2010-10-20 02:36:31 +00:00
|
|
|
{
|
2003-01-27 17:27:40 +00:00
|
|
|
if(U_FAILURE(ec)) {
|
2001-08-16 00:55:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
msgPattern.parse(pattern, &parseError, ec);
|
|
|
|
cacheExplicitFormats(ec);
|
2003-01-27 17:27:40 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
resetPattern();
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageFormat::resetPattern() {
|
|
|
|
msgPattern.clear();
|
|
|
|
uhash_close(cachedFormatters);
|
|
|
|
cachedFormatters = NULL;
|
|
|
|
uhash_close(customFormatArgStarts);
|
|
|
|
customFormatArgStarts = NULL;
|
|
|
|
argTypeCount = 0;
|
|
|
|
hasArgTypeConflicts = FALSE;
|
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
void
|
|
|
|
MessageFormat::applyPattern(const UnicodeString& pattern,
|
|
|
|
UMessagePatternApostropheMode aposMode,
|
|
|
|
UParseError* parseError,
|
|
|
|
UErrorCode& status) {
|
|
|
|
if (aposMode != msgPattern.getApostropheMode()) {
|
|
|
|
msgPattern.clearPatternAndSetApostropheMode(aposMode);
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
applyPattern(pattern, *parseError, status);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
2010-10-20 02:36:31 +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 {
|
2011-04-25 20:47:32 +00:00
|
|
|
if ((customFormatArgStarts != NULL && 0 != uhash_count(customFormatArgStarts)) ||
|
|
|
|
0 == msgPattern.countParts()
|
|
|
|
) {
|
|
|
|
appendTo.setToBogus();
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
return appendTo.append(msgPattern.getPatternString());
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t MessageFormat::nextTopLevelArgStart(int32_t partIndex) const {
|
|
|
|
if (partIndex != 0) {
|
|
|
|
partIndex = msgPattern.getLimitPartIndex(partIndex);
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
UMessagePatternPartType type = msgPattern.getPartType(++partIndex);
|
|
|
|
if (type == UMSGPAT_PART_TYPE_ARG_START) {
|
|
|
|
return partIndex;
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
if (type == UMSGPAT_PART_TYPE_MSG_LIMIT) {
|
|
|
|
return -1;
|
2010-10-20 02:36:31 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
void MessageFormat::setArgStartFormat(int32_t argStart,
|
|
|
|
Format* formatter,
|
|
|
|
UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
if (cachedFormatters == NULL) {
|
|
|
|
cachedFormatters=uhash_open(uhash_hashLong, uhash_compareLong,
|
|
|
|
equalFormatsForHash, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete formatter;
|
|
|
|
return;
|
2010-10-20 02:36:31 +00:00
|
|
|
}
|
2011-06-03 05:23:57 +00:00
|
|
|
uhash_setValueDeleter(cachedFormatters, uprv_deleteUObject);
|
2011-04-25 20:47:32 +00:00
|
|
|
}
|
|
|
|
if (formatter == NULL) {
|
|
|
|
formatter = new DummyFormat();
|
|
|
|
}
|
|
|
|
uhash_iput(cachedFormatters, argStart, formatter, &status);
|
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
|
2011-06-10 18:56:08 +00:00
|
|
|
UBool MessageFormat::argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber) {
|
2011-04-25 20:47:32 +00:00
|
|
|
const MessagePattern::Part& part = msgPattern.getPart(partIndex);
|
|
|
|
return part.getType() == UMSGPAT_PART_TYPE_ARG_NAME ?
|
|
|
|
msgPattern.partSubstringMatches(part, argName) :
|
|
|
|
part.getValue() == argNumber; // ARG_NUMBER
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets a custom formatter for a MessagePattern ARG_START part index.
|
|
|
|
// "Custom" formatters are provided by the user via setFormat() or similar APIs.
|
|
|
|
void MessageFormat::setCustomArgStartFormat(int32_t argStart,
|
|
|
|
Format* formatter,
|
|
|
|
UErrorCode& status) {
|
|
|
|
setArgStartFormat(argStart, formatter, status);
|
|
|
|
if (customFormatArgStarts == NULL) {
|
|
|
|
customFormatArgStarts=uhash_open(uhash_hashLong, uhash_compareLong,
|
|
|
|
NULL, &status);
|
|
|
|
}
|
|
|
|
uhash_iputi(customFormatArgStarts, argStart, 1, &status);
|
|
|
|
}
|
|
|
|
|
|
|
|
Format* MessageFormat::getCachedFormatter(int32_t argumentNumber) const {
|
|
|
|
if (cachedFormatters == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
void* ptr = uhash_iget(cachedFormatters, argumentNumber);
|
|
|
|
if (ptr != NULL && dynamic_cast<DummyFormat*>((Format*)ptr) == NULL) {
|
|
|
|
return (Format*) ptr;
|
|
|
|
} else {
|
|
|
|
// Not cached, or a DummyFormat representing setFormat(NULL).
|
|
|
|
return NULL;
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
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
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
// Throw away any cached formatters.
|
|
|
|
if (cachedFormatters != NULL) {
|
|
|
|
uhash_removeAll(cachedFormatters);
|
|
|
|
}
|
|
|
|
if (customFormatArgStarts != NULL) {
|
|
|
|
uhash_removeAll(customFormatArgStarts);
|
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
int32_t formatNumber = 0;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
for (int32_t partIndex = 0;
|
|
|
|
formatNumber < count && U_SUCCESS(status) &&
|
|
|
|
(partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
|
|
|
|
setCustomArgStartFormat(partIndex, newFormats[formatNumber], status);
|
|
|
|
++formatNumber;
|
|
|
|
}
|
|
|
|
// Delete those that didn't get used (if any).
|
|
|
|
for (; formatNumber < count; ++formatNumber) {
|
|
|
|
delete newFormats[formatNumber];
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
|
|
|
|
2010-10-20 02:36:31 +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.
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
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
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
// Throw away any cached formatters.
|
|
|
|
if (cachedFormatters != NULL) {
|
|
|
|
uhash_removeAll(cachedFormatters);
|
|
|
|
}
|
|
|
|
if (customFormatArgStarts != NULL) {
|
|
|
|
uhash_removeAll(customFormatArgStarts);
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
2003-01-27 17:27:40 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int32_t formatNumber = 0;
|
|
|
|
for (int32_t partIndex = 0;
|
|
|
|
formatNumber < count && U_SUCCESS(status) && (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
|
|
|
|
Format* newFormat = NULL;
|
|
|
|
if (newFormats[formatNumber] != NULL) {
|
|
|
|
newFormat = newFormats[formatNumber]->clone();
|
|
|
|
if (newFormat == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setCustomArgStartFormat(partIndex, newFormat, status);
|
|
|
|
++formatNumber;
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
resetPattern();
|
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
2003-01-27 17:27:40 +00:00
|
|
|
MessageFormat::adoptFormat(int32_t n, Format *newFormat) {
|
2011-04-25 20:47:32 +00:00
|
|
|
LocalPointer<Format> p(newFormat);
|
|
|
|
if (n >= 0) {
|
|
|
|
int32_t formatNumber = 0;
|
|
|
|
for (int32_t partIndex = 0; (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
|
|
|
|
if (n == formatNumber) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
setCustomArgStartFormat(partIndex, p.orphan(), status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++formatNumber;
|
|
|
|
}
|
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
|
2010-10-20 02:36:31 +00:00
|
|
|
MessageFormat::adoptFormat(const UnicodeString& formatName,
|
2007-12-17 01:39:55 +00:00
|
|
|
Format* formatToAdopt,
|
|
|
|
UErrorCode& status) {
|
2011-04-25 20:47:32 +00:00
|
|
|
LocalPointer<Format> p(formatToAdopt);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int32_t argNumber = MessagePattern::validateArgumentName(formatName);
|
|
|
|
if (argNumber < UMSGPAT_ARG_NAME_NOT_NUMBER) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2007-12-17 01:39:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
for (int32_t partIndex = 0;
|
|
|
|
(partIndex = nextTopLevelArgStart(partIndex)) >= 0 && U_SUCCESS(status);
|
|
|
|
) {
|
|
|
|
if (argNameMatches(partIndex + 1, formatName, argNumber)) {
|
|
|
|
Format* f;
|
|
|
|
if (p.isValid()) {
|
|
|
|
f = p.orphan();
|
|
|
|
} else if (formatToAdopt == NULL) {
|
|
|
|
f = NULL;
|
2007-12-17 01:39:55 +00:00
|
|
|
} else {
|
2011-04-25 20:47:32 +00:00
|
|
|
f = formatToAdopt->clone();
|
|
|
|
if (f == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
setCustomArgStartFormat(partIndex, f, status);
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2011-04-25 20:47:32 +00:00
|
|
|
|
|
|
|
if (n >= 0) {
|
|
|
|
int32_t formatNumber = 0;
|
|
|
|
for (int32_t partIndex = 0;
|
|
|
|
(partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
|
|
|
|
if (n == formatNumber) {
|
|
|
|
Format* new_format = newFormat.clone();
|
|
|
|
if (new_format) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
setCustomArgStartFormat(partIndex, new_format, status);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++formatNumber;
|
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) {
|
2011-04-25 20:47:32 +00:00
|
|
|
if (U_FAILURE(status) || cachedFormatters == NULL) return NULL;
|
2007-12-17 01:39:55 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
int32_t argNumber = MessagePattern::validateArgumentName(formatName);
|
|
|
|
if (argNumber < UMSGPAT_ARG_NAME_NOT_NUMBER) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
for (int32_t partIndex = 0; (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
|
|
|
|
if (argNameMatches(partIndex + 1, formatName, argNumber)) {
|
|
|
|
return getCachedFormatter(partIndex);
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
2011-04-25 20:47:32 +00:00
|
|
|
if (U_FAILURE(status)) return;
|
|
|
|
|
|
|
|
int32_t argNumber = MessagePattern::validateArgumentName(formatName);
|
|
|
|
if (argNumber < UMSGPAT_ARG_NAME_NOT_NUMBER) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2007-12-17 01:39:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
for (int32_t partIndex = 0;
|
|
|
|
(partIndex = nextTopLevelArgStart(partIndex)) >= 0 && U_SUCCESS(status);
|
|
|
|
) {
|
|
|
|
if (argNameMatches(partIndex + 1, formatName, argNumber)) {
|
2007-12-17 01:39:55 +00:00
|
|
|
if (&newFormat == NULL) {
|
2011-04-25 20:47:32 +00:00
|
|
|
setCustomArgStartFormat(partIndex, NULL, status);
|
2007-12-17 01:39:55 +00:00
|
|
|
} else {
|
2011-04-25 20:47:32 +00:00
|
|
|
Format* new_format = newFormat.clone();
|
|
|
|
if (new_format == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setCustomArgStartFormat(partIndex, new_format, status);
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2011-04-25 20:47:32 +00:00
|
|
|
MessageFormat* t = const_cast<MessageFormat*> (this);
|
2003-01-28 22:17:40 +00:00
|
|
|
cnt = 0;
|
|
|
|
if (formatAliases == NULL) {
|
2011-04-25 20:47:32 +00:00
|
|
|
t->formatAliasesCapacity = (argTypeCount<10) ? 10 : argTypeCount;
|
2003-01-28 22:17:40 +00:00
|
|
|
Format** a = (Format**)
|
|
|
|
uprv_malloc(sizeof(Format*) * formatAliasesCapacity);
|
|
|
|
if (a == NULL) {
|
2011-04-25 20:47:32 +00:00
|
|
|
t->formatAliasesCapacity = 0;
|
2003-01-28 22:17:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
t->formatAliases = a;
|
2011-04-25 20:47:32 +00:00
|
|
|
} else if (argTypeCount > formatAliasesCapacity) {
|
2003-01-28 22:17:40 +00:00
|
|
|
Format** a = (Format**)
|
2011-04-25 20:47:32 +00:00
|
|
|
uprv_realloc(formatAliases, sizeof(Format*) * argTypeCount);
|
2003-01-28 22:17:40 +00:00
|
|
|
if (a == NULL) {
|
2011-04-25 20:47:32 +00:00
|
|
|
t->formatAliasesCapacity = 0;
|
2003-01-28 22:17:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
t->formatAliases = a;
|
2011-04-25 20:47:32 +00:00
|
|
|
t->formatAliasesCapacity = argTypeCount;
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
|
|
|
|
for (int32_t partIndex = 0; (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
|
|
|
|
t->formatAliases[cnt++] = getCachedFormatter(partIndex);
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
|
2003-01-28 22:17:40 +00:00
|
|
|
return (const Format**)formatAliases;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
UnicodeString MessageFormat::getArgName(int32_t partIndex) {
|
|
|
|
const MessagePattern::Part& part = msgPattern.getPart(partIndex);
|
|
|
|
if (part.getType() == UMSGPAT_PART_TYPE_ARG_NAME) {
|
|
|
|
return msgPattern.getSubstring(part);
|
|
|
|
} else {
|
|
|
|
UnicodeString temp;
|
|
|
|
return itos(part.getValue(), temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
StringEnumeration*
|
|
|
|
MessageFormat::getFormatNames(UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) return NULL;
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2007-12-17 01:39:55 +00:00
|
|
|
UVector *fFormatNames = new UVector(status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-03 05:23:57 +00:00
|
|
|
fFormatNames->setDeleter(uprv_deleteUObject);
|
2011-04-25 20:47:32 +00:00
|
|
|
|
|
|
|
for (int32_t partIndex = 0; (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
|
|
|
|
fFormatNames->addElement(new UnicodeString(getArgName(partIndex)), status);
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
MessageFormat::format(const Formattable* source,
|
2010-10-20 02:36:31 +00:00
|
|
|
int32_t cnt,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& ignore,
|
1999-08-16 21:50:52 +00:00
|
|
|
UErrorCode& success) const
|
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
return format(source, NULL, cnt, appendTo, &ignore, success);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Internally creates a MessageFormat instance based on the
|
2010-10-20 02:36:31 +00:00
|
|
|
// pattern and formats the arguments Formattable array and
|
2002-11-23 01:11:50 +00:00
|
|
|
// copy into the appendTo buffer.
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
MessageFormat::format( const UnicodeString& pattern,
|
|
|
|
const Formattable* arguments,
|
|
|
|
int32_t cnt,
|
2010-10-20 02:36:31 +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);
|
2011-04-25 20:47:32 +00:00
|
|
|
return temp.format(arguments, NULL, cnt, appendTo, NULL, success);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
2010-10-20 02:36:31 +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.
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
2010-10-20 02:36:31 +00:00
|
|
|
MessageFormat::format(const Formattable& source,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& ignore,
|
1999-08-16 21:50:52 +00:00
|
|
|
UErrorCode& success) const
|
|
|
|
{
|
2010-10-20 02:36:31 +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
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
int32_t cnt;
|
1999-08-16 21:50:52 +00:00
|
|
|
const Formattable* tmpPtr = source.getArray(cnt);
|
2011-04-25 20:47:32 +00:00
|
|
|
return format(tmpPtr, NULL, cnt, appendTo, &ignore, 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 {
|
2011-04-25 20:47:32 +00:00
|
|
|
return format(arguments, argumentNames, count, appendTo, NULL, success);
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
// Does linear search to find the match for an ArgName.
|
|
|
|
const Formattable* MessageFormat::getArgFromListByName(const Formattable* arguments,
|
|
|
|
const UnicodeString *argumentNames,
|
|
|
|
int32_t cnt, UnicodeString& name) const {
|
|
|
|
for (int32_t i = 0; i < cnt; ++i) {
|
|
|
|
if (0 == argumentNames[i].compare(name)) {
|
|
|
|
return arguments + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
UnicodeString&
|
2007-12-17 01:39:55 +00:00
|
|
|
MessageFormat::format(const Formattable* arguments,
|
|
|
|
const UnicodeString *argumentNames,
|
2010-10-20 02:36:31 +00:00
|
|
|
int32_t cnt,
|
|
|
|
UnicodeString& appendTo,
|
2011-04-25 20:47:32 +00:00
|
|
|
FieldPosition* pos,
|
|
|
|
UErrorCode& status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
2002-11-23 01:11:50 +00:00
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
UnicodeStringAppendable usapp(appendTo);
|
|
|
|
AppendableWrapper app(usapp);
|
|
|
|
format(0, 0.0, arguments, argumentNames, cnt, app, pos, status);
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if argumentNames is NULL, this means arguments is a numeric array.
|
|
|
|
// arguments can not be NULL.
|
|
|
|
void MessageFormat::format(int32_t msgStart, double pluralNumber,
|
|
|
|
const Formattable* arguments,
|
|
|
|
const UnicodeString *argumentNames,
|
|
|
|
int32_t cnt,
|
|
|
|
AppendableWrapper& appendTo,
|
|
|
|
FieldPosition* ignore,
|
|
|
|
UErrorCode& success) const {
|
|
|
|
if (U_FAILURE(success)) {
|
|
|
|
return;
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
const UnicodeString& msgString = msgPattern.getPatternString();
|
|
|
|
int32_t prevIndex = msgPattern.getPart(msgStart).getLimit();
|
|
|
|
for (int32_t i = msgStart + 1; U_SUCCESS(success) ; ++i) {
|
|
|
|
const MessagePattern::Part* part = &msgPattern.getPart(i);
|
|
|
|
const UMessagePatternPartType type = part->getType();
|
|
|
|
int32_t index = part->getIndex();
|
|
|
|
appendTo.append(msgString, prevIndex, index - prevIndex);
|
|
|
|
if (type == UMSGPAT_PART_TYPE_MSG_LIMIT) {
|
|
|
|
return;
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
prevIndex = part->getLimit();
|
|
|
|
if (type == UMSGPAT_PART_TYPE_REPLACE_NUMBER) {
|
|
|
|
const NumberFormat* nf = getDefaultNumberFormat(success);
|
|
|
|
appendTo.formatAndAppend(nf, Formattable(pluralNumber), success);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (type != UMSGPAT_PART_TYPE_ARG_START) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int32_t argLimit = msgPattern.getLimitPartIndex(i);
|
|
|
|
UMessagePatternArgType argType = part->getArgType();
|
|
|
|
part = &msgPattern.getPart(++i);
|
|
|
|
const Formattable* arg;
|
|
|
|
UnicodeString noArg;
|
|
|
|
if (argumentNames == NULL) {
|
|
|
|
int32_t argNumber = part->getValue(); // ARG_NUMBER
|
|
|
|
if (0 <= argNumber && argNumber < cnt) {
|
|
|
|
arg = arguments + argNumber;
|
|
|
|
} else {
|
|
|
|
arg = NULL;
|
|
|
|
noArg.append(LEFT_CURLY_BRACE);
|
|
|
|
itos(argNumber, noArg);
|
|
|
|
noArg.append(RIGHT_CURLY_BRACE);
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
} else {
|
|
|
|
UnicodeString key;
|
|
|
|
if (part->getType() == UMSGPAT_PART_TYPE_ARG_NAME) {
|
|
|
|
key = msgPattern.getSubstring(*part);
|
|
|
|
} else /* UMSGPAT_PART_TYPE_ARG_NUMBER */ {
|
|
|
|
itos(part->getValue(), key);
|
|
|
|
}
|
|
|
|
arg = getArgFromListByName(arguments, argumentNames, cnt, key);
|
|
|
|
if (arg == NULL) {
|
|
|
|
noArg.append(LEFT_CURLY_BRACE);
|
|
|
|
noArg.append(key);
|
|
|
|
noArg.append(RIGHT_CURLY_BRACE);
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
++i;
|
|
|
|
int32_t prevDestLength = appendTo.length();
|
|
|
|
const Format* formatter = NULL;
|
|
|
|
if (!noArg.isEmpty()) {
|
|
|
|
appendTo.append(noArg);
|
|
|
|
} else if (arg == NULL) {
|
|
|
|
appendTo.append(NULL_STRING, 4);
|
|
|
|
} else if ((formatter = getCachedFormatter(i -2))) {
|
|
|
|
// Handles all ArgType.SIMPLE, and formatters from setFormat() and its siblings.
|
|
|
|
if (dynamic_cast<const ChoiceFormat*>(formatter) ||
|
|
|
|
dynamic_cast<const PluralFormat*>(formatter) ||
|
|
|
|
dynamic_cast<const SelectFormat*>(formatter)) {
|
|
|
|
// We only handle nested formats here if they were provided via
|
|
|
|
// setFormat() or its siblings. Otherwise they are not cached and instead
|
|
|
|
// handled below according to argType.
|
|
|
|
UnicodeString subMsgString;
|
|
|
|
formatter->format(*arg, subMsgString, success);
|
|
|
|
if (subMsgString.indexOf(LEFT_CURLY_BRACE) >= 0 ||
|
|
|
|
(subMsgString.indexOf(SINGLE_QUOTE) >= 0 && !MessageImpl::jdkAposMode(msgPattern))
|
|
|
|
) {
|
|
|
|
MessageFormat subMsgFormat(subMsgString, fLocale, success);
|
|
|
|
subMsgFormat.format(0, 0, arguments, argumentNames, cnt, appendTo, ignore, success);
|
|
|
|
} else {
|
|
|
|
appendTo.append(subMsgString);
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
} else {
|
|
|
|
appendTo.formatAndAppend(formatter, *arg, success);
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
} else if (argType == UMSGPAT_ARG_TYPE_NONE || (cachedFormatters && uhash_iget(cachedFormatters, i - 2))) {
|
|
|
|
// We arrive here if getCachedFormatter returned NULL, but there was actually an element in the hash table.
|
|
|
|
// This can only happen if the hash table contained a DummyFormat, so the if statement above is a check
|
|
|
|
// for the hash table containind DummyFormat.
|
|
|
|
if (arg->isNumeric()) {
|
|
|
|
const NumberFormat* nf = getDefaultNumberFormat(success);
|
|
|
|
appendTo.formatAndAppend(nf, *arg, success);
|
|
|
|
} else if (arg->getType() == Formattable::kDate) {
|
|
|
|
const DateFormat* df = getDefaultDateFormat(success);
|
|
|
|
appendTo.formatAndAppend(df, *arg, success);
|
|
|
|
} else {
|
|
|
|
appendTo.append(arg->getString(success));
|
|
|
|
}
|
|
|
|
} else if (argType == UMSGPAT_ARG_TYPE_CHOICE) {
|
|
|
|
if (!arg->isNumeric()) {
|
|
|
|
success = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
// We must use the Formattable::getDouble() variant with the UErrorCode parameter
|
|
|
|
// because only this one converts non-double numeric types to double.
|
|
|
|
const double number = arg->getDouble(success);
|
|
|
|
int32_t subMsgStart = ChoiceFormat::findSubMessage(msgPattern, i, number);
|
|
|
|
formatComplexSubMessage(subMsgStart, 0, arguments, argumentNames,
|
|
|
|
cnt, appendTo, success);
|
2012-04-07 04:27:27 +00:00
|
|
|
} else if (UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType)) {
|
2011-04-25 20:47:32 +00:00
|
|
|
if (!arg->isNumeric()) {
|
|
|
|
success = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2012-04-07 04:27:27 +00:00
|
|
|
const PluralFormat::PluralSelector &selector =
|
|
|
|
argType == UMSGPAT_ARG_TYPE_PLURAL ? pluralProvider : ordinalProvider;
|
2011-04-25 20:47:32 +00:00
|
|
|
// We must use the Formattable::getDouble() variant with the UErrorCode parameter
|
|
|
|
// because only this one converts non-double numeric types to double.
|
|
|
|
double number = arg->getDouble(success);
|
2012-04-07 04:27:27 +00:00
|
|
|
int32_t subMsgStart = PluralFormat::findSubMessage(msgPattern, i, selector, number,
|
2011-04-25 20:47:32 +00:00
|
|
|
success);
|
2011-09-07 23:18:54 +00:00
|
|
|
double offset = msgPattern.getPluralOffset(i);
|
2011-04-25 20:47:32 +00:00
|
|
|
formatComplexSubMessage(subMsgStart, number-offset, arguments, argumentNames,
|
|
|
|
cnt, appendTo, success);
|
|
|
|
} else if (argType == UMSGPAT_ARG_TYPE_SELECT) {
|
|
|
|
int32_t subMsgStart = SelectFormat::findSubMessage(msgPattern, i, arg->getString(success), success);
|
|
|
|
formatComplexSubMessage(subMsgStart, 0, arguments, argumentNames,
|
|
|
|
cnt, appendTo, success);
|
|
|
|
} else {
|
|
|
|
// This should never happen.
|
|
|
|
success = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
ignore = updateMetaData(appendTo, prevDestLength, ignore, arg);
|
|
|
|
prevIndex = msgPattern.getPart(argLimit).getLimit();
|
|
|
|
i = argLimit;
|
|
|
|
}
|
|
|
|
}
|
2003-11-19 04:42:44 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
|
|
|
|
void MessageFormat::formatComplexSubMessage(int32_t msgStart,
|
|
|
|
double pluralNumber,
|
|
|
|
const Formattable* arguments,
|
|
|
|
const UnicodeString *argumentNames,
|
|
|
|
int32_t cnt,
|
|
|
|
AppendableWrapper& appendTo,
|
|
|
|
UErrorCode& success) const {
|
|
|
|
if (U_FAILURE(success)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MessageImpl::jdkAposMode(msgPattern)) {
|
|
|
|
format(msgStart, pluralNumber, arguments, argumentNames, cnt, appendTo, NULL, success);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// JDK compatibility mode: (see JDK MessageFormat.format() API docs)
|
|
|
|
// - remove SKIP_SYNTAX; that is, remove half of the apostrophes
|
|
|
|
// - if the result string contains an open curly brace '{' then
|
|
|
|
// instantiate a temporary MessageFormat object and format again;
|
|
|
|
// otherwise just append the result string
|
|
|
|
const UnicodeString& msgString = msgPattern.getPatternString();
|
|
|
|
UnicodeString sb;
|
|
|
|
int32_t prevIndex = msgPattern.getPart(msgStart).getLimit();
|
|
|
|
for (int32_t i = msgStart;;) {
|
|
|
|
const MessagePattern::Part& part = msgPattern.getPart(++i);
|
|
|
|
const UMessagePatternPartType type = part.getType();
|
|
|
|
int32_t index = part.getIndex();
|
|
|
|
if (type == UMSGPAT_PART_TYPE_MSG_LIMIT) {
|
|
|
|
sb.append(msgString, prevIndex, index - prevIndex);
|
|
|
|
break;
|
|
|
|
} else if (type == UMSGPAT_PART_TYPE_REPLACE_NUMBER || type == UMSGPAT_PART_TYPE_SKIP_SYNTAX) {
|
|
|
|
sb.append(msgString, prevIndex, index - prevIndex);
|
|
|
|
if (type == UMSGPAT_PART_TYPE_REPLACE_NUMBER) {
|
|
|
|
const NumberFormat* nf = getDefaultNumberFormat(success);
|
|
|
|
sb.append(nf->format(pluralNumber, sb, success));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
prevIndex = part.getLimit();
|
|
|
|
} else if (type == UMSGPAT_PART_TYPE_ARG_START) {
|
|
|
|
sb.append(msgString, prevIndex, index - prevIndex);
|
|
|
|
prevIndex = index;
|
|
|
|
i = msgPattern.getLimitPartIndex(i);
|
|
|
|
index = msgPattern.getPart(i).getLimit();
|
|
|
|
MessageImpl::appendReducedApostrophes(msgString, prevIndex, index, sb);
|
|
|
|
prevIndex = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sb.indexOf(LEFT_CURLY_BRACE) >= 0) {
|
2011-05-05 22:46:07 +00:00
|
|
|
UnicodeString emptyPattern; // gcc 3.3.3 fails with "UnicodeString()" as the first parameter.
|
|
|
|
MessageFormat subMsgFormat(emptyPattern, fLocale, success);
|
2011-04-25 20:47:32 +00:00
|
|
|
subMsgFormat.applyPattern(sb, UMSGPAT_APOS_DOUBLE_REQUIRED, NULL, success);
|
|
|
|
subMsgFormat.format(0, 0, arguments, argumentNames, cnt, appendTo, NULL, success);
|
|
|
|
} else {
|
|
|
|
appendTo.append(sb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UnicodeString MessageFormat::getLiteralStringUntilNextArgument(int32_t from) const {
|
|
|
|
const UnicodeString& msgString=msgPattern.getPatternString();
|
|
|
|
int32_t prevIndex=msgPattern.getPart(from).getLimit();
|
|
|
|
UnicodeString b;
|
|
|
|
for (int32_t i = from + 1; ; ++i) {
|
|
|
|
const MessagePattern::Part& part = msgPattern.getPart(i);
|
|
|
|
const UMessagePatternPartType type=part.getType();
|
|
|
|
int32_t index=part.getIndex();
|
|
|
|
b.append(msgString, prevIndex, index - prevIndex);
|
|
|
|
if(type==UMSGPAT_PART_TYPE_ARG_START || type==UMSGPAT_PART_TYPE_MSG_LIMIT) {
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
// Unexpected Part "part" in parsed message.
|
|
|
|
U_ASSERT(type==UMSGPAT_PART_TYPE_SKIP_SYNTAX || type==UMSGPAT_PART_TYPE_INSERT_CHAR);
|
|
|
|
prevIndex=part.getLimit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FieldPosition* MessageFormat::updateMetaData(AppendableWrapper& /*dest*/, int32_t /*prevLength*/,
|
|
|
|
FieldPosition* /*fp*/, const Formattable* /*argId*/) const {
|
|
|
|
// Unlike in Java, there are no field attributes defined for MessageFormat. Do nothing.
|
|
|
|
return NULL;
|
|
|
|
/*
|
|
|
|
if (fp != NULL && Field.ARGUMENT.equals(fp.getFieldAttribute())) {
|
|
|
|
fp->setBeginIndex(prevLength);
|
|
|
|
fp->setEndIndex(dest.get_length());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return fp;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageFormat::copyObjects(const MessageFormat& that, UErrorCode& ec) {
|
|
|
|
// Deep copy pointer fields.
|
|
|
|
// We need not copy the formatAliases because they are re-filled
|
|
|
|
// in each getFormats() call.
|
|
|
|
// The defaultNumberFormat, defaultDateFormat and pluralProvider.rules
|
|
|
|
// also get created on demand.
|
|
|
|
argTypeCount = that.argTypeCount;
|
|
|
|
if (argTypeCount > 0) {
|
|
|
|
if (!allocateArgTypes(argTypeCount, ec)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uprv_memcpy(argTypes, that.argTypes, argTypeCount * sizeof(argTypes[0]));
|
|
|
|
}
|
|
|
|
if (cachedFormatters != NULL) {
|
|
|
|
uhash_removeAll(cachedFormatters);
|
|
|
|
}
|
|
|
|
if (customFormatArgStarts != NULL) {
|
|
|
|
uhash_removeAll(customFormatArgStarts);
|
|
|
|
}
|
|
|
|
if (that.cachedFormatters) {
|
|
|
|
if (cachedFormatters == NULL) {
|
|
|
|
cachedFormatters=uhash_open(uhash_hashLong, uhash_compareLong,
|
|
|
|
equalFormatsForHash, &ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return;
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
2011-06-03 05:23:57 +00:00
|
|
|
uhash_setValueDeleter(cachedFormatters, uprv_deleteUObject);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
|
|
|
|
const int32_t count = uhash_count(that.cachedFormatters);
|
|
|
|
int32_t pos, idx;
|
|
|
|
for (idx = 0, pos = -1; idx < count && U_SUCCESS(ec); ++idx) {
|
|
|
|
const UHashElement* cur = uhash_nextElement(that.cachedFormatters, &pos);
|
|
|
|
Format* newFormat = ((Format*)(cur->value.pointer))->clone();
|
|
|
|
if (newFormat) {
|
|
|
|
uhash_iput(cachedFormatters, cur->key.integer, newFormat, &ec);
|
|
|
|
} else {
|
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
}
|
|
|
|
if (that.customFormatArgStarts) {
|
|
|
|
if (customFormatArgStarts == NULL) {
|
|
|
|
customFormatArgStarts=uhash_open(uhash_hashLong, uhash_compareLong,
|
|
|
|
NULL, &ec);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
const int32_t count = uhash_count(that.customFormatArgStarts);
|
|
|
|
int32_t pos, idx;
|
|
|
|
for (idx = 0, pos = -1; idx < count && U_SUCCESS(ec); ++idx) {
|
|
|
|
const UHashElement* cur = uhash_nextElement(that.customFormatArgStarts, &pos);
|
|
|
|
uhash_iputi(customFormatArgStarts, cur->key.integer, cur->value.integer, &ec);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-12-02 18:34:10 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
Formattable*
|
2011-04-25 20:47:32 +00:00
|
|
|
MessageFormat::parse(int32_t msgStart,
|
|
|
|
const UnicodeString& source,
|
2003-01-27 17:27:40 +00:00
|
|
|
ParsePosition& pos,
|
2011-04-25 20:47:32 +00:00
|
|
|
int32_t& count,
|
|
|
|
UErrorCode& ec) const {
|
|
|
|
count = 0;
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
pos.setErrorIndex(pos.getIndex());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// parse() does not work with named arguments.
|
|
|
|
if (msgPattern.hasNamedArguments()) {
|
|
|
|
ec = U_ARGUMENT_TYPE_MISMATCH;
|
|
|
|
pos.setErrorIndex(pos.getIndex());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
LocalArray<Formattable> resultArray(new Formattable[argTypeCount ? argTypeCount : 1]);
|
|
|
|
const UnicodeString& msgString=msgPattern.getPatternString();
|
|
|
|
int32_t prevIndex=msgPattern.getPart(msgStart).getLimit();
|
2003-01-27 17:27:40 +00:00
|
|
|
int32_t sourceOffset = pos.getIndex();
|
2011-04-25 20:47:32 +00:00
|
|
|
ParsePosition tempStatus(0);
|
|
|
|
|
|
|
|
for(int32_t i=msgStart+1; ; ++i) {
|
|
|
|
UBool haveArgResult = FALSE;
|
|
|
|
const MessagePattern::Part* part=&msgPattern.getPart(i);
|
|
|
|
const UMessagePatternPartType type=part->getType();
|
|
|
|
int32_t index=part->getIndex();
|
|
|
|
// Make sure the literal string matches.
|
|
|
|
int32_t len = index - prevIndex;
|
|
|
|
if (len == 0 || (0 == msgString.compare(prevIndex, len, source, sourceOffset, len))) {
|
2001-06-25 21:15:56 +00:00
|
|
|
sourceOffset += len;
|
2011-04-25 20:47:32 +00:00
|
|
|
prevIndex += len;
|
|
|
|
} else {
|
|
|
|
pos.setErrorIndex(sourceOffset);
|
|
|
|
return NULL; // leave index as is to signal error
|
2010-10-20 02:36:31 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
if(type==UMSGPAT_PART_TYPE_MSG_LIMIT) {
|
|
|
|
// Things went well! Done.
|
|
|
|
pos.setIndex(sourceOffset);
|
|
|
|
return resultArray.orphan();
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
if(type==UMSGPAT_PART_TYPE_SKIP_SYNTAX || type==UMSGPAT_PART_TYPE_INSERT_CHAR) {
|
|
|
|
prevIndex=part->getLimit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// We do not support parsing Plural formats. (No REPLACE_NUMBER here.)
|
|
|
|
// Unexpected Part "part" in parsed message.
|
|
|
|
U_ASSERT(type==UMSGPAT_PART_TYPE_ARG_START);
|
|
|
|
int32_t argLimit=msgPattern.getLimitPartIndex(i);
|
|
|
|
|
|
|
|
UMessagePatternArgType argType=part->getArgType();
|
|
|
|
part=&msgPattern.getPart(++i);
|
|
|
|
int32_t argNumber = part->getValue(); // ARG_NUMBER
|
|
|
|
UnicodeString key;
|
|
|
|
++i;
|
|
|
|
const Format* formatter = NULL;
|
|
|
|
Formattable& argResult = resultArray[argNumber];
|
|
|
|
|
|
|
|
if(cachedFormatters!=NULL && (formatter = getCachedFormatter(i - 2))!=NULL) {
|
|
|
|
// Just parse using the formatter.
|
|
|
|
tempStatus.setIndex(sourceOffset);
|
|
|
|
formatter->parseObject(source, argResult, tempStatus);
|
|
|
|
if (tempStatus.getIndex() == sourceOffset) {
|
|
|
|
pos.setErrorIndex(sourceOffset);
|
|
|
|
return NULL; // leave index as is to signal error
|
|
|
|
}
|
|
|
|
sourceOffset = tempStatus.getIndex();
|
|
|
|
haveArgResult = TRUE;
|
|
|
|
} else if(
|
|
|
|
argType==UMSGPAT_ARG_TYPE_NONE || (cachedFormatters && uhash_iget(cachedFormatters, i -2))) {
|
|
|
|
// We arrive here if getCachedFormatter returned NULL, but there was actually an element in the hash table.
|
|
|
|
// This can only happen if the hash table contained a DummyFormat, so the if statement above is a check
|
|
|
|
// for the hash table containind DummyFormat.
|
|
|
|
|
|
|
|
// Match as a string.
|
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
|
2011-04-25 20:47:32 +00:00
|
|
|
UnicodeString stringAfterArgument = getLiteralStringUntilNextArgument(argLimit);
|
2001-06-25 21:15:56 +00:00
|
|
|
int32_t next;
|
2011-04-25 20:47:32 +00:00
|
|
|
if (!stringAfterArgument.isEmpty()) {
|
|
|
|
next = source.indexOf(stringAfterArgument, sourceOffset);
|
|
|
|
} else {
|
2001-06-25 21:15:56 +00:00
|
|
|
next = source.length();
|
|
|
|
}
|
|
|
|
if (next < 0) {
|
2011-04-25 20:47:32 +00:00
|
|
|
pos.setErrorIndex(sourceOffset);
|
|
|
|
return NULL; // leave index as is to signal error
|
|
|
|
} else {
|
|
|
|
UnicodeString strValue(source.tempSubString(sourceOffset, next - sourceOffset));
|
|
|
|
UnicodeString compValue;
|
|
|
|
compValue.append(LEFT_CURLY_BRACE);
|
|
|
|
itos(argNumber, compValue);
|
|
|
|
compValue.append(RIGHT_CURLY_BRACE);
|
|
|
|
if (0 != strValue.compare(compValue)) {
|
|
|
|
argResult.setString(strValue);
|
|
|
|
haveArgResult = TRUE;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
|
|
|
sourceOffset = next;
|
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
} else if(argType==UMSGPAT_ARG_TYPE_CHOICE) {
|
|
|
|
tempStatus.setIndex(sourceOffset);
|
|
|
|
double choiceResult = ChoiceFormat::parseArgument(msgPattern, i, source, tempStatus);
|
|
|
|
if (tempStatus.getIndex() == sourceOffset) {
|
|
|
|
pos.setErrorIndex(sourceOffset);
|
|
|
|
return NULL; // leave index as is to signal error
|
2003-01-27 17:27:40 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
argResult.setDouble(choiceResult);
|
|
|
|
haveArgResult = TRUE;
|
|
|
|
sourceOffset = tempStatus.getIndex();
|
2012-04-07 04:27:27 +00:00
|
|
|
} else if(UMSGPAT_ARG_TYPE_HAS_PLURAL_STYLE(argType) || argType==UMSGPAT_ARG_TYPE_SELECT) {
|
2011-04-25 20:47:32 +00:00
|
|
|
// Parsing not supported.
|
|
|
|
ec = U_UNSUPPORTED_ERROR;
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
// This should never happen.
|
|
|
|
ec = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
return NULL;
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
if (haveArgResult && count <= argNumber) {
|
|
|
|
count = argNumber + 1;
|
|
|
|
}
|
|
|
|
prevIndex=msgPattern.getPart(argLimit).getLimit();
|
|
|
|
i=argLimit;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +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.
|
2003-01-27 17:27:40 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
Formattable*
|
|
|
|
MessageFormat::parse(const UnicodeString& source,
|
|
|
|
ParsePosition& pos,
|
|
|
|
int32_t& count) const {
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
return parse(0, source, pos, count, ec);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
2010-10-20 02:36:31 +00:00
|
|
|
// Parses the source string and returns the array of
|
|
|
|
// Formattable objects and the array count. The caller
|
1999-08-16 21:50:52 +00:00
|
|
|
// owns the returned array.
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
Formattable*
|
2010-10-20 02:36:31 +00:00
|
|
|
MessageFormat::parse(const UnicodeString& source,
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t& cnt,
|
|
|
|
UErrorCode& success) const
|
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
if (msgPattern.hasNamedArguments()) {
|
2007-12-17 01:39:55 +00:00
|
|
|
success = U_ARGUMENT_TYPE_MISMATCH;
|
2010-10-20 02:36:31 +00:00
|
|
|
return NULL;
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Parses the source text and copy into the result buffer.
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
MessageFormat::parseObject( const UnicodeString& source,
|
|
|
|
Formattable& result,
|
|
|
|
ParsePosition& status) const
|
|
|
|
{
|
|
|
|
int32_t cnt = 0;
|
|
|
|
Formattable* tmpResult = parse(source, status, cnt);
|
2010-10-20 02:36:31 +00:00
|
|
|
if (tmpResult != NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
result.adoptArray(tmpResult, cnt);
|
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
|
|
|
UnicodeString
|
2005-05-27 22:07:16 +00:00
|
|
|
MessageFormat::autoQuoteApostrophe(const UnicodeString& pattern, UErrorCode& status) {
|
2011-04-25 20:47:32 +00:00
|
|
|
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);
|
|
|
|
result.releaseBuffer(U_SUCCESS(status) ? len : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
result.setToBogus();
|
2005-05-27 22:07:16 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
return result;
|
2005-05-27 22:07:16 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
void MessageFormat::cacheExplicitFormats(UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) {
|
2003-01-27 17:27:40 +00:00
|
|
|
return;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
if (cachedFormatters != NULL) {
|
|
|
|
uhash_removeAll(cachedFormatters);
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
if (customFormatArgStarts != NULL) {
|
|
|
|
uhash_removeAll(customFormatArgStarts);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The last two "parts" can at most be ARG_LIMIT and MSG_LIMIT
|
|
|
|
// which we need not examine.
|
|
|
|
int32_t limit = msgPattern.countParts() - 2;
|
|
|
|
argTypeCount = 0;
|
|
|
|
// We also need not look at the first two "parts"
|
|
|
|
// (at most MSG_START and ARG_START) in this loop.
|
|
|
|
// We determine the argTypeCount first so that we can allocateArgTypes
|
|
|
|
// so that the next loop can set argTypes[argNumber].
|
|
|
|
// (This is for the C API which needs the argTypes to read its va_arg list.)
|
|
|
|
for (int32_t i = 2; i < limit && U_SUCCESS(status); ++i) {
|
|
|
|
const MessagePattern::Part& part = msgPattern.getPart(i);
|
|
|
|
if (part.getType() == UMSGPAT_PART_TYPE_ARG_NUMBER) {
|
|
|
|
const int argNumber = part.getValue();
|
|
|
|
if (argNumber >= argTypeCount) {
|
|
|
|
argTypeCount = argNumber + 1;
|
|
|
|
}
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
2001-06-25 21:15:56 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
if (!allocateArgTypes(argTypeCount, status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Set all argTypes to kObject, as a "none" value, for lack of any better value.
|
|
|
|
// We never use kObject for real arguments.
|
|
|
|
for (int32_t i = 0; i < argTypeCount; ++i) {
|
|
|
|
argTypes[i] = Formattable::kObject;
|
|
|
|
}
|
|
|
|
hasArgTypeConflicts = FALSE;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
// This loop starts at part index 1 because we do need to examine
|
|
|
|
// ARG_START parts. (But we can ignore the MSG_START.)
|
|
|
|
for (int32_t i = 1; i < limit && U_SUCCESS(status); ++i) {
|
|
|
|
const MessagePattern::Part* part = &msgPattern.getPart(i);
|
|
|
|
if (part->getType() != UMSGPAT_PART_TYPE_ARG_START) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
UMessagePatternArgType argType = part->getArgType();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
int32_t argNumber = -1;
|
|
|
|
part = &msgPattern.getPart(i + 1);
|
|
|
|
if (part->getType() == UMSGPAT_PART_TYPE_ARG_NUMBER) {
|
|
|
|
argNumber = part->getValue();
|
|
|
|
}
|
|
|
|
Formattable::Type formattableType;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
switch (argType) {
|
|
|
|
case UMSGPAT_ARG_TYPE_NONE:
|
|
|
|
formattableType = Formattable::kString;
|
|
|
|
break;
|
|
|
|
case UMSGPAT_ARG_TYPE_SIMPLE: {
|
|
|
|
int32_t index = i;
|
|
|
|
i += 2;
|
|
|
|
UnicodeString explicitType = msgPattern.getSubstring(msgPattern.getPart(i++));
|
|
|
|
UnicodeString style;
|
|
|
|
if ((part = &msgPattern.getPart(i))->getType() == UMSGPAT_PART_TYPE_ARG_STYLE) {
|
|
|
|
style = msgPattern.getSubstring(*part);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
UParseError parseError;
|
|
|
|
Format* formatter = createAppropriateFormat(explicitType, style, formattableType, parseError, status);
|
|
|
|
setArgStartFormat(index, formatter, status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UMSGPAT_ARG_TYPE_CHOICE:
|
|
|
|
case UMSGPAT_ARG_TYPE_PLURAL:
|
2012-04-13 17:37:12 +00:00
|
|
|
case UMSGPAT_ARG_TYPE_SELECTORDINAL:
|
2011-04-25 20:47:32 +00:00
|
|
|
formattableType = Formattable::kDouble;
|
|
|
|
break;
|
|
|
|
case UMSGPAT_ARG_TYPE_SELECT:
|
|
|
|
formattableType = Formattable::kString;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
status = U_INTERNAL_PROGRAM_ERROR; // Should be unreachable.
|
|
|
|
formattableType = Formattable::kString;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (argNumber != -1) {
|
|
|
|
if (argTypes[argNumber] != Formattable::kObject && argTypes[argNumber] != formattableType) {
|
|
|
|
hasArgTypeConflicts = TRUE;
|
|
|
|
}
|
|
|
|
argTypes[argNumber] = formattableType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2003-01-27 17:27:40 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
Format* MessageFormat::createAppropriateFormat(UnicodeString& type, UnicodeString& style,
|
|
|
|
Formattable::Type& formattableType, UParseError& parseError,
|
|
|
|
UErrorCode& ec) {
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Format* fmt = NULL;
|
|
|
|
int32_t typeID, styleID;
|
|
|
|
DateFormat::EStyle date_style;
|
|
|
|
|
|
|
|
switch (typeID = findKeyword(type, TYPE_IDS)) {
|
|
|
|
case 0: // number
|
|
|
|
formattableType = Formattable::kDouble;
|
|
|
|
switch (findKeyword(style, NUMBER_STYLE_IDS)) {
|
2003-01-27 17:27:40 +00:00
|
|
|
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
|
2011-04-25 20:47:32 +00:00
|
|
|
formattableType = 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);
|
2010-05-19 17:29:33 +00:00
|
|
|
if (fmt) {
|
|
|
|
DecimalFormat* decfmt = dynamic_cast<DecimalFormat*>(fmt);
|
|
|
|
if (decfmt != NULL) {
|
2011-04-25 20:47:32 +00:00
|
|
|
decfmt->applyPattern(style,parseError,ec);
|
2010-05-19 17:29:33 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
case 1: // date
|
|
|
|
case 2: // time
|
|
|
|
formattableType = Formattable::kDate;
|
|
|
|
styleID = findKeyword(style, DATE_STYLE_IDS);
|
|
|
|
date_style = (styleID >= 0) ? DATE_STYLES[styleID] : DateFormat::kDefault;
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
if (typeID == 1) {
|
|
|
|
fmt = DateFormat::createDateInstance(date_style, fLocale);
|
2003-01-27 17:27:40 +00:00
|
|
|
} else {
|
2011-04-25 20:47:32 +00:00
|
|
|
fmt = DateFormat::createTimeInstance(date_style, fLocale);
|
2002-01-30 06:18:26 +00:00
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
|
2010-05-19 17:29:33 +00:00
|
|
|
if (styleID < 0 && fmt != NULL) {
|
|
|
|
SimpleDateFormat* sdtfmt = dynamic_cast<SimpleDateFormat*>(fmt);
|
|
|
|
if (sdtfmt != NULL) {
|
2011-04-25 20:47:32 +00:00
|
|
|
sdtfmt->applyPattern(style);
|
2010-05-19 17:29:33 +00:00
|
|
|
}
|
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
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
case 3: // spellout
|
|
|
|
formattableType = Formattable::kDouble;
|
|
|
|
fmt = makeRBNF(URBNF_SPELLOUT, fLocale, style, ec);
|
2004-08-04 05:57:45 +00:00
|
|
|
break;
|
2011-04-25 20:47:32 +00:00
|
|
|
case 4: // ordinal
|
|
|
|
formattableType = Formattable::kDouble;
|
|
|
|
fmt = makeRBNF(URBNF_ORDINAL, fLocale, style, ec);
|
2004-08-04 05:57:45 +00:00
|
|
|
break;
|
2011-04-25 20:47:32 +00:00
|
|
|
case 5: // duration
|
|
|
|
formattableType = Formattable::kDouble;
|
|
|
|
fmt = makeRBNF(URBNF_DURATION, fLocale, style, ec);
|
2007-12-17 01:39:55 +00:00
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
default:
|
2011-04-25 20:47:32 +00:00
|
|
|
formattableType = Formattable::kString;
|
2003-01-27 17:27:40 +00:00
|
|
|
ec = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
return fmt;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
|
|
|
|
//-------------------------------------
|
2010-10-20 02:36:31 +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
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
if (s.isEmpty()) {
|
2004-08-04 05:57:45 +00:00
|
|
|
return 0; // default
|
2011-04-25 20:47:32 +00:00
|
|
|
}
|
2001-06-25 21:15:56 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
int32_t length = s.length();
|
|
|
|
const UChar *ps = PatternProps::trimWhiteSpace(s.getBuffer(), length);
|
|
|
|
UnicodeString buffer(FALSE, ps, length);
|
2001-06-25 21:15:56 +00:00
|
|
|
// Trims the space characters and turns all characters
|
|
|
|
// in s to lower case.
|
2011-04-25 20:47:32 +00:00
|
|
|
buffer.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
|
|
|
}
|
2010-10-20 02:36:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* Convenience method that ought to be in NumberFormat
|
|
|
|
*/
|
2010-10-20 02:36:31 +00:00
|
|
|
NumberFormat*
|
1999-08-16 21:50:52 +00:00
|
|
|
MessageFormat::createIntegerFormat(const Locale& locale, UErrorCode& status) const {
|
2001-06-25 21:15:56 +00:00
|
|
|
NumberFormat *temp = NumberFormat::createInstance(locale, status);
|
2010-05-19 17:29:33 +00:00
|
|
|
DecimalFormat *temp2;
|
|
|
|
if (temp != NULL && (temp2 = dynamic_cast<DecimalFormat*>(temp)) != NULL) {
|
2001-06-25 21:15:56 +00:00
|
|
|
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);
|
2010-10-20 02:36:31 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
2003-02-05 00:53:44 +00:00
|
|
|
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 {
|
2011-04-25 20:47:32 +00:00
|
|
|
return msgPattern.hasNamedArguments();
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
int32_t
|
|
|
|
MessageFormat::getArgTypeCount() const {
|
|
|
|
return argTypeCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool MessageFormat::equalFormats(const void* left, const void* right) {
|
|
|
|
return *(const Format*)left==*(const Format*)right;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UBool MessageFormat::DummyFormat::operator==(const Format&) const {
|
2007-12-17 01:39:55 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
Format* MessageFormat::DummyFormat::clone() const {
|
|
|
|
return new DummyFormat();
|
2010-10-20 02:36:31 +00:00
|
|
|
}
|
2008-09-19 19:05:37 +00:00
|
|
|
|
2012-08-03 19:09:42 +00:00
|
|
|
UnicodeString& MessageFormat::DummyFormat::format(const Formattable&,
|
2012-05-03 05:50:26 +00:00
|
|
|
UnicodeString& appendTo,
|
|
|
|
UErrorCode& status) const {
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
}
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
UnicodeString& MessageFormat::DummyFormat::format(const Formattable&,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition&,
|
|
|
|
UErrorCode& status) const {
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
}
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
2012-05-03 05:50:26 +00:00
|
|
|
UnicodeString& MessageFormat::DummyFormat::format(const Formattable&,
|
|
|
|
UnicodeString& appendTo,
|
2012-08-03 19:09:42 +00:00
|
|
|
FieldPositionIterator*,
|
2012-05-03 05:50:26 +00:00
|
|
|
UErrorCode& status) const {
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
}
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
void MessageFormat::DummyFormat::parseObject(const UnicodeString&,
|
|
|
|
Formattable&,
|
|
|
|
ParsePosition& ) const {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 {
|
2011-04-25 20:47:32 +00:00
|
|
|
return (fFormatNames==NULL) ? 0 : fFormatNames->size();
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FormatNameEnumeration::~FormatNameEnumeration() {
|
2011-04-25 20:47:32 +00:00
|
|
|
delete fFormatNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-07 04:27:27 +00:00
|
|
|
MessageFormat::PluralSelectorProvider::PluralSelectorProvider(const Locale* loc, UPluralType t)
|
|
|
|
: locale(loc), rules(NULL), type(t) {
|
2011-04-25 20:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageFormat::PluralSelectorProvider::~PluralSelectorProvider() {
|
|
|
|
// We own the rules but not the locale.
|
|
|
|
delete rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString MessageFormat::PluralSelectorProvider::select(double number, UErrorCode& ec) const {
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return UnicodeString(FALSE, OTHER_STRING, 5);
|
|
|
|
}
|
|
|
|
MessageFormat::PluralSelectorProvider* t = const_cast<MessageFormat::PluralSelectorProvider*>(this);
|
|
|
|
if(rules == NULL) {
|
2012-04-07 04:27:27 +00:00
|
|
|
t->rules = PluralRules::forLocale(*locale, type, ec);
|
2011-04-25 20:47:32 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return UnicodeString(FALSE, OTHER_STRING, 5);
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
return rules->select(number);
|
2007-12-17 01:39:55 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
|
|
|
|
void MessageFormat::PluralSelectorProvider::reset(const Locale* loc) {
|
|
|
|
locale = loc;
|
|
|
|
delete rules;
|
|
|
|
rules = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|