intl/icu/source/i18n/decfmtst.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 *******************************************************************************
michael@0 3 * Copyright (C) 2009-2013, International Business Machines Corporation and *
michael@0 4 * others. All Rights Reserved. *
michael@0 5 *******************************************************************************
michael@0 6 *
michael@0 7 * This file contains the class DecimalFormatStaticSets
michael@0 8 *
michael@0 9 * DecimalFormatStaticSets holds the UnicodeSets that are needed for lenient
michael@0 10 * parsing of decimal and group separators.
michael@0 11 ********************************************************************************
michael@0 12 */
michael@0 13
michael@0 14 #include "unicode/utypes.h"
michael@0 15
michael@0 16 #if !UCONFIG_NO_FORMATTING
michael@0 17
michael@0 18 #include "unicode/unistr.h"
michael@0 19 #include "unicode/uniset.h"
michael@0 20 #include "unicode/uchar.h"
michael@0 21 #include "cmemory.h"
michael@0 22 #include "cstring.h"
michael@0 23 #include "uassert.h"
michael@0 24 #include "ucln_in.h"
michael@0 25 #include "umutex.h"
michael@0 26
michael@0 27 #include "decfmtst.h"
michael@0 28
michael@0 29 U_NAMESPACE_BEGIN
michael@0 30
michael@0 31
michael@0 32 //------------------------------------------------------------------------------
michael@0 33 //
michael@0 34 // Unicode Set pattern strings for all of the required constant sets.
michael@0 35 // Initialized with hex values for portability to EBCDIC based machines.
michael@0 36 // Really ugly, but there's no good way to avoid it.
michael@0 37 //
michael@0 38 //------------------------------------------------------------------------------
michael@0 39
michael@0 40 static const UChar gDotEquivalentsPattern[] = {
michael@0 41 // [ . \u2024 \u3002 \uFE12 \uFE52 \uFF0E \uFF61 ]
michael@0 42 0x005B, 0x002E, 0x2024, 0x3002, 0xFE12, 0xFE52, 0xFF0E, 0xFF61, 0x005D, 0x0000};
michael@0 43
michael@0 44 static const UChar gCommaEquivalentsPattern[] = {
michael@0 45 // [ , \u060C \u066B \u3001 \uFE10 \uFE11 \uFE50 \uFE51 \uFF0C \uFF64 ]
michael@0 46 0x005B, 0x002C, 0x060C, 0x066B, 0x3001, 0xFE10, 0xFE11, 0xFE50, 0xFE51, 0xFF0C, 0xFF64, 0x005D, 0x0000};
michael@0 47
michael@0 48 static const UChar gOtherGroupingSeparatorsPattern[] = {
michael@0 49 // [ \ SPACE ' NBSP \u066C \u2000 - \u200A \u2018 \u2019 \u202F \u205F \u3000 \uFF07 ]
michael@0 50 0x005B, 0x005C, 0x0020, 0x0027, 0x00A0, 0x066C, 0x2000, 0x002D, 0x200A, 0x2018, 0x2019, 0x202F, 0x205F, 0x3000, 0xFF07, 0x005D, 0x0000};
michael@0 51
michael@0 52 static const UChar gDashEquivalentsPattern[] = {
michael@0 53 // [ \ - HYPHEN F_DASH N_DASH MINUS ]
michael@0 54 0x005B, 0x005C, 0x002D, 0x2010, 0x2012, 0x2013, 0x2212, 0x005D, 0x0000};
michael@0 55
michael@0 56 static const UChar gStrictDotEquivalentsPattern[] = {
michael@0 57 // [ . \u2024 \uFE52 \uFF0E \uFF61 ]
michael@0 58 0x005B, 0x002E, 0x2024, 0xFE52, 0xFF0E, 0xFF61, 0x005D, 0x0000};
michael@0 59
michael@0 60 static const UChar gStrictCommaEquivalentsPattern[] = {
michael@0 61 // [ , \u066B \uFE10 \uFE50 \uFF0C ]
michael@0 62 0x005B, 0x002C, 0x066B, 0xFE10, 0xFE50, 0xFF0C, 0x005D, 0x0000};
michael@0 63
michael@0 64 static const UChar gStrictOtherGroupingSeparatorsPattern[] = {
michael@0 65 // [ \ SPACE ' NBSP \u066C \u2000 - \u200A \u2018 \u2019 \u202F \u205F \u3000 \uFF07 ]
michael@0 66 0x005B, 0x005C, 0x0020, 0x0027, 0x00A0, 0x066C, 0x2000, 0x002D, 0x200A, 0x2018, 0x2019, 0x202F, 0x205F, 0x3000, 0xFF07, 0x005D, 0x0000};
michael@0 67
michael@0 68 static const UChar gStrictDashEquivalentsPattern[] = {
michael@0 69 // [ \ - MINUS ]
michael@0 70 0x005B, 0x005C, 0x002D, 0x2212, 0x005D, 0x0000};
michael@0 71
michael@0 72 static UChar32 gMinusSigns[] = {
michael@0 73 0x002D,
michael@0 74 0x207B,
michael@0 75 0x208B,
michael@0 76 0x2212,
michael@0 77 0x2796,
michael@0 78 0xFE63,
michael@0 79 0xFF0D};
michael@0 80
michael@0 81 static UChar32 gPlusSigns[] = {
michael@0 82 0x002B,
michael@0 83 0x207A,
michael@0 84 0x208A,
michael@0 85 0x2795,
michael@0 86 0xfB29,
michael@0 87 0xFE62,
michael@0 88 0xFF0B};
michael@0 89
michael@0 90 static void initUnicodeSet(const UChar32 *raw, int32_t len, UnicodeSet *s) {
michael@0 91 for (int32_t i = 0; i < len; ++i) {
michael@0 92 s->add(raw[i]);
michael@0 93 }
michael@0 94 }
michael@0 95
michael@0 96 DecimalFormatStaticSets::DecimalFormatStaticSets(UErrorCode &status)
michael@0 97 : fDotEquivalents(NULL),
michael@0 98 fCommaEquivalents(NULL),
michael@0 99 fOtherGroupingSeparators(NULL),
michael@0 100 fDashEquivalents(NULL),
michael@0 101 fStrictDotEquivalents(NULL),
michael@0 102 fStrictCommaEquivalents(NULL),
michael@0 103 fStrictOtherGroupingSeparators(NULL),
michael@0 104 fStrictDashEquivalents(NULL),
michael@0 105 fDefaultGroupingSeparators(NULL),
michael@0 106 fStrictDefaultGroupingSeparators(NULL),
michael@0 107 fMinusSigns(NULL),
michael@0 108 fPlusSigns(NULL)
michael@0 109 {
michael@0 110 fDotEquivalents = new UnicodeSet(UnicodeString(TRUE, gDotEquivalentsPattern, -1), status);
michael@0 111 fCommaEquivalents = new UnicodeSet(UnicodeString(TRUE, gCommaEquivalentsPattern, -1), status);
michael@0 112 fOtherGroupingSeparators = new UnicodeSet(UnicodeString(TRUE, gOtherGroupingSeparatorsPattern, -1), status);
michael@0 113 fDashEquivalents = new UnicodeSet(UnicodeString(TRUE, gDashEquivalentsPattern, -1), status);
michael@0 114
michael@0 115 fStrictDotEquivalents = new UnicodeSet(UnicodeString(TRUE, gStrictDotEquivalentsPattern, -1), status);
michael@0 116 fStrictCommaEquivalents = new UnicodeSet(UnicodeString(TRUE, gStrictCommaEquivalentsPattern, -1), status);
michael@0 117 fStrictOtherGroupingSeparators = new UnicodeSet(UnicodeString(TRUE, gStrictOtherGroupingSeparatorsPattern, -1), status);
michael@0 118 fStrictDashEquivalents = new UnicodeSet(UnicodeString(TRUE, gStrictDashEquivalentsPattern, -1), status);
michael@0 119
michael@0 120
michael@0 121 fDefaultGroupingSeparators = new UnicodeSet(*fDotEquivalents);
michael@0 122 fDefaultGroupingSeparators->addAll(*fCommaEquivalents);
michael@0 123 fDefaultGroupingSeparators->addAll(*fOtherGroupingSeparators);
michael@0 124
michael@0 125 fStrictDefaultGroupingSeparators = new UnicodeSet(*fStrictDotEquivalents);
michael@0 126 fStrictDefaultGroupingSeparators->addAll(*fStrictCommaEquivalents);
michael@0 127 fStrictDefaultGroupingSeparators->addAll(*fStrictOtherGroupingSeparators);
michael@0 128
michael@0 129 fMinusSigns = new UnicodeSet();
michael@0 130 fPlusSigns = new UnicodeSet();
michael@0 131
michael@0 132 // Check for null pointers
michael@0 133 if (fDotEquivalents == NULL || fCommaEquivalents == NULL || fOtherGroupingSeparators == NULL || fDashEquivalents == NULL ||
michael@0 134 fStrictDotEquivalents == NULL || fStrictCommaEquivalents == NULL || fStrictOtherGroupingSeparators == NULL || fStrictDashEquivalents == NULL ||
michael@0 135 fDefaultGroupingSeparators == NULL || fStrictOtherGroupingSeparators == NULL ||
michael@0 136 fMinusSigns == NULL || fPlusSigns == NULL) {
michael@0 137 cleanup();
michael@0 138 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 139 return;
michael@0 140 }
michael@0 141
michael@0 142 initUnicodeSet(
michael@0 143 gMinusSigns,
michael@0 144 sizeof(gMinusSigns) / sizeof(gMinusSigns[0]),
michael@0 145 fMinusSigns);
michael@0 146 initUnicodeSet(
michael@0 147 gPlusSigns,
michael@0 148 sizeof(gPlusSigns) / sizeof(gPlusSigns[0]),
michael@0 149 fPlusSigns);
michael@0 150
michael@0 151 // Freeze all the sets
michael@0 152 fDotEquivalents->freeze();
michael@0 153 fCommaEquivalents->freeze();
michael@0 154 fOtherGroupingSeparators->freeze();
michael@0 155 fDashEquivalents->freeze();
michael@0 156 fStrictDotEquivalents->freeze();
michael@0 157 fStrictCommaEquivalents->freeze();
michael@0 158 fStrictOtherGroupingSeparators->freeze();
michael@0 159 fStrictDashEquivalents->freeze();
michael@0 160 fDefaultGroupingSeparators->freeze();
michael@0 161 fStrictDefaultGroupingSeparators->freeze();
michael@0 162 fMinusSigns->freeze();
michael@0 163 fPlusSigns->freeze();
michael@0 164 }
michael@0 165
michael@0 166 DecimalFormatStaticSets::~DecimalFormatStaticSets() {
michael@0 167 cleanup();
michael@0 168 }
michael@0 169
michael@0 170 void DecimalFormatStaticSets::cleanup() { // Be sure to clean up newly added fields!
michael@0 171 delete fDotEquivalents; fDotEquivalents = NULL;
michael@0 172 delete fCommaEquivalents; fCommaEquivalents = NULL;
michael@0 173 delete fOtherGroupingSeparators; fOtherGroupingSeparators = NULL;
michael@0 174 delete fDashEquivalents; fDashEquivalents = NULL;
michael@0 175 delete fStrictDotEquivalents; fStrictDotEquivalents = NULL;
michael@0 176 delete fStrictCommaEquivalents; fStrictCommaEquivalents = NULL;
michael@0 177 delete fStrictOtherGroupingSeparators; fStrictOtherGroupingSeparators = NULL;
michael@0 178 delete fStrictDashEquivalents; fStrictDashEquivalents = NULL;
michael@0 179 delete fDefaultGroupingSeparators; fDefaultGroupingSeparators = NULL;
michael@0 180 delete fStrictDefaultGroupingSeparators; fStrictDefaultGroupingSeparators = NULL;
michael@0 181 delete fStrictOtherGroupingSeparators; fStrictOtherGroupingSeparators = NULL;
michael@0 182 delete fMinusSigns; fMinusSigns = NULL;
michael@0 183 delete fPlusSigns; fPlusSigns = NULL;
michael@0 184 }
michael@0 185
michael@0 186 static DecimalFormatStaticSets *gStaticSets;
michael@0 187 static icu::UInitOnce gStaticSetsInitOnce = U_INITONCE_INITIALIZER;
michael@0 188
michael@0 189
michael@0 190 //------------------------------------------------------------------------------
michael@0 191 //
michael@0 192 // decfmt_cleanup Memory cleanup function, free/delete all
michael@0 193 // cached memory. Called by ICU's u_cleanup() function.
michael@0 194 //
michael@0 195 //------------------------------------------------------------------------------
michael@0 196 U_CDECL_BEGIN
michael@0 197 static UBool U_CALLCONV
michael@0 198 decimfmt_cleanup(void)
michael@0 199 {
michael@0 200 delete gStaticSets;
michael@0 201 gStaticSets = NULL;
michael@0 202 gStaticSetsInitOnce.reset();
michael@0 203 return TRUE;
michael@0 204 }
michael@0 205
michael@0 206 static void U_CALLCONV initSets(UErrorCode &status) {
michael@0 207 U_ASSERT(gStaticSets == NULL);
michael@0 208 ucln_i18n_registerCleanup(UCLN_I18N_DECFMT, decimfmt_cleanup);
michael@0 209 gStaticSets = new DecimalFormatStaticSets(status);
michael@0 210 if (U_FAILURE(status)) {
michael@0 211 delete gStaticSets;
michael@0 212 gStaticSets = NULL;
michael@0 213 return;
michael@0 214 }
michael@0 215 if (gStaticSets == NULL) {
michael@0 216 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 217 }
michael@0 218 }
michael@0 219 U_CDECL_END
michael@0 220
michael@0 221 const DecimalFormatStaticSets *DecimalFormatStaticSets::getStaticSets(UErrorCode &status) {
michael@0 222 umtx_initOnce(gStaticSetsInitOnce, initSets, status);
michael@0 223 return gStaticSets;
michael@0 224 }
michael@0 225
michael@0 226
michael@0 227 const UnicodeSet *DecimalFormatStaticSets::getSimilarDecimals(UChar32 decimal, UBool strictParse)
michael@0 228 {
michael@0 229 UErrorCode status = U_ZERO_ERROR;
michael@0 230 umtx_initOnce(gStaticSetsInitOnce, initSets, status);
michael@0 231 if (U_FAILURE(status)) {
michael@0 232 return NULL;
michael@0 233 }
michael@0 234
michael@0 235 if (gStaticSets->fDotEquivalents->contains(decimal)) {
michael@0 236 return strictParse ? gStaticSets->fStrictDotEquivalents : gStaticSets->fDotEquivalents;
michael@0 237 }
michael@0 238
michael@0 239 if (gStaticSets->fCommaEquivalents->contains(decimal)) {
michael@0 240 return strictParse ? gStaticSets->fStrictCommaEquivalents : gStaticSets->fCommaEquivalents;
michael@0 241 }
michael@0 242
michael@0 243 // if there is no match, return NULL
michael@0 244 return NULL;
michael@0 245 }
michael@0 246
michael@0 247
michael@0 248 U_NAMESPACE_END
michael@0 249 #endif // !UCONFIG_NO_FORMATTING

mercurial