Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 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 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 6 | #if !UCONFIG_NO_FORMATTING |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 7 | |
| 8 | #include "uassert.h" |
| 9 | #include "unicode/numberformatter.h" |
| 10 | #include "number_decimalquantity.h" |
| 11 | #include "number_formatimpl.h" |
| 12 | #include "umutex.h" |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 13 | #include "number_asformat.h" |
| 14 | #include "number_skeletons.h" |
| 15 | #include "number_utils.h" |
| 16 | #include "number_utypes.h" |
| 17 | #include "util.h" |
| 18 | #include "fphdlimp.h" |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 19 | |
| 20 | using namespace icu; |
| 21 | using namespace icu::number; |
| 22 | using namespace icu::number::impl; |
| 23 | |
| 24 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 25 | Derived NumberFormatterSettings<Derived>::notation(const Notation& notation) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 26 | Derived copy(*this); |
| 27 | // NOTE: Slicing is OK. |
| 28 | copy.fMacros.notation = notation; |
| 29 | return copy; |
| 30 | } |
| 31 | |
| 32 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 33 | Derived NumberFormatterSettings<Derived>::notation(const Notation& notation)&& { |
| 34 | Derived move(std::move(*this)); |
| 35 | // NOTE: Slicing is OK. |
| 36 | move.fMacros.notation = notation; |
| 37 | return move; |
| 38 | } |
| 39 | |
| 40 | template<typename Derived> |
| 41 | Derived NumberFormatterSettings<Derived>::unit(const icu::MeasureUnit& unit) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 42 | Derived copy(*this); |
| 43 | // NOTE: Slicing occurs here. However, CurrencyUnit can be restored from MeasureUnit. |
| 44 | // TimeUnit may be affected, but TimeUnit is not as relevant to number formatting. |
| 45 | copy.fMacros.unit = unit; |
| 46 | return copy; |
| 47 | } |
| 48 | |
| 49 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 50 | Derived NumberFormatterSettings<Derived>::unit(const icu::MeasureUnit& unit)&& { |
| 51 | Derived move(std::move(*this)); |
| 52 | // See comments above about slicing. |
| 53 | move.fMacros.unit = unit; |
| 54 | return move; |
| 55 | } |
| 56 | |
| 57 | template<typename Derived> |
| 58 | Derived NumberFormatterSettings<Derived>::adoptUnit(icu::MeasureUnit* unit) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 59 | Derived copy(*this); |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 60 | // Just move the unit into the MacroProps by value, and delete it since we have ownership. |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 61 | // NOTE: Slicing occurs here. However, CurrencyUnit can be restored from MeasureUnit. |
| 62 | // TimeUnit may be affected, but TimeUnit is not as relevant to number formatting. |
| 63 | if (unit != nullptr) { |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 64 | // TODO: On nullptr, reset to default value? |
| 65 | copy.fMacros.unit = std::move(*unit); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 66 | delete unit; |
| 67 | } |
| 68 | return copy; |
| 69 | } |
| 70 | |
| 71 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 72 | Derived NumberFormatterSettings<Derived>::adoptUnit(icu::MeasureUnit* unit)&& { |
| 73 | Derived move(std::move(*this)); |
| 74 | // See comments above about slicing and ownership. |
| 75 | if (unit != nullptr) { |
| 76 | // TODO: On nullptr, reset to default value? |
| 77 | move.fMacros.unit = std::move(*unit); |
| 78 | delete unit; |
| 79 | } |
| 80 | return move; |
| 81 | } |
| 82 | |
| 83 | template<typename Derived> |
| 84 | Derived NumberFormatterSettings<Derived>::perUnit(const icu::MeasureUnit& perUnit) const& { |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 85 | Derived copy(*this); |
| 86 | // See comments above about slicing. |
| 87 | copy.fMacros.perUnit = perUnit; |
| 88 | return copy; |
| 89 | } |
| 90 | |
| 91 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 92 | Derived NumberFormatterSettings<Derived>::perUnit(const icu::MeasureUnit& perUnit)&& { |
| 93 | Derived move(std::move(*this)); |
| 94 | // See comments above about slicing. |
| 95 | move.fMacros.perUnit = perUnit; |
| 96 | return move; |
| 97 | } |
| 98 | |
| 99 | template<typename Derived> |
| 100 | Derived NumberFormatterSettings<Derived>::adoptPerUnit(icu::MeasureUnit* perUnit) const& { |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 101 | Derived copy(*this); |
| 102 | // See comments above about slicing and ownership. |
| 103 | if (perUnit != nullptr) { |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 104 | // TODO: On nullptr, reset to default value? |
| 105 | copy.fMacros.perUnit = std::move(*perUnit); |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 106 | delete perUnit; |
| 107 | } |
| 108 | return copy; |
| 109 | } |
| 110 | |
| 111 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 112 | Derived NumberFormatterSettings<Derived>::adoptPerUnit(icu::MeasureUnit* perUnit)&& { |
| 113 | Derived move(std::move(*this)); |
| 114 | // See comments above about slicing and ownership. |
| 115 | if (perUnit != nullptr) { |
| 116 | // TODO: On nullptr, reset to default value? |
| 117 | move.fMacros.perUnit = std::move(*perUnit); |
| 118 | delete perUnit; |
| 119 | } |
| 120 | return move; |
| 121 | } |
| 122 | |
| 123 | template<typename Derived> |
| 124 | Derived NumberFormatterSettings<Derived>::precision(const Precision& precision) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 125 | Derived copy(*this); |
| 126 | // NOTE: Slicing is OK. |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 127 | copy.fMacros.precision = precision; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 128 | return copy; |
| 129 | } |
| 130 | |
| 131 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 132 | Derived NumberFormatterSettings<Derived>::precision(const Precision& precision)&& { |
| 133 | Derived move(std::move(*this)); |
| 134 | // NOTE: Slicing is OK. |
| 135 | move.fMacros.precision = precision; |
| 136 | return move; |
| 137 | } |
| 138 | |
| 139 | template<typename Derived> |
| 140 | Derived NumberFormatterSettings<Derived>::roundingMode(UNumberFormatRoundingMode roundingMode) const& { |
| 141 | Derived copy(*this); |
| 142 | copy.fMacros.roundingMode = roundingMode; |
| 143 | return copy; |
| 144 | } |
| 145 | |
| 146 | template<typename Derived> |
| 147 | Derived NumberFormatterSettings<Derived>::roundingMode(UNumberFormatRoundingMode roundingMode)&& { |
| 148 | Derived move(std::move(*this)); |
| 149 | move.fMacros.roundingMode = roundingMode; |
| 150 | return move; |
| 151 | } |
| 152 | |
| 153 | template<typename Derived> |
| 154 | Derived NumberFormatterSettings<Derived>::grouping(UGroupingStrategy strategy) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 155 | Derived copy(*this); |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 156 | // NOTE: This is slightly different than how the setting is stored in Java |
| 157 | // because we want to put it on the stack. |
| 158 | copy.fMacros.grouper = Grouper::forStrategy(strategy); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 159 | return copy; |
| 160 | } |
| 161 | |
| 162 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 163 | Derived NumberFormatterSettings<Derived>::grouping(UGroupingStrategy strategy)&& { |
| 164 | Derived move(std::move(*this)); |
| 165 | move.fMacros.grouper = Grouper::forStrategy(strategy); |
| 166 | return move; |
| 167 | } |
| 168 | |
| 169 | template<typename Derived> |
| 170 | Derived NumberFormatterSettings<Derived>::integerWidth(const IntegerWidth& style) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 171 | Derived copy(*this); |
| 172 | copy.fMacros.integerWidth = style; |
| 173 | return copy; |
| 174 | } |
| 175 | |
| 176 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 177 | Derived NumberFormatterSettings<Derived>::integerWidth(const IntegerWidth& style)&& { |
| 178 | Derived move(std::move(*this)); |
| 179 | move.fMacros.integerWidth = style; |
| 180 | return move; |
| 181 | } |
| 182 | |
| 183 | template<typename Derived> |
| 184 | Derived NumberFormatterSettings<Derived>::symbols(const DecimalFormatSymbols& symbols) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 185 | Derived copy(*this); |
| 186 | copy.fMacros.symbols.setTo(symbols); |
| 187 | return copy; |
| 188 | } |
| 189 | |
| 190 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 191 | Derived NumberFormatterSettings<Derived>::symbols(const DecimalFormatSymbols& symbols)&& { |
| 192 | Derived move(std::move(*this)); |
| 193 | move.fMacros.symbols.setTo(symbols); |
| 194 | return move; |
| 195 | } |
| 196 | |
| 197 | template<typename Derived> |
| 198 | Derived NumberFormatterSettings<Derived>::adoptSymbols(NumberingSystem* ns) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 199 | Derived copy(*this); |
| 200 | copy.fMacros.symbols.setTo(ns); |
| 201 | return copy; |
| 202 | } |
| 203 | |
| 204 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 205 | Derived NumberFormatterSettings<Derived>::adoptSymbols(NumberingSystem* ns)&& { |
| 206 | Derived move(std::move(*this)); |
| 207 | move.fMacros.symbols.setTo(ns); |
| 208 | return move; |
| 209 | } |
| 210 | |
| 211 | template<typename Derived> |
| 212 | Derived NumberFormatterSettings<Derived>::unitWidth(UNumberUnitWidth width) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 213 | Derived copy(*this); |
| 214 | copy.fMacros.unitWidth = width; |
| 215 | return copy; |
| 216 | } |
| 217 | |
| 218 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 219 | Derived NumberFormatterSettings<Derived>::unitWidth(UNumberUnitWidth width)&& { |
| 220 | Derived move(std::move(*this)); |
| 221 | move.fMacros.unitWidth = width; |
| 222 | return move; |
| 223 | } |
| 224 | |
| 225 | template<typename Derived> |
| 226 | Derived NumberFormatterSettings<Derived>::sign(UNumberSignDisplay style) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 227 | Derived copy(*this); |
| 228 | copy.fMacros.sign = style; |
| 229 | return copy; |
| 230 | } |
| 231 | |
| 232 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 233 | Derived NumberFormatterSettings<Derived>::sign(UNumberSignDisplay style)&& { |
| 234 | Derived move(std::move(*this)); |
| 235 | move.fMacros.sign = style; |
| 236 | return move; |
| 237 | } |
| 238 | |
| 239 | template<typename Derived> |
| 240 | Derived NumberFormatterSettings<Derived>::decimal(UNumberDecimalSeparatorDisplay style) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 241 | Derived copy(*this); |
| 242 | copy.fMacros.decimal = style; |
| 243 | return copy; |
| 244 | } |
| 245 | |
| 246 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 247 | Derived NumberFormatterSettings<Derived>::decimal(UNumberDecimalSeparatorDisplay style)&& { |
| 248 | Derived move(std::move(*this)); |
| 249 | move.fMacros.decimal = style; |
| 250 | return move; |
| 251 | } |
| 252 | |
| 253 | template<typename Derived> |
| 254 | Derived NumberFormatterSettings<Derived>::scale(const Scale& scale) const& { |
| 255 | Derived copy(*this); |
| 256 | copy.fMacros.scale = scale; |
| 257 | return copy; |
| 258 | } |
| 259 | |
| 260 | template<typename Derived> |
| 261 | Derived NumberFormatterSettings<Derived>::scale(const Scale& scale)&& { |
| 262 | Derived move(std::move(*this)); |
| 263 | move.fMacros.scale = scale; |
| 264 | return move; |
| 265 | } |
| 266 | |
| 267 | template<typename Derived> |
| 268 | Derived NumberFormatterSettings<Derived>::padding(const Padder& padder) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 269 | Derived copy(*this); |
| 270 | copy.fMacros.padder = padder; |
| 271 | return copy; |
| 272 | } |
| 273 | |
| 274 | template<typename Derived> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 275 | Derived NumberFormatterSettings<Derived>::padding(const Padder& padder)&& { |
| 276 | Derived move(std::move(*this)); |
| 277 | move.fMacros.padder = padder; |
| 278 | return move; |
| 279 | } |
| 280 | |
| 281 | template<typename Derived> |
| 282 | Derived NumberFormatterSettings<Derived>::threshold(int32_t threshold) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 283 | Derived copy(*this); |
| 284 | copy.fMacros.threshold = threshold; |
| 285 | return copy; |
| 286 | } |
| 287 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 288 | template<typename Derived> |
| 289 | Derived NumberFormatterSettings<Derived>::threshold(int32_t threshold)&& { |
| 290 | Derived move(std::move(*this)); |
| 291 | move.fMacros.threshold = threshold; |
| 292 | return move; |
| 293 | } |
| 294 | |
| 295 | template<typename Derived> |
| 296 | Derived NumberFormatterSettings<Derived>::macros(const impl::MacroProps& macros) const& { |
| 297 | Derived copy(*this); |
| 298 | copy.fMacros = macros; |
| 299 | return copy; |
| 300 | } |
| 301 | |
| 302 | template<typename Derived> |
| 303 | Derived NumberFormatterSettings<Derived>::macros(const impl::MacroProps& macros)&& { |
| 304 | Derived move(std::move(*this)); |
| 305 | move.fMacros = macros; |
| 306 | return move; |
| 307 | } |
| 308 | |
| 309 | template<typename Derived> |
| 310 | Derived NumberFormatterSettings<Derived>::macros(impl::MacroProps&& macros) const& { |
| 311 | Derived copy(*this); |
| 312 | copy.fMacros = std::move(macros); |
| 313 | return copy; |
| 314 | } |
| 315 | |
| 316 | template<typename Derived> |
| 317 | Derived NumberFormatterSettings<Derived>::macros(impl::MacroProps&& macros)&& { |
| 318 | Derived move(std::move(*this)); |
| 319 | move.fMacros = std::move(macros); |
| 320 | return move; |
| 321 | } |
| 322 | |
| 323 | template<typename Derived> |
| 324 | UnicodeString NumberFormatterSettings<Derived>::toSkeleton(UErrorCode& status) const { |
| 325 | if (fMacros.copyErrorTo(status)) { |
| 326 | return ICU_Utility::makeBogusString(); |
| 327 | } |
| 328 | return skeleton::generate(fMacros, status); |
| 329 | } |
| 330 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 331 | // Declare all classes that implement NumberFormatterSettings |
| 332 | // See https://stackoverflow.com/a/495056/1407170 |
| 333 | template |
| 334 | class icu::number::NumberFormatterSettings<icu::number::UnlocalizedNumberFormatter>; |
| 335 | template |
| 336 | class icu::number::NumberFormatterSettings<icu::number::LocalizedNumberFormatter>; |
| 337 | |
| 338 | |
| 339 | UnlocalizedNumberFormatter NumberFormatter::with() { |
| 340 | UnlocalizedNumberFormatter result; |
| 341 | return result; |
| 342 | } |
| 343 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 344 | LocalizedNumberFormatter NumberFormatter::withLocale(const Locale& locale) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 345 | return with().locale(locale); |
| 346 | } |
| 347 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 348 | UnlocalizedNumberFormatter |
| 349 | NumberFormatter::forSkeleton(const UnicodeString& skeleton, UErrorCode& status) { |
| 350 | return skeleton::create(skeleton, status); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 353 | |
| 354 | template<typename T> using NFS = NumberFormatterSettings<T>; |
| 355 | using LNF = LocalizedNumberFormatter; |
| 356 | using UNF = UnlocalizedNumberFormatter; |
| 357 | |
| 358 | UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(const UNF& other) |
| 359 | : UNF(static_cast<const NFS<UNF>&>(other)) {} |
| 360 | |
| 361 | UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(const NFS<UNF>& other) |
| 362 | : NFS<UNF>(other) { |
| 363 | // No additional fields to assign |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 366 | UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(UNF&& src) U_NOEXCEPT |
| 367 | : UNF(static_cast<NFS<UNF>&&>(src)) {} |
| 368 | |
| 369 | UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(NFS<UNF>&& src) U_NOEXCEPT |
| 370 | : NFS<UNF>(std::move(src)) { |
| 371 | // No additional fields to assign |
| 372 | } |
| 373 | |
| 374 | UnlocalizedNumberFormatter& UnlocalizedNumberFormatter::operator=(const UNF& other) { |
| 375 | NFS<UNF>::operator=(static_cast<const NFS<UNF>&>(other)); |
| 376 | // No additional fields to assign |
| 377 | return *this; |
| 378 | } |
| 379 | |
| 380 | UnlocalizedNumberFormatter& UnlocalizedNumberFormatter::operator=(UNF&& src) U_NOEXCEPT { |
| 381 | NFS<UNF>::operator=(static_cast<NFS<UNF>&&>(src)); |
| 382 | // No additional fields to assign |
| 383 | return *this; |
| 384 | } |
| 385 | |
| 386 | LocalizedNumberFormatter::LocalizedNumberFormatter(const LNF& other) |
| 387 | : LNF(static_cast<const NFS<LNF>&>(other)) {} |
| 388 | |
| 389 | LocalizedNumberFormatter::LocalizedNumberFormatter(const NFS<LNF>& other) |
| 390 | : NFS<LNF>(other) { |
| 391 | // No additional fields to assign (let call count and compiled formatter reset to defaults) |
| 392 | } |
| 393 | |
| 394 | LocalizedNumberFormatter::LocalizedNumberFormatter(LocalizedNumberFormatter&& src) U_NOEXCEPT |
| 395 | : LNF(static_cast<NFS<LNF>&&>(src)) {} |
| 396 | |
| 397 | LocalizedNumberFormatter::LocalizedNumberFormatter(NFS<LNF>&& src) U_NOEXCEPT |
| 398 | : NFS<LNF>(std::move(src)) { |
| 399 | // For the move operators, copy over the compiled formatter. |
| 400 | // Note: if the formatter is not compiled, call count information is lost. |
| 401 | if (static_cast<LNF&&>(src).fCompiled != nullptr) { |
| 402 | lnfMoveHelper(static_cast<LNF&&>(src)); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | LocalizedNumberFormatter& LocalizedNumberFormatter::operator=(const LNF& other) { |
| 407 | NFS<LNF>::operator=(static_cast<const NFS<LNF>&>(other)); |
| 408 | // No additional fields to assign (let call count and compiled formatter reset to defaults) |
| 409 | return *this; |
| 410 | } |
| 411 | |
| 412 | LocalizedNumberFormatter& LocalizedNumberFormatter::operator=(LNF&& src) U_NOEXCEPT { |
| 413 | NFS<LNF>::operator=(static_cast<NFS<LNF>&&>(src)); |
| 414 | // For the move operators, copy over the compiled formatter. |
| 415 | // Note: if the formatter is not compiled, call count information is lost. |
| 416 | if (static_cast<LNF&&>(src).fCompiled != nullptr) { |
| 417 | // Formatter is compiled |
| 418 | lnfMoveHelper(static_cast<LNF&&>(src)); |
| 419 | } else { |
| 420 | // Reset to default values. |
| 421 | auto* callCount = reinterpret_cast<u_atomic_int32_t*>(fUnsafeCallCount); |
| 422 | umtx_storeRelease(*callCount, 0); |
| 423 | fCompiled = nullptr; |
| 424 | } |
| 425 | return *this; |
| 426 | } |
| 427 | |
| 428 | void LocalizedNumberFormatter::lnfMoveHelper(LNF&& src) { |
| 429 | // Copy over the compiled formatter and set call count to INT32_MIN as in computeCompiled(). |
| 430 | // Don't copy the call count directly because doing so requires a loadAcquire/storeRelease. |
| 431 | // The bits themselves appear to be platform-dependent, so copying them might not be safe. |
| 432 | auto* callCount = reinterpret_cast<u_atomic_int32_t*>(fUnsafeCallCount); |
| 433 | umtx_storeRelease(*callCount, INT32_MIN); |
| 434 | fCompiled = src.fCompiled; |
| 435 | // Reset the source object to leave it in a safe state. |
| 436 | auto* srcCallCount = reinterpret_cast<u_atomic_int32_t*>(src.fUnsafeCallCount); |
| 437 | umtx_storeRelease(*srcCallCount, 0); |
| 438 | src.fCompiled = nullptr; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | LocalizedNumberFormatter::~LocalizedNumberFormatter() { |
| 443 | delete fCompiled; |
| 444 | } |
| 445 | |
| 446 | LocalizedNumberFormatter::LocalizedNumberFormatter(const MacroProps& macros, const Locale& locale) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 447 | fMacros = macros; |
| 448 | fMacros.locale = locale; |
| 449 | } |
| 450 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 451 | LocalizedNumberFormatter::LocalizedNumberFormatter(MacroProps&& macros, const Locale& locale) { |
| 452 | fMacros = std::move(macros); |
| 453 | fMacros.locale = locale; |
| 454 | } |
| 455 | |
| 456 | LocalizedNumberFormatter UnlocalizedNumberFormatter::locale(const Locale& locale) const& { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 457 | return LocalizedNumberFormatter(fMacros, locale); |
| 458 | } |
| 459 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 460 | LocalizedNumberFormatter UnlocalizedNumberFormatter::locale(const Locale& locale)&& { |
| 461 | return LocalizedNumberFormatter(std::move(fMacros), locale); |
| 462 | } |
| 463 | |
| 464 | SymbolsWrapper::SymbolsWrapper(const SymbolsWrapper& other) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 465 | doCopyFrom(other); |
| 466 | } |
| 467 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 468 | SymbolsWrapper::SymbolsWrapper(SymbolsWrapper&& src) U_NOEXCEPT { |
| 469 | doMoveFrom(std::move(src)); |
| 470 | } |
| 471 | |
| 472 | SymbolsWrapper& SymbolsWrapper::operator=(const SymbolsWrapper& other) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 473 | if (this == &other) { |
| 474 | return *this; |
| 475 | } |
| 476 | doCleanup(); |
| 477 | doCopyFrom(other); |
| 478 | return *this; |
| 479 | } |
| 480 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 481 | SymbolsWrapper& SymbolsWrapper::operator=(SymbolsWrapper&& src) U_NOEXCEPT { |
| 482 | if (this == &src) { |
| 483 | return *this; |
| 484 | } |
| 485 | doCleanup(); |
| 486 | doMoveFrom(std::move(src)); |
| 487 | return *this; |
| 488 | } |
| 489 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 490 | SymbolsWrapper::~SymbolsWrapper() { |
| 491 | doCleanup(); |
| 492 | } |
| 493 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 494 | void SymbolsWrapper::setTo(const DecimalFormatSymbols& dfs) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 495 | doCleanup(); |
| 496 | fType = SYMPTR_DFS; |
| 497 | fPtr.dfs = new DecimalFormatSymbols(dfs); |
| 498 | } |
| 499 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 500 | void SymbolsWrapper::setTo(const NumberingSystem* ns) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 501 | doCleanup(); |
| 502 | fType = SYMPTR_NS; |
| 503 | fPtr.ns = ns; |
| 504 | } |
| 505 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 506 | void SymbolsWrapper::doCopyFrom(const SymbolsWrapper& other) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 507 | fType = other.fType; |
| 508 | switch (fType) { |
| 509 | case SYMPTR_NONE: |
| 510 | // No action necessary |
| 511 | break; |
| 512 | case SYMPTR_DFS: |
| 513 | // Memory allocation failures are exposed in copyErrorTo() |
| 514 | if (other.fPtr.dfs != nullptr) { |
| 515 | fPtr.dfs = new DecimalFormatSymbols(*other.fPtr.dfs); |
| 516 | } else { |
| 517 | fPtr.dfs = nullptr; |
| 518 | } |
| 519 | break; |
| 520 | case SYMPTR_NS: |
| 521 | // Memory allocation failures are exposed in copyErrorTo() |
| 522 | if (other.fPtr.ns != nullptr) { |
| 523 | fPtr.ns = new NumberingSystem(*other.fPtr.ns); |
| 524 | } else { |
| 525 | fPtr.ns = nullptr; |
| 526 | } |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 531 | void SymbolsWrapper::doMoveFrom(SymbolsWrapper&& src) { |
| 532 | fType = src.fType; |
| 533 | switch (fType) { |
| 534 | case SYMPTR_NONE: |
| 535 | // No action necessary |
| 536 | break; |
| 537 | case SYMPTR_DFS: |
| 538 | fPtr.dfs = src.fPtr.dfs; |
| 539 | src.fPtr.dfs = nullptr; |
| 540 | break; |
| 541 | case SYMPTR_NS: |
| 542 | fPtr.ns = src.fPtr.ns; |
| 543 | src.fPtr.ns = nullptr; |
| 544 | break; |
| 545 | } |
| 546 | } |
| 547 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 548 | void SymbolsWrapper::doCleanup() { |
| 549 | switch (fType) { |
| 550 | case SYMPTR_NONE: |
| 551 | // No action necessary |
| 552 | break; |
| 553 | case SYMPTR_DFS: |
| 554 | delete fPtr.dfs; |
| 555 | break; |
| 556 | case SYMPTR_NS: |
| 557 | delete fPtr.ns; |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | bool SymbolsWrapper::isDecimalFormatSymbols() const { |
| 563 | return fType == SYMPTR_DFS; |
| 564 | } |
| 565 | |
| 566 | bool SymbolsWrapper::isNumberingSystem() const { |
| 567 | return fType == SYMPTR_NS; |
| 568 | } |
| 569 | |
| 570 | const DecimalFormatSymbols* SymbolsWrapper::getDecimalFormatSymbols() const { |
| 571 | U_ASSERT(fType == SYMPTR_DFS); |
| 572 | return fPtr.dfs; |
| 573 | } |
| 574 | |
| 575 | const NumberingSystem* SymbolsWrapper::getNumberingSystem() const { |
| 576 | U_ASSERT(fType == SYMPTR_NS); |
| 577 | return fPtr.ns; |
| 578 | } |
| 579 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 580 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 581 | FormattedNumber LocalizedNumberFormatter::formatInt(int64_t value, UErrorCode& status) const { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 582 | if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); } |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 583 | auto results = new UFormattedNumberData(); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 584 | if (results == nullptr) { |
| 585 | status = U_MEMORY_ALLOCATION_ERROR; |
| 586 | return FormattedNumber(status); |
| 587 | } |
| 588 | results->quantity.setToLong(value); |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 589 | formatImpl(results, status); |
| 590 | |
| 591 | // Do not save the results object if we encountered a failure. |
| 592 | if (U_SUCCESS(status)) { |
| 593 | return FormattedNumber(results); |
| 594 | } else { |
| 595 | delete results; |
| 596 | return FormattedNumber(status); |
| 597 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 600 | FormattedNumber LocalizedNumberFormatter::formatDouble(double value, UErrorCode& status) const { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 601 | if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); } |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 602 | auto results = new UFormattedNumberData(); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 603 | if (results == nullptr) { |
| 604 | status = U_MEMORY_ALLOCATION_ERROR; |
| 605 | return FormattedNumber(status); |
| 606 | } |
| 607 | results->quantity.setToDouble(value); |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 608 | formatImpl(results, status); |
| 609 | |
| 610 | // Do not save the results object if we encountered a failure. |
| 611 | if (U_SUCCESS(status)) { |
| 612 | return FormattedNumber(results); |
| 613 | } else { |
| 614 | delete results; |
| 615 | return FormattedNumber(status); |
| 616 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 619 | FormattedNumber LocalizedNumberFormatter::formatDecimal(StringPiece value, UErrorCode& status) const { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 620 | if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); } |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 621 | auto results = new UFormattedNumberData(); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 622 | if (results == nullptr) { |
| 623 | status = U_MEMORY_ALLOCATION_ERROR; |
| 624 | return FormattedNumber(status); |
| 625 | } |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 626 | results->quantity.setToDecNumber(value, status); |
| 627 | formatImpl(results, status); |
| 628 | |
| 629 | // Do not save the results object if we encountered a failure. |
| 630 | if (U_SUCCESS(status)) { |
| 631 | return FormattedNumber(results); |
| 632 | } else { |
| 633 | delete results; |
| 634 | return FormattedNumber(status); |
| 635 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | FormattedNumber |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 639 | LocalizedNumberFormatter::formatDecimalQuantity(const DecimalQuantity& dq, UErrorCode& status) const { |
| 640 | if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); } |
| 641 | auto results = new UFormattedNumberData(); |
| 642 | if (results == nullptr) { |
| 643 | status = U_MEMORY_ALLOCATION_ERROR; |
| 644 | return FormattedNumber(status); |
| 645 | } |
| 646 | results->quantity = dq; |
| 647 | formatImpl(results, status); |
| 648 | |
| 649 | // Do not save the results object if we encountered a failure. |
| 650 | if (U_SUCCESS(status)) { |
| 651 | return FormattedNumber(results); |
| 652 | } else { |
| 653 | delete results; |
| 654 | return FormattedNumber(status); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | void LocalizedNumberFormatter::formatImpl(impl::UFormattedNumberData* results, UErrorCode& status) const { |
| 659 | if (computeCompiled(status)) { |
| 660 | fCompiled->apply(results->quantity, results->string, status); |
| 661 | } else { |
| 662 | NumberFormatterImpl::applyStatic(fMacros, results->quantity, results->string, status); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | void LocalizedNumberFormatter::getAffixImpl(bool isPrefix, bool isNegative, UnicodeString& result, |
| 667 | UErrorCode& status) const { |
| 668 | NumberStringBuilder string; |
| 669 | auto signum = static_cast<int8_t>(isNegative ? -1 : 1); |
| 670 | // Always return affixes for plural form OTHER. |
| 671 | static const StandardPlural::Form plural = StandardPlural::OTHER; |
| 672 | int32_t prefixLength; |
| 673 | if (computeCompiled(status)) { |
| 674 | prefixLength = fCompiled->getPrefixSuffix(signum, plural, string, status); |
| 675 | } else { |
| 676 | prefixLength = NumberFormatterImpl::getPrefixSuffixStatic(fMacros, signum, plural, string, status); |
| 677 | } |
| 678 | result.remove(); |
| 679 | if (isPrefix) { |
| 680 | result.append(string.toTempUnicodeString().tempSubStringBetween(0, prefixLength)); |
| 681 | } else { |
| 682 | result.append(string.toTempUnicodeString().tempSubStringBetween(prefixLength, string.length())); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | bool LocalizedNumberFormatter::computeCompiled(UErrorCode& status) const { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 687 | // fUnsafeCallCount contains memory to be interpreted as an atomic int, most commonly |
| 688 | // std::atomic<int32_t>. Since the type of atomic int is platform-dependent, we cast the |
| 689 | // bytes in fUnsafeCallCount to u_atomic_int32_t, a typedef for the platform-dependent |
| 690 | // atomic int type defined in umutex.h. |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 691 | static_assert( |
| 692 | sizeof(u_atomic_int32_t) <= sizeof(fUnsafeCallCount), |
| 693 | "Atomic integer size on this platform exceeds the size allocated by fUnsafeCallCount"); |
| 694 | auto* callCount = reinterpret_cast<u_atomic_int32_t*>( |
| 695 | const_cast<LocalizedNumberFormatter*>(this)->fUnsafeCallCount); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 696 | |
| 697 | // A positive value in the atomic int indicates that the data structure is not yet ready; |
| 698 | // a negative value indicates that it is ready. If, after the increment, the atomic int |
| 699 | // is exactly threshold, then it is the current thread's job to build the data structure. |
| 700 | // Note: We set the callCount to INT32_MIN so that if another thread proceeds to increment |
| 701 | // the atomic int, the value remains below zero. |
| 702 | int32_t currentCount = umtx_loadAcquire(*callCount); |
| 703 | if (0 <= currentCount && currentCount <= fMacros.threshold && fMacros.threshold > 0) { |
| 704 | currentCount = umtx_atomic_inc(callCount); |
| 705 | } |
| 706 | |
| 707 | if (currentCount == fMacros.threshold && fMacros.threshold > 0) { |
| 708 | // Build the data structure and then use it (slow to fast path). |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 709 | const NumberFormatterImpl* compiled = NumberFormatterImpl::fromMacros(fMacros, status); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 710 | U_ASSERT(fCompiled == nullptr); |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 711 | const_cast<LocalizedNumberFormatter*>(this)->fCompiled = compiled; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 712 | umtx_storeRelease(*callCount, INT32_MIN); |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 713 | return true; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 714 | } else if (currentCount < 0) { |
| 715 | // The data structure is already built; use it (fast path). |
| 716 | U_ASSERT(fCompiled != nullptr); |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 717 | return true; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 718 | } else { |
| 719 | // Format the number without building the data structure (slow path). |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 720 | return false; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 724 | const impl::NumberFormatterImpl* LocalizedNumberFormatter::getCompiled() const { |
| 725 | return fCompiled; |
| 726 | } |
| 727 | |
| 728 | int32_t LocalizedNumberFormatter::getCallCount() const { |
| 729 | auto* callCount = reinterpret_cast<u_atomic_int32_t*>( |
| 730 | const_cast<LocalizedNumberFormatter*>(this)->fUnsafeCallCount); |
| 731 | return umtx_loadAcquire(*callCount); |
| 732 | } |
| 733 | |
| 734 | Format* LocalizedNumberFormatter::toFormat(UErrorCode& status) const { |
| 735 | LocalPointer<LocalizedNumberFormatterAsFormat> retval( |
| 736 | new LocalizedNumberFormatterAsFormat(*this, fMacros.locale), status); |
| 737 | return retval.orphan(); |
| 738 | } |
| 739 | |
| 740 | |
| 741 | FormattedNumber::FormattedNumber(FormattedNumber&& src) U_NOEXCEPT |
| 742 | : fResults(src.fResults), fErrorCode(src.fErrorCode) { |
| 743 | // Disown src.fResults to prevent double-deletion |
| 744 | src.fResults = nullptr; |
| 745 | src.fErrorCode = U_INVALID_STATE_ERROR; |
| 746 | } |
| 747 | |
| 748 | FormattedNumber& FormattedNumber::operator=(FormattedNumber&& src) U_NOEXCEPT { |
| 749 | delete fResults; |
| 750 | fResults = src.fResults; |
| 751 | fErrorCode = src.fErrorCode; |
| 752 | // Disown src.fResults to prevent double-deletion |
| 753 | src.fResults = nullptr; |
| 754 | src.fErrorCode = U_INVALID_STATE_ERROR; |
| 755 | return *this; |
| 756 | } |
| 757 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 758 | UnicodeString FormattedNumber::toString() const { |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 759 | UErrorCode localStatus = U_ZERO_ERROR; |
| 760 | return toString(localStatus); |
| 761 | } |
| 762 | |
| 763 | UnicodeString FormattedNumber::toString(UErrorCode& status) const { |
| 764 | if (U_FAILURE(status)) { |
| 765 | return ICU_Utility::makeBogusString(); |
| 766 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 767 | if (fResults == nullptr) { |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 768 | status = fErrorCode; |
| 769 | return ICU_Utility::makeBogusString(); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 770 | } |
| 771 | return fResults->string.toUnicodeString(); |
| 772 | } |
| 773 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 774 | Appendable& FormattedNumber::appendTo(Appendable& appendable) { |
| 775 | UErrorCode localStatus = U_ZERO_ERROR; |
| 776 | return appendTo(appendable, localStatus); |
| 777 | } |
| 778 | |
| 779 | Appendable& FormattedNumber::appendTo(Appendable& appendable, UErrorCode& status) { |
| 780 | if (U_FAILURE(status)) { |
| 781 | return appendable; |
| 782 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 783 | if (fResults == nullptr) { |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 784 | status = fErrorCode; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 785 | return appendable; |
| 786 | } |
| 787 | appendable.appendString(fResults->string.chars(), fResults->string.length()); |
| 788 | return appendable; |
| 789 | } |
| 790 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 791 | void FormattedNumber::populateFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) { |
| 792 | if (U_FAILURE(status)) { |
| 793 | return; |
| 794 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 795 | if (fResults == nullptr) { |
| 796 | status = fErrorCode; |
| 797 | return; |
| 798 | } |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 799 | // in case any users were depending on the old behavior: |
| 800 | fieldPosition.setBeginIndex(0); |
| 801 | fieldPosition.setEndIndex(0); |
| 802 | fResults->string.nextFieldPosition(fieldPosition, status); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 805 | UBool FormattedNumber::nextFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) const { |
| 806 | if (U_FAILURE(status)) { |
| 807 | return FALSE; |
| 808 | } |
| 809 | if (fResults == nullptr) { |
| 810 | status = fErrorCode; |
| 811 | return FALSE; |
| 812 | } |
| 813 | // NOTE: MSVC sometimes complains when implicitly converting between bool and UBool |
| 814 | return fResults->string.nextFieldPosition(fieldPosition, status) ? TRUE : FALSE; |
| 815 | } |
| 816 | |
| 817 | void FormattedNumber::populateFieldPositionIterator(FieldPositionIterator& iterator, UErrorCode& status) { |
| 818 | getAllFieldPositions(iterator, status); |
| 819 | } |
| 820 | |
| 821 | void FormattedNumber::getAllFieldPositions(FieldPositionIterator& iterator, UErrorCode& status) const { |
| 822 | FieldPositionIteratorHandler fpih(&iterator, status); |
| 823 | getAllFieldPositionsImpl(fpih, status); |
| 824 | } |
| 825 | |
| 826 | void FormattedNumber::getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, |
| 827 | UErrorCode& status) const { |
| 828 | if (U_FAILURE(status)) { |
| 829 | return; |
| 830 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 831 | if (fResults == nullptr) { |
| 832 | status = fErrorCode; |
| 833 | return; |
| 834 | } |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame^] | 835 | fResults->string.getAllFieldPositions(fpih, status); |
| 836 | } |
| 837 | |
| 838 | void FormattedNumber::getDecimalQuantity(DecimalQuantity& output, UErrorCode& status) const { |
| 839 | if (U_FAILURE(status)) { |
| 840 | return; |
| 841 | } |
| 842 | if (fResults == nullptr) { |
| 843 | status = fErrorCode; |
| 844 | return; |
| 845 | } |
| 846 | output = fResults->quantity; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | FormattedNumber::~FormattedNumber() { |
| 850 | delete fResults; |
| 851 | } |
| 852 | |
| 853 | #endif /* #if !UCONFIG_NO_FORMATTING */ |