1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (c) 2004-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Author: Alan Liu
9 * Created: April 26, 2004
10 * Since: ICU 3.0
11 **********************************************************************
12 */
13 #include "utypeinfo.h" // for 'typeid' to work
14
15 #include "unicode/measunit.h"
16
17 #if !UCONFIG_NO_FORMATTING
18
19 #include "unicode/uenum.h"
20 #include "ustrenum.h"
21 #include "cstring.h"
22 #include "uassert.h"
23
24 U_NAMESPACE_BEGIN
25
26 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MeasureUnit)
27
28 // All code between the "Start generated code" comment and
29 // the "End generated code" comment is auto generated code
30 // and must not be edited manually. For instructions on how to correctly
31 // update this code, refer to:
32 // http://site.icu-project.org/design/formatting/measureformat/updating-measure-unit
33 //
34 // Start generated code
35
36
37 static const int32_t gOffsets[] = {
38 0,
39 2,
40 7,
41 16,
42 22,
43 26,
44 325,
45 336,
46 347,
47 351,
48 357,
49 361,
50 381,
51 382,
52 393,
53 396,
54 402,
55 408,
56 412,
57 416,
58 441
59 };
60
61 static const int32_t gIndexes[] = {
62 0,
63 2,
64 7,
65 16,
66 22,
67 26,
68 26,
69 37,
70 48,
71 52,
72 58,
73 62,
74 82,
75 83,
76 94,
77 97,
78 103,
79 109,
80 113,
81 117,
82 142
83 };
84
85 // Must be sorted alphabetically.
86 static const char * const gTypes[] = {
87 "acceleration",
88 "angle",
89 "area",
90 "concentr",
91 "consumption",
92 "currency",
93 "digital",
94 "duration",
95 "electric",
96 "energy",
97 "frequency",
98 "length",
99 "light",
100 "mass",
101 "none",
102 "power",
103 "pressure",
104 "speed",
105 "temperature",
106 "volume"
107 };
108
109 // Must be grouped by type and sorted alphabetically within each type.
110 static const char * const gSubTypes[] = {
111 "g-force",
112 "meter-per-second-squared",
113 "arc-minute",
114 "arc-second",
115 "degree",
116 "radian",
117 "revolution",
118 "acre",
119 "hectare",
120 "square-centimeter",
121 "square-foot",
122 "square-inch",
123 "square-kilometer",
124 "square-meter",
125 "square-mile",
126 "square-yard",
127 "karat",
128 "milligram-per-deciliter",
129 "millimole-per-liter",
130 "part-per-million",
131 "percent",
132 "permille",
133 "liter-per-100kilometers",
134 "liter-per-kilometer",
135 "mile-per-gallon",
136 "mile-per-gallon-imperial",
137 "ADP",
138 "AED",
139 "AFA",
140 "AFN",
141 "ALK",
142 "ALL",
143 "AMD",
144 "ANG",
145 "AOA",
146 "AOK",
147 "AON",
148 "AOR",
149 "ARA",
150 "ARP",
151 "ARS",
152 "ARY",
153 "ATS",
154 "AUD",
155 "AWG",
156 "AYM",
157 "AZM",
158 "AZN",
159 "BAD",
160 "BAM",
161 "BBD",
162 "BDT",
163 "BEC",
164 "BEF",
165 "BEL",
166 "BGJ",
167 "BGK",
168 "BGL",
169 "BGN",
170 "BHD",
171 "BIF",
172 "BMD",
173 "BND",
174 "BOB",
175 "BOP",
176 "BOV",
177 "BRB",
178 "BRC",
179 "BRE",
180 "BRL",
181 "BRN",
182 "BRR",
183 "BSD",
184 "BTN",
185 "BUK",
186 "BWP",
187 "BYB",
188 "BYN",
189 "BYR",
190 "BZD",
191 "CAD",
192 "CDF",
193 "CHC",
194 "CHE",
195 "CHF",
196 "CHW",
197 "CLF",
198 "CLP",
199 "CNY",
200 "COP",
201 "COU",
202 "CRC",
203 "CSD",
204 "CSJ",
205 "CSK",
206 "CUC",
207 "CUP",
208 "CVE",
209 "CYP",
210 "CZK",
211 "DDM",
212 "DEM",
213 "DJF",
214 "DKK",
215 "DOP",
216 "DZD",
217 "ECS",
218 "ECV",
219 "EEK",
220 "EGP",
221 "ERN",
222 "ESA",
223 "ESB",
224 "ESP",
225 "ETB",
226 "EUR",
227 "FIM",
228 "FJD",
229 "FKP",
230 "FRF",
231 "GBP",
232 "GEK",
233 "GEL",
234 "GHC",
235 "GHP",
236 "GHS",
237 "GIP",
238 "GMD",
239 "GNE",
240 "GNF",
241 "GNS",
242 "GQE",
243 "GRD",
244 "GTQ",
245 "GWE",
246 "GWP",
247 "GYD",
248 "HKD",
249 "HNL",
250 "HRD",
251 "HRK",
252 "HTG",
253 "HUF",
254 "IDR",
255 "IEP",
256 "ILP",
257 "ILR",
258 "ILS",
259 "INR",
260 "IQD",
261 "IRR",
262 "ISJ",
263 "ISK",
264 "ITL",
265 "JMD",
266 "JOD",
267 "JPY",
268 "KES",
269 "KGS",
270 "KHR",
271 "KMF",
272 "KPW",
273 "KRW",
274 "KWD",
275 "KYD",
276 "KZT",
277 "LAJ",
278 "LAK",
279 "LBP",
280 "LKR",
281 "LRD",
282 "LSL",
283 "LSM",
284 "LTL",
285 "LTT",
286 "LUC",
287 "LUF",
288 "LUL",
289 "LVL",
290 "LVR",
291 "LYD",
292 "MAD",
293 "MDL",
294 "MGA",
295 "MGF",
296 "MKD",
297 "MLF",
298 "MMK",
299 "MNT",
300 "MOP",
301 "MRO",
302 "MRU",
303 "MTL",
304 "MTP",
305 "MUR",
306 "MVQ",
307 "MVR",
308 "MWK",
309 "MXN",
310 "MXP",
311 "MXV",
312 "MYR",
313 "MZE",
314 "MZM",
315 "MZN",
316 "NAD",
317 "NGN",
318 "NIC",
319 "NIO",
320 "NLG",
321 "NOK",
322 "NPR",
323 "NZD",
324 "OMR",
325 "PAB",
326 "PEH",
327 "PEI",
328 "PEN",
329 "PES",
330 "PGK",
331 "PHP",
332 "PKR",
333 "PLN",
334 "PLZ",
335 "PTE",
336 "PYG",
337 "QAR",
338 "RHD",
339 "ROK",
340 "ROL",
341 "RON",
342 "RSD",
343 "RUB",
344 "RUR",
345 "RWF",
346 "SAR",
347 "SBD",
348 "SCR",
349 "SDD",
350 "SDG",
351 "SDP",
352 "SEK",
353 "SGD",
354 "SHP",
355 "SIT",
356 "SKK",
357 "SLL",
358 "SOS",
359 "SRD",
360 "SRG",
361 "SSP",
362 "STD",
363 "STN",
364 "SUR",
365 "SVC",
366 "SYP",
367 "SZL",
368 "THB",
369 "TJR",
370 "TJS",
371 "TMM",
372 "TMT",
373 "TND",
374 "TOP",
375 "TPE",
376 "TRL",
377 "TRY",
378 "TTD",
379 "TWD",
380 "TZS",
381 "UAH",
382 "UAK",
383 "UGS",
384 "UGW",
385 "UGX",
386 "USD",
387 "USN",
388 "USS",
389 "UYI",
390 "UYN",
391 "UYP",
392 "UYU",
393 "UYW",
394 "UZS",
395 "VEB",
396 "VEF",
397 "VES",
398 "VNC",
399 "VND",
400 "VUV",
401 "WST",
402 "XAF",
403 "XAG",
404 "XAU",
405 "XBA",
406 "XBB",
407 "XBC",
408 "XBD",
409 "XCD",
410 "XDR",
411 "XEU",
412 "XOF",
413 "XPD",
414 "XPF",
415 "XPT",
416 "XSU",
417 "XTS",
418 "XUA",
419 "XXX",
420 "YDD",
421 "YER",
422 "YUD",
423 "YUM",
424 "YUN",
425 "ZAL",
426 "ZAR",
427 "ZMK",
428 "ZMW",
429 "ZRN",
430 "ZRZ",
431 "ZWC",
432 "ZWD",
433 "ZWL",
434 "ZWN",
435 "ZWR",
436 "bit",
437 "byte",
438 "gigabit",
439 "gigabyte",
440 "kilobit",
441 "kilobyte",
442 "megabit",
443 "megabyte",
444 "petabyte",
445 "terabit",
446 "terabyte",
447 "century",
448 "day",
449 "hour",
450 "microsecond",
451 "millisecond",
452 "minute",
453 "month",
454 "nanosecond",
455 "second",
456 "week",
457 "year",
458 "ampere",
459 "milliampere",
460 "ohm",
461 "volt",
462 "calorie",
463 "foodcalorie",
464 "joule",
465 "kilocalorie",
466 "kilojoule",
467 "kilowatt-hour",
468 "gigahertz",
469 "hertz",
470 "kilohertz",
471 "megahertz",
472 "astronomical-unit",
473 "centimeter",
474 "decimeter",
475 "fathom",
476 "foot",
477 "furlong",
478 "inch",
479 "kilometer",
480 "light-year",
481 "meter",
482 "micrometer",
483 "mile",
484 "mile-scandinavian",
485 "millimeter",
486 "nanometer",
487 "nautical-mile",
488 "parsec",
489 "picometer",
490 "point",
491 "yard",
492 "lux",
493 "carat",
494 "gram",
495 "kilogram",
496 "metric-ton",
497 "microgram",
498 "milligram",
499 "ounce",
500 "ounce-troy",
501 "pound",
502 "stone",
503 "ton",
504 "base",
505 "percent",
506 "permille",
507 "gigawatt",
508 "horsepower",
509 "kilowatt",
510 "megawatt",
511 "milliwatt",
512 "watt",
513 "atmosphere",
514 "hectopascal",
515 "inch-hg",
516 "millibar",
517 "millimeter-of-mercury",
518 "pound-per-square-inch",
519 "kilometer-per-hour",
520 "knot",
521 "meter-per-second",
522 "mile-per-hour",
523 "celsius",
524 "fahrenheit",
525 "generic",
526 "kelvin",
527 "acre-foot",
528 "bushel",
529 "centiliter",
530 "cubic-centimeter",
531 "cubic-foot",
532 "cubic-inch",
533 "cubic-kilometer",
534 "cubic-meter",
535 "cubic-mile",
536 "cubic-yard",
537 "cup",
538 "cup-metric",
539 "deciliter",
540 "fluid-ounce",
541 "gallon",
542 "gallon-imperial",
543 "hectoliter",
544 "liter",
545 "megaliter",
546 "milliliter",
547 "pint",
548 "pint-metric",
549 "quart",
550 "tablespoon",
551 "teaspoon"
552 };
553
554 // Must be sorted by first value and then second value.
555 static int32_t unitPerUnitToSingleUnit[][4] = {
556 {368, 338, 17, 0},
557 {370, 344, 17, 2},
558 {372, 338, 17, 3},
559 {372, 430, 4, 2},
560 {372, 431, 4, 3},
561 {387, 428, 3, 1},
562 {390, 11, 16, 5},
563 {433, 368, 4, 1}
564 };
565
566 // Shortcuts to the base unit in order to make the default constructor fast
567 static const int32_t kBaseTypeIdx = 14;
568 static const int32_t kBaseSubTypeIdx = 0;
569
createGForce(UErrorCode & status)570 MeasureUnit *MeasureUnit::createGForce(UErrorCode &status) {
571 return MeasureUnit::create(0, 0, status);
572 }
573
createMeterPerSecondSquared(UErrorCode & status)574 MeasureUnit *MeasureUnit::createMeterPerSecondSquared(UErrorCode &status) {
575 return MeasureUnit::create(0, 1, status);
576 }
577
createArcMinute(UErrorCode & status)578 MeasureUnit *MeasureUnit::createArcMinute(UErrorCode &status) {
579 return MeasureUnit::create(1, 0, status);
580 }
581
createArcSecond(UErrorCode & status)582 MeasureUnit *MeasureUnit::createArcSecond(UErrorCode &status) {
583 return MeasureUnit::create(1, 1, status);
584 }
585
createDegree(UErrorCode & status)586 MeasureUnit *MeasureUnit::createDegree(UErrorCode &status) {
587 return MeasureUnit::create(1, 2, status);
588 }
589
createRadian(UErrorCode & status)590 MeasureUnit *MeasureUnit::createRadian(UErrorCode &status) {
591 return MeasureUnit::create(1, 3, status);
592 }
593
createRevolutionAngle(UErrorCode & status)594 MeasureUnit *MeasureUnit::createRevolutionAngle(UErrorCode &status) {
595 return MeasureUnit::create(1, 4, status);
596 }
597
createAcre(UErrorCode & status)598 MeasureUnit *MeasureUnit::createAcre(UErrorCode &status) {
599 return MeasureUnit::create(2, 0, status);
600 }
601
createHectare(UErrorCode & status)602 MeasureUnit *MeasureUnit::createHectare(UErrorCode &status) {
603 return MeasureUnit::create(2, 1, status);
604 }
605
createSquareCentimeter(UErrorCode & status)606 MeasureUnit *MeasureUnit::createSquareCentimeter(UErrorCode &status) {
607 return MeasureUnit::create(2, 2, status);
608 }
609
createSquareFoot(UErrorCode & status)610 MeasureUnit *MeasureUnit::createSquareFoot(UErrorCode &status) {
611 return MeasureUnit::create(2, 3, status);
612 }
613
createSquareInch(UErrorCode & status)614 MeasureUnit *MeasureUnit::createSquareInch(UErrorCode &status) {
615 return MeasureUnit::create(2, 4, status);
616 }
617
createSquareKilometer(UErrorCode & status)618 MeasureUnit *MeasureUnit::createSquareKilometer(UErrorCode &status) {
619 return MeasureUnit::create(2, 5, status);
620 }
621
createSquareMeter(UErrorCode & status)622 MeasureUnit *MeasureUnit::createSquareMeter(UErrorCode &status) {
623 return MeasureUnit::create(2, 6, status);
624 }
625
createSquareMile(UErrorCode & status)626 MeasureUnit *MeasureUnit::createSquareMile(UErrorCode &status) {
627 return MeasureUnit::create(2, 7, status);
628 }
629
createSquareYard(UErrorCode & status)630 MeasureUnit *MeasureUnit::createSquareYard(UErrorCode &status) {
631 return MeasureUnit::create(2, 8, status);
632 }
633
createKarat(UErrorCode & status)634 MeasureUnit *MeasureUnit::createKarat(UErrorCode &status) {
635 return MeasureUnit::create(3, 0, status);
636 }
637
createMilligramPerDeciliter(UErrorCode & status)638 MeasureUnit *MeasureUnit::createMilligramPerDeciliter(UErrorCode &status) {
639 return MeasureUnit::create(3, 1, status);
640 }
641
createMillimolePerLiter(UErrorCode & status)642 MeasureUnit *MeasureUnit::createMillimolePerLiter(UErrorCode &status) {
643 return MeasureUnit::create(3, 2, status);
644 }
645
createPartPerMillion(UErrorCode & status)646 MeasureUnit *MeasureUnit::createPartPerMillion(UErrorCode &status) {
647 return MeasureUnit::create(3, 3, status);
648 }
649
createPercent(UErrorCode & status)650 MeasureUnit *MeasureUnit::createPercent(UErrorCode &status) {
651 return MeasureUnit::create(3, 4, status);
652 }
653
createPermille(UErrorCode & status)654 MeasureUnit *MeasureUnit::createPermille(UErrorCode &status) {
655 return MeasureUnit::create(3, 5, status);
656 }
657
createLiterPer100Kilometers(UErrorCode & status)658 MeasureUnit *MeasureUnit::createLiterPer100Kilometers(UErrorCode &status) {
659 return MeasureUnit::create(4, 0, status);
660 }
661
createLiterPerKilometer(UErrorCode & status)662 MeasureUnit *MeasureUnit::createLiterPerKilometer(UErrorCode &status) {
663 return MeasureUnit::create(4, 1, status);
664 }
665
createMilePerGallon(UErrorCode & status)666 MeasureUnit *MeasureUnit::createMilePerGallon(UErrorCode &status) {
667 return MeasureUnit::create(4, 2, status);
668 }
669
createMilePerGallonImperial(UErrorCode & status)670 MeasureUnit *MeasureUnit::createMilePerGallonImperial(UErrorCode &status) {
671 return MeasureUnit::create(4, 3, status);
672 }
673
createBit(UErrorCode & status)674 MeasureUnit *MeasureUnit::createBit(UErrorCode &status) {
675 return MeasureUnit::create(6, 0, status);
676 }
677
createByte(UErrorCode & status)678 MeasureUnit *MeasureUnit::createByte(UErrorCode &status) {
679 return MeasureUnit::create(6, 1, status);
680 }
681
createGigabit(UErrorCode & status)682 MeasureUnit *MeasureUnit::createGigabit(UErrorCode &status) {
683 return MeasureUnit::create(6, 2, status);
684 }
685
createGigabyte(UErrorCode & status)686 MeasureUnit *MeasureUnit::createGigabyte(UErrorCode &status) {
687 return MeasureUnit::create(6, 3, status);
688 }
689
createKilobit(UErrorCode & status)690 MeasureUnit *MeasureUnit::createKilobit(UErrorCode &status) {
691 return MeasureUnit::create(6, 4, status);
692 }
693
createKilobyte(UErrorCode & status)694 MeasureUnit *MeasureUnit::createKilobyte(UErrorCode &status) {
695 return MeasureUnit::create(6, 5, status);
696 }
697
createMegabit(UErrorCode & status)698 MeasureUnit *MeasureUnit::createMegabit(UErrorCode &status) {
699 return MeasureUnit::create(6, 6, status);
700 }
701
createMegabyte(UErrorCode & status)702 MeasureUnit *MeasureUnit::createMegabyte(UErrorCode &status) {
703 return MeasureUnit::create(6, 7, status);
704 }
705
createPetabyte(UErrorCode & status)706 MeasureUnit *MeasureUnit::createPetabyte(UErrorCode &status) {
707 return MeasureUnit::create(6, 8, status);
708 }
709
createTerabit(UErrorCode & status)710 MeasureUnit *MeasureUnit::createTerabit(UErrorCode &status) {
711 return MeasureUnit::create(6, 9, status);
712 }
713
createTerabyte(UErrorCode & status)714 MeasureUnit *MeasureUnit::createTerabyte(UErrorCode &status) {
715 return MeasureUnit::create(6, 10, status);
716 }
717
createCentury(UErrorCode & status)718 MeasureUnit *MeasureUnit::createCentury(UErrorCode &status) {
719 return MeasureUnit::create(7, 0, status);
720 }
721
createDay(UErrorCode & status)722 MeasureUnit *MeasureUnit::createDay(UErrorCode &status) {
723 return MeasureUnit::create(7, 1, status);
724 }
725
createHour(UErrorCode & status)726 MeasureUnit *MeasureUnit::createHour(UErrorCode &status) {
727 return MeasureUnit::create(7, 2, status);
728 }
729
createMicrosecond(UErrorCode & status)730 MeasureUnit *MeasureUnit::createMicrosecond(UErrorCode &status) {
731 return MeasureUnit::create(7, 3, status);
732 }
733
createMillisecond(UErrorCode & status)734 MeasureUnit *MeasureUnit::createMillisecond(UErrorCode &status) {
735 return MeasureUnit::create(7, 4, status);
736 }
737
createMinute(UErrorCode & status)738 MeasureUnit *MeasureUnit::createMinute(UErrorCode &status) {
739 return MeasureUnit::create(7, 5, status);
740 }
741
createMonth(UErrorCode & status)742 MeasureUnit *MeasureUnit::createMonth(UErrorCode &status) {
743 return MeasureUnit::create(7, 6, status);
744 }
745
createNanosecond(UErrorCode & status)746 MeasureUnit *MeasureUnit::createNanosecond(UErrorCode &status) {
747 return MeasureUnit::create(7, 7, status);
748 }
749
createSecond(UErrorCode & status)750 MeasureUnit *MeasureUnit::createSecond(UErrorCode &status) {
751 return MeasureUnit::create(7, 8, status);
752 }
753
createWeek(UErrorCode & status)754 MeasureUnit *MeasureUnit::createWeek(UErrorCode &status) {
755 return MeasureUnit::create(7, 9, status);
756 }
757
createYear(UErrorCode & status)758 MeasureUnit *MeasureUnit::createYear(UErrorCode &status) {
759 return MeasureUnit::create(7, 10, status);
760 }
761
createAmpere(UErrorCode & status)762 MeasureUnit *MeasureUnit::createAmpere(UErrorCode &status) {
763 return MeasureUnit::create(8, 0, status);
764 }
765
createMilliampere(UErrorCode & status)766 MeasureUnit *MeasureUnit::createMilliampere(UErrorCode &status) {
767 return MeasureUnit::create(8, 1, status);
768 }
769
createOhm(UErrorCode & status)770 MeasureUnit *MeasureUnit::createOhm(UErrorCode &status) {
771 return MeasureUnit::create(8, 2, status);
772 }
773
createVolt(UErrorCode & status)774 MeasureUnit *MeasureUnit::createVolt(UErrorCode &status) {
775 return MeasureUnit::create(8, 3, status);
776 }
777
createCalorie(UErrorCode & status)778 MeasureUnit *MeasureUnit::createCalorie(UErrorCode &status) {
779 return MeasureUnit::create(9, 0, status);
780 }
781
createFoodcalorie(UErrorCode & status)782 MeasureUnit *MeasureUnit::createFoodcalorie(UErrorCode &status) {
783 return MeasureUnit::create(9, 1, status);
784 }
785
createJoule(UErrorCode & status)786 MeasureUnit *MeasureUnit::createJoule(UErrorCode &status) {
787 return MeasureUnit::create(9, 2, status);
788 }
789
createKilocalorie(UErrorCode & status)790 MeasureUnit *MeasureUnit::createKilocalorie(UErrorCode &status) {
791 return MeasureUnit::create(9, 3, status);
792 }
793
createKilojoule(UErrorCode & status)794 MeasureUnit *MeasureUnit::createKilojoule(UErrorCode &status) {
795 return MeasureUnit::create(9, 4, status);
796 }
797
createKilowattHour(UErrorCode & status)798 MeasureUnit *MeasureUnit::createKilowattHour(UErrorCode &status) {
799 return MeasureUnit::create(9, 5, status);
800 }
801
createGigahertz(UErrorCode & status)802 MeasureUnit *MeasureUnit::createGigahertz(UErrorCode &status) {
803 return MeasureUnit::create(10, 0, status);
804 }
805
createHertz(UErrorCode & status)806 MeasureUnit *MeasureUnit::createHertz(UErrorCode &status) {
807 return MeasureUnit::create(10, 1, status);
808 }
809
createKilohertz(UErrorCode & status)810 MeasureUnit *MeasureUnit::createKilohertz(UErrorCode &status) {
811 return MeasureUnit::create(10, 2, status);
812 }
813
createMegahertz(UErrorCode & status)814 MeasureUnit *MeasureUnit::createMegahertz(UErrorCode &status) {
815 return MeasureUnit::create(10, 3, status);
816 }
817
createAstronomicalUnit(UErrorCode & status)818 MeasureUnit *MeasureUnit::createAstronomicalUnit(UErrorCode &status) {
819 return MeasureUnit::create(11, 0, status);
820 }
821
createCentimeter(UErrorCode & status)822 MeasureUnit *MeasureUnit::createCentimeter(UErrorCode &status) {
823 return MeasureUnit::create(11, 1, status);
824 }
825
createDecimeter(UErrorCode & status)826 MeasureUnit *MeasureUnit::createDecimeter(UErrorCode &status) {
827 return MeasureUnit::create(11, 2, status);
828 }
829
createFathom(UErrorCode & status)830 MeasureUnit *MeasureUnit::createFathom(UErrorCode &status) {
831 return MeasureUnit::create(11, 3, status);
832 }
833
createFoot(UErrorCode & status)834 MeasureUnit *MeasureUnit::createFoot(UErrorCode &status) {
835 return MeasureUnit::create(11, 4, status);
836 }
837
createFurlong(UErrorCode & status)838 MeasureUnit *MeasureUnit::createFurlong(UErrorCode &status) {
839 return MeasureUnit::create(11, 5, status);
840 }
841
createInch(UErrorCode & status)842 MeasureUnit *MeasureUnit::createInch(UErrorCode &status) {
843 return MeasureUnit::create(11, 6, status);
844 }
845
createKilometer(UErrorCode & status)846 MeasureUnit *MeasureUnit::createKilometer(UErrorCode &status) {
847 return MeasureUnit::create(11, 7, status);
848 }
849
createLightYear(UErrorCode & status)850 MeasureUnit *MeasureUnit::createLightYear(UErrorCode &status) {
851 return MeasureUnit::create(11, 8, status);
852 }
853
createMeter(UErrorCode & status)854 MeasureUnit *MeasureUnit::createMeter(UErrorCode &status) {
855 return MeasureUnit::create(11, 9, status);
856 }
857
createMicrometer(UErrorCode & status)858 MeasureUnit *MeasureUnit::createMicrometer(UErrorCode &status) {
859 return MeasureUnit::create(11, 10, status);
860 }
861
createMile(UErrorCode & status)862 MeasureUnit *MeasureUnit::createMile(UErrorCode &status) {
863 return MeasureUnit::create(11, 11, status);
864 }
865
createMileScandinavian(UErrorCode & status)866 MeasureUnit *MeasureUnit::createMileScandinavian(UErrorCode &status) {
867 return MeasureUnit::create(11, 12, status);
868 }
869
createMillimeter(UErrorCode & status)870 MeasureUnit *MeasureUnit::createMillimeter(UErrorCode &status) {
871 return MeasureUnit::create(11, 13, status);
872 }
873
createNanometer(UErrorCode & status)874 MeasureUnit *MeasureUnit::createNanometer(UErrorCode &status) {
875 return MeasureUnit::create(11, 14, status);
876 }
877
createNauticalMile(UErrorCode & status)878 MeasureUnit *MeasureUnit::createNauticalMile(UErrorCode &status) {
879 return MeasureUnit::create(11, 15, status);
880 }
881
createParsec(UErrorCode & status)882 MeasureUnit *MeasureUnit::createParsec(UErrorCode &status) {
883 return MeasureUnit::create(11, 16, status);
884 }
885
createPicometer(UErrorCode & status)886 MeasureUnit *MeasureUnit::createPicometer(UErrorCode &status) {
887 return MeasureUnit::create(11, 17, status);
888 }
889
createPoint(UErrorCode & status)890 MeasureUnit *MeasureUnit::createPoint(UErrorCode &status) {
891 return MeasureUnit::create(11, 18, status);
892 }
893
createYard(UErrorCode & status)894 MeasureUnit *MeasureUnit::createYard(UErrorCode &status) {
895 return MeasureUnit::create(11, 19, status);
896 }
897
createLux(UErrorCode & status)898 MeasureUnit *MeasureUnit::createLux(UErrorCode &status) {
899 return MeasureUnit::create(12, 0, status);
900 }
901
createCarat(UErrorCode & status)902 MeasureUnit *MeasureUnit::createCarat(UErrorCode &status) {
903 return MeasureUnit::create(13, 0, status);
904 }
905
createGram(UErrorCode & status)906 MeasureUnit *MeasureUnit::createGram(UErrorCode &status) {
907 return MeasureUnit::create(13, 1, status);
908 }
909
createKilogram(UErrorCode & status)910 MeasureUnit *MeasureUnit::createKilogram(UErrorCode &status) {
911 return MeasureUnit::create(13, 2, status);
912 }
913
createMetricTon(UErrorCode & status)914 MeasureUnit *MeasureUnit::createMetricTon(UErrorCode &status) {
915 return MeasureUnit::create(13, 3, status);
916 }
917
createMicrogram(UErrorCode & status)918 MeasureUnit *MeasureUnit::createMicrogram(UErrorCode &status) {
919 return MeasureUnit::create(13, 4, status);
920 }
921
createMilligram(UErrorCode & status)922 MeasureUnit *MeasureUnit::createMilligram(UErrorCode &status) {
923 return MeasureUnit::create(13, 5, status);
924 }
925
createOunce(UErrorCode & status)926 MeasureUnit *MeasureUnit::createOunce(UErrorCode &status) {
927 return MeasureUnit::create(13, 6, status);
928 }
929
createOunceTroy(UErrorCode & status)930 MeasureUnit *MeasureUnit::createOunceTroy(UErrorCode &status) {
931 return MeasureUnit::create(13, 7, status);
932 }
933
createPound(UErrorCode & status)934 MeasureUnit *MeasureUnit::createPound(UErrorCode &status) {
935 return MeasureUnit::create(13, 8, status);
936 }
937
createStone(UErrorCode & status)938 MeasureUnit *MeasureUnit::createStone(UErrorCode &status) {
939 return MeasureUnit::create(13, 9, status);
940 }
941
createTon(UErrorCode & status)942 MeasureUnit *MeasureUnit::createTon(UErrorCode &status) {
943 return MeasureUnit::create(13, 10, status);
944 }
945
createGigawatt(UErrorCode & status)946 MeasureUnit *MeasureUnit::createGigawatt(UErrorCode &status) {
947 return MeasureUnit::create(15, 0, status);
948 }
949
createHorsepower(UErrorCode & status)950 MeasureUnit *MeasureUnit::createHorsepower(UErrorCode &status) {
951 return MeasureUnit::create(15, 1, status);
952 }
953
createKilowatt(UErrorCode & status)954 MeasureUnit *MeasureUnit::createKilowatt(UErrorCode &status) {
955 return MeasureUnit::create(15, 2, status);
956 }
957
createMegawatt(UErrorCode & status)958 MeasureUnit *MeasureUnit::createMegawatt(UErrorCode &status) {
959 return MeasureUnit::create(15, 3, status);
960 }
961
createMilliwatt(UErrorCode & status)962 MeasureUnit *MeasureUnit::createMilliwatt(UErrorCode &status) {
963 return MeasureUnit::create(15, 4, status);
964 }
965
createWatt(UErrorCode & status)966 MeasureUnit *MeasureUnit::createWatt(UErrorCode &status) {
967 return MeasureUnit::create(15, 5, status);
968 }
969
createAtmosphere(UErrorCode & status)970 MeasureUnit *MeasureUnit::createAtmosphere(UErrorCode &status) {
971 return MeasureUnit::create(16, 0, status);
972 }
973
createHectopascal(UErrorCode & status)974 MeasureUnit *MeasureUnit::createHectopascal(UErrorCode &status) {
975 return MeasureUnit::create(16, 1, status);
976 }
977
createInchHg(UErrorCode & status)978 MeasureUnit *MeasureUnit::createInchHg(UErrorCode &status) {
979 return MeasureUnit::create(16, 2, status);
980 }
981
createMillibar(UErrorCode & status)982 MeasureUnit *MeasureUnit::createMillibar(UErrorCode &status) {
983 return MeasureUnit::create(16, 3, status);
984 }
985
createMillimeterOfMercury(UErrorCode & status)986 MeasureUnit *MeasureUnit::createMillimeterOfMercury(UErrorCode &status) {
987 return MeasureUnit::create(16, 4, status);
988 }
989
createPoundPerSquareInch(UErrorCode & status)990 MeasureUnit *MeasureUnit::createPoundPerSquareInch(UErrorCode &status) {
991 return MeasureUnit::create(16, 5, status);
992 }
993
createKilometerPerHour(UErrorCode & status)994 MeasureUnit *MeasureUnit::createKilometerPerHour(UErrorCode &status) {
995 return MeasureUnit::create(17, 0, status);
996 }
997
createKnot(UErrorCode & status)998 MeasureUnit *MeasureUnit::createKnot(UErrorCode &status) {
999 return MeasureUnit::create(17, 1, status);
1000 }
1001
createMeterPerSecond(UErrorCode & status)1002 MeasureUnit *MeasureUnit::createMeterPerSecond(UErrorCode &status) {
1003 return MeasureUnit::create(17, 2, status);
1004 }
1005
createMilePerHour(UErrorCode & status)1006 MeasureUnit *MeasureUnit::createMilePerHour(UErrorCode &status) {
1007 return MeasureUnit::create(17, 3, status);
1008 }
1009
createCelsius(UErrorCode & status)1010 MeasureUnit *MeasureUnit::createCelsius(UErrorCode &status) {
1011 return MeasureUnit::create(18, 0, status);
1012 }
1013
createFahrenheit(UErrorCode & status)1014 MeasureUnit *MeasureUnit::createFahrenheit(UErrorCode &status) {
1015 return MeasureUnit::create(18, 1, status);
1016 }
1017
createGenericTemperature(UErrorCode & status)1018 MeasureUnit *MeasureUnit::createGenericTemperature(UErrorCode &status) {
1019 return MeasureUnit::create(18, 2, status);
1020 }
1021
createKelvin(UErrorCode & status)1022 MeasureUnit *MeasureUnit::createKelvin(UErrorCode &status) {
1023 return MeasureUnit::create(18, 3, status);
1024 }
1025
createAcreFoot(UErrorCode & status)1026 MeasureUnit *MeasureUnit::createAcreFoot(UErrorCode &status) {
1027 return MeasureUnit::create(19, 0, status);
1028 }
1029
createBushel(UErrorCode & status)1030 MeasureUnit *MeasureUnit::createBushel(UErrorCode &status) {
1031 return MeasureUnit::create(19, 1, status);
1032 }
1033
createCentiliter(UErrorCode & status)1034 MeasureUnit *MeasureUnit::createCentiliter(UErrorCode &status) {
1035 return MeasureUnit::create(19, 2, status);
1036 }
1037
createCubicCentimeter(UErrorCode & status)1038 MeasureUnit *MeasureUnit::createCubicCentimeter(UErrorCode &status) {
1039 return MeasureUnit::create(19, 3, status);
1040 }
1041
createCubicFoot(UErrorCode & status)1042 MeasureUnit *MeasureUnit::createCubicFoot(UErrorCode &status) {
1043 return MeasureUnit::create(19, 4, status);
1044 }
1045
createCubicInch(UErrorCode & status)1046 MeasureUnit *MeasureUnit::createCubicInch(UErrorCode &status) {
1047 return MeasureUnit::create(19, 5, status);
1048 }
1049
createCubicKilometer(UErrorCode & status)1050 MeasureUnit *MeasureUnit::createCubicKilometer(UErrorCode &status) {
1051 return MeasureUnit::create(19, 6, status);
1052 }
1053
createCubicMeter(UErrorCode & status)1054 MeasureUnit *MeasureUnit::createCubicMeter(UErrorCode &status) {
1055 return MeasureUnit::create(19, 7, status);
1056 }
1057
createCubicMile(UErrorCode & status)1058 MeasureUnit *MeasureUnit::createCubicMile(UErrorCode &status) {
1059 return MeasureUnit::create(19, 8, status);
1060 }
1061
createCubicYard(UErrorCode & status)1062 MeasureUnit *MeasureUnit::createCubicYard(UErrorCode &status) {
1063 return MeasureUnit::create(19, 9, status);
1064 }
1065
createCup(UErrorCode & status)1066 MeasureUnit *MeasureUnit::createCup(UErrorCode &status) {
1067 return MeasureUnit::create(19, 10, status);
1068 }
1069
createCupMetric(UErrorCode & status)1070 MeasureUnit *MeasureUnit::createCupMetric(UErrorCode &status) {
1071 return MeasureUnit::create(19, 11, status);
1072 }
1073
createDeciliter(UErrorCode & status)1074 MeasureUnit *MeasureUnit::createDeciliter(UErrorCode &status) {
1075 return MeasureUnit::create(19, 12, status);
1076 }
1077
createFluidOunce(UErrorCode & status)1078 MeasureUnit *MeasureUnit::createFluidOunce(UErrorCode &status) {
1079 return MeasureUnit::create(19, 13, status);
1080 }
1081
createGallon(UErrorCode & status)1082 MeasureUnit *MeasureUnit::createGallon(UErrorCode &status) {
1083 return MeasureUnit::create(19, 14, status);
1084 }
1085
createGallonImperial(UErrorCode & status)1086 MeasureUnit *MeasureUnit::createGallonImperial(UErrorCode &status) {
1087 return MeasureUnit::create(19, 15, status);
1088 }
1089
createHectoliter(UErrorCode & status)1090 MeasureUnit *MeasureUnit::createHectoliter(UErrorCode &status) {
1091 return MeasureUnit::create(19, 16, status);
1092 }
1093
createLiter(UErrorCode & status)1094 MeasureUnit *MeasureUnit::createLiter(UErrorCode &status) {
1095 return MeasureUnit::create(19, 17, status);
1096 }
1097
createMegaliter(UErrorCode & status)1098 MeasureUnit *MeasureUnit::createMegaliter(UErrorCode &status) {
1099 return MeasureUnit::create(19, 18, status);
1100 }
1101
createMilliliter(UErrorCode & status)1102 MeasureUnit *MeasureUnit::createMilliliter(UErrorCode &status) {
1103 return MeasureUnit::create(19, 19, status);
1104 }
1105
createPint(UErrorCode & status)1106 MeasureUnit *MeasureUnit::createPint(UErrorCode &status) {
1107 return MeasureUnit::create(19, 20, status);
1108 }
1109
createPintMetric(UErrorCode & status)1110 MeasureUnit *MeasureUnit::createPintMetric(UErrorCode &status) {
1111 return MeasureUnit::create(19, 21, status);
1112 }
1113
createQuart(UErrorCode & status)1114 MeasureUnit *MeasureUnit::createQuart(UErrorCode &status) {
1115 return MeasureUnit::create(19, 22, status);
1116 }
1117
createTablespoon(UErrorCode & status)1118 MeasureUnit *MeasureUnit::createTablespoon(UErrorCode &status) {
1119 return MeasureUnit::create(19, 23, status);
1120 }
1121
createTeaspoon(UErrorCode & status)1122 MeasureUnit *MeasureUnit::createTeaspoon(UErrorCode &status) {
1123 return MeasureUnit::create(19, 24, status);
1124 }
1125
1126 // End generated code
1127
binarySearch(const char * const * array,int32_t start,int32_t end,const char * key)1128 static int32_t binarySearch(
1129 const char * const * array, int32_t start, int32_t end, const char * key) {
1130 while (start < end) {
1131 int32_t mid = (start + end) / 2;
1132 int32_t cmp = uprv_strcmp(array[mid], key);
1133 if (cmp < 0) {
1134 start = mid + 1;
1135 continue;
1136 }
1137 if (cmp == 0) {
1138 return mid;
1139 }
1140 end = mid;
1141 }
1142 return -1;
1143 }
1144
MeasureUnit()1145 MeasureUnit::MeasureUnit() {
1146 fCurrency[0] = 0;
1147 fTypeId = kBaseTypeIdx;
1148 fSubTypeId = kBaseSubTypeIdx;
1149 }
1150
MeasureUnit(const MeasureUnit & other)1151 MeasureUnit::MeasureUnit(const MeasureUnit &other)
1152 : fTypeId(other.fTypeId), fSubTypeId(other.fSubTypeId) {
1153 uprv_strcpy(fCurrency, other.fCurrency);
1154 }
1155
operator =(const MeasureUnit & other)1156 MeasureUnit &MeasureUnit::operator=(const MeasureUnit &other) {
1157 if (this == &other) {
1158 return *this;
1159 }
1160 fTypeId = other.fTypeId;
1161 fSubTypeId = other.fSubTypeId;
1162 uprv_strcpy(fCurrency, other.fCurrency);
1163 return *this;
1164 }
1165
clone() const1166 UObject *MeasureUnit::clone() const {
1167 return new MeasureUnit(*this);
1168 }
1169
~MeasureUnit()1170 MeasureUnit::~MeasureUnit() {
1171 }
1172
getType() const1173 const char *MeasureUnit::getType() const {
1174 return gTypes[fTypeId];
1175 }
1176
getSubtype() const1177 const char *MeasureUnit::getSubtype() const {
1178 return fCurrency[0] == 0 ? gSubTypes[getOffset()] : fCurrency;
1179 }
1180
operator ==(const UObject & other) const1181 UBool MeasureUnit::operator==(const UObject& other) const {
1182 if (this == &other) { // Same object, equal
1183 return TRUE;
1184 }
1185 if (typeid(*this) != typeid(other)) { // Different types, not equal
1186 return FALSE;
1187 }
1188 const MeasureUnit &rhs = static_cast<const MeasureUnit&>(other);
1189 return (
1190 fTypeId == rhs.fTypeId
1191 && fSubTypeId == rhs.fSubTypeId
1192 && uprv_strcmp(fCurrency, rhs.fCurrency) == 0);
1193 }
1194
getIndex() const1195 int32_t MeasureUnit::getIndex() const {
1196 return gIndexes[fTypeId] + fSubTypeId;
1197 }
1198
getAvailable(MeasureUnit * dest,int32_t destCapacity,UErrorCode & errorCode)1199 int32_t MeasureUnit::getAvailable(
1200 MeasureUnit *dest,
1201 int32_t destCapacity,
1202 UErrorCode &errorCode) {
1203 if (U_FAILURE(errorCode)) {
1204 return 0;
1205 }
1206 if (destCapacity < UPRV_LENGTHOF(gSubTypes)) {
1207 errorCode = U_BUFFER_OVERFLOW_ERROR;
1208 return UPRV_LENGTHOF(gSubTypes);
1209 }
1210 int32_t idx = 0;
1211 for (int32_t typeIdx = 0; typeIdx < UPRV_LENGTHOF(gTypes); ++typeIdx) {
1212 int32_t len = gOffsets[typeIdx + 1] - gOffsets[typeIdx];
1213 for (int32_t subTypeIdx = 0; subTypeIdx < len; ++subTypeIdx) {
1214 dest[idx].setTo(typeIdx, subTypeIdx);
1215 ++idx;
1216 }
1217 }
1218 U_ASSERT(idx == UPRV_LENGTHOF(gSubTypes));
1219 return UPRV_LENGTHOF(gSubTypes);
1220 }
1221
getAvailable(const char * type,MeasureUnit * dest,int32_t destCapacity,UErrorCode & errorCode)1222 int32_t MeasureUnit::getAvailable(
1223 const char *type,
1224 MeasureUnit *dest,
1225 int32_t destCapacity,
1226 UErrorCode &errorCode) {
1227 if (U_FAILURE(errorCode)) {
1228 return 0;
1229 }
1230 int32_t typeIdx = binarySearch(gTypes, 0, UPRV_LENGTHOF(gTypes), type);
1231 if (typeIdx == -1) {
1232 return 0;
1233 }
1234 int32_t len = gOffsets[typeIdx + 1] - gOffsets[typeIdx];
1235 if (destCapacity < len) {
1236 errorCode = U_BUFFER_OVERFLOW_ERROR;
1237 return len;
1238 }
1239 for (int subTypeIdx = 0; subTypeIdx < len; ++subTypeIdx) {
1240 dest[subTypeIdx].setTo(typeIdx, subTypeIdx);
1241 }
1242 return len;
1243 }
1244
getAvailableTypes(UErrorCode & errorCode)1245 StringEnumeration* MeasureUnit::getAvailableTypes(UErrorCode &errorCode) {
1246 UEnumeration *uenum = uenum_openCharStringsEnumeration(
1247 gTypes, UPRV_LENGTHOF(gTypes), &errorCode);
1248 if (U_FAILURE(errorCode)) {
1249 uenum_close(uenum);
1250 return NULL;
1251 }
1252 StringEnumeration *result = new UStringEnumeration(uenum);
1253 if (result == NULL) {
1254 errorCode = U_MEMORY_ALLOCATION_ERROR;
1255 uenum_close(uenum);
1256 return NULL;
1257 }
1258 return result;
1259 }
1260
getIndexCount()1261 int32_t MeasureUnit::getIndexCount() {
1262 return gIndexes[UPRV_LENGTHOF(gIndexes) - 1];
1263 }
1264
internalGetIndexForTypeAndSubtype(const char * type,const char * subtype)1265 int32_t MeasureUnit::internalGetIndexForTypeAndSubtype(const char *type, const char *subtype) {
1266 int32_t t = binarySearch(gTypes, 0, UPRV_LENGTHOF(gTypes), type);
1267 if (t < 0) {
1268 return t;
1269 }
1270 int32_t st = binarySearch(gSubTypes, gOffsets[t], gOffsets[t + 1], subtype);
1271 if (st < 0) {
1272 return st;
1273 }
1274 return gIndexes[t] + st - gOffsets[t];
1275 }
1276
resolveUnitPerUnit(const MeasureUnit & unit,const MeasureUnit & perUnit,bool * isResolved)1277 MeasureUnit MeasureUnit::resolveUnitPerUnit(
1278 const MeasureUnit &unit, const MeasureUnit &perUnit, bool* isResolved) {
1279 int32_t unitOffset = unit.getOffset();
1280 int32_t perUnitOffset = perUnit.getOffset();
1281
1282 // binary search for (unitOffset, perUnitOffset)
1283 int32_t start = 0;
1284 int32_t end = UPRV_LENGTHOF(unitPerUnitToSingleUnit);
1285 while (start < end) {
1286 int32_t mid = (start + end) / 2;
1287 int32_t *midRow = unitPerUnitToSingleUnit[mid];
1288 if (unitOffset < midRow[0]) {
1289 end = mid;
1290 } else if (unitOffset > midRow[0]) {
1291 start = mid + 1;
1292 } else if (perUnitOffset < midRow[1]) {
1293 end = mid;
1294 } else if (perUnitOffset > midRow[1]) {
1295 start = mid + 1;
1296 } else {
1297 // We found a resolution for our unit / per-unit combo
1298 // return it.
1299 *isResolved = true;
1300 return MeasureUnit(midRow[2], midRow[3]);
1301 }
1302 }
1303
1304 *isResolved = false;
1305 return MeasureUnit();
1306 }
1307
create(int typeId,int subTypeId,UErrorCode & status)1308 MeasureUnit *MeasureUnit::create(int typeId, int subTypeId, UErrorCode &status) {
1309 if (U_FAILURE(status)) {
1310 return NULL;
1311 }
1312 MeasureUnit *result = new MeasureUnit(typeId, subTypeId);
1313 if (result == NULL) {
1314 status = U_MEMORY_ALLOCATION_ERROR;
1315 }
1316 return result;
1317 }
1318
initTime(const char * timeId)1319 void MeasureUnit::initTime(const char *timeId) {
1320 int32_t result = binarySearch(gTypes, 0, UPRV_LENGTHOF(gTypes), "duration");
1321 U_ASSERT(result != -1);
1322 fTypeId = result;
1323 result = binarySearch(gSubTypes, gOffsets[fTypeId], gOffsets[fTypeId + 1], timeId);
1324 U_ASSERT(result != -1);
1325 fSubTypeId = result - gOffsets[fTypeId];
1326 }
1327
initCurrency(const char * isoCurrency)1328 void MeasureUnit::initCurrency(const char *isoCurrency) {
1329 int32_t result = binarySearch(gTypes, 0, UPRV_LENGTHOF(gTypes), "currency");
1330 U_ASSERT(result != -1);
1331 fTypeId = result;
1332 result = binarySearch(
1333 gSubTypes, gOffsets[fTypeId], gOffsets[fTypeId + 1], isoCurrency);
1334 if (result != -1) {
1335 fSubTypeId = result - gOffsets[fTypeId];
1336 } else {
1337 uprv_strncpy(fCurrency, isoCurrency, UPRV_LENGTHOF(fCurrency));
1338 fCurrency[3] = 0;
1339 }
1340 }
1341
initNoUnit(const char * subtype)1342 void MeasureUnit::initNoUnit(const char *subtype) {
1343 int32_t result = binarySearch(gTypes, 0, UPRV_LENGTHOF(gTypes), "none");
1344 U_ASSERT(result != -1);
1345 fTypeId = result;
1346 result = binarySearch(gSubTypes, gOffsets[fTypeId], gOffsets[fTypeId + 1], subtype);
1347 U_ASSERT(result != -1);
1348 fSubTypeId = result - gOffsets[fTypeId];
1349 }
1350
setTo(int32_t typeId,int32_t subTypeId)1351 void MeasureUnit::setTo(int32_t typeId, int32_t subTypeId) {
1352 fTypeId = typeId;
1353 fSubTypeId = subTypeId;
1354 fCurrency[0] = 0;
1355 }
1356
getOffset() const1357 int32_t MeasureUnit::getOffset() const {
1358 return gOffsets[fTypeId] + fSubTypeId;
1359 }
1360
1361 U_NAMESPACE_END
1362
1363 #endif /* !UNCONFIG_NO_FORMATTING */
1364