2017-09-27 00:25:20 +00:00
|
|
|
// © 2017 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
|
2017-10-04 22:51:06 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2017-10-05 00:47:38 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING && !UPRV_INCOMPLETE_CPP11_SUPPORT
|
2017-09-27 05:31:57 +00:00
|
|
|
#ifndef __NUMBER_UTILS_H__
|
|
|
|
#define __NUMBER_UTILS_H__
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
#include "unicode/numberformatter.h"
|
|
|
|
#include "number_types.h"
|
|
|
|
#include "number_decimalquantity.h"
|
|
|
|
#include "number_scientific.h"
|
|
|
|
#include "number_patternstring.h"
|
|
|
|
#include "number_modifiers.h"
|
2018-03-15 10:08:26 +00:00
|
|
|
#include "number_multiplier.h"
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN namespace number {
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
class UnicodeStringCharSequence : public CharSequence {
|
|
|
|
public:
|
2018-02-08 08:49:50 +00:00
|
|
|
explicit UnicodeStringCharSequence(const UnicodeString& other) {
|
2017-09-27 00:25:20 +00:00
|
|
|
fStr = other;
|
|
|
|
}
|
|
|
|
|
2017-10-04 01:23:58 +00:00
|
|
|
~UnicodeStringCharSequence() U_OVERRIDE = default;
|
2017-09-27 00:25:20 +00:00
|
|
|
|
2017-10-04 01:23:58 +00:00
|
|
|
int32_t length() const U_OVERRIDE {
|
2017-09-27 00:25:20 +00:00
|
|
|
return fStr.length();
|
|
|
|
}
|
|
|
|
|
2017-10-04 01:23:58 +00:00
|
|
|
char16_t charAt(int32_t index) const U_OVERRIDE {
|
2017-09-27 00:25:20 +00:00
|
|
|
return fStr.charAt(index);
|
|
|
|
}
|
|
|
|
|
2017-10-04 01:23:58 +00:00
|
|
|
UChar32 codePointAt(int32_t index) const U_OVERRIDE {
|
2017-09-27 00:25:20 +00:00
|
|
|
return fStr.char32At(index);
|
|
|
|
}
|
|
|
|
|
2017-10-04 01:23:58 +00:00
|
|
|
UnicodeString toUnicodeString() const U_OVERRIDE {
|
2017-09-27 00:25:20 +00:00
|
|
|
// Allocate a UnicodeString of the correct length
|
|
|
|
UnicodeString output(length(), 0, -1);
|
|
|
|
for (int32_t i = 0; i < length(); i++) {
|
|
|
|
output.append(charAt(i));
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
UnicodeString fStr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MicroProps : public MicroPropsGenerator {
|
|
|
|
|
|
|
|
// NOTE: All of these fields are properly initialized in NumberFormatterImpl.
|
|
|
|
Rounder rounding;
|
|
|
|
Grouper grouping;
|
|
|
|
Padder padding;
|
|
|
|
IntegerWidth integerWidth;
|
|
|
|
UNumberSignDisplay sign;
|
|
|
|
UNumberDecimalSeparatorDisplay decimal;
|
|
|
|
bool useCurrency;
|
|
|
|
|
|
|
|
// Note: This struct has no direct ownership of the following pointers.
|
2018-02-08 08:49:50 +00:00
|
|
|
const DecimalFormatSymbols* symbols;
|
|
|
|
const Modifier* modOuter;
|
|
|
|
const Modifier* modMiddle;
|
|
|
|
const Modifier* modInner;
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
// The following "helper" fields may optionally be used during the MicroPropsGenerator.
|
|
|
|
// They live here to retain memory.
|
|
|
|
struct {
|
|
|
|
ScientificModifier scientificModifier;
|
|
|
|
EmptyModifier emptyWeakModifier{false};
|
|
|
|
EmptyModifier emptyStrongModifier{true};
|
2018-03-15 10:08:26 +00:00
|
|
|
MultiplierChain multiplier;
|
2017-09-27 00:25:20 +00:00
|
|
|
} helpers;
|
|
|
|
|
|
|
|
|
|
|
|
MicroProps() = default;
|
|
|
|
|
2018-02-08 08:49:50 +00:00
|
|
|
MicroProps(const MicroProps& other) = default;
|
2017-09-27 00:25:20 +00:00
|
|
|
|
2018-02-08 08:49:50 +00:00
|
|
|
MicroProps& operator=(const MicroProps& other) = default;
|
2017-09-27 00:25:20 +00:00
|
|
|
|
2018-02-08 08:49:50 +00:00
|
|
|
void processQuantity(DecimalQuantity&, MicroProps& micros, UErrorCode& status) const U_OVERRIDE {
|
|
|
|
(void) status;
|
2017-09-27 00:25:20 +00:00
|
|
|
if (this == µs) {
|
|
|
|
// Unsafe path: no need to perform a copy.
|
|
|
|
U_ASSERT(!exhausted);
|
|
|
|
micros.exhausted = true;
|
|
|
|
U_ASSERT(exhausted);
|
|
|
|
} else {
|
|
|
|
// Safe path: copy self into the output micros.
|
|
|
|
micros = *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Internal fields:
|
|
|
|
bool exhausted = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This struct provides the result of the number formatting pipeline to FormattedNumber.
|
|
|
|
*
|
|
|
|
* The DecimalQuantity is not currently being used by FormattedNumber, but at some point it could be used
|
|
|
|
* to add a toDecNumber() or similar method.
|
|
|
|
*/
|
|
|
|
struct NumberFormatterResults : public UMemory {
|
|
|
|
DecimalQuantity quantity;
|
|
|
|
NumberStringBuilder string;
|
|
|
|
};
|
|
|
|
|
2018-02-08 08:49:50 +00:00
|
|
|
inline int32_t insertDigitFromSymbols(NumberStringBuilder& output, int32_t index, int8_t digit,
|
|
|
|
const DecimalFormatSymbols& symbols, Field field,
|
|
|
|
UErrorCode& status) {
|
|
|
|
if (symbols.getCodePointZero() != -1) {
|
|
|
|
return output.insertCodePoint(index, symbols.getCodePointZero() + digit, field, status);
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
2018-02-08 08:49:50 +00:00
|
|
|
return output.insert(index, symbols.getConstDigitSymbol(digit), field, status);
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace impl
|
|
|
|
} // namespace number
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2017-09-27 05:31:57 +00:00
|
|
|
#endif //__NUMBER_UTILS_H__
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|