/src/icu/source/i18n/units_router.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
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 "charstr.h"  | 
9  |  | #include "cmemory.h"  | 
10  |  | #include "cstring.h"  | 
11  |  | #include "measunit_impl.h"  | 
12  |  | #include "number_decimalquantity.h"  | 
13  |  | #include "number_roundingutils.h"  | 
14  |  | #include "resource.h"  | 
15  |  | #include "unicode/measure.h"  | 
16  |  | #include "units_data.h"  | 
17  |  | #include "units_router.h"  | 
18  |  | #include <cmath>  | 
19  |  |  | 
20  |  | U_NAMESPACE_BEGIN  | 
21  |  | namespace units { | 
22  |  |  | 
23  |  | using number::Precision;  | 
24  |  | using number::impl::parseIncrementOption;  | 
25  |  |  | 
26  |  | Precision UnitsRouter::parseSkeletonToPrecision(icu::UnicodeString precisionSkeleton,  | 
27  | 0  |                                                 UErrorCode &status) { | 
28  | 0  |     if (U_FAILURE(status)) { | 
29  |  |         // As a member of UsagePrefsHandler, which is a friend of Precision, we  | 
30  |  |         // get access to the default constructor.  | 
31  | 0  |         return {}; | 
32  | 0  |     }  | 
33  | 0  |     constexpr int32_t kSkelPrefixLen = 20;  | 
34  | 0  |     if (!precisionSkeleton.startsWith(UNICODE_STRING_SIMPLE("precision-increment/"))) { | 
35  | 0  |         status = U_INVALID_FORMAT_ERROR;  | 
36  | 0  |         return {}; | 
37  | 0  |     }  | 
38  | 0  |     U_ASSERT(precisionSkeleton[kSkelPrefixLen - 1] == u'/');  | 
39  | 0  |     StringSegment segment(precisionSkeleton, false);  | 
40  | 0  |     segment.adjustOffset(kSkelPrefixLen);  | 
41  | 0  |     Precision result;  | 
42  | 0  |     parseIncrementOption(segment, result, status);  | 
43  | 0  |     return result;  | 
44  | 0  | }  | 
45  |  |  | 
46  |  | UnitsRouter::UnitsRouter(StringPiece inputUnitIdentifier, StringPiece region, StringPiece usage,  | 
47  | 0  |                          UErrorCode &status) { | 
48  | 0  |     this->init(MeasureUnit::forIdentifier(inputUnitIdentifier, status), region, usage, status);  | 
49  | 0  | }  | 
50  |  |  | 
51  |  | UnitsRouter::UnitsRouter(const MeasureUnit &inputUnit, StringPiece region, StringPiece usage,  | 
52  | 0  |                          UErrorCode &status) { | 
53  | 0  |     this->init(std::move(inputUnit), region, usage, status);  | 
54  | 0  | }  | 
55  |  |  | 
56  |  | void UnitsRouter::init(const MeasureUnit &inputUnit, StringPiece region, StringPiece usage,  | 
57  | 0  |                        UErrorCode &status) { | 
58  |  | 
  | 
59  | 0  |     if (U_FAILURE(status)) { | 
60  | 0  |         return;  | 
61  | 0  |     }  | 
62  |  |  | 
63  |  |     // TODO: do we want to pass in ConversionRates and UnitPreferences instead  | 
64  |  |     // of loading in each UnitsRouter instance? (Or make global?)  | 
65  | 0  |     ConversionRates conversionRates(status);  | 
66  | 0  |     UnitPreferences prefs(status);  | 
67  |  | 
  | 
68  | 0  |     MeasureUnitImpl inputUnitImpl = MeasureUnitImpl::forMeasureUnitMaybeCopy(inputUnit, status);  | 
69  | 0  |     MeasureUnit baseUnit =  | 
70  | 0  |         (extractCompoundBaseUnit(inputUnitImpl, conversionRates, status)).build(status);  | 
71  | 0  |     CharString category = getUnitQuantity(baseUnit.getIdentifier(), status);  | 
72  | 0  |     if (U_FAILURE(status)) { | 
73  | 0  |         return;  | 
74  | 0  |     }  | 
75  |  |  | 
76  | 0  |     const UnitPreference *const *unitPreferences;  | 
77  | 0  |     int32_t preferencesCount = 0;  | 
78  | 0  |     prefs.getPreferencesFor(category.toStringPiece(), usage, region, unitPreferences, preferencesCount,  | 
79  | 0  |                             status);  | 
80  |  | 
  | 
81  | 0  |     for (int i = 0; i < preferencesCount; ++i) { | 
82  | 0  |         U_ASSERT(unitPreferences[i] != nullptr);  | 
83  | 0  |         const auto &preference = *unitPreferences[i];  | 
84  |  | 
  | 
85  | 0  |         MeasureUnitImpl complexTargetUnitImpl =  | 
86  | 0  |             MeasureUnitImpl::forIdentifier(preference.unit.data(), status);  | 
87  | 0  |         if (U_FAILURE(status)) { | 
88  | 0  |             return;  | 
89  | 0  |         }  | 
90  |  |  | 
91  | 0  |         UnicodeString precision = preference.skeleton;  | 
92  |  |  | 
93  |  |         // For now, we only have "precision-increment" in Units Preferences skeleton.  | 
94  |  |         // Therefore, we check if the skeleton starts with "precision-increment" and force the program to  | 
95  |  |         // fail otherwise.  | 
96  |  |         // NOTE:  | 
97  |  |         //  It is allowed to have an empty precision.  | 
98  | 0  |         if (!precision.isEmpty() && !precision.startsWith(u"precision-increment", 19)) { | 
99  | 0  |             status = U_INTERNAL_PROGRAM_ERROR;  | 
100  | 0  |             return;  | 
101  | 0  |         }  | 
102  |  |  | 
103  | 0  |         outputUnits_.emplaceBackAndCheckErrorCode(status,  | 
104  | 0  |                                                   complexTargetUnitImpl.copy(status).build(status));  | 
105  | 0  |         converterPreferences_.emplaceBackAndCheckErrorCode(status, inputUnitImpl, complexTargetUnitImpl,  | 
106  | 0  |                                                            preference.geq, std::move(precision),  | 
107  | 0  |                                                            conversionRates, status);  | 
108  |  | 
  | 
109  | 0  |         if (U_FAILURE(status)) { | 
110  | 0  |             return;  | 
111  | 0  |         }  | 
112  | 0  |     }  | 
113  | 0  | }  | 
114  |  |  | 
115  | 0  | RouteResult UnitsRouter::route(double quantity, icu::number::impl::RoundingImpl *rounder, UErrorCode &status) const { | 
116  |  |     // Find the matching preference  | 
117  | 0  |     const ConverterPreference *converterPreference = nullptr;  | 
118  | 0  |     for (int32_t i = 0, n = converterPreferences_.length(); i < n; i++) { | 
119  | 0  |         converterPreference = converterPreferences_[i];  | 
120  | 0  |         if (converterPreference->converter.greaterThanOrEqual(std::abs(quantity) * (1 + DBL_EPSILON),  | 
121  | 0  |                                                               converterPreference->limit)) { | 
122  | 0  |             break;  | 
123  | 0  |         }  | 
124  | 0  |     }  | 
125  | 0  |     U_ASSERT(converterPreference != nullptr);  | 
126  |  |  | 
127  |  |     // Set up the rounder for this preference's precision  | 
128  | 0  |     if (rounder != nullptr && rounder->fPrecision.isBogus()) { | 
129  | 0  |         if (converterPreference->precision.length() > 0) { | 
130  | 0  |             rounder->fPrecision = parseSkeletonToPrecision(converterPreference->precision, status);  | 
131  | 0  |         } else { | 
132  |  |             // We use the same rounding mode as COMPACT notation: known to be a  | 
133  |  |             // human-friendly rounding mode: integers, but add a decimal digit  | 
134  |  |             // as needed to ensure we have at least 2 significant digits.  | 
135  | 0  |             rounder->fPrecision = Precision::integer().withMinDigits(2);  | 
136  | 0  |         }  | 
137  | 0  |     }  | 
138  |  | 
  | 
139  | 0  |     return RouteResult(converterPreference->converter.convert(quantity, rounder, status),  | 
140  | 0  |                        converterPreference->targetUnit.copy(status));  | 
141  | 0  | }  | 
142  |  |  | 
143  | 0  | const MaybeStackVector<MeasureUnit> *UnitsRouter::getOutputUnits() const { | 
144  |  |     // TODO: consider pulling this from converterPreferences_ and dropping  | 
145  |  |     // outputUnits_?  | 
146  | 0  |     return &outputUnits_;  | 
147  | 0  | }  | 
148  |  |  | 
149  |  | } // namespace units  | 
150  |  | U_NAMESPACE_END  | 
151  |  |  | 
152  |  | #endif /* #if !UCONFIG_NO_FORMATTING */  |