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