123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- // © 2016 and later: Unicode, Inc. and others.
- // License & terms of use: http://www.unicode.org/copyright.html
- /*
- **********************************************************************
- * Copyright (c) 2014, International Business Machines
- * Corporation and others. All Rights Reserved.
- **********************************************************************
- */
- #include "unicode/utypes.h"
- #if !UCONFIG_NO_FORMATTING
- #include "unicode/scientificnumberformatter.h"
- #include "unicode/dcfmtsym.h"
- #include "unicode/fpositer.h"
- #include "unicode/utf16.h"
- #include "unicode/uniset.h"
- #include "unicode/decimfmt.h"
- #include "static_unicode_sets.h"
- U_NAMESPACE_BEGIN
- static const char16_t kSuperscriptDigits[] = {
- 0x2070,
- 0xB9,
- 0xB2,
- 0xB3,
- 0x2074,
- 0x2075,
- 0x2076,
- 0x2077,
- 0x2078,
- 0x2079};
- static const char16_t kSuperscriptPlusSign = 0x207A;
- static const char16_t kSuperscriptMinusSign = 0x207B;
- 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;
- }
- result.append(kSuperscriptDigits[digit]);
- i += U16_LENGTH(c);
- }
- return true;
- }
- ScientificNumberFormatter *ScientificNumberFormatter::createSuperscriptInstance(
- DecimalFormat *fmtToAdopt, UErrorCode &status) {
- return createInstance(fmtToAdopt, new SuperscriptStyle(), status);
- }
- ScientificNumberFormatter *ScientificNumberFormatter::createSuperscriptInstance(
- const Locale &locale, UErrorCode &status) {
- return createInstance(
- static_cast<DecimalFormat *>(
- DecimalFormat::createScientificInstance(locale, status)),
- new SuperscriptStyle(),
- status);
- }
- ScientificNumberFormatter *ScientificNumberFormatter::createMarkupInstance(
- DecimalFormat *fmtToAdopt,
- const UnicodeString &beginMarkup,
- const UnicodeString &endMarkup,
- UErrorCode &status) {
- return createInstance(
- fmtToAdopt,
- new MarkupStyle(beginMarkup, endMarkup),
- status);
- }
- ScientificNumberFormatter *ScientificNumberFormatter::createMarkupInstance(
- const Locale &locale,
- const UnicodeString &beginMarkup,
- const UnicodeString &endMarkup,
- UErrorCode &status) {
- return createInstance(
- static_cast<DecimalFormat *>(
- DecimalFormat::createScientificInstance(locale, status)),
- new MarkupStyle(beginMarkup, endMarkup),
- status);
- }
- ScientificNumberFormatter *ScientificNumberFormatter::createInstance(
- DecimalFormat *fmtToAdopt,
- Style *styleToAdopt,
- UErrorCode &status) {
- LocalPointer<DecimalFormat> fmt(fmtToAdopt);
- LocalPointer<Style> style(styleToAdopt);
- if (U_FAILURE(status)) {
- return nullptr;
- }
- ScientificNumberFormatter *result =
- new ScientificNumberFormatter(
- fmt.getAlias(),
- style.getAlias(),
- status);
- if (result == nullptr) {
- status = U_MEMORY_ALLOCATION_ERROR;
- return nullptr;
- }
- fmt.orphan();
- style.orphan();
- if (U_FAILURE(status)) {
- delete result;
- return nullptr;
- }
- return result;
- }
- ScientificNumberFormatter::SuperscriptStyle *ScientificNumberFormatter::SuperscriptStyle::clone() const {
- return new ScientificNumberFormatter::SuperscriptStyle(*this);
- }
- UnicodeString &ScientificNumberFormatter::SuperscriptStyle::format(
- const UnicodeString &original,
- FieldPositionIterator &fpi,
- const UnicodeString &preExponent,
- UnicodeString &appendTo,
- UErrorCode &status) const {
- if (U_FAILURE(status)) {
- return appendTo;
- }
- FieldPosition fp;
- int32_t copyFromOffset = 0;
- while (fpi.next(fp)) {
- switch (fp.getField()) {
- case UNUM_EXPONENT_SYMBOL_FIELD:
- appendTo.append(
- original,
- copyFromOffset,
- fp.getBeginIndex() - copyFromOffset);
- copyFromOffset = fp.getEndIndex();
- appendTo.append(preExponent);
- break;
- case UNUM_EXPONENT_SIGN_FIELD:
- {
- using namespace icu::numparse::impl;
- int32_t beginIndex = fp.getBeginIndex();
- int32_t endIndex = fp.getEndIndex();
- UChar32 aChar = original.char32At(beginIndex);
- if (unisets::get(unisets::MINUS_SIGN)->contains(aChar)) {
- appendTo.append(
- original,
- copyFromOffset,
- beginIndex - copyFromOffset);
- appendTo.append(kSuperscriptMinusSign);
- } else if (unisets::get(unisets::PLUS_SIGN)->contains(aChar)) {
- appendTo.append(
- original,
- copyFromOffset,
- beginIndex - copyFromOffset);
- appendTo.append(kSuperscriptPlusSign);
- } else {
- status = U_INVALID_CHAR_FOUND;
- return appendTo;
- }
- copyFromOffset = endIndex;
- }
- break;
- case UNUM_EXPONENT_FIELD:
- appendTo.append(
- original,
- copyFromOffset,
- fp.getBeginIndex() - copyFromOffset);
- if (!copyAsSuperscript(
- original,
- fp.getBeginIndex(),
- fp.getEndIndex(),
- appendTo,
- status)) {
- return appendTo;
- }
- copyFromOffset = fp.getEndIndex();
- break;
- default:
- break;
- }
- }
- appendTo.append(
- original, copyFromOffset, original.length() - copyFromOffset);
- return appendTo;
- }
- ScientificNumberFormatter::MarkupStyle *ScientificNumberFormatter::MarkupStyle::clone() const {
- return new ScientificNumberFormatter::MarkupStyle(*this);
- }
- UnicodeString &ScientificNumberFormatter::MarkupStyle::format(
- const UnicodeString &original,
- FieldPositionIterator &fpi,
- const UnicodeString &preExponent,
- UnicodeString &appendTo,
- UErrorCode &status) const {
- if (U_FAILURE(status)) {
- return appendTo;
- }
- FieldPosition fp;
- int32_t copyFromOffset = 0;
- while (fpi.next(fp)) {
- switch (fp.getField()) {
- case UNUM_EXPONENT_SYMBOL_FIELD:
- appendTo.append(
- original,
- copyFromOffset,
- fp.getBeginIndex() - copyFromOffset);
- copyFromOffset = fp.getEndIndex();
- appendTo.append(preExponent);
- appendTo.append(fBeginMarkup);
- break;
- case UNUM_EXPONENT_FIELD:
- appendTo.append(
- original,
- copyFromOffset,
- fp.getEndIndex() - copyFromOffset);
- copyFromOffset = fp.getEndIndex();
- appendTo.append(fEndMarkup);
- break;
- default:
- break;
- }
- }
- appendTo.append(
- original, copyFromOffset, original.length() - copyFromOffset);
- return appendTo;
- }
- ScientificNumberFormatter::ScientificNumberFormatter(
- DecimalFormat *fmtToAdopt, Style *styleToAdopt, UErrorCode &status)
- : fPreExponent(),
- fDecimalFormat(fmtToAdopt),
- fStyle(styleToAdopt) {
- if (U_FAILURE(status)) {
- return;
- }
- if (fDecimalFormat == nullptr || fStyle == nullptr) {
- status = U_ILLEGAL_ARGUMENT_ERROR;
- return;
- }
- const DecimalFormatSymbols *sym = fDecimalFormat->getDecimalFormatSymbols();
- if (sym == nullptr) {
- status = U_ILLEGAL_ARGUMENT_ERROR;
- return;
- }
- getPreExponent(*sym, fPreExponent);
- }
- ScientificNumberFormatter::ScientificNumberFormatter(
- const ScientificNumberFormatter &other)
- : UObject(other),
- fPreExponent(other.fPreExponent),
- fDecimalFormat(nullptr),
- fStyle(nullptr) {
- fDecimalFormat = static_cast<DecimalFormat *>(
- other.fDecimalFormat->clone());
- fStyle = other.fStyle->clone();
- }
- ScientificNumberFormatter::~ScientificNumberFormatter() {
- delete fDecimalFormat;
- delete fStyle;
- }
- UnicodeString &ScientificNumberFormatter::format(
- const Formattable &number,
- UnicodeString &appendTo,
- UErrorCode &status) const {
- if (U_FAILURE(status)) {
- return appendTo;
- }
- UnicodeString original;
- FieldPositionIterator fpi;
- fDecimalFormat->format(number, original, &fpi, status);
- return fStyle->format(
- original,
- fpi,
- fPreExponent,
- appendTo,
- status);
- }
- void ScientificNumberFormatter::getPreExponent(
- const DecimalFormatSymbols &dfs, UnicodeString &preExponent) {
- preExponent.append(dfs.getConstSymbol(
- DecimalFormatSymbols::kExponentMultiplicationSymbol));
- preExponent.append(dfs.getConstSymbol(DecimalFormatSymbols::kOneDigitSymbol));
- preExponent.append(dfs.getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol));
- }
- U_NAMESPACE_END
- #endif /* !UCONFIG_NO_FORMATTING */
|