| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
#ifndef intl_components_NumberFormatFields_h_ |
| 5 |
#define intl_components_NumberFormatFields_h_ |
| 6 |
#include "mozilla/intl/ICUError.h" |
| 7 |
#include "mozilla/intl/NumberPart.h" |
| 8 |
#include "mozilla/Maybe.h" |
| 9 |
#include "mozilla/Result.h" |
| 10 |
#include "mozilla/Vector.h" |
| 11 |
|
| 12 |
#include "unicode/unum.h" |
| 13 |
|
| 14 |
struct UFormattedNumber; |
| 15 |
struct UFormattedValue; |
| 16 |
|
| 17 |
namespace mozilla::intl { |
| 18 |
|
| 19 |
struct NumberFormatField { |
| 20 |
uint32_t begin; |
| 21 |
uint32_t end; |
| 22 |
NumberPartType type; |
| 23 |
|
| 24 |
// Needed for vector-resizing scratch space. |
| 25 |
NumberFormatField() = default; |
| 26 |
|
| 27 |
NumberFormatField(uint32_t begin, uint32_t end, NumberPartType type) |
| 28 |
: begin(begin), end(end), type(type) {} |
| 29 |
}; |
| 30 |
|
| 31 |
struct NumberPartSourceMap { |
| 32 |
struct Range { |
| 33 |
uint32_t begin = 0; |
| 34 |
uint32_t end = 0; |
| 35 |
}; |
| 36 |
|
| 37 |
// Begin and end position of the start range. |
| 38 |
Range start; |
| 39 |
|
| 40 |
// Begin and end position of the end range. |
| 41 |
Range end; |
| 42 |
|
| 43 |
NumberPartSource source(uint32_t endIndex) { |
| 44 |
if (start.begin < endIndex && endIndex <= start.end) { |
| 45 |
return NumberPartSource::Start; |
| 46 |
} |
| 47 |
if (end.begin < endIndex && endIndex <= end.end) { |
| 48 |
return NumberPartSource::End; |
| 49 |
} |
| 50 |
return NumberPartSource::Shared; |
| 51 |
} |
| 52 |
|
| 53 |
NumberPartSource source(const NumberFormatField& field) { |
| 54 |
return source(field.end); |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
class NumberFormatFields { |
| 59 |
using FieldsVector = Vector<NumberFormatField, 16>; |
| 60 |
|
| 61 |
FieldsVector fields_; |
| 62 |
|
| 63 |
public: |
| 64 |
[[nodiscard]] bool append(NumberPartType type, int32_t begin, int32_t end); |
| 65 |
|
| 66 |
[[nodiscard]] bool toPartsVector(size_t overallLength, |
| 67 |
NumberPartVector& parts) { |
| 68 |
return toPartsVector(overallLength, {}, parts); |
| 69 |
} |
| 70 |
|
| 71 |
[[nodiscard]] bool toPartsVector(size_t overallLength, |
| 72 |
const NumberPartSourceMap& sourceMap, |
| 73 |
NumberPartVector& parts); |
| 74 |
}; |
| 75 |
|
| 76 |
Result<std::u16string_view, ICUError> FormatResultToParts( |
| 77 |
const UFormattedNumber* value, Maybe<double> number, bool isNegative, |
| 78 |
bool formatForUnit, NumberPartVector& parts); |
| 79 |
|
| 80 |
Result<std::u16string_view, ICUError> FormatResultToParts( |
| 81 |
const UFormattedValue* value, Maybe<double> number, bool isNegative, |
| 82 |
bool formatForUnit, NumberPartVector& parts); |
| 83 |
|
| 84 |
Maybe<NumberPartType> GetPartTypeForNumberField(UNumberFormatFields fieldName, |
| 85 |
Maybe<double> number, |
| 86 |
bool isNegative, |
| 87 |
bool formatForUnit); |
| 88 |
|
| 89 |
} // namespace mozilla::intl |
| 90 |
|
| 91 |
#endif |