]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/measunit.cpp
ICU-551.51.4.tar.gz
[apple/icu.git] / icuSources / i18n / measunit.cpp
CommitLineData
57a6839d
A
1/*
2**********************************************************************
b331163b 3* Copyright (c) 2004-2015, International Business Machines
57a6839d
A
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
57a6839d
A
22U_NAMESPACE_BEGIN
23
24UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MeasureUnit)
25
b331163b
A
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
57a6839d
A
34static const int32_t gOffsets[] = {
35 0,
b331163b
A
36 2,
37 6,
38 15,
39 17,
40 277,
41 287,
42 297,
43 301,
57a6839d 44 307,
b331163b
A
45 311,
46 329,
47 330,
48 341,
49 347,
50 352,
51 353,
52 356,
53 360,
54 382
57a6839d
A
55};
56
57static const int32_t gIndexes[] = {
58 0,
b331163b
A
59 2,
60 6,
61 15,
62 17,
63 17,
64 27,
65 37,
66 41,
57a6839d 67 47,
b331163b
A
68 51,
69 69,
70 70,
71 81,
72 87,
73 92,
74 93,
75 96,
76 100,
77 122
57a6839d
A
78};
79
b331163b 80// Must be sorted alphabetically.
57a6839d
A
81static const char * const gTypes[] = {
82 "acceleration",
83 "angle",
84 "area",
b331163b 85 "consumption",
57a6839d 86 "currency",
b331163b 87 "digital",
57a6839d 88 "duration",
b331163b
A
89 "electric",
90 "energy",
91 "frequency",
57a6839d 92 "length",
b331163b 93 "light",
57a6839d
A
94 "mass",
95 "power",
96 "pressure",
b331163b 97 "proportion",
57a6839d
A
98 "speed",
99 "temperature",
b331163b 100 "volume"
57a6839d
A
101};
102
b331163b 103// Must be grouped by type and sorted alphabetically within each type.
57a6839d 104static const char * const gSubTypes[] = {
b331163b
A
105 "g-force",
106 "meter-per-second-squared",
107 "arc-minute",
57a6839d
A
108 "arc-second",
109 "degree",
b331163b
A
110 "radian",
111 "acre",
57a6839d 112 "hectare",
b331163b 113 "square-centimeter",
57a6839d 114 "square-foot",
b331163b 115 "square-inch",
57a6839d
A
116 "square-kilometer",
117 "square-meter",
118 "square-mile",
b331163b
A
119 "square-yard",
120 "liter-per-kilometer",
121 "mile-per-gallon",
122 "ADP",
57a6839d
A
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",
b331163b
A
382 "bit",
383 "byte",
384 "gigabit",
385 "gigabyte",
386 "kilobit",
387 "kilobyte",
388 "megabit",
389 "megabyte",
390 "terabit",
391 "terabyte",
392 "day",
57a6839d 393 "hour",
b331163b 394 "microsecond",
57a6839d
A
395 "millisecond",
396 "minute",
397 "month",
b331163b 398 "nanosecond",
57a6839d
A
399 "second",
400 "week",
401 "year",
b331163b
A
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",
57a6839d 420 "foot",
b331163b 421 "furlong",
57a6839d
A
422 "inch",
423 "kilometer",
424 "light-year",
425 "meter",
b331163b 426 "micrometer",
57a6839d
A
427 "mile",
428 "millimeter",
b331163b
A
429 "nanometer",
430 "nautical-mile",
431 "parsec",
57a6839d
A
432 "picometer",
433 "yard",
b331163b
A
434 "lux",
435 "carat",
436 "gram",
57a6839d 437 "kilogram",
b331163b
A
438 "metric-ton",
439 "microgram",
440 "milligram",
57a6839d 441 "ounce",
b331163b 442 "ounce-troy",
57a6839d 443 "pound",
b331163b
A
444 "stone",
445 "ton",
446 "gigawatt",
447 "horsepower",
57a6839d 448 "kilowatt",
b331163b
A
449 "megawatt",
450 "milliwatt",
57a6839d 451 "watt",
b331163b 452 "hectopascal",
57a6839d
A
453 "inch-hg",
454 "millibar",
b331163b
A
455 "millimeter-of-mercury",
456 "pound-per-square-inch",
457 "karat",
458 "kilometer-per-hour",
57a6839d
A
459 "meter-per-second",
460 "mile-per-hour",
b331163b 461 "celsius",
57a6839d 462 "fahrenheit",
b331163b
A
463 "kelvin",
464 "generic",
465 "acre-foot",
466 "bushel",
467 "centiliter",
468 "cubic-centimeter",
469 "cubic-foot",
470 "cubic-inch",
471 "cubic-kilometer",
472 "cubic-meter",
57a6839d 473 "cubic-mile",
b331163b
A
474 "cubic-yard",
475 "cup",
476 "deciliter",
477 "fluid-ounce",
478 "gallon",
479 "hectoliter",
57a6839d 480 "liter",
b331163b
A
481 "megaliter",
482 "milliliter",
483 "pint",
484 "quart",
485 "tablespoon",
486 "teaspoon"
487};
488
489// Must be sorted by first value and then second value.
490static int32_t unitPerUnitToSingleUnit[][4] = {
491 {318, 288, 16, 0},
492 {320, 294, 16, 1},
493 {322, 288, 16, 2},
494 {322, 372, 3, 1},
495 {338, 10, 14, 4},
496 {375, 318, 3, 0}
497};
57a6839d
A
498
499MeasureUnit *MeasureUnit::createGForce(UErrorCode &status) {
500 return MeasureUnit::create(0, 0, status);
501}
502
b331163b
A
503MeasureUnit *MeasureUnit::createMeterPerSecondSquared(UErrorCode &status) {
504 return MeasureUnit::create(0, 1, status);
505}
506
57a6839d
A
507MeasureUnit *MeasureUnit::createArcMinute(UErrorCode &status) {
508 return MeasureUnit::create(1, 0, status);
509}
510
511MeasureUnit *MeasureUnit::createArcSecond(UErrorCode &status) {
512 return MeasureUnit::create(1, 1, status);
513}
514
515MeasureUnit *MeasureUnit::createDegree(UErrorCode &status) {
516 return MeasureUnit::create(1, 2, status);
517}
518
b331163b
A
519MeasureUnit *MeasureUnit::createRadian(UErrorCode &status) {
520 return MeasureUnit::create(1, 3, status);
521}
522
57a6839d
A
523MeasureUnit *MeasureUnit::createAcre(UErrorCode &status) {
524 return MeasureUnit::create(2, 0, status);
525}
526
527MeasureUnit *MeasureUnit::createHectare(UErrorCode &status) {
528 return MeasureUnit::create(2, 1, status);
529}
530
b331163b 531MeasureUnit *MeasureUnit::createSquareCentimeter(UErrorCode &status) {
57a6839d
A
532 return MeasureUnit::create(2, 2, status);
533}
534
b331163b 535MeasureUnit *MeasureUnit::createSquareFoot(UErrorCode &status) {
57a6839d
A
536 return MeasureUnit::create(2, 3, status);
537}
538
b331163b 539MeasureUnit *MeasureUnit::createSquareInch(UErrorCode &status) {
57a6839d
A
540 return MeasureUnit::create(2, 4, status);
541}
542
b331163b 543MeasureUnit *MeasureUnit::createSquareKilometer(UErrorCode &status) {
57a6839d
A
544 return MeasureUnit::create(2, 5, status);
545}
546
b331163b
A
547MeasureUnit *MeasureUnit::createSquareMeter(UErrorCode &status) {
548 return MeasureUnit::create(2, 6, status);
549}
550
551MeasureUnit *MeasureUnit::createSquareMile(UErrorCode &status) {
552 return MeasureUnit::create(2, 7, status);
553}
554
555MeasureUnit *MeasureUnit::createSquareYard(UErrorCode &status) {
556 return MeasureUnit::create(2, 8, status);
557}
558
559MeasureUnit *MeasureUnit::createLiterPerKilometer(UErrorCode &status) {
560 return MeasureUnit::create(3, 0, status);
561}
562
563MeasureUnit *MeasureUnit::createMilePerGallon(UErrorCode &status) {
564 return MeasureUnit::create(3, 1, status);
565}
566
567MeasureUnit *MeasureUnit::createBit(UErrorCode &status) {
568 return MeasureUnit::create(5, 0, status);
569}
570
571MeasureUnit *MeasureUnit::createByte(UErrorCode &status) {
572 return MeasureUnit::create(5, 1, status);
573}
574
575MeasureUnit *MeasureUnit::createGigabit(UErrorCode &status) {
576 return MeasureUnit::create(5, 2, status);
577}
578
579MeasureUnit *MeasureUnit::createGigabyte(UErrorCode &status) {
580 return MeasureUnit::create(5, 3, status);
581}
582
583MeasureUnit *MeasureUnit::createKilobit(UErrorCode &status) {
584 return MeasureUnit::create(5, 4, status);
585}
586
587MeasureUnit *MeasureUnit::createKilobyte(UErrorCode &status) {
588 return MeasureUnit::create(5, 5, status);
589}
590
591MeasureUnit *MeasureUnit::createMegabit(UErrorCode &status) {
592 return MeasureUnit::create(5, 6, status);
593}
594
595MeasureUnit *MeasureUnit::createMegabyte(UErrorCode &status) {
596 return MeasureUnit::create(5, 7, status);
597}
598
599MeasureUnit *MeasureUnit::createTerabit(UErrorCode &status) {
600 return MeasureUnit::create(5, 8, status);
601}
602
603MeasureUnit *MeasureUnit::createTerabyte(UErrorCode &status) {
604 return MeasureUnit::create(5, 9, status);
605}
606
57a6839d 607MeasureUnit *MeasureUnit::createDay(UErrorCode &status) {
b331163b 608 return MeasureUnit::create(6, 0, status);
57a6839d
A
609}
610
611MeasureUnit *MeasureUnit::createHour(UErrorCode &status) {
b331163b
A
612 return MeasureUnit::create(6, 1, status);
613}
614
615MeasureUnit *MeasureUnit::createMicrosecond(UErrorCode &status) {
616 return MeasureUnit::create(6, 2, status);
57a6839d
A
617}
618
619MeasureUnit *MeasureUnit::createMillisecond(UErrorCode &status) {
b331163b 620 return MeasureUnit::create(6, 3, status);
57a6839d
A
621}
622
623MeasureUnit *MeasureUnit::createMinute(UErrorCode &status) {
b331163b 624 return MeasureUnit::create(6, 4, status);
57a6839d
A
625}
626
627MeasureUnit *MeasureUnit::createMonth(UErrorCode &status) {
b331163b
A
628 return MeasureUnit::create(6, 5, status);
629}
630
631MeasureUnit *MeasureUnit::createNanosecond(UErrorCode &status) {
632 return MeasureUnit::create(6, 6, status);
57a6839d
A
633}
634
635MeasureUnit *MeasureUnit::createSecond(UErrorCode &status) {
b331163b 636 return MeasureUnit::create(6, 7, status);
57a6839d
A
637}
638
639MeasureUnit *MeasureUnit::createWeek(UErrorCode &status) {
b331163b 640 return MeasureUnit::create(6, 8, status);
57a6839d
A
641}
642
643MeasureUnit *MeasureUnit::createYear(UErrorCode &status) {
b331163b
A
644 return MeasureUnit::create(6, 9, status);
645}
646
647MeasureUnit *MeasureUnit::createAmpere(UErrorCode &status) {
648 return MeasureUnit::create(7, 0, status);
649}
650
651MeasureUnit *MeasureUnit::createMilliampere(UErrorCode &status) {
652 return MeasureUnit::create(7, 1, status);
653}
654
655MeasureUnit *MeasureUnit::createOhm(UErrorCode &status) {
656 return MeasureUnit::create(7, 2, status);
657}
658
659MeasureUnit *MeasureUnit::createVolt(UErrorCode &status) {
660 return MeasureUnit::create(7, 3, status);
661}
662
663MeasureUnit *MeasureUnit::createCalorie(UErrorCode &status) {
664 return MeasureUnit::create(8, 0, status);
665}
666
667MeasureUnit *MeasureUnit::createFoodcalorie(UErrorCode &status) {
668 return MeasureUnit::create(8, 1, status);
669}
670
671MeasureUnit *MeasureUnit::createJoule(UErrorCode &status) {
672 return MeasureUnit::create(8, 2, status);
673}
674
675MeasureUnit *MeasureUnit::createKilocalorie(UErrorCode &status) {
676 return MeasureUnit::create(8, 3, status);
677}
678
679MeasureUnit *MeasureUnit::createKilojoule(UErrorCode &status) {
680 return MeasureUnit::create(8, 4, status);
681}
682
683MeasureUnit *MeasureUnit::createKilowattHour(UErrorCode &status) {
684 return MeasureUnit::create(8, 5, status);
685}
686
687MeasureUnit *MeasureUnit::createGigahertz(UErrorCode &status) {
688 return MeasureUnit::create(9, 0, status);
689}
690
691MeasureUnit *MeasureUnit::createHertz(UErrorCode &status) {
692 return MeasureUnit::create(9, 1, status);
693}
694
695MeasureUnit *MeasureUnit::createKilohertz(UErrorCode &status) {
696 return MeasureUnit::create(9, 2, status);
697}
698
699MeasureUnit *MeasureUnit::createMegahertz(UErrorCode &status) {
700 return MeasureUnit::create(9, 3, status);
701}
702
703MeasureUnit *MeasureUnit::createAstronomicalUnit(UErrorCode &status) {
704 return MeasureUnit::create(10, 0, status);
57a6839d
A
705}
706
707MeasureUnit *MeasureUnit::createCentimeter(UErrorCode &status) {
b331163b
A
708 return MeasureUnit::create(10, 1, status);
709}
710
711MeasureUnit *MeasureUnit::createDecimeter(UErrorCode &status) {
712 return MeasureUnit::create(10, 2, status);
713}
714
715MeasureUnit *MeasureUnit::createFathom(UErrorCode &status) {
716 return MeasureUnit::create(10, 3, status);
57a6839d
A
717}
718
719MeasureUnit *MeasureUnit::createFoot(UErrorCode &status) {
b331163b
A
720 return MeasureUnit::create(10, 4, status);
721}
722
723MeasureUnit *MeasureUnit::createFurlong(UErrorCode &status) {
724 return MeasureUnit::create(10, 5, status);
57a6839d
A
725}
726
727MeasureUnit *MeasureUnit::createInch(UErrorCode &status) {
b331163b 728 return MeasureUnit::create(10, 6, status);
57a6839d
A
729}
730
731MeasureUnit *MeasureUnit::createKilometer(UErrorCode &status) {
b331163b 732 return MeasureUnit::create(10, 7, status);
57a6839d
A
733}
734
735MeasureUnit *MeasureUnit::createLightYear(UErrorCode &status) {
b331163b 736 return MeasureUnit::create(10, 8, status);
57a6839d
A
737}
738
739MeasureUnit *MeasureUnit::createMeter(UErrorCode &status) {
b331163b
A
740 return MeasureUnit::create(10, 9, status);
741}
742
743MeasureUnit *MeasureUnit::createMicrometer(UErrorCode &status) {
744 return MeasureUnit::create(10, 10, status);
57a6839d
A
745}
746
747MeasureUnit *MeasureUnit::createMile(UErrorCode &status) {
b331163b 748 return MeasureUnit::create(10, 11, status);
57a6839d
A
749}
750
751MeasureUnit *MeasureUnit::createMillimeter(UErrorCode &status) {
b331163b
A
752 return MeasureUnit::create(10, 12, status);
753}
754
755MeasureUnit *MeasureUnit::createNanometer(UErrorCode &status) {
756 return MeasureUnit::create(10, 13, status);
757}
758
759MeasureUnit *MeasureUnit::createNauticalMile(UErrorCode &status) {
760 return MeasureUnit::create(10, 14, status);
761}
762
763MeasureUnit *MeasureUnit::createParsec(UErrorCode &status) {
764 return MeasureUnit::create(10, 15, status);
57a6839d
A
765}
766
767MeasureUnit *MeasureUnit::createPicometer(UErrorCode &status) {
b331163b 768 return MeasureUnit::create(10, 16, status);
57a6839d
A
769}
770
771MeasureUnit *MeasureUnit::createYard(UErrorCode &status) {
b331163b
A
772 return MeasureUnit::create(10, 17, status);
773}
774
775MeasureUnit *MeasureUnit::createLux(UErrorCode &status) {
776 return MeasureUnit::create(11, 0, status);
777}
778
779MeasureUnit *MeasureUnit::createCarat(UErrorCode &status) {
780 return MeasureUnit::create(12, 0, status);
57a6839d
A
781}
782
783MeasureUnit *MeasureUnit::createGram(UErrorCode &status) {
b331163b 784 return MeasureUnit::create(12, 1, status);
57a6839d
A
785}
786
787MeasureUnit *MeasureUnit::createKilogram(UErrorCode &status) {
b331163b
A
788 return MeasureUnit::create(12, 2, status);
789}
790
791MeasureUnit *MeasureUnit::createMetricTon(UErrorCode &status) {
792 return MeasureUnit::create(12, 3, status);
793}
794
795MeasureUnit *MeasureUnit::createMicrogram(UErrorCode &status) {
796 return MeasureUnit::create(12, 4, status);
797}
798
799MeasureUnit *MeasureUnit::createMilligram(UErrorCode &status) {
800 return MeasureUnit::create(12, 5, status);
57a6839d
A
801}
802
803MeasureUnit *MeasureUnit::createOunce(UErrorCode &status) {
b331163b
A
804 return MeasureUnit::create(12, 6, status);
805}
806
807MeasureUnit *MeasureUnit::createOunceTroy(UErrorCode &status) {
808 return MeasureUnit::create(12, 7, status);
57a6839d
A
809}
810
811MeasureUnit *MeasureUnit::createPound(UErrorCode &status) {
b331163b 812 return MeasureUnit::create(12, 8, status);
57a6839d
A
813}
814
815MeasureUnit *MeasureUnit::createStone(UErrorCode &status) {
b331163b
A
816 return MeasureUnit::create(12, 9, status);
817}
818
819MeasureUnit *MeasureUnit::createTon(UErrorCode &status) {
820 return MeasureUnit::create(12, 10, status);
821}
822
823MeasureUnit *MeasureUnit::createGigawatt(UErrorCode &status) {
824 return MeasureUnit::create(13, 0, status);
57a6839d
A
825}
826
827MeasureUnit *MeasureUnit::createHorsepower(UErrorCode &status) {
b331163b 828 return MeasureUnit::create(13, 1, status);
57a6839d
A
829}
830
831MeasureUnit *MeasureUnit::createKilowatt(UErrorCode &status) {
b331163b
A
832 return MeasureUnit::create(13, 2, status);
833}
834
835MeasureUnit *MeasureUnit::createMegawatt(UErrorCode &status) {
836 return MeasureUnit::create(13, 3, status);
837}
838
839MeasureUnit *MeasureUnit::createMilliwatt(UErrorCode &status) {
840 return MeasureUnit::create(13, 4, status);
57a6839d
A
841}
842
843MeasureUnit *MeasureUnit::createWatt(UErrorCode &status) {
b331163b 844 return MeasureUnit::create(13, 5, status);
57a6839d
A
845}
846
847MeasureUnit *MeasureUnit::createHectopascal(UErrorCode &status) {
b331163b 848 return MeasureUnit::create(14, 0, status);
57a6839d
A
849}
850
851MeasureUnit *MeasureUnit::createInchHg(UErrorCode &status) {
b331163b 852 return MeasureUnit::create(14, 1, status);
57a6839d
A
853}
854
855MeasureUnit *MeasureUnit::createMillibar(UErrorCode &status) {
b331163b
A
856 return MeasureUnit::create(14, 2, status);
857}
858
859MeasureUnit *MeasureUnit::createMillimeterOfMercury(UErrorCode &status) {
860 return MeasureUnit::create(14, 3, status);
861}
862
863MeasureUnit *MeasureUnit::createPoundPerSquareInch(UErrorCode &status) {
864 return MeasureUnit::create(14, 4, status);
865}
866
867MeasureUnit *MeasureUnit::createKarat(UErrorCode &status) {
868 return MeasureUnit::create(15, 0, status);
57a6839d
A
869}
870
871MeasureUnit *MeasureUnit::createKilometerPerHour(UErrorCode &status) {
b331163b 872 return MeasureUnit::create(16, 0, status);
57a6839d
A
873}
874
875MeasureUnit *MeasureUnit::createMeterPerSecond(UErrorCode &status) {
b331163b 876 return MeasureUnit::create(16, 1, status);
57a6839d
A
877}
878
879MeasureUnit *MeasureUnit::createMilePerHour(UErrorCode &status) {
b331163b 880 return MeasureUnit::create(16, 2, status);
57a6839d
A
881}
882
883MeasureUnit *MeasureUnit::createCelsius(UErrorCode &status) {
b331163b 884 return MeasureUnit::create(17, 0, status);
57a6839d
A
885}
886
887MeasureUnit *MeasureUnit::createFahrenheit(UErrorCode &status) {
b331163b
A
888 return MeasureUnit::create(17, 1, status);
889}
890
891MeasureUnit *MeasureUnit::createKelvin(UErrorCode &status) {
892 return MeasureUnit::create(17, 2, status);
893}
894
895// Apple-specific for now
896MeasureUnit *MeasureUnit::createGenericTemperature(UErrorCode &status) {
897 return MeasureUnit::create(17, 3, status);
898}
899
900MeasureUnit *MeasureUnit::createAcreFoot(UErrorCode &status) {
901 return MeasureUnit::create(18, 0, status);
902}
903
904MeasureUnit *MeasureUnit::createBushel(UErrorCode &status) {
905 return MeasureUnit::create(18, 1, status);
906}
907
908MeasureUnit *MeasureUnit::createCentiliter(UErrorCode &status) {
909 return MeasureUnit::create(18, 2, status);
910}
911
912MeasureUnit *MeasureUnit::createCubicCentimeter(UErrorCode &status) {
913 return MeasureUnit::create(18, 3, status);
914}
915
916MeasureUnit *MeasureUnit::createCubicFoot(UErrorCode &status) {
917 return MeasureUnit::create(18, 4, status);
918}
919
920MeasureUnit *MeasureUnit::createCubicInch(UErrorCode &status) {
921 return MeasureUnit::create(18, 5, status);
57a6839d
A
922}
923
924MeasureUnit *MeasureUnit::createCubicKilometer(UErrorCode &status) {
b331163b
A
925 return MeasureUnit::create(18, 6, status);
926}
927
928MeasureUnit *MeasureUnit::createCubicMeter(UErrorCode &status) {
929 return MeasureUnit::create(18, 7, status);
57a6839d
A
930}
931
932MeasureUnit *MeasureUnit::createCubicMile(UErrorCode &status) {
b331163b
A
933 return MeasureUnit::create(18, 8, status);
934}
935
936MeasureUnit *MeasureUnit::createCubicYard(UErrorCode &status) {
937 return MeasureUnit::create(18, 9, status);
938}
939
940MeasureUnit *MeasureUnit::createCup(UErrorCode &status) {
941 return MeasureUnit::create(18, 10, status);
942}
943
944MeasureUnit *MeasureUnit::createDeciliter(UErrorCode &status) {
945 return MeasureUnit::create(18, 11, status);
946}
947
948MeasureUnit *MeasureUnit::createFluidOunce(UErrorCode &status) {
949 return MeasureUnit::create(18, 12, status);
950}
951
952MeasureUnit *MeasureUnit::createGallon(UErrorCode &status) {
953 return MeasureUnit::create(18, 13, status);
954}
955
956MeasureUnit *MeasureUnit::createHectoliter(UErrorCode &status) {
957 return MeasureUnit::create(18, 14, status);
57a6839d
A
958}
959
960MeasureUnit *MeasureUnit::createLiter(UErrorCode &status) {
b331163b 961 return MeasureUnit::create(18, 15, status);
57a6839d
A
962}
963
b331163b
A
964MeasureUnit *MeasureUnit::createMegaliter(UErrorCode &status) {
965 return MeasureUnit::create(18, 16, status);
57a6839d
A
966}
967
b331163b
A
968MeasureUnit *MeasureUnit::createMilliliter(UErrorCode &status) {
969 return MeasureUnit::create(18, 17, status);
57a6839d
A
970}
971
b331163b
A
972MeasureUnit *MeasureUnit::createPint(UErrorCode &status) {
973 return MeasureUnit::create(18, 18, status);
57a6839d
A
974}
975
b331163b
A
976MeasureUnit *MeasureUnit::createQuart(UErrorCode &status) {
977 return MeasureUnit::create(18, 19, status);
57a6839d
A
978}
979
b331163b
A
980MeasureUnit *MeasureUnit::createTablespoon(UErrorCode &status) {
981 return MeasureUnit::create(18, 20, status);
982}
983
984MeasureUnit *MeasureUnit::createTeaspoon(UErrorCode &status) {
985 return MeasureUnit::create(18, 21, status);
57a6839d
A
986}
987
b331163b 988// End generated code
57a6839d
A
989
990static int32_t binarySearch(
991 const char * const * array, int32_t start, int32_t end, const char * key) {
992 while (start < end) {
993 int32_t mid = (start + end) / 2;
994 int32_t cmp = uprv_strcmp(array[mid], key);
995 if (cmp < 0) {
996 start = mid + 1;
997 continue;
998 }
999 if (cmp == 0) {
1000 return mid;
1001 }
1002 end = mid;
1003 }
1004 return -1;
1005}
1006
1007MeasureUnit::MeasureUnit(const MeasureUnit &other)
1008 : fTypeId(other.fTypeId), fSubTypeId(other.fSubTypeId) {
1009 uprv_strcpy(fCurrency, other.fCurrency);
1010}
1011
1012MeasureUnit &MeasureUnit::operator=(const MeasureUnit &other) {
1013 if (this == &other) {
1014 return *this;
1015 }
1016 fTypeId = other.fTypeId;
1017 fSubTypeId = other.fSubTypeId;
1018 uprv_strcpy(fCurrency, other.fCurrency);
1019 return *this;
1020}
1021
1022UObject *MeasureUnit::clone() const {
1023 return new MeasureUnit(*this);
1024}
1025
1026MeasureUnit::~MeasureUnit() {
1027}
1028
1029const char *MeasureUnit::getType() const {
1030 return gTypes[fTypeId];
1031}
1032
1033const char *MeasureUnit::getSubtype() const {
1034 return fCurrency[0] == 0 ? gSubTypes[getOffset()] : fCurrency;
1035}
1036
1037UBool MeasureUnit::operator==(const UObject& other) const {
1038 if (this == &other) { // Same object, equal
1039 return TRUE;
1040 }
1041 if (typeid(*this) != typeid(other)) { // Different types, not equal
1042 return FALSE;
1043 }
1044 const MeasureUnit &rhs = static_cast<const MeasureUnit&>(other);
1045 return (
1046 fTypeId == rhs.fTypeId
1047 && fSubTypeId == rhs.fSubTypeId
1048 && uprv_strcmp(fCurrency, rhs.fCurrency) == 0);
1049}
1050
1051int32_t MeasureUnit::getIndex() const {
1052 return gIndexes[fTypeId] + fSubTypeId;
1053}
1054
1055int32_t MeasureUnit::getAvailable(
1056 MeasureUnit *dest,
1057 int32_t destCapacity,
1058 UErrorCode &errorCode) {
1059 if (U_FAILURE(errorCode)) {
1060 return 0;
1061 }
b331163b 1062 if (destCapacity < UPRV_LENGTHOF(gSubTypes)) {
57a6839d 1063 errorCode = U_BUFFER_OVERFLOW_ERROR;
b331163b 1064 return UPRV_LENGTHOF(gSubTypes);
57a6839d
A
1065 }
1066 int32_t idx = 0;
b331163b 1067 for (int32_t typeIdx = 0; typeIdx < UPRV_LENGTHOF(gTypes); ++typeIdx) {
57a6839d
A
1068 int32_t len = gOffsets[typeIdx + 1] - gOffsets[typeIdx];
1069 for (int32_t subTypeIdx = 0; subTypeIdx < len; ++subTypeIdx) {
1070 dest[idx].setTo(typeIdx, subTypeIdx);
1071 ++idx;
1072 }
1073 }
b331163b
A
1074 U_ASSERT(idx == UPRV_LENGTHOF(gSubTypes));
1075 return UPRV_LENGTHOF(gSubTypes);
57a6839d
A
1076}
1077
1078int32_t MeasureUnit::getAvailable(
1079 const char *type,
1080 MeasureUnit *dest,
1081 int32_t destCapacity,
1082 UErrorCode &errorCode) {
1083 if (U_FAILURE(errorCode)) {
1084 return 0;
1085 }
b331163b 1086 int32_t typeIdx = binarySearch(gTypes, 0, UPRV_LENGTHOF(gTypes), type);
57a6839d 1087 if (typeIdx == -1) {
b331163b 1088 return 0;
57a6839d
A
1089 }
1090 int32_t len = gOffsets[typeIdx + 1] - gOffsets[typeIdx];
1091 if (destCapacity < len) {
1092 errorCode = U_BUFFER_OVERFLOW_ERROR;
1093 return len;
1094 }
1095 for (int subTypeIdx = 0; subTypeIdx < len; ++subTypeIdx) {
1096 dest[subTypeIdx].setTo(typeIdx, subTypeIdx);
1097 }
1098 return len;
1099}
1100
1101StringEnumeration* MeasureUnit::getAvailableTypes(UErrorCode &errorCode) {
1102 UEnumeration *uenum = uenum_openCharStringsEnumeration(
b331163b 1103 gTypes, UPRV_LENGTHOF(gTypes), &errorCode);
57a6839d
A
1104 if (U_FAILURE(errorCode)) {
1105 uenum_close(uenum);
1106 return NULL;
1107 }
1108 StringEnumeration *result = new UStringEnumeration(uenum);
1109 if (result == NULL) {
1110 errorCode = U_MEMORY_ALLOCATION_ERROR;
1111 uenum_close(uenum);
1112 return NULL;
1113 }
1114 return result;
1115}
1116
1117int32_t MeasureUnit::getIndexCount() {
b331163b
A
1118 return gIndexes[UPRV_LENGTHOF(gIndexes) - 1];
1119}
1120
1121MeasureUnit *MeasureUnit::resolveUnitPerUnit(
1122 const MeasureUnit &unit, const MeasureUnit &perUnit) {
1123 int32_t unitOffset = unit.getOffset();
1124 int32_t perUnitOffset = perUnit.getOffset();
1125
1126 // binary search for (unitOffset, perUnitOffset)
1127 int32_t start = 0;
1128 int32_t end = UPRV_LENGTHOF(unitPerUnitToSingleUnit);
1129 while (start < end) {
1130 int32_t mid = (start + end) / 2;
1131 int32_t *midRow = unitPerUnitToSingleUnit[mid];
1132 if (unitOffset < midRow[0]) {
1133 end = mid;
1134 } else if (unitOffset > midRow[0]) {
1135 start = mid + 1;
1136 } else if (perUnitOffset < midRow[1]) {
1137 end = mid;
1138 } else if (perUnitOffset > midRow[1]) {
1139 start = mid + 1;
1140 } else {
1141 // We found a resolution for our unit / per-unit combo
1142 // return it.
1143 return new MeasureUnit(midRow[2], midRow[3]);
1144 }
1145 }
1146 return NULL;
57a6839d
A
1147}
1148
1149MeasureUnit *MeasureUnit::create(int typeId, int subTypeId, UErrorCode &status) {
1150 if (U_FAILURE(status)) {
1151 return NULL;
1152 }
1153 MeasureUnit *result = new MeasureUnit(typeId, subTypeId);
1154 if (result == NULL) {
1155 status = U_MEMORY_ALLOCATION_ERROR;
1156 }
1157 return result;
1158}
1159
1160void MeasureUnit::initTime(const char *timeId) {
b331163b 1161 int32_t result = binarySearch(gTypes, 0, UPRV_LENGTHOF(gTypes), "duration");
57a6839d
A
1162 U_ASSERT(result != -1);
1163 fTypeId = result;
1164 result = binarySearch(gSubTypes, gOffsets[fTypeId], gOffsets[fTypeId + 1], timeId);
1165 U_ASSERT(result != -1);
1166 fSubTypeId = result - gOffsets[fTypeId];
1167}
1168
1169void MeasureUnit::initCurrency(const char *isoCurrency) {
b331163b 1170 int32_t result = binarySearch(gTypes, 0, UPRV_LENGTHOF(gTypes), "currency");
57a6839d
A
1171 U_ASSERT(result != -1);
1172 fTypeId = result;
1173 result = binarySearch(
1174 gSubTypes, gOffsets[fTypeId], gOffsets[fTypeId + 1], isoCurrency);
1175 if (result != -1) {
1176 fSubTypeId = result - gOffsets[fTypeId];
1177 } else {
b331163b 1178 uprv_strncpy(fCurrency, isoCurrency, UPRV_LENGTHOF(fCurrency));
57a6839d
A
1179 }
1180}
1181
1182void MeasureUnit::setTo(int32_t typeId, int32_t subTypeId) {
1183 fTypeId = typeId;
1184 fSubTypeId = subTypeId;
1185 fCurrency[0] = 0;
1186}
1187
1188int32_t MeasureUnit::getOffset() const {
1189 return gOffsets[fTypeId] + fSubTypeId;
1190}
1191
1192U_NAMESPACE_END
1193
1194#endif /* !UNCONFIG_NO_FORMATTING */