1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-11-22 20:25:35 +00:00
|
|
|
*******************************************************************************
|
2008-01-09 20:36:55 +00:00
|
|
|
* Copyright (C) 1997-2008, 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"
|
2004-10-18 02:43:33 +00:00
|
|
|
#include "putilimp.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) /*-*/
|
2001-06-21 20:46:12 +00:00
|
|
|
#define INFINITY ((UChar)0x221E)
|
2001-06-11 17:21:28 +00:00
|
|
|
|
2003-08-31 20:53:46 +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)
|
|
|
|
: fChoiceLimits(0),
|
2001-06-11 17:21:28 +00:00
|
|
|
fClosures(0),
|
1999-08-16 21:50:52 +00:00
|
|
|
fChoiceFormats(0),
|
|
|
|
fCount(0)
|
|
|
|
{
|
|
|
|
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 )
|
|
|
|
: fChoiceLimits(0),
|
2001-06-11 17:21:28 +00:00
|
|
|
fClosures(0),
|
1999-08-16 21:50:52 +00:00
|
|
|
fChoiceFormats(0),
|
|
|
|
fCount(0)
|
|
|
|
{
|
|
|
|
setChoices(limits, formats, cnt );
|
|
|
|
}
|
|
|
|
|
2001-06-11 17:21:28 +00:00
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
ChoiceFormat::ChoiceFormat(const double* limits,
|
|
|
|
const UBool* closures,
|
|
|
|
const UnicodeString* formats,
|
|
|
|
int32_t cnt )
|
|
|
|
: fChoiceLimits(0),
|
|
|
|
fClosures(0),
|
|
|
|
fChoiceFormats(0),
|
|
|
|
fCount(0)
|
|
|
|
{
|
|
|
|
setChoices(limits, closures, formats, cnt );
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
fChoiceLimits(0),
|
|
|
|
fClosures(0),
|
|
|
|
fChoiceFormats(0)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
*this = that;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
: fChoiceLimits(0),
|
|
|
|
fClosures(0),
|
|
|
|
fChoiceFormats(0),
|
|
|
|
fCount(0)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
if (fCount != thatAlias.fCount) return FALSE;
|
|
|
|
// Checks the limits, the corresponding format string and LE or LT flags.
|
|
|
|
// LE means less than and equal to, LT means less than.
|
|
|
|
for (int32_t i = 0; i < fCount; i++) {
|
|
|
|
if ((fChoiceLimits[i] != thatAlias.fChoiceLimits[i]) ||
|
2001-06-11 17:21:28 +00:00
|
|
|
(fClosures[i] != thatAlias.fClosures[i]) ||
|
1999-08-16 21:50:52 +00:00
|
|
|
(fChoiceFormats[i] != thatAlias.fChoiceFormats[i]))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// copy constructor
|
|
|
|
|
|
|
|
const ChoiceFormat&
|
|
|
|
ChoiceFormat::operator=(const ChoiceFormat& that)
|
|
|
|
{
|
|
|
|
if (this != &that) {
|
|
|
|
NumberFormat::operator=(that);
|
|
|
|
fCount = that.fCount;
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(fChoiceLimits);
|
|
|
|
fChoiceLimits = NULL;
|
|
|
|
uprv_free(fClosures);
|
|
|
|
fClosures = NULL;
|
|
|
|
delete [] fChoiceFormats;
|
|
|
|
fChoiceFormats = NULL;
|
|
|
|
|
|
|
|
fChoiceLimits = (double*) uprv_malloc( sizeof(double) * fCount);
|
|
|
|
fClosures = (UBool*) uprv_malloc( sizeof(UBool) * fCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
fChoiceFormats = new UnicodeString[fCount];
|
2008-01-09 20:36:55 +00:00
|
|
|
|
|
|
|
// check for memory allocation error
|
|
|
|
if (!fChoiceLimits || !fClosures || !fChoiceFormats) {
|
2008-01-21 22:52:48 +00:00
|
|
|
if (fChoiceLimits) {
|
|
|
|
uprv_free(fChoiceLimits);
|
|
|
|
fChoiceLimits = NULL;
|
|
|
|
}
|
|
|
|
if (fClosures) {
|
|
|
|
uprv_free(fClosures);
|
|
|
|
fClosures = NULL;
|
|
|
|
}
|
|
|
|
if (fChoiceFormats) {
|
|
|
|
delete[] fChoiceFormats;
|
|
|
|
fChoiceFormats = NULL;
|
|
|
|
}
|
2008-01-09 20:36:55 +00:00
|
|
|
} else {
|
2008-01-21 22:52:48 +00:00
|
|
|
uprv_arrayCopy(that.fChoiceLimits, fChoiceLimits, fCount);
|
|
|
|
uprv_arrayCopy(that.fClosures, fClosures, fCount);
|
|
|
|
uprv_arrayCopy(that.fChoiceFormats, fChoiceFormats, fCount);
|
2008-01-09 20:36:55 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
ChoiceFormat::~ChoiceFormat()
|
|
|
|
{
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(fChoiceLimits);
|
|
|
|
fChoiceLimits = NULL;
|
|
|
|
uprv_free(fClosures);
|
|
|
|
fClosures = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
delete [] fChoiceFormats;
|
2002-02-28 01:42:40 +00:00
|
|
|
fChoiceFormats = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
fCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2001-08-16 00:55:16 +00:00
|
|
|
* Convert a string to a double value
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
double
|
2001-08-17 21:50:48 +00:00
|
|
|
ChoiceFormat::stod(const UnicodeString& string)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-08-17 21:50:48 +00:00
|
|
|
char source[256];
|
2001-08-16 00:55:16 +00:00
|
|
|
char* end;
|
2001-08-17 21:50:48 +00:00
|
|
|
|
2004-09-30 21:35:30 +00:00
|
|
|
string.extract(0, string.length(), source, (int32_t)sizeof(source), US_INV); /* invariant codepage */
|
2001-08-16 00:55:16 +00:00
|
|
|
return uprv_strtod(source,&end);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
/**
|
2008-01-21 22:52:48 +00:00
|
|
|
* Convert a double value to a string without the overhead of ICU.
|
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)
|
|
|
|
{
|
|
|
|
UParseError parseError;
|
2001-08-17 21:50:48 +00:00
|
|
|
applyPattern(pattern, parseError, 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)
|
|
|
|
{
|
|
|
|
if (U_FAILURE(status))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear error struct
|
2001-11-15 21:13:37 +00:00
|
|
|
parseError.offset = -1;
|
2001-08-16 00:55:16 +00:00
|
|
|
parseError.preContext[0] = parseError.postContext[0] = (UChar)0;
|
|
|
|
|
|
|
|
// Perform 2 passes. The first computes the number of limits in
|
|
|
|
// this pattern (fCount), which is 1 more than the number of
|
|
|
|
// literal VERTICAL_BAR characters.
|
|
|
|
int32_t count = 1;
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<pattern.length(); ++i) {
|
|
|
|
UChar c = pattern[i];
|
|
|
|
if (c == SINGLE_QUOTE) {
|
|
|
|
// Skip over the entire quote, including embedded
|
|
|
|
// contiguous pairs of SINGLE_QUOTE.
|
|
|
|
for (;;) {
|
|
|
|
do {
|
|
|
|
++i;
|
|
|
|
} while (i<pattern.length() &&
|
|
|
|
pattern[i] != SINGLE_QUOTE);
|
|
|
|
if ((i+1)<pattern.length() &&
|
|
|
|
pattern[i+1] == SINGLE_QUOTE) {
|
|
|
|
// SINGLE_QUOTE pair; skip over it
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (c == VERTICAL_BAR) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the required storage.
|
2002-02-28 01:42:40 +00:00
|
|
|
double *newLimits = (double*) uprv_malloc( sizeof(double) * count);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (newLimits == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
UBool *newClosures = (UBool*) uprv_malloc( sizeof(UBool) * count);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (newClosures == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2002-07-16 17:50:42 +00:00
|
|
|
uprv_free(newLimits);
|
2002-06-29 09:31:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
UnicodeString *newFormats = new UnicodeString[count];
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (newFormats == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2002-07-16 17:50:42 +00:00
|
|
|
uprv_free(newLimits);
|
|
|
|
uprv_free(newClosures);
|
2002-06-29 09:31:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
|
|
|
|
// Perform the second pass
|
|
|
|
int32_t k = 0; // index into newXxx[] arrays
|
|
|
|
UnicodeString buf; // scratch buffer
|
|
|
|
UBool inQuote = FALSE;
|
|
|
|
UBool inNumber = TRUE; // TRUE before < or #, FALSE after
|
2002-06-29 09:31:05 +00:00
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
for (i=0; i<pattern.length(); ++i) {
|
|
|
|
UChar c = pattern[i];
|
|
|
|
if (c == SINGLE_QUOTE) {
|
|
|
|
// Check for SINGLE_QUOTE pair indicating a literal quote
|
|
|
|
if ((i+1) < pattern.length() &&
|
|
|
|
pattern[i+1] == SINGLE_QUOTE) {
|
|
|
|
buf += SINGLE_QUOTE;
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
inQuote = !inQuote;
|
|
|
|
}
|
|
|
|
} else if (inQuote) {
|
|
|
|
buf += c;
|
|
|
|
} else if (c == LESS_THAN || c == LESS_EQUAL || c == LESS_EQUAL2) {
|
|
|
|
if (!inNumber || buf.length() == 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
inNumber = FALSE;
|
|
|
|
|
|
|
|
double limit;
|
|
|
|
buf.trim();
|
2003-08-31 20:53:46 +00:00
|
|
|
if (!buf.compare(gPositiveInfinity, POSITIVE_INF_STRLEN)) {
|
2001-08-16 00:55:16 +00:00
|
|
|
limit = uprv_getInfinity();
|
2003-08-31 20:53:46 +00:00
|
|
|
} else if (!buf.compare(gNegativeInfinity, NEGATIVE_INF_STRLEN)) {
|
2001-08-16 00:55:16 +00:00
|
|
|
limit = -uprv_getInfinity();
|
|
|
|
} else {
|
2001-08-17 21:50:48 +00:00
|
|
|
limit = stod(buf);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (k == count) {
|
|
|
|
// This shouldn't happen. If it does, it means that
|
|
|
|
// the count determined in the first pass did not
|
|
|
|
// match the number of elements found in the second
|
|
|
|
// pass.
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
newLimits[k] = limit;
|
|
|
|
newClosures[k] = (c == LESS_THAN);
|
|
|
|
|
|
|
|
if (k > 0 && limit <= newLimits[k-1]) {
|
|
|
|
// Each limit must be strictly > than the previous
|
|
|
|
// limit. One exception: Two subsequent limits may be
|
|
|
|
// == if the first closure is FALSE and the second
|
|
|
|
// closure is TRUE. This places the limit value in
|
|
|
|
// the second interval.
|
|
|
|
if (!(limit == newLimits[k-1] &&
|
|
|
|
!newClosures[k-1] &&
|
|
|
|
newClosures[k])) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.truncate(0);
|
|
|
|
} else if (c == VERTICAL_BAR) {
|
|
|
|
if (inNumber) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
inNumber = TRUE;
|
|
|
|
|
|
|
|
newFormats[k] = buf;
|
|
|
|
++k;
|
|
|
|
buf.truncate(0);
|
|
|
|
} else {
|
|
|
|
buf += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k != (count-1) || inNumber || inQuote) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
newFormats[k] = buf;
|
|
|
|
|
|
|
|
// Don't modify this object until the parse succeeds
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(fChoiceLimits);
|
|
|
|
uprv_free(fClosures);
|
2001-08-16 00:55:16 +00:00
|
|
|
delete[] fChoiceFormats;
|
|
|
|
fCount = count;
|
|
|
|
fChoiceLimits = newLimits;
|
|
|
|
fClosures = newClosures;
|
|
|
|
fChoiceFormats = newFormats;
|
|
|
|
return;
|
|
|
|
|
2001-10-09 21:30:02 +00:00
|
|
|
error:
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
syntaxError(pattern,i,parseError);
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(newLimits);
|
|
|
|
uprv_free(newClosures);
|
2001-08-16 00:55:16 +00:00
|
|
|
delete[] newFormats;
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Reconstruct the original input pattern.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
ChoiceFormat::toPattern(UnicodeString& result) const
|
|
|
|
{
|
|
|
|
result.remove();
|
|
|
|
for (int32_t i = 0; i < fCount; ++i) {
|
|
|
|
if (i != 0) {
|
2001-06-11 17:21:28 +00:00
|
|
|
result += VERTICAL_BAR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
UnicodeString buf;
|
2001-06-11 17:21:28 +00:00
|
|
|
if (uprv_isPositiveInfinity(fChoiceLimits[i])) {
|
|
|
|
result += INFINITY;
|
|
|
|
} else if (uprv_isNegativeInfinity(fChoiceLimits[i])) {
|
|
|
|
result += MINUS;
|
|
|
|
result += INFINITY;
|
|
|
|
} else {
|
2001-08-17 21:50:48 +00:00
|
|
|
result += dtos(fChoiceLimits[i], buf);
|
2001-03-06 19:44:45 +00:00
|
|
|
}
|
2001-06-14 23:23:04 +00:00
|
|
|
if (fClosures[i]) {
|
|
|
|
result += LESS_THAN;
|
|
|
|
} else {
|
|
|
|
result += LESS_EQUAL;
|
|
|
|
}
|
2001-06-11 17:21:28 +00:00
|
|
|
// Append fChoiceFormats[i], using quotes if there are special
|
|
|
|
// characters. Single quotes themselves must be escaped in
|
|
|
|
// either case.
|
|
|
|
const UnicodeString& text = fChoiceFormats[i];
|
|
|
|
UBool needQuote = text.indexOf(LESS_THAN) >= 0
|
|
|
|
|| text.indexOf(LESS_EQUAL) >= 0
|
|
|
|
|| text.indexOf(LESS_EQUAL2) >= 0
|
|
|
|
|| text.indexOf(VERTICAL_BAR) >= 0;
|
|
|
|
if (needQuote) {
|
|
|
|
result += SINGLE_QUOTE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-06-11 17:21:28 +00:00
|
|
|
if (text.indexOf(SINGLE_QUOTE) < 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
result += text;
|
2001-06-11 17:21:28 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
else {
|
2001-06-11 17:21:28 +00:00
|
|
|
for (int32_t j = 0; j < text.length(); ++j) {
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar c = text[j];
|
|
|
|
result += c;
|
2001-06-11 17:21:28 +00:00
|
|
|
if (c == SINGLE_QUOTE) {
|
1999-08-16 21:50:52 +00:00
|
|
|
result += c;
|
2001-06-11 17:21:28 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
2001-06-11 17:21:28 +00:00
|
|
|
if (needQuote) {
|
|
|
|
result += SINGLE_QUOTE;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
setChoices(limits, 0, formats, cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// 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
|
|
|
{
|
|
|
|
if(limits == 0 || formats == 0)
|
|
|
|
return;
|
|
|
|
|
2008-01-09 20:36:55 +00:00
|
|
|
if (fChoiceLimits) {
|
2008-01-21 22:52:48 +00:00
|
|
|
uprv_free(fChoiceLimits);
|
2008-01-09 20:36:55 +00:00
|
|
|
}
|
|
|
|
if (fClosures) {
|
2008-01-21 22:52:48 +00:00
|
|
|
uprv_free(fClosures);
|
2008-01-09 20:36:55 +00:00
|
|
|
}
|
|
|
|
if (fChoiceFormats) {
|
2008-01-21 22:52:48 +00:00
|
|
|
delete [] fChoiceFormats;
|
2008-01-09 20:36:55 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Note that the old arrays are deleted and this owns
|
|
|
|
// the created array.
|
|
|
|
fCount = cnt;
|
2002-02-28 01:42:40 +00:00
|
|
|
fChoiceLimits = (double*) uprv_malloc( sizeof(double) * fCount);
|
|
|
|
fClosures = (UBool*) uprv_malloc( sizeof(UBool) * fCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
fChoiceFormats = new UnicodeString[fCount];
|
|
|
|
|
2008-01-09 20:36:55 +00:00
|
|
|
//check for memory allocation error
|
|
|
|
if (!fChoiceLimits || !fClosures || !fChoiceFormats) {
|
2008-01-21 22:52:48 +00:00
|
|
|
if (fChoiceLimits) {
|
|
|
|
uprv_free(fChoiceLimits);
|
|
|
|
fChoiceLimits = NULL;
|
|
|
|
}
|
|
|
|
if (fClosures) {
|
|
|
|
uprv_free(fClosures);
|
|
|
|
fClosures = NULL;
|
|
|
|
}
|
|
|
|
if (fChoiceFormats) {
|
|
|
|
delete[] fChoiceFormats;
|
|
|
|
fChoiceFormats = NULL;
|
|
|
|
}
|
|
|
|
return;
|
2008-01-09 20:36:55 +00:00
|
|
|
}
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_arrayCopy(limits, fChoiceLimits, fCount);
|
|
|
|
uprv_arrayCopy(formats, fChoiceFormats, fCount);
|
2001-06-11 17:21:28 +00:00
|
|
|
|
|
|
|
if (closures != 0) {
|
|
|
|
uprv_arrayCopy(closures, fClosures, fCount);
|
|
|
|
} else {
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<fCount; ++i) {
|
|
|
|
fClosures[i] = FALSE;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
cnt = fCount;
|
|
|
|
return fChoiceLimits;
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
cnt = fCount;
|
|
|
|
return fClosures;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
cnt = fCount;
|
|
|
|
return fChoiceFormats;
|
|
|
|
}
|
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
|
|
|
// -------------------------------------
|
|
|
|
// Formats a long number, it's actually formatted as
|
|
|
|
// a double. The returned format string may differ
|
|
|
|
// from the input number because of this.
|
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
|
|
|
{
|
|
|
|
// find the number
|
|
|
|
int32_t i;
|
|
|
|
for (i = 0; i < fCount; ++i) {
|
2001-06-11 17:21:28 +00:00
|
|
|
if (fClosures[i]) {
|
|
|
|
if (!(number > fChoiceLimits[i])) {
|
|
|
|
// same as number <= fChoiceLimits, except catches NaN
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (!(number >= fChoiceLimits[i])) {
|
1999-08-16 21:50:52 +00:00
|
|
|
// same as number < fChoiceLimits, except catches NaN
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--i;
|
2001-06-11 17:21:28 +00:00
|
|
|
if (i < 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
i = 0;
|
2001-06-11 17:21:28 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
// return either a formatted number, or a string
|
2002-11-23 01:11:50 +00:00
|
|
|
appendTo += fChoiceFormats[i];
|
|
|
|
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
|
|
|
// -------------------------------------
|
|
|
|
// 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
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString buffer;
|
|
|
|
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)) {
|
|
|
|
buffer.remove();
|
|
|
|
appendTo += format(objDouble, buffer, pos);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// 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& obj,
|
2002-11-23 01:11:50 +00:00
|
|
|
UnicodeString& appendTo,
|
1999-08-16 21:50:52 +00:00
|
|
|
FieldPosition& pos,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2002-11-23 01:11:50 +00:00
|
|
|
return NumberFormat::format(obj, appendTo, pos, 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
|
|
|
void
|
|
|
|
ChoiceFormat::parse(const UnicodeString& text,
|
|
|
|
Formattable& result,
|
|
|
|
ParsePosition& status) const
|
|
|
|
{
|
|
|
|
// find the best number (defined as the one with the longest parse)
|
|
|
|
int32_t start = status.getIndex();
|
|
|
|
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;
|
|
|
|
for (int i = 0; i < fCount; ++i) {
|
2003-04-21 22:46:36 +00:00
|
|
|
int32_t len = fChoiceFormats[i].length();
|
|
|
|
if (text.compare(start, len, fChoiceFormats[i]) == 0) {
|
|
|
|
status.setIndex(start + len);
|
1999-08-16 21:50:52 +00:00
|
|
|
tempNumber = fChoiceLimits[i];
|
|
|
|
if (status.getIndex() > furthest) {
|
|
|
|
furthest = status.getIndex();
|
|
|
|
bestNumber = tempNumber;
|
1999-12-08 02:11:04 +00:00
|
|
|
if (furthest == text.length())
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status.setIndex(furthest);
|
|
|
|
if (status.getIndex() == start) {
|
|
|
|
status.setErrorIndex(furthest);
|
|
|
|
}
|
|
|
|
result.setDouble(bestNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Parses the text and return the Formattable object.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
ChoiceFormat::parse(const UnicodeString& text,
|
|
|
|
Formattable& result,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
|
|
|
NumberFormat::parse(text, result, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
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
|