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