1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-11-22 20:25:35 +00:00
|
|
|
*******************************************************************************
|
2013-03-21 22:09:06 +00:00
|
|
|
* Copyright (C) 1997-2013, International Business Machines Corporation and *
|
1999-11-22 20:25:35 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File CHOICFMT.CPP
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 02/19/97 aliu Converted from java.
|
|
|
|
* 03/20/97 helena Finished first cut of implementation and got rid
|
|
|
|
* of nextDouble/previousDouble and replaced with
|
|
|
|
* boolean array.
|
|
|
|
* 4/10/97 aliu Clean up. Modified to work on AIX.
|
|
|
|
* 06/04/97 helena Fixed applyPattern(), toPattern() and not to include
|
|
|
|
* wchar.h.
|
|
|
|
* 07/09/97 helena Made ParsePosition into a class.
|
|
|
|
* 08/06/97 nos removed overloaded constructor, fixed 'format(array)'
|
2001-03-06 19:44:45 +00:00
|
|
|
* 07/22/98 stephen JDK 1.2 Sync - removed UBool array (doubleFlags)
|
1999-08-16 21:50:52 +00:00
|
|
|
* 02/22/99 stephen Removed character literals for EBCDIC safety
|
|
|
|
********************************************************************************
|
|
|
|
*/
|
|
|
|
|
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/choicfmt.h"
|
|
|
|
#include "unicode/numfmt.h"
|
|
|
|
#include "unicode/locid.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "cpputils.h"
|
2004-10-08 17:38:36 +00:00
|
|
|
#include "cstring.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "messageimpl.h"
|
2004-10-18 02:43:33 +00:00
|
|
|
#include "putilimp.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "uassert.h"
|
2006-04-08 09:48:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <float.h>
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// *****************************************************************************
|
|
|
|
// class ChoiceFormat
|
|
|
|
// *****************************************************************************
|
2001-03-06 19:44:45 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ChoiceFormat)
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-06-11 17:21:28 +00:00
|
|
|
// Special characters used by ChoiceFormat. There are two characters
|
|
|
|
// used interchangeably to indicate <=. Either is parsed, but only
|
|
|
|
// LESS_EQUAL is generated by toPattern().
|
|
|
|
#define SINGLE_QUOTE ((UChar)0x0027) /*'*/
|
|
|
|
#define LESS_THAN ((UChar)0x003C) /*<*/
|
|
|
|
#define LESS_EQUAL ((UChar)0x0023) /*#*/
|
2001-06-21 20:46:12 +00:00
|
|
|
#define LESS_EQUAL2 ((UChar)0x2264)
|
2001-06-11 17:21:28 +00:00
|
|
|
#define VERTICAL_BAR ((UChar)0x007C) /*|*/
|
|
|
|
#define MINUS ((UChar)0x002D) /*-*/
|
2009-03-13 03:11:47 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
static const UChar LEFT_CURLY_BRACE = 0x7B; /*{*/
|
|
|
|
static const UChar RIGHT_CURLY_BRACE = 0x7D; /*}*/
|
|
|
|
|
2009-03-13 03:11:47 +00:00
|
|
|
#ifdef INFINITY
|
|
|
|
#undef INFINITY
|
|
|
|
#endif
|
2001-06-21 20:46:12 +00:00
|
|
|
#define INFINITY ((UChar)0x221E)
|
2001-06-11 17:21:28 +00:00
|
|
|
|
2012-05-03 05:50:26 +00:00
|
|
|
//static const UChar gPositiveInfinity[] = {INFINITY, 0};
|
|
|
|
//static const UChar gNegativeInfinity[] = {MINUS, INFINITY, 0};
|
2001-06-21 20:58:03 +00:00
|
|
|
#define POSITIVE_INF_STRLEN 1
|
|
|
|
#define NEGATIVE_INF_STRLEN 2
|
2001-06-11 17:21:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Creates a ChoiceFormat instance based on the pattern.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
ChoiceFormat::ChoiceFormat(const UnicodeString& newPattern,
|
|
|
|
UErrorCode& status)
|
2011-04-25 20:47:32 +00:00
|
|
|
: constructorErrorCode(status),
|
|
|
|
msgPattern(status)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
applyPattern(newPattern, status);
|
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Creates a ChoiceFormat instance with the limit array and
|
|
|
|
// format strings for each limit.
|
|
|
|
|
|
|
|
ChoiceFormat::ChoiceFormat(const double* limits,
|
|
|
|
const UnicodeString* formats,
|
|
|
|
int32_t cnt )
|
2011-04-25 20:47:32 +00:00
|
|
|
: constructorErrorCode(U_ZERO_ERROR),
|
|
|
|
msgPattern(constructorErrorCode)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
setChoices(limits, NULL, formats, cnt, constructorErrorCode);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-06-11 17:21:28 +00:00
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
ChoiceFormat::ChoiceFormat(const double* limits,
|
|
|
|
const UBool* closures,
|
|
|
|
const UnicodeString* formats,
|
|
|
|
int32_t cnt )
|
2011-04-25 20:47:32 +00:00
|
|
|
: constructorErrorCode(U_ZERO_ERROR),
|
|
|
|
msgPattern(constructorErrorCode)
|
2001-06-11 17:21:28 +00:00
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
setChoices(limits, closures, formats, cnt, constructorErrorCode);
|
2001-06-11 17:21:28 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// copy constructor
|
|
|
|
|
|
|
|
ChoiceFormat::ChoiceFormat(const ChoiceFormat& that)
|
2001-10-09 21:30:02 +00:00
|
|
|
: NumberFormat(that),
|
2011-04-25 20:47:32 +00:00
|
|
|
constructorErrorCode(that.constructorErrorCode),
|
|
|
|
msgPattern(that.msgPattern)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Private constructor that creates a
|
|
|
|
// ChoiceFormat instance based on the
|
|
|
|
// pattern and populates UParseError
|
|
|
|
|
|
|
|
ChoiceFormat::ChoiceFormat(const UnicodeString& newPattern,
|
|
|
|
UParseError& parseError,
|
|
|
|
UErrorCode& status)
|
2011-04-25 20:47:32 +00:00
|
|
|
: constructorErrorCode(status),
|
|
|
|
msgPattern(status)
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
|
|
|
applyPattern(newPattern,parseError, status);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
ChoiceFormat::operator==(const Format& that) const
|
|
|
|
{
|
|
|
|
if (this == &that) return TRUE;
|
|
|
|
if (!NumberFormat::operator==(that)) return FALSE;
|
|
|
|
ChoiceFormat& thatAlias = (ChoiceFormat&)that;
|
2011-04-25 20:47:32 +00:00
|
|
|
return msgPattern == thatAlias.msgPattern;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// copy constructor
|
|
|
|
|
|
|
|
const ChoiceFormat&
|
|
|
|
ChoiceFormat::operator=(const ChoiceFormat& that)
|
|
|
|
{
|
|
|
|
if (this != &that) {
|
|
|
|
NumberFormat::operator=(that);
|
2011-04-25 20:47:32 +00:00
|
|
|
constructorErrorCode = that.constructorErrorCode;
|
|
|
|
msgPattern = that.msgPattern;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
ChoiceFormat::~ChoiceFormat()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
/**
|
2011-04-25 20:47:32 +00:00
|
|
|
* Convert a double value to a string without the overhead of NumberFormat.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
UnicodeString&
|
|
|
|
ChoiceFormat::dtos(double value,
|
2001-08-17 21:50:48 +00:00
|
|
|
UnicodeString& string)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2006-04-08 09:48:13 +00:00
|
|
|
/* Buffer to contain the digits and any extra formatting stuff. */
|
|
|
|
char temp[DBL_DIG + 16];
|
|
|
|
char *itrPtr = temp;
|
2008-01-21 22:52:48 +00:00
|
|
|
char *expPtr;
|
2006-04-08 09:48:13 +00:00
|
|
|
|
2008-01-21 17:27:53 +00:00
|
|
|
sprintf(temp, "%.*g", DBL_DIG, value);
|
2006-04-08 09:48:13 +00:00
|
|
|
|
|
|
|
/* Find and convert the decimal point.
|
|
|
|
Using setlocale on some machines will cause sprintf to use a comma for certain locales.
|
|
|
|
*/
|
|
|
|
while (*itrPtr && (*itrPtr == '-' || isdigit(*itrPtr))) {
|
|
|
|
itrPtr++;
|
|
|
|
}
|
2008-01-21 17:27:53 +00:00
|
|
|
if (*itrPtr != 0 && *itrPtr != 'e') {
|
2008-01-21 22:52:48 +00:00
|
|
|
/* We reached something that looks like a decimal point.
|
|
|
|
In case someone used setlocale(), which changes the decimal point. */
|
2006-04-08 09:48:13 +00:00
|
|
|
*itrPtr = '.';
|
2008-01-21 22:52:48 +00:00
|
|
|
itrPtr++;
|
|
|
|
}
|
|
|
|
/* Search for the exponent */
|
|
|
|
while (*itrPtr && *itrPtr != 'e') {
|
|
|
|
itrPtr++;
|
|
|
|
}
|
2008-01-22 00:11:43 +00:00
|
|
|
if (*itrPtr == 'e') {
|
2008-01-21 22:52:48 +00:00
|
|
|
itrPtr++;
|
2008-01-22 00:11:43 +00:00
|
|
|
/* Verify the exponent sign */
|
|
|
|
if (*itrPtr == '+' || *itrPtr == '-') {
|
|
|
|
itrPtr++;
|
|
|
|
}
|
|
|
|
/* Remove leading zeros. You will see this on Windows machines. */
|
|
|
|
expPtr = itrPtr;
|
|
|
|
while (*itrPtr == '0') {
|
|
|
|
itrPtr++;
|
|
|
|
}
|
|
|
|
if (*itrPtr && expPtr != itrPtr) {
|
|
|
|
/* Shift the exponent without zeros. */
|
|
|
|
while (*itrPtr) {
|
|
|
|
*(expPtr++) = *(itrPtr++);
|
|
|
|
}
|
|
|
|
// NULL terminate
|
|
|
|
*expPtr = 0;
|
2006-04-08 09:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-22 00:11:43 +00:00
|
|
|
|
2004-09-15 23:44:48 +00:00
|
|
|
string = UnicodeString(temp, -1, US_INV); /* invariant codepage */
|
1999-08-16 21:50:52 +00:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// calls the overloaded applyPattern method.
|
|
|
|
|
|
|
|
void
|
|
|
|
ChoiceFormat::applyPattern(const UnicodeString& pattern,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
msgPattern.parseChoiceStyle(pattern, NULL, status);
|
|
|
|
constructorErrorCode = status;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Applies the pattern to this ChoiceFormat instance.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
void
|
|
|
|
ChoiceFormat::applyPattern(const UnicodeString& pattern,
|
|
|
|
UParseError& parseError,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
msgPattern.parseChoiceStyle(pattern, &parseError, status);
|
|
|
|
constructorErrorCode = status;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
2011-04-25 20:47:32 +00:00
|
|
|
// Returns the input pattern string.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
ChoiceFormat::toPattern(UnicodeString& result) const
|
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
return result = msgPattern.getPatternString();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Sets the limit and format arrays.
|
|
|
|
void
|
|
|
|
ChoiceFormat::setChoices( const double* limits,
|
|
|
|
const UnicodeString* formats,
|
|
|
|
int32_t cnt )
|
2001-06-11 17:21:28 +00:00
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
UErrorCode errorCode = U_ZERO_ERROR;
|
|
|
|
setChoices(limits, NULL, formats, cnt, errorCode);
|
2001-06-11 17:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Sets the limit and format arrays.
|
|
|
|
void
|
|
|
|
ChoiceFormat::setChoices( const double* limits,
|
|
|
|
const UBool* closures,
|
|
|
|
const UnicodeString* formats,
|
|
|
|
int32_t cnt )
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
UErrorCode errorCode = U_ZERO_ERROR;
|
|
|
|
setChoices(limits, closures, formats, cnt, errorCode);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
void
|
|
|
|
ChoiceFormat::setChoices(const double* limits,
|
|
|
|
const UBool* closures,
|
|
|
|
const UnicodeString* formats,
|
|
|
|
int32_t count,
|
|
|
|
UErrorCode &errorCode) {
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
return;
|
2008-01-09 20:36:55 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
if (limits == NULL || formats == NULL) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
2008-01-09 20:36:55 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
// Reconstruct the original input pattern.
|
|
|
|
// Modified version of the pre-ICU 4.8 toPattern() implementation.
|
|
|
|
UnicodeString result;
|
|
|
|
for (int32_t i = 0; i < count; ++i) {
|
|
|
|
if (i != 0) {
|
|
|
|
result += VERTICAL_BAR;
|
2008-01-21 22:52:48 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
UnicodeString buf;
|
|
|
|
if (uprv_isPositiveInfinity(limits[i])) {
|
|
|
|
result += INFINITY;
|
|
|
|
} else if (uprv_isNegativeInfinity(limits[i])) {
|
|
|
|
result += MINUS;
|
|
|
|
result += INFINITY;
|
|
|
|
} else {
|
|
|
|
result += dtos(limits[i], buf);
|
2008-01-21 22:52:48 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
if (closures != NULL && closures[i]) {
|
|
|
|
result += LESS_THAN;
|
|
|
|
} else {
|
|
|
|
result += LESS_EQUAL;
|
2008-01-21 22:52:48 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
// Append formats[i], using quotes if there are special
|
|
|
|
// characters. Single quotes themselves must be escaped in
|
|
|
|
// either case.
|
|
|
|
const UnicodeString& text = formats[i];
|
|
|
|
int32_t textLength = text.length();
|
|
|
|
int32_t nestingLevel = 0;
|
|
|
|
for (int32_t j = 0; j < textLength; ++j) {
|
|
|
|
UChar c = text[j];
|
|
|
|
if (c == SINGLE_QUOTE && nestingLevel == 0) {
|
|
|
|
// Double each top-level apostrophe.
|
|
|
|
result.append(c);
|
|
|
|
} else if (c == VERTICAL_BAR && nestingLevel == 0) {
|
|
|
|
// Surround each pipe symbol with apostrophes for quoting.
|
|
|
|
// If the next character is an apostrophe, then that will be doubled,
|
|
|
|
// and although the parser will see the apostrophe pairs beginning
|
|
|
|
// and ending one character earlier than our doubling, the result
|
|
|
|
// is as desired.
|
|
|
|
// | -> '|'
|
|
|
|
// |' -> '|'''
|
|
|
|
// |'' -> '|''''' etc.
|
|
|
|
result.append(SINGLE_QUOTE).append(c).append(SINGLE_QUOTE);
|
|
|
|
continue; // Skip the append(c) at the end of the loop body.
|
|
|
|
} else if (c == LEFT_CURLY_BRACE) {
|
|
|
|
++nestingLevel;
|
|
|
|
} else if (c == RIGHT_CURLY_BRACE && nestingLevel > 0) {
|
|
|
|
--nestingLevel;
|
|
|
|
}
|
|
|
|
result.append(c);
|
2001-06-11 17:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
// Apply the reconstructed pattern.
|
|
|
|
applyPattern(result, errorCode);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Gets the limit array.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
const double*
|
|
|
|
ChoiceFormat::getLimits(int32_t& cnt) const
|
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
cnt = 0;
|
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
2001-06-11 17:21:28 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Gets the closures array.
|
|
|
|
|
|
|
|
const UBool*
|
|
|
|
ChoiceFormat::getClosures(int32_t& cnt) const
|
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
cnt = 0;
|
|
|
|
return NULL;
|
2001-06-11 17:21:28 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Gets the format array.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
const UnicodeString*
|
|
|
|
ChoiceFormat::getFormats(int32_t& cnt) const
|
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
cnt = 0;
|
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
2003-10-31 22:47:25 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Formats an int64 number, it's actually formatted as
|
|
|
|
// a double. The returned format string may differ
|
|
|
|
// from the input number because of this.
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
ChoiceFormat::format(int64_t number,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& status) const
|
|
|
|
{
|
|
|
|
return format((double) number, appendTo, status);
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
2011-04-25 20:47:32 +00:00
|
|
|
// Formats an int32_t number, it's actually formatted as
|
|
|
|
// a double.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
ChoiceFormat::format(int32_t number,
|
2002-11-23 01:11:50 +00:00
|
|
|
UnicodeString& appendTo,
|
1999-08-16 21:50:52 +00:00
|
|
|
FieldPosition& status) const
|
|
|
|
{
|
2002-11-23 01:11:50 +00:00
|
|
|
return format((double) number, appendTo, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Formats a double number.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
ChoiceFormat::format(double number,
|
2002-11-23 01:11:50 +00:00
|
|
|
UnicodeString& appendTo,
|
2000-08-15 18:25:20 +00:00
|
|
|
FieldPosition& /*pos*/) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
if (msgPattern.countParts() == 0) {
|
|
|
|
// No pattern was applied, or it failed.
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
// Get the appropriate sub-message.
|
|
|
|
int32_t msgStart = findSubMessage(msgPattern, 0, number);
|
|
|
|
if (!MessageImpl::jdkAposMode(msgPattern)) {
|
|
|
|
int32_t patternStart = msgPattern.getPart(msgStart).getLimit();
|
|
|
|
int32_t msgLimit = msgPattern.getLimitPartIndex(msgStart);
|
|
|
|
appendTo.append(msgPattern.getPatternString(),
|
|
|
|
patternStart,
|
|
|
|
msgPattern.getPatternIndex(msgLimit) - patternStart);
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
// JDK compatibility mode: Remove SKIP_SYNTAX.
|
|
|
|
return MessageImpl::appendSubMessageWithoutSkipSyntax(msgPattern, msgStart, appendTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
ChoiceFormat::findSubMessage(const MessagePattern &pattern, int32_t partIndex, double number) {
|
|
|
|
int32_t count = pattern.countParts();
|
|
|
|
int32_t msgStart;
|
|
|
|
// Iterate over (ARG_INT|DOUBLE, ARG_SELECTOR, message) tuples
|
|
|
|
// until ARG_LIMIT or end of choice-only pattern.
|
|
|
|
// Ignore the first number and selector and start the loop on the first message.
|
|
|
|
partIndex += 2;
|
|
|
|
for (;;) {
|
|
|
|
// Skip but remember the current sub-message.
|
|
|
|
msgStart = partIndex;
|
|
|
|
partIndex = pattern.getLimitPartIndex(partIndex);
|
|
|
|
if (++partIndex >= count) {
|
|
|
|
// Reached the end of the choice-only pattern.
|
|
|
|
// Return with the last sub-message.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const MessagePattern::Part &part = pattern.getPart(partIndex++);
|
|
|
|
UMessagePatternPartType type = part.getType();
|
|
|
|
if (type == UMSGPAT_PART_TYPE_ARG_LIMIT) {
|
|
|
|
// Reached the end of the ChoiceFormat style.
|
|
|
|
// Return with the last sub-message.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// part is an ARG_INT or ARG_DOUBLE
|
|
|
|
U_ASSERT(MessagePattern::Part::hasNumericValue(type));
|
|
|
|
double boundary = pattern.getNumericValue(part);
|
|
|
|
// Fetch the ARG_SELECTOR character.
|
|
|
|
int32_t selectorIndex = pattern.getPatternIndex(partIndex++);
|
|
|
|
UChar boundaryChar = pattern.getPatternString().charAt(selectorIndex);
|
|
|
|
if (boundaryChar == LESS_THAN ? !(number > boundary) : !(number >= boundary)) {
|
|
|
|
// The number is in the interval between the previous boundary and the current one.
|
|
|
|
// Return with the sub-message between them.
|
|
|
|
// The !(a>b) and !(a>=b) comparisons are equivalent to
|
|
|
|
// (a<=b) and (a<b) except they "catch" NaN.
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
return msgStart;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Formats an array of objects. Checks if the data type of the objects
|
|
|
|
// to get the right value for formatting.
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
ChoiceFormat::format(const Formattable* objs,
|
|
|
|
int32_t cnt,
|
2002-11-23 01:11:50 +00:00
|
|
|
UnicodeString& appendTo,
|
1999-08-16 21:50:52 +00:00
|
|
|
FieldPosition& pos,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
|
|
|
if(cnt < 0) {
|
1999-10-07 00:07:53 +00:00
|
|
|
status = 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
|
|
|
if (msgPattern.countParts() == 0) {
|
|
|
|
status = U_INVALID_STATE_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
for (int32_t i = 0; i < cnt; i++) {
|
2004-03-27 04:49:02 +00:00
|
|
|
double objDouble = objs[i].getDouble(status);
|
2003-11-19 04:42:44 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
2011-04-25 20:47:32 +00:00
|
|
|
format(objDouble, appendTo, pos);
|
2003-11-19 04:42:44 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-11-23 01:11:50 +00:00
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
ChoiceFormat::parse(const UnicodeString& text,
|
|
|
|
Formattable& result,
|
2011-04-25 20:47:32 +00:00
|
|
|
ParsePosition& pos) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
result.setDouble(parseArgument(msgPattern, 0, text, pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
ChoiceFormat::parseArgument(
|
|
|
|
const MessagePattern &pattern, int32_t partIndex,
|
|
|
|
const UnicodeString &source, ParsePosition &pos) {
|
1999-08-16 21:50:52 +00:00
|
|
|
// find the best number (defined as the one with the longest parse)
|
2011-04-25 20:47:32 +00:00
|
|
|
int32_t start = pos.getIndex();
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t furthest = start;
|
1999-12-28 23:57:50 +00:00
|
|
|
double bestNumber = uprv_getNaN();
|
1999-08-16 21:50:52 +00:00
|
|
|
double tempNumber = 0.0;
|
2011-04-25 20:47:32 +00:00
|
|
|
int32_t count = pattern.countParts();
|
|
|
|
while (partIndex < count && pattern.getPartType(partIndex) != UMSGPAT_PART_TYPE_ARG_LIMIT) {
|
|
|
|
tempNumber = pattern.getNumericValue(pattern.getPart(partIndex));
|
|
|
|
partIndex += 2; // skip the numeric part and ignore the ARG_SELECTOR
|
|
|
|
int32_t msgLimit = pattern.getLimitPartIndex(partIndex);
|
|
|
|
int32_t len = matchStringUntilLimitPart(pattern, partIndex, msgLimit, source, start);
|
|
|
|
if (len >= 0) {
|
|
|
|
int32_t newIndex = start + len;
|
|
|
|
if (newIndex > furthest) {
|
|
|
|
furthest = newIndex;
|
1999-08-16 21:50:52 +00:00
|
|
|
bestNumber = tempNumber;
|
2011-04-25 20:47:32 +00:00
|
|
|
if (furthest == source.length()) {
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
2011-04-25 20:47:32 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
partIndex = msgLimit + 1;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
if (furthest == start) {
|
|
|
|
pos.setErrorIndex(start);
|
|
|
|
} else {
|
|
|
|
pos.setIndex(furthest);
|
|
|
|
}
|
|
|
|
return bestNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
ChoiceFormat::matchStringUntilLimitPart(
|
|
|
|
const MessagePattern &pattern, int32_t partIndex, int32_t limitPartIndex,
|
|
|
|
const UnicodeString &source, int32_t sourceOffset) {
|
|
|
|
int32_t matchingSourceLength = 0;
|
|
|
|
const UnicodeString &msgString = pattern.getPatternString();
|
|
|
|
int32_t prevIndex = pattern.getPart(partIndex).getLimit();
|
|
|
|
for (;;) {
|
|
|
|
const MessagePattern::Part &part = pattern.getPart(++partIndex);
|
|
|
|
if (partIndex == limitPartIndex || part.getType() == UMSGPAT_PART_TYPE_SKIP_SYNTAX) {
|
|
|
|
int32_t index = part.getIndex();
|
|
|
|
int32_t length = index - prevIndex;
|
|
|
|
if (length != 0 && 0 != source.compare(sourceOffset, length, msgString, prevIndex, length)) {
|
|
|
|
return -1; // mismatch
|
|
|
|
}
|
|
|
|
matchingSourceLength += length;
|
|
|
|
if (partIndex == limitPartIndex) {
|
|
|
|
return matchingSourceLength;
|
|
|
|
}
|
|
|
|
prevIndex = part.getLimit(); // SKIP_SYNTAX
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
Format*
|
|
|
|
ChoiceFormat::clone() const
|
|
|
|
{
|
|
|
|
ChoiceFormat *aCopy = new ChoiceFormat(*this);
|
|
|
|
return aCopy;
|
|
|
|
}
|
|
|
|
|
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
|