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