2014-04-09 23:00:45 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
|
|
|
* Copyright (c) 2014, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
#include "unicode/scientificformathelper.h"
|
|
|
|
#include "unicode/dcfmtsym.h"
|
|
|
|
#include "unicode/fpositer.h"
|
|
|
|
#include "unicode/utf16.h"
|
2014-04-10 18:13:29 +00:00
|
|
|
#include "unicode/uniset.h"
|
|
|
|
#include "decfmtst.h"
|
2014-04-09 23:00:45 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2014-06-17 14:24:40 +00:00
|
|
|
static const UChar kSuperscriptDigits[] = {0x2070, 0xB9, 0xB2, 0xB3, 0x2074, 0x2075, 0x2076, 0x2077, 0x2078, 0x2079};
|
2014-04-10 18:13:29 +00:00
|
|
|
|
2014-06-17 14:24:40 +00:00
|
|
|
static const UChar kSuperscriptPlusSign = 0x207A;
|
|
|
|
static const UChar kSuperscriptMinusSign = 0x207B;
|
2014-04-09 23:00:45 +00:00
|
|
|
|
|
|
|
ScientificFormatHelper::ScientificFormatHelper(
|
2014-04-10 18:13:29 +00:00
|
|
|
const DecimalFormatSymbols &dfs, UErrorCode &status)
|
|
|
|
: fPreExponent(), fStaticSets(NULL) {
|
2014-04-09 23:00:45 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-10 18:13:29 +00:00
|
|
|
fPreExponent.append(dfs.getConstSymbol(
|
|
|
|
DecimalFormatSymbols::kExponentMultiplicationSymbol));
|
2014-04-09 23:00:45 +00:00
|
|
|
fPreExponent.append(dfs.getSymbol(DecimalFormatSymbols::kOneDigitSymbol));
|
|
|
|
fPreExponent.append(dfs.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol));
|
2014-04-10 18:13:29 +00:00
|
|
|
fStaticSets = DecimalFormatStaticSets::getStaticSets(status);
|
2014-04-09 23:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScientificFormatHelper::ScientificFormatHelper(
|
2014-04-10 18:13:29 +00:00
|
|
|
const ScientificFormatHelper &other)
|
2014-06-17 14:26:41 +00:00
|
|
|
: UObject(other),
|
|
|
|
fPreExponent(other.fPreExponent),
|
|
|
|
fStaticSets(other.fStaticSets) {
|
2014-04-09 23:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScientificFormatHelper &ScientificFormatHelper::operator=(const ScientificFormatHelper &other) {
|
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
fPreExponent = other.fPreExponent;
|
2014-04-10 18:13:29 +00:00
|
|
|
fStaticSets = other.fStaticSets;
|
2014-04-09 23:00:45 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScientificFormatHelper::~ScientificFormatHelper() {
|
|
|
|
}
|
|
|
|
|
2014-04-30 20:15:10 +00:00
|
|
|
UnicodeString &ScientificFormatHelper::insertMarkup(
|
2014-04-09 23:00:45 +00:00
|
|
|
const UnicodeString &s,
|
|
|
|
FieldPositionIterator &fpi,
|
|
|
|
const UnicodeString &beginMarkup,
|
|
|
|
const UnicodeString &endMarkup,
|
|
|
|
UnicodeString &result,
|
2014-06-13 16:40:17 +00:00
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return result;
|
|
|
|
}
|
2014-04-09 23:00:45 +00:00
|
|
|
FieldPosition fp;
|
|
|
|
int32_t copyFromOffset = 0;
|
2014-06-13 16:40:17 +00:00
|
|
|
UBool exponentSymbolFieldPresent = FALSE;
|
|
|
|
UBool exponentFieldPresent = FALSE;
|
2014-04-09 23:00:45 +00:00
|
|
|
while (fpi.next(fp)) {
|
|
|
|
switch (fp.getField()) {
|
|
|
|
case UNUM_EXPONENT_SYMBOL_FIELD:
|
2014-06-13 16:40:17 +00:00
|
|
|
exponentSymbolFieldPresent = TRUE;
|
2014-04-09 23:00:45 +00:00
|
|
|
result.append(s, copyFromOffset, fp.getBeginIndex() - copyFromOffset);
|
|
|
|
copyFromOffset = fp.getEndIndex();
|
|
|
|
result.append(fPreExponent);
|
|
|
|
result.append(beginMarkup);
|
|
|
|
break;
|
|
|
|
case UNUM_EXPONENT_FIELD:
|
2014-06-13 16:40:17 +00:00
|
|
|
exponentFieldPresent = TRUE;
|
2014-04-09 23:00:45 +00:00
|
|
|
result.append(s, copyFromOffset, fp.getEndIndex() - copyFromOffset);
|
|
|
|
copyFromOffset = fp.getEndIndex();
|
|
|
|
result.append(endMarkup);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-13 16:40:17 +00:00
|
|
|
if (!exponentSymbolFieldPresent || !exponentFieldPresent) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return result;
|
|
|
|
}
|
2014-04-09 23:00:45 +00:00
|
|
|
result.append(s, copyFromOffset, s.length() - copyFromOffset);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UBool copyAsSuperscript(
|
|
|
|
const UnicodeString &s,
|
|
|
|
int32_t beginIndex,
|
|
|
|
int32_t endIndex,
|
|
|
|
UnicodeString &result,
|
|
|
|
UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
for (int32_t i = beginIndex; i < endIndex;) {
|
|
|
|
UChar32 c = s.char32At(i);
|
|
|
|
int32_t digit = u_charDigitValue(c);
|
|
|
|
if (digit < 0) {
|
|
|
|
status = U_INVALID_CHAR_FOUND;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-04-10 18:13:29 +00:00
|
|
|
result.append(kSuperscriptDigits[digit]);
|
2014-04-09 23:00:45 +00:00
|
|
|
i += U16_LENGTH(c);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString &ScientificFormatHelper::toSuperscriptExponentDigits(
|
|
|
|
const UnicodeString &s,
|
|
|
|
FieldPositionIterator &fpi,
|
|
|
|
UnicodeString &result,
|
|
|
|
UErrorCode &status) const {
|
2014-06-13 16:40:17 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return result;
|
|
|
|
}
|
2014-04-09 23:00:45 +00:00
|
|
|
FieldPosition fp;
|
|
|
|
int32_t copyFromOffset = 0;
|
2014-06-13 16:40:17 +00:00
|
|
|
UBool exponentSymbolFieldPresent = FALSE;
|
|
|
|
UBool exponentFieldPresent = FALSE;
|
2014-04-09 23:00:45 +00:00
|
|
|
while (fpi.next(fp)) {
|
|
|
|
switch (fp.getField()) {
|
|
|
|
case UNUM_EXPONENT_SYMBOL_FIELD:
|
2014-06-13 16:40:17 +00:00
|
|
|
exponentSymbolFieldPresent = TRUE;
|
2014-04-09 23:00:45 +00:00
|
|
|
result.append(s, copyFromOffset, fp.getBeginIndex() - copyFromOffset);
|
|
|
|
copyFromOffset = fp.getEndIndex();
|
|
|
|
result.append(fPreExponent);
|
|
|
|
break;
|
|
|
|
case UNUM_EXPONENT_SIGN_FIELD:
|
|
|
|
{
|
|
|
|
int32_t beginIndex = fp.getBeginIndex();
|
|
|
|
int32_t endIndex = fp.getEndIndex();
|
2014-04-10 18:13:29 +00:00
|
|
|
UChar32 aChar = s.char32At(beginIndex);
|
|
|
|
if (fStaticSets->fMinusSigns->contains(aChar)) {
|
|
|
|
result.append(s, copyFromOffset, beginIndex - copyFromOffset);
|
|
|
|
result.append(kSuperscriptMinusSign);
|
|
|
|
} else if (fStaticSets->fPlusSigns->contains(aChar)) {
|
2014-04-09 23:00:45 +00:00
|
|
|
result.append(s, copyFromOffset, beginIndex - copyFromOffset);
|
2014-04-10 18:13:29 +00:00
|
|
|
result.append(kSuperscriptPlusSign);
|
2014-04-09 23:00:45 +00:00
|
|
|
} else {
|
|
|
|
status = U_INVALID_CHAR_FOUND;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
copyFromOffset = endIndex;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UNUM_EXPONENT_FIELD:
|
2014-06-13 16:40:17 +00:00
|
|
|
exponentFieldPresent = TRUE;
|
2014-04-09 23:00:45 +00:00
|
|
|
result.append(s, copyFromOffset, fp.getBeginIndex() - copyFromOffset);
|
|
|
|
if (!copyAsSuperscript(
|
|
|
|
s, fp.getBeginIndex(), fp.getEndIndex(), result, status)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
copyFromOffset = fp.getEndIndex();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-13 16:40:17 +00:00
|
|
|
if (!exponentSymbolFieldPresent || !exponentFieldPresent) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return result;
|
|
|
|
}
|
2014-04-09 23:00:45 +00:00
|
|
|
result.append(s, copyFromOffset, s.length() - copyFromOffset);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* !UCONFIG_NO_FORMATTING */
|