Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 1 | // © 2020 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 "number_usageprefs.h" |
| 9 | #include "cstring.h" |
| 10 | #include "number_decimalquantity.h" |
| 11 | #include "number_microprops.h" |
| 12 | #include "number_roundingutils.h" |
| 13 | #include "number_skeletons.h" |
| 14 | #include "unicode/char16ptr.h" |
| 15 | #include "unicode/currunit.h" |
| 16 | #include "unicode/fmtable.h" |
| 17 | #include "unicode/measure.h" |
| 18 | #include "unicode/numberformatter.h" |
| 19 | #include "unicode/platform.h" |
| 20 | #include "unicode/unum.h" |
| 21 | #include "unicode/urename.h" |
| 22 | #include "units_data.h" |
| 23 | |
| 24 | using namespace icu; |
| 25 | using namespace icu::number; |
| 26 | using namespace icu::number::impl; |
| 27 | using icu::StringSegment; |
| 28 | using icu::units::ConversionRates; |
| 29 | |
| 30 | // Copy constructor |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 31 | StringProp::StringProp(const StringProp &other) : StringProp() { |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 32 | this->operator=(other); |
| 33 | } |
| 34 | |
| 35 | // Copy assignment operator |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 36 | StringProp &StringProp::operator=(const StringProp &other) { |
| 37 | if (this == &other) { return *this; } // self-assignment: no-op |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 38 | fLength = 0; |
| 39 | fError = other.fError; |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 40 | if (fValue != nullptr) { |
| 41 | uprv_free(fValue); |
| 42 | fValue = nullptr; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 43 | } |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 44 | if (other.fValue == nullptr) { |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 45 | return *this; |
| 46 | } |
| 47 | if (U_FAILURE(other.fError)) { |
| 48 | // We don't bother trying to allocating memory if we're in any case busy |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 49 | // copying an errored StringProp. |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 50 | return *this; |
| 51 | } |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 52 | fValue = (char *)uprv_malloc(other.fLength + 1); |
| 53 | if (fValue == nullptr) { |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 54 | fError = U_MEMORY_ALLOCATION_ERROR; |
| 55 | return *this; |
| 56 | } |
| 57 | fLength = other.fLength; |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 58 | uprv_strncpy(fValue, other.fValue, fLength + 1); |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 59 | return *this; |
| 60 | } |
| 61 | |
| 62 | // Move constructor |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 63 | StringProp::StringProp(StringProp &&src) U_NOEXCEPT : fValue(src.fValue), |
| 64 | fLength(src.fLength), |
| 65 | fError(src.fError) { |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 66 | // Take ownership away from src if necessary |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 67 | src.fValue = nullptr; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // Move assignment operator |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 71 | StringProp &StringProp::operator=(StringProp &&src) U_NOEXCEPT { |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 72 | if (this == &src) { |
| 73 | return *this; |
| 74 | } |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 75 | if (fValue != nullptr) { |
| 76 | uprv_free(fValue); |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 77 | } |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 78 | fValue = src.fValue; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 79 | fLength = src.fLength; |
| 80 | fError = src.fError; |
| 81 | // Take ownership away from src if necessary |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 82 | src.fValue = nullptr; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 83 | return *this; |
| 84 | } |
| 85 | |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 86 | StringProp::~StringProp() { |
| 87 | if (fValue != nullptr) { |
| 88 | uprv_free(fValue); |
| 89 | fValue = nullptr; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 93 | void StringProp::set(StringPiece value) { |
| 94 | if (fValue != nullptr) { |
| 95 | uprv_free(fValue); |
| 96 | fValue = nullptr; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 97 | } |
| 98 | fLength = value.length(); |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 99 | fValue = (char *)uprv_malloc(fLength + 1); |
| 100 | if (fValue == nullptr) { |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 101 | fLength = 0; |
| 102 | fError = U_MEMORY_ALLOCATION_ERROR; |
| 103 | return; |
| 104 | } |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 105 | uprv_strncpy(fValue, value.data(), fLength); |
| 106 | fValue[fLength] = 0; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Populates micros.mixedMeasures and modifies quantity, based on the values in |
| 110 | // measures. |
| 111 | void mixedMeasuresToMicros(const MaybeStackVector<Measure> &measures, DecimalQuantity *quantity, |
| 112 | MicroProps *micros, UErrorCode status) { |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 113 | micros->mixedMeasuresCount = measures.length(); |
| 114 | |
| 115 | if (micros->mixedMeasures.getCapacity() < micros->mixedMeasuresCount) { |
| 116 | if (micros->mixedMeasures.resize(micros->mixedMeasuresCount) == nullptr) { |
| 117 | status = U_MEMORY_ALLOCATION_ERROR; |
| 118 | return; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 119 | } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 120 | } |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 121 | |
| 122 | for (int32_t i = 0; i < micros->mixedMeasuresCount; i++) { |
| 123 | switch (measures[i]->getNumber().getType()) { |
| 124 | case Formattable::kInt64: |
| 125 | micros->mixedMeasures[i] = measures[i]->getNumber().getInt64(); |
| 126 | break; |
| 127 | |
| 128 | case Formattable::kDouble: |
| 129 | U_ASSERT(micros->indexOfQuantity < 0); |
| 130 | quantity->setToDouble(measures[i]->getNumber().getDouble()); |
| 131 | micros->indexOfQuantity = i; |
| 132 | break; |
| 133 | |
| 134 | default: |
| 135 | U_ASSERT(0 == "Found a Measure Number which is neither a double nor an int"); |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 136 | UPRV_UNREACHABLE_EXIT; |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 137 | break; |
| 138 | } |
| 139 | |
| 140 | if (U_FAILURE(status)) { |
| 141 | return; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (micros->indexOfQuantity < 0) { |
| 146 | // There is no quantity. |
| 147 | status = U_INTERNAL_PROGRAM_ERROR; |
| 148 | } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | UsagePrefsHandler::UsagePrefsHandler(const Locale &locale, |
| 152 | const MeasureUnit &inputUnit, |
| 153 | const StringPiece usage, |
| 154 | const MicroPropsGenerator *parent, |
| 155 | UErrorCode &status) |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 156 | : fUnitsRouter(inputUnit, locale, usage, status), |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 157 | fParent(parent) { |
| 158 | } |
| 159 | |
| 160 | void UsagePrefsHandler::processQuantity(DecimalQuantity &quantity, MicroProps µs, |
| 161 | UErrorCode &status) const { |
| 162 | fParent->processQuantity(quantity, micros, status); |
| 163 | if (U_FAILURE(status)) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | quantity.roundToInfinity(); // Enables toDouble |
| 168 | const units::RouteResult routed = fUnitsRouter.route(quantity.toDouble(), µs.rounder, status); |
| 169 | if (U_FAILURE(status)) { |
| 170 | return; |
| 171 | } |
| 172 | const MaybeStackVector<Measure>& routedMeasures = routed.measures; |
| 173 | micros.outputUnit = routed.outputUnit.copy(status).build(status); |
| 174 | if (U_FAILURE(status)) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | mixedMeasuresToMicros(routedMeasures, &quantity, µs, status); |
| 179 | } |
| 180 | |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 181 | UnitConversionHandler::UnitConversionHandler(const MeasureUnit &targetUnit, |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 182 | const MicroPropsGenerator *parent, UErrorCode &status) |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 183 | : fOutputUnit(targetUnit), fParent(parent) { |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 184 | MeasureUnitImpl tempInput, tempOutput; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 185 | |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 186 | ConversionRates conversionRates(status); |
| 187 | if (U_FAILURE(status)) { |
| 188 | return; |
| 189 | } |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 190 | |
| 191 | const MeasureUnitImpl &targetUnitImpl = |
| 192 | MeasureUnitImpl::forMeasureUnit(targetUnit, tempOutput, status); |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 193 | fUnitConverter.adoptInsteadAndCheckErrorCode( |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 194 | new ComplexUnitsConverter(targetUnitImpl, conversionRates, status), status); |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void UnitConversionHandler::processQuantity(DecimalQuantity &quantity, MicroProps µs, |
| 198 | UErrorCode &status) const { |
| 199 | fParent->processQuantity(quantity, micros, status); |
| 200 | if (U_FAILURE(status)) { |
| 201 | return; |
| 202 | } |
| 203 | quantity.roundToInfinity(); // Enables toDouble |
| 204 | MaybeStackVector<Measure> measures = |
| 205 | fUnitConverter->convert(quantity.toDouble(), µs.rounder, status); |
| 206 | micros.outputUnit = fOutputUnit; |
| 207 | if (U_FAILURE(status)) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | mixedMeasuresToMicros(measures, &quantity, µs, status); |
| 212 | } |
| 213 | |
| 214 | #endif /* #if !UCONFIG_NO_FORMATTING */ |