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