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