#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/choicfmt.h"
#include "unicode/numfmt.h"
#include "unicode/locid.h"
#include "cpputils.h"
#include "cstring.h"
#include "putilimp.h"
#include <stdio.h>
#include <float.h>
U_NAMESPACE_BEGIN
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ChoiceFormat)
#define SINGLE_QUOTE ((UChar)0x0027)
#define LESS_THAN ((UChar)0x003C)
#define LESS_EQUAL ((UChar)0x0023)
#define LESS_EQUAL2 ((UChar)0x2264)
#define VERTICAL_BAR ((UChar)0x007C)
#define MINUS ((UChar)0x002D)
#define INFINITY ((UChar)0x221E)
static const UChar gPositiveInfinity[] = {INFINITY, 0};
static const UChar gNegativeInfinity[] = {MINUS, INFINITY, 0};
#define POSITIVE_INF_STRLEN 1
#define NEGATIVE_INF_STRLEN 2
ChoiceFormat::ChoiceFormat(const UnicodeString& newPattern,
UErrorCode& status)
: fChoiceLimits(0),
fClosures(0),
fChoiceFormats(0),
fCount(0)
{
applyPattern(newPattern, status);
}
ChoiceFormat::ChoiceFormat(const double* limits,
const UnicodeString* formats,
int32_t cnt )
: fChoiceLimits(0),
fClosures(0),
fChoiceFormats(0),
fCount(0)
{
setChoices(limits, formats, cnt );
}
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 );
}
ChoiceFormat::ChoiceFormat(const ChoiceFormat& that)
: NumberFormat(that),
fChoiceLimits(0),
fClosures(0),
fChoiceFormats(0)
{
*this = that;
}
ChoiceFormat::ChoiceFormat(const UnicodeString& newPattern,
UParseError& parseError,
UErrorCode& status)
: fChoiceLimits(0),
fClosures(0),
fChoiceFormats(0),
fCount(0)
{
applyPattern(newPattern,parseError, status);
}
UBool
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;
for (int32_t i = 0; i < fCount; i++) {
if ((fChoiceLimits[i] != thatAlias.fChoiceLimits[i]) ||
(fClosures[i] != thatAlias.fClosures[i]) ||
(fChoiceFormats[i] != thatAlias.fChoiceFormats[i]))
return FALSE;
}
return TRUE;
}
const ChoiceFormat&
ChoiceFormat::operator=(const ChoiceFormat& that)
{
if (this != &that) {
NumberFormat::operator=(that);
fCount = that.fCount;
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);
fChoiceFormats = new UnicodeString[fCount];
if (!fChoiceLimits || !fClosures || !fChoiceFormats) {
if (fChoiceLimits) {
uprv_free(fChoiceLimits);
fChoiceLimits = NULL;
}
if (fClosures) {
uprv_free(fClosures);
fClosures = NULL;
}
if (fChoiceFormats) {
delete[] fChoiceFormats;
fChoiceFormats = NULL;
}
} else {
uprv_arrayCopy(that.fChoiceLimits, fChoiceLimits, fCount);
uprv_arrayCopy(that.fClosures, fClosures, fCount);
uprv_arrayCopy(that.fChoiceFormats, fChoiceFormats, fCount);
}
}
return *this;
}
ChoiceFormat::~ChoiceFormat()
{
uprv_free(fChoiceLimits);
fChoiceLimits = NULL;
uprv_free(fClosures);
fClosures = NULL;
delete [] fChoiceFormats;
fChoiceFormats = NULL;
fCount = 0;
}
double
ChoiceFormat::stod(const UnicodeString& string)
{
char source[256];
char* end;
string.extract(0, string.length(), source, (int32_t)sizeof(source), US_INV);
return uprv_strtod(source,&end);
}
UnicodeString&
ChoiceFormat::dtos(double value,
UnicodeString& string)
{
char temp[DBL_DIG + 16];
char *itrPtr = temp;
char *expPtr;
sprintf(temp, "%.*g", DBL_DIG, value);
while (*itrPtr && (*itrPtr == '-' || isdigit(*itrPtr))) {
itrPtr++;
}
if (*itrPtr != 0 && *itrPtr != 'e') {
*itrPtr = '.';
itrPtr++;
}
while (*itrPtr && *itrPtr != 'e') {
itrPtr++;
}
if (*itrPtr == 'e') {
itrPtr++;
if (*itrPtr == '+' || *itrPtr == '-') {
itrPtr++;
}
expPtr = itrPtr;
while (*itrPtr == '0') {
itrPtr++;
}
if (*itrPtr && expPtr != itrPtr) {
while (*itrPtr) {
*(expPtr++) = *(itrPtr++);
}
*expPtr = 0;
}
}
string = UnicodeString(temp, -1, US_INV);
return string;
}
void
ChoiceFormat::applyPattern(const UnicodeString& pattern,
UErrorCode& status)
{
UParseError parseError;
applyPattern(pattern, parseError, status);
}
void
ChoiceFormat::applyPattern(const UnicodeString& pattern,
UParseError& parseError,
UErrorCode& status)
{
if (U_FAILURE(status))
{
return;
}
parseError.offset = -1;
parseError.preContext[0] = parseError.postContext[0] = (UChar)0;
int32_t count = 1;
int32_t i;
for (i=0; i<pattern.length(); ++i) {
UChar c = pattern[i];
if (c == SINGLE_QUOTE) {
for (;;) {
do {
++i;
} while (i<pattern.length() &&
pattern[i] != SINGLE_QUOTE);
if ((i+1)<pattern.length() &&
pattern[i+1] == SINGLE_QUOTE) {
++i;
} else {
break;
}
}
} else if (c == VERTICAL_BAR) {
++count;
}
}
double *newLimits = (double*) uprv_malloc( sizeof(double) * count);
if (newLimits == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
UBool *newClosures = (UBool*) uprv_malloc( sizeof(UBool) * count);
if (newClosures == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
uprv_free(newLimits);
return;
}
UnicodeString *newFormats = new UnicodeString[count];
if (newFormats == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
uprv_free(newLimits);
uprv_free(newClosures);
return;
}
int32_t k = 0; UnicodeString buf; UBool inQuote = FALSE;
UBool inNumber = TRUE;
for (i=0; i<pattern.length(); ++i) {
UChar c = pattern[i];
if (c == SINGLE_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();
if (!buf.compare(gPositiveInfinity, POSITIVE_INF_STRLEN)) {
limit = uprv_getInfinity();
} else if (!buf.compare(gNegativeInfinity, NEGATIVE_INF_STRLEN)) {
limit = -uprv_getInfinity();
} else {
limit = stod(buf);
}
if (k == count) {
goto error;
}
newLimits[k] = limit;
newClosures[k] = (c == LESS_THAN);
if (k > 0 && limit <= newLimits[k-1]) {
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;
uprv_free(fChoiceLimits);
uprv_free(fClosures);
delete[] fChoiceFormats;
fCount = count;
fChoiceLimits = newLimits;
fClosures = newClosures;
fChoiceFormats = newFormats;
return;
error:
status = U_ILLEGAL_ARGUMENT_ERROR;
syntaxError(pattern,i,parseError);
uprv_free(newLimits);
uprv_free(newClosures);
delete[] newFormats;
return;
}
UnicodeString&
ChoiceFormat::toPattern(UnicodeString& result) const
{
result.remove();
for (int32_t i = 0; i < fCount; ++i) {
if (i != 0) {
result += VERTICAL_BAR;
}
UnicodeString buf;
if (uprv_isPositiveInfinity(fChoiceLimits[i])) {
result += INFINITY;
} else if (uprv_isNegativeInfinity(fChoiceLimits[i])) {
result += MINUS;
result += INFINITY;
} else {
result += dtos(fChoiceLimits[i], buf);
}
if (fClosures[i]) {
result += LESS_THAN;
} else {
result += LESS_EQUAL;
}
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;
}
if (text.indexOf(SINGLE_QUOTE) < 0) {
result += text;
}
else {
for (int32_t j = 0; j < text.length(); ++j) {
UChar c = text[j];
result += c;
if (c == SINGLE_QUOTE) {
result += c;
}
}
}
if (needQuote) {
result += SINGLE_QUOTE;
}
}
return result;
}
void
ChoiceFormat::setChoices( const double* limits,
const UnicodeString* formats,
int32_t cnt )
{
setChoices(limits, 0, formats, cnt);
}
void
ChoiceFormat::setChoices( const double* limits,
const UBool* closures,
const UnicodeString* formats,
int32_t cnt )
{
if(limits == 0 || formats == 0)
return;
if (fChoiceLimits) {
uprv_free(fChoiceLimits);
}
if (fClosures) {
uprv_free(fClosures);
}
if (fChoiceFormats) {
delete [] fChoiceFormats;
}
fCount = cnt;
fChoiceLimits = (double*) uprv_malloc( sizeof(double) * fCount);
fClosures = (UBool*) uprv_malloc( sizeof(UBool) * fCount);
fChoiceFormats = new UnicodeString[fCount];
if (!fChoiceLimits || !fClosures || !fChoiceFormats) {
if (fChoiceLimits) {
uprv_free(fChoiceLimits);
fChoiceLimits = NULL;
}
if (fClosures) {
uprv_free(fClosures);
fClosures = NULL;
}
if (fChoiceFormats) {
delete[] fChoiceFormats;
fChoiceFormats = NULL;
}
return;
}
uprv_arrayCopy(limits, fChoiceLimits, fCount);
uprv_arrayCopy(formats, fChoiceFormats, fCount);
if (closures != 0) {
uprv_arrayCopy(closures, fClosures, fCount);
} else {
int32_t i;
for (i=0; i<fCount; ++i) {
fClosures[i] = FALSE;
}
}
}
const double*
ChoiceFormat::getLimits(int32_t& cnt) const
{
cnt = fCount;
return fChoiceLimits;
}
const UBool*
ChoiceFormat::getClosures(int32_t& cnt) const
{
cnt = fCount;
return fClosures;
}
const UnicodeString*
ChoiceFormat::getFormats(int32_t& cnt) const
{
cnt = fCount;
return fChoiceFormats;
}
UnicodeString&
ChoiceFormat::format(int64_t number,
UnicodeString& appendTo,
FieldPosition& status) const
{
return format((double) number, appendTo, status);
}
UnicodeString&
ChoiceFormat::format(int32_t number,
UnicodeString& appendTo,
FieldPosition& status) const
{
return format((double) number, appendTo, status);
}
UnicodeString&
ChoiceFormat::format(double number,
UnicodeString& appendTo,
FieldPosition& ) const
{
int32_t i;
for (i = 0; i < fCount; ++i) {
if (fClosures[i]) {
if (!(number > fChoiceLimits[i])) {
break;
}
} else if (!(number >= fChoiceLimits[i])) {
break;
}
}
--i;
if (i < 0) {
i = 0;
}
appendTo += fChoiceFormats[i];
return appendTo;
}
UnicodeString&
ChoiceFormat::format(const Formattable* objs,
int32_t cnt,
UnicodeString& appendTo,
FieldPosition& pos,
UErrorCode& status) const
{
if(cnt < 0) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return appendTo;
}
UnicodeString buffer;
for (int32_t i = 0; i < cnt; i++) {
double objDouble = objs[i].getDouble(status);
if (U_SUCCESS(status)) {
buffer.remove();
appendTo += format(objDouble, buffer, pos);
}
}
return appendTo;
}
UnicodeString&
ChoiceFormat::format(const Formattable& obj,
UnicodeString& appendTo,
FieldPosition& pos,
UErrorCode& status) const
{
return NumberFormat::format(obj, appendTo, pos, status);
}
void
ChoiceFormat::parse(const UnicodeString& text,
Formattable& result,
ParsePosition& status) const
{
int32_t start = status.getIndex();
int32_t furthest = start;
double bestNumber = uprv_getNaN();
double tempNumber = 0.0;
for (int i = 0; i < fCount; ++i) {
int32_t len = fChoiceFormats[i].length();
if (text.compare(start, len, fChoiceFormats[i]) == 0) {
status.setIndex(start + len);
tempNumber = fChoiceLimits[i];
if (status.getIndex() > furthest) {
furthest = status.getIndex();
bestNumber = tempNumber;
if (furthest == text.length())
break;
}
}
}
status.setIndex(furthest);
if (status.getIndex() == start) {
status.setErrorIndex(furthest);
}
result.setDouble(bestNumber);
}
void
ChoiceFormat::parse(const UnicodeString& text,
Formattable& result,
UErrorCode& status) const
{
NumberFormat::parse(text, result, status);
}
Format*
ChoiceFormat::clone() const
{
ChoiceFormat *aCopy = new ChoiceFormat(*this);
return aCopy;
}
U_NAMESPACE_END
#endif