1
1
/*
2
- * Copyright (C) 2011 Google Inc. All rights reserved.
2
+ * Copyright (C) 2011,2012 Google Inc. All rights reserved.
3
3
*
4
4
* Redistribution and use in source and binary forms, with or without
5
5
* modification, are permitted provided that the following conditions are
@@ -81,38 +81,38 @@ LocaleICU* LocaleICU::currentLocale()
81
81
return currentLocale;
82
82
}
83
83
84
- void LocaleICU::setDecimalSymbol ( unsigned index, UNumberFormatSymbol symbol)
84
+ String LocaleICU::decimalSymbol ( UNumberFormatSymbol symbol)
85
85
{
86
86
UErrorCode status = U_ZERO_ERROR;
87
87
int32_t bufferLength = unum_getSymbol (m_numberFormat, symbol, 0 , 0 , &status);
88
88
ASSERT (U_SUCCESS (status) || status == U_BUFFER_OVERFLOW_ERROR);
89
89
if (U_FAILURE (status) && status != U_BUFFER_OVERFLOW_ERROR)
90
- return ;
90
+ return String () ;
91
91
Vector<UChar> buffer (bufferLength);
92
92
status = U_ZERO_ERROR;
93
93
unum_getSymbol (m_numberFormat, symbol, buffer.data (), bufferLength, &status);
94
94
if (U_FAILURE (status))
95
- return ;
96
- m_decimalSymbols[index] = String::adopt (buffer);
95
+ return String () ;
96
+ return String::adopt (buffer);
97
97
}
98
98
99
- void LocaleICU::setDecimalTextAttribute (String& destination, UNumberFormatTextAttribute tag)
99
+ String LocaleICU::decimalTextAttribute ( UNumberFormatTextAttribute tag)
100
100
{
101
101
UErrorCode status = U_ZERO_ERROR;
102
102
int32_t bufferLength = unum_getTextAttribute (m_numberFormat, tag, 0 , 0 , &status);
103
103
ASSERT (U_SUCCESS (status) || status == U_BUFFER_OVERFLOW_ERROR);
104
104
if (U_FAILURE (status) && status != U_BUFFER_OVERFLOW_ERROR)
105
- return ;
105
+ return String () ;
106
106
Vector<UChar> buffer (bufferLength);
107
107
status = U_ZERO_ERROR;
108
108
unum_getTextAttribute (m_numberFormat, tag, buffer.data (), bufferLength, &status);
109
109
ASSERT (U_SUCCESS (status));
110
110
if (U_FAILURE (status))
111
- return ;
112
- destination = String::adopt (buffer);
111
+ return String () ;
112
+ return String::adopt (buffer);
113
113
}
114
114
115
- void LocaleICU::initializeDecimalFormat ()
115
+ void LocaleICU::initializeNumberLocalizerData ()
116
116
{
117
117
if (m_didCreateDecimalFormat)
118
118
return ;
@@ -122,155 +122,21 @@ void LocaleICU::initializeDecimalFormat()
122
122
if (!U_SUCCESS (status))
123
123
return ;
124
124
125
- setDecimalSymbol (0 , UNUM_ZERO_DIGIT_SYMBOL);
126
- setDecimalSymbol (1 , UNUM_ONE_DIGIT_SYMBOL);
127
- setDecimalSymbol (2 , UNUM_TWO_DIGIT_SYMBOL);
128
- setDecimalSymbol (3 , UNUM_THREE_DIGIT_SYMBOL);
129
- setDecimalSymbol (4 , UNUM_FOUR_DIGIT_SYMBOL);
130
- setDecimalSymbol (5 , UNUM_FIVE_DIGIT_SYMBOL);
131
- setDecimalSymbol (6 , UNUM_SIX_DIGIT_SYMBOL);
132
- setDecimalSymbol (7 , UNUM_SEVEN_DIGIT_SYMBOL);
133
- setDecimalSymbol (8 , UNUM_EIGHT_DIGIT_SYMBOL);
134
- setDecimalSymbol (9 , UNUM_NINE_DIGIT_SYMBOL);
135
- setDecimalSymbol (DecimalSeparatorIndex, UNUM_DECIMAL_SEPARATOR_SYMBOL);
136
- setDecimalSymbol (GroupSeparatorIndex, UNUM_GROUPING_SEPARATOR_SYMBOL);
137
- setDecimalTextAttribute (m_positivePrefix, UNUM_POSITIVE_PREFIX);
138
- setDecimalTextAttribute (m_positiveSuffix, UNUM_POSITIVE_SUFFIX);
139
- setDecimalTextAttribute (m_negativePrefix, UNUM_NEGATIVE_PREFIX);
140
- setDecimalTextAttribute (m_negativeSuffix, UNUM_NEGATIVE_SUFFIX);
141
- ASSERT (!m_positivePrefix.isEmpty () || !m_positiveSuffix.isEmpty () || !m_negativePrefix.isEmpty () || !m_negativeSuffix.isEmpty ());
142
- }
143
-
144
- String LocaleICU::convertToLocalizedNumber (const String& input)
145
- {
146
- initializeDecimalFormat ();
147
- if (!m_numberFormat || input.isEmpty ())
148
- return input;
149
-
150
- unsigned i = 0 ;
151
- bool isNegative = false ;
152
- UnicodeString ustring;
153
- StringBuilder builder;
154
- builder.reserveCapacity (input.length ());
155
-
156
- if (input[0 ] == ' -' ) {
157
- ++i;
158
- isNegative = true ;
159
- builder.append (m_negativePrefix);
160
- } else
161
- builder.append (m_positivePrefix);
162
-
163
- for (; i < input.length (); ++i) {
164
- switch (input[i]) {
165
- case ' 0' :
166
- case ' 1' :
167
- case ' 2' :
168
- case ' 3' :
169
- case ' 4' :
170
- case ' 5' :
171
- case ' 6' :
172
- case ' 7' :
173
- case ' 8' :
174
- case ' 9' :
175
- builder.append (m_decimalSymbols[input[i] - ' 0' ]);
176
- break ;
177
- case ' .' :
178
- builder.append (m_decimalSymbols[DecimalSeparatorIndex]);
179
- break ;
180
- default :
181
- ASSERT_NOT_REACHED ();
182
- }
183
- }
184
-
185
- builder.append (isNegative ? m_negativeSuffix : m_positiveSuffix);
186
-
187
- return builder.toString ();
188
- }
189
-
190
- static bool matches (const String& text, unsigned position, const String& part)
191
- {
192
- if (part.isEmpty ())
193
- return true ;
194
- if (position + part.length () > text.length ())
195
- return false ;
196
- for (unsigned i = 0 ; i < part.length (); ++i) {
197
- if (text[position + i] != part[i])
198
- return false ;
199
- }
200
- return true ;
201
- }
202
-
203
- bool LocaleICU::detectSignAndGetDigitRange (const String& input, bool & isNegative, unsigned & startIndex, unsigned & endIndex)
204
- {
205
- startIndex = 0 ;
206
- endIndex = input.length ();
207
- if (m_negativePrefix.isEmpty () && m_negativeSuffix.isEmpty ()) {
208
- if (input.startsWith (m_positivePrefix) && input.endsWith (m_positiveSuffix)) {
209
- isNegative = false ;
210
- startIndex = m_positivePrefix.length ();
211
- endIndex -= m_positiveSuffix.length ();
212
- } else
213
- isNegative = true ;
214
- } else {
215
- if (input.startsWith (m_negativePrefix) && input.endsWith (m_negativeSuffix)) {
216
- isNegative = true ;
217
- startIndex = m_negativePrefix.length ();
218
- endIndex -= m_negativeSuffix.length ();
219
- } else {
220
- isNegative = false ;
221
- if (input.startsWith (m_positivePrefix) && input.endsWith (m_positiveSuffix)) {
222
- startIndex = m_positivePrefix.length ();
223
- endIndex -= m_positiveSuffix.length ();
224
- } else
225
- return false ;
226
- }
227
- }
228
- return true ;
229
- }
230
-
231
- unsigned LocaleICU::matchedDecimalSymbolIndex (const String& input, unsigned & position)
232
- {
233
- for (unsigned symbolIndex = 0 ; symbolIndex < DecimalSymbolsSize; ++symbolIndex) {
234
- if (m_decimalSymbols[symbolIndex].length () && matches (input, position, m_decimalSymbols[symbolIndex])) {
235
- position += m_decimalSymbols[symbolIndex].length ();
236
- return symbolIndex;
237
- }
238
- }
239
- return DecimalSymbolsSize;
240
- }
241
-
242
- String LocaleICU::convertFromLocalizedNumber (const String& localized)
243
- {
244
- initializeDecimalFormat ();
245
- String input = localized.stripWhiteSpace ();
246
- if (!m_numberFormat || input.isEmpty ())
247
- return input;
248
-
249
- bool isNegative;
250
- unsigned startIndex;
251
- unsigned endIndex;
252
- if (!detectSignAndGetDigitRange (input, isNegative, startIndex, endIndex)) {
253
- // Input is broken. Returning an invalid number string.
254
- return " *" ;
255
- }
256
-
257
- StringBuilder builder;
258
- builder.reserveCapacity (input.length ());
259
- if (isNegative)
260
- builder.append (" -" );
261
- for (unsigned i = startIndex; i < endIndex;) {
262
- unsigned symbolIndex = matchedDecimalSymbolIndex (input, i);
263
- if (symbolIndex >= DecimalSymbolsSize)
264
- return " *" ;
265
- if (symbolIndex == DecimalSeparatorIndex)
266
- builder.append (' .' );
267
- else if (symbolIndex == GroupSeparatorIndex) {
268
- // Ignore group separators.
269
-
270
- } else
271
- builder.append (static_cast <UChar>(' 0' + symbolIndex));
272
- }
273
- return builder.toString ();
125
+ Vector<String, DecimalSymbolsSize> symbols;
126
+ symbols.append (decimalSymbol (UNUM_ZERO_DIGIT_SYMBOL));
127
+ symbols.append (decimalSymbol (UNUM_ONE_DIGIT_SYMBOL));
128
+ symbols.append (decimalSymbol (UNUM_TWO_DIGIT_SYMBOL));
129
+ symbols.append (decimalSymbol (UNUM_THREE_DIGIT_SYMBOL));
130
+ symbols.append (decimalSymbol (UNUM_FOUR_DIGIT_SYMBOL));
131
+ symbols.append (decimalSymbol (UNUM_FIVE_DIGIT_SYMBOL));
132
+ symbols.append (decimalSymbol (UNUM_SIX_DIGIT_SYMBOL));
133
+ symbols.append (decimalSymbol (UNUM_SEVEN_DIGIT_SYMBOL));
134
+ symbols.append (decimalSymbol (UNUM_EIGHT_DIGIT_SYMBOL));
135
+ symbols.append (decimalSymbol (UNUM_NINE_DIGIT_SYMBOL));
136
+ symbols.append (decimalSymbol (UNUM_DECIMAL_SEPARATOR_SYMBOL));
137
+ symbols.append (decimalSymbol (UNUM_GROUPING_SEPARATOR_SYMBOL));
138
+ ASSERT (symbols.size () == DecimalSymbolsSize);
139
+ setNumberLocalizerData (symbols, decimalTextAttribute (UNUM_POSITIVE_PREFIX), decimalTextAttribute (UNUM_POSITIVE_SUFFIX), decimalTextAttribute (UNUM_NEGATIVE_PREFIX), decimalTextAttribute (UNUM_NEGATIVE_SUFFIX));
274
140
}
275
141
276
142
bool LocaleICU::initializeShortDateFormat ()
@@ -501,13 +367,6 @@ unsigned LocaleICU::firstDayOfWeek()
501
367
#endif
502
368
503
369
#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
504
-
505
- String LocaleICU::localizedDecimalSeparator ()
506
- {
507
- initializeDecimalFormat ();
508
- return m_decimalSymbols[DecimalSeparatorIndex];
509
- }
510
-
511
370
static PassOwnPtr<Vector<String> > createFallbackAMPMLabels ()
512
371
{
513
372
OwnPtr<Vector<String> > labels = adoptPtr (new Vector<String>());
0 commit comments