]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/number_scientific.cpp
ICU-62107.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / number_scientific.cpp
CommitLineData
0f5d89e8
A
1// © 2017 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3
4#include "unicode/utypes.h"
5
6#if !UCONFIG_NO_FORMATTING
7
8#include <cstdlib>
9#include "number_scientific.h"
10#include "number_utils.h"
11#include "number_stringbuilder.h"
12#include "unicode/unum.h"
13#include "number_microprops.h"
14
15using namespace icu;
16using namespace icu::number;
17using namespace icu::number::impl;
18
19// NOTE: The object lifecycle of ScientificModifier and ScientificHandler differ greatly in Java and C++.
20//
21// During formatting, we need to provide an object with state (the exponent) as the inner modifier.
22//
23// In Java, where the priority is put on reducing object creations, the unsafe code path re-uses the
24// ScientificHandler as a ScientificModifier, and the safe code path pre-computes 25 ScientificModifier
25// instances. This scheme reduces the number of object creations by 1 in both safe and unsafe.
26//
27// In C++, MicroProps provides a pre-allocated ScientificModifier, and ScientificHandler simply populates
28// the state (the exponent) into that ScientificModifier. There is no difference between safe and unsafe.
29
30ScientificModifier::ScientificModifier() : fExponent(0), fHandler(nullptr) {}
31
32void ScientificModifier::set(int32_t exponent, const ScientificHandler *handler) {
33 // ScientificModifier should be set only once.
34 U_ASSERT(fHandler == nullptr);
35 fExponent = exponent;
36 fHandler = handler;
37}
38
39int32_t ScientificModifier::apply(NumberStringBuilder &output, int32_t /*leftIndex*/, int32_t rightIndex,
40 UErrorCode &status) const {
41 // FIXME: Localized exponent separator location.
42 int i = rightIndex;
43 // Append the exponent separator and sign
44 i += output.insert(
45 i,
46 fHandler->fSymbols->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kExponentialSymbol),
47 UNUM_EXPONENT_SYMBOL_FIELD,
48 status);
49 if (fExponent < 0 && fHandler->fSettings.fExponentSignDisplay != UNUM_SIGN_NEVER) {
50 i += output.insert(
51 i,
52 fHandler->fSymbols
53 ->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kMinusSignSymbol),
54 UNUM_EXPONENT_SIGN_FIELD,
55 status);
56 } else if (fExponent >= 0 && fHandler->fSettings.fExponentSignDisplay == UNUM_SIGN_ALWAYS) {
57 i += output.insert(
58 i,
59 fHandler->fSymbols
60 ->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kPlusSignSymbol),
61 UNUM_EXPONENT_SIGN_FIELD,
62 status);
63 }
64 // Append the exponent digits (using a simple inline algorithm)
65 int32_t disp = std::abs(fExponent);
66 for (int j = 0; j < fHandler->fSettings.fMinExponentDigits || disp > 0; j++, disp /= 10) {
67 auto d = static_cast<int8_t>(disp % 10);
68 i += utils::insertDigitFromSymbols(
69 output,
70 i - j,
71 d,
72 *fHandler->fSymbols,
73 UNUM_EXPONENT_FIELD,
74 status);
75 }
76 return i - rightIndex;
77}
78
79int32_t ScientificModifier::getPrefixLength(UErrorCode &status) const {
80 (void)status;
81 // TODO: Localized exponent separator location.
82 return 0;
83}
84
85int32_t ScientificModifier::getCodePointCount(UErrorCode &status) const {
86 (void)status;
87 // This method is not used for strong modifiers.
88 U_ASSERT(false);
89 return 0;
90}
91
92bool ScientificModifier::isStrong() const {
93 // Scientific is always strong
94 return true;
95}
96
97// Note: Visual Studio does not compile this function without full name space. Why?
98icu::number::impl::ScientificHandler::ScientificHandler(const Notation *notation, const DecimalFormatSymbols *symbols,
99 const MicroPropsGenerator *parent) :
100 fSettings(notation->fUnion.scientific), fSymbols(symbols), fParent(parent) {}
101
102void ScientificHandler::processQuantity(DecimalQuantity &quantity, MicroProps &micros,
103 UErrorCode &status) const {
104 fParent->processQuantity(quantity, micros, status);
105 if (U_FAILURE(status)) { return; }
106
107 // Treat zero as if it had magnitude 0
108 int32_t exponent;
109 if (quantity.isZero()) {
110 if (fSettings.fRequireMinInt && micros.rounder.isSignificantDigits()) {
111 // Show "00.000E0" on pattern "00.000E0"
112 micros.rounder.apply(quantity, fSettings.fEngineeringInterval, status);
113 exponent = 0;
114 } else {
115 micros.rounder.apply(quantity, status);
116 exponent = 0;
117 }
118 } else {
119 exponent = -micros.rounder.chooseMultiplierAndApply(quantity, *this, status);
120 }
121
122 // Use MicroProps's helper ScientificModifier and save it as the modInner.
123 ScientificModifier &mod = micros.helpers.scientificModifier;
124 mod.set(exponent, this);
125 micros.modInner = &mod;
126
127 // We already performed rounding. Do not perform it again.
128 micros.rounder = RoundingImpl::passThrough();
129}
130
131int32_t ScientificHandler::getMultiplier(int32_t magnitude) const {
132 int32_t interval = fSettings.fEngineeringInterval;
133 int32_t digitsShown;
134 if (fSettings.fRequireMinInt) {
135 // For patterns like "000.00E0" and ".00E0"
136 digitsShown = interval;
137 } else if (interval <= 1) {
138 // For patterns like "0.00E0" and "@@@E0"
139 digitsShown = 1;
140 } else {
141 // For patterns like "##0.00"
142 digitsShown = ((magnitude % interval + interval) % interval) + 1;
143 }
144 return digitsShown - magnitude - 1;
145}
146
147#endif /* #if !UCONFIG_NO_FORMATTING */