Skip to content

Commit f0d37e1

Browse files
committed
Move number localization code in LocaleICU.cpp to new class
https://bugs.webkit.org/show_bug.cgi?id=92976 Reviewed by Kentaro Hara. The number localization code by character mapping is usefull for non-ICU platforms. No new tests. This is just a refactoring, and is covered by Source/WebKit/chromium/tests/LocalizedNumberICUTest.cpp. * WebCore.gypi: Add NumberLocalizer.{cpp,h}. * platform/text/LocaleICU.cpp: (WebCore::LocaleICU::decimalSymbol): Renamed from setDecimalSymbol. This function returns the resultant string instead of setting it to a data member. (WebCore::LocaleICU::decimalTextAttribute): Renamed from setDecimalTextAttributel. This function returns the resultant string instead of setting it to the specified string. (WebCore::LocaleICU::initializeNumberLocalizerData): Renamed from initializeDecimalFormat. Calls NumberLocaizer::setNumberLocalizerData. (WebCore::LocaleICU::localizedDecimalSeparator): Rename initializeDecimalFormat to initializeNumberLocalizerData. * platform/text/LocaleICU.h: (LocaleICU): Remove some members, and inherit NumberLocalizer. * platform/text/NumberLocalizer.cpp: Added. Move the code from LocaleICU.cpp (WebCore): (WebCore::NumberLocalizer::~NumberLocalizer): (WebCore::NumberLocalizer::setNumberLocalizerData): Added. (WebCore::NumberLocalizer::convertToLocalizedNumber): (WebCore::matches): (WebCore::NumberLocalizer::detectSignAndGetDigitRange): (WebCore::NumberLocalizer::matchedDecimalSymbolIndex): (WebCore::NumberLocalizer::convertFromLocalizedNumber): (WebCore::NumberLocalizer::localizedDecimalSeparator): * platform/text/NumberLocalizer.h: Added. (NumberLocalizer): (WebCore::NumberLocalizer::NumberLocalizer): Canonical link: https://commits.webkit.org/110830@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@124459 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent b8b5d32 commit f0d37e1

File tree

6 files changed

+337
-189
lines changed

6 files changed

+337
-189
lines changed

Source/WebCore/ChangeLog

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
1+
2012-08-02 Kent Tamura <tkent@chromium.org>
2+
3+
Move number localization code in LocaleICU.cpp to new class
4+
https://bugs.webkit.org/show_bug.cgi?id=92976
5+
6+
Reviewed by Kentaro Hara.
7+
8+
The number localization code by character mapping is usefull for non-ICU
9+
platforms.
10+
11+
No new tests. This is just a refactoring, and is covered by
12+
Source/WebKit/chromium/tests/LocalizedNumberICUTest.cpp.
13+
14+
* WebCore.gypi: Add NumberLocalizer.{cpp,h}.
15+
* platform/text/LocaleICU.cpp:
16+
(WebCore::LocaleICU::decimalSymbol):
17+
Renamed from setDecimalSymbol. This function returns the resultant
18+
string instead of setting it to a data member.
19+
(WebCore::LocaleICU::decimalTextAttribute):
20+
Renamed from setDecimalTextAttributel. This function returns the
21+
resultant string instead of setting it to the specified string.
22+
(WebCore::LocaleICU::initializeNumberLocalizerData):
23+
Renamed from initializeDecimalFormat.
24+
Calls NumberLocaizer::setNumberLocalizerData.
25+
(WebCore::LocaleICU::localizedDecimalSeparator):
26+
Rename initializeDecimalFormat to initializeNumberLocalizerData.
27+
* platform/text/LocaleICU.h:
28+
(LocaleICU): Remove some members, and inherit NumberLocalizer.
29+
* platform/text/NumberLocalizer.cpp: Added. Move the code from LocaleICU.cpp
30+
(WebCore):
31+
(WebCore::NumberLocalizer::~NumberLocalizer):
32+
(WebCore::NumberLocalizer::setNumberLocalizerData): Added.
33+
(WebCore::NumberLocalizer::convertToLocalizedNumber):
34+
(WebCore::matches):
35+
(WebCore::NumberLocalizer::detectSignAndGetDigitRange):
36+
(WebCore::NumberLocalizer::matchedDecimalSymbolIndex):
37+
(WebCore::NumberLocalizer::convertFromLocalizedNumber):
38+
(WebCore::NumberLocalizer::localizedDecimalSeparator):
39+
* platform/text/NumberLocalizer.h: Added.
40+
(NumberLocalizer):
41+
(WebCore::NumberLocalizer::NumberLocalizer):
42+
143
2012-08-02 Alexander Pavlov <apavlov@chromium.org>
244

345
Web Inspector: Move DOM breakpoints-related context menu items into a submenu

Source/WebCore/WebCore.gypi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4431,6 +4431,8 @@
44314431
'platform/text/LocalizedNumber.h',
44324432
'platform/text/LocalizedNumberICU.cpp',
44334433
'platform/text/LocalizedNumberNone.cpp',
4434+
'platform/text/NumberLocalizer.cpp',
4435+
'platform/text/NumberLocalizer.h',
44344436
'platform/text/ParserUtilities.h',
44354437
'platform/text/QuotedPrintable.h',
44364438
'platform/text/QuotedPrintable.cpp',

Source/WebCore/platform/text/LocaleICU.cpp

Lines changed: 25 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2011 Google Inc. All rights reserved.
2+
* Copyright (C) 2011,2012 Google Inc. All rights reserved.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are
@@ -81,38 +81,38 @@ LocaleICU* LocaleICU::currentLocale()
8181
return currentLocale;
8282
}
8383

84-
void LocaleICU::setDecimalSymbol(unsigned index, UNumberFormatSymbol symbol)
84+
String LocaleICU::decimalSymbol(UNumberFormatSymbol symbol)
8585
{
8686
UErrorCode status = U_ZERO_ERROR;
8787
int32_t bufferLength = unum_getSymbol(m_numberFormat, symbol, 0, 0, &status);
8888
ASSERT(U_SUCCESS(status) || status == U_BUFFER_OVERFLOW_ERROR);
8989
if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR)
90-
return;
90+
return String();
9191
Vector<UChar> buffer(bufferLength);
9292
status = U_ZERO_ERROR;
9393
unum_getSymbol(m_numberFormat, symbol, buffer.data(), bufferLength, &status);
9494
if (U_FAILURE(status))
95-
return;
96-
m_decimalSymbols[index] = String::adopt(buffer);
95+
return String();
96+
return String::adopt(buffer);
9797
}
9898

99-
void LocaleICU::setDecimalTextAttribute(String& destination, UNumberFormatTextAttribute tag)
99+
String LocaleICU::decimalTextAttribute(UNumberFormatTextAttribute tag)
100100
{
101101
UErrorCode status = U_ZERO_ERROR;
102102
int32_t bufferLength = unum_getTextAttribute(m_numberFormat, tag, 0, 0, &status);
103103
ASSERT(U_SUCCESS(status) || status == U_BUFFER_OVERFLOW_ERROR);
104104
if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR)
105-
return;
105+
return String();
106106
Vector<UChar> buffer(bufferLength);
107107
status = U_ZERO_ERROR;
108108
unum_getTextAttribute(m_numberFormat, tag, buffer.data(), bufferLength, &status);
109109
ASSERT(U_SUCCESS(status));
110110
if (U_FAILURE(status))
111-
return;
112-
destination = String::adopt(buffer);
111+
return String();
112+
return String::adopt(buffer);
113113
}
114114

115-
void LocaleICU::initializeDecimalFormat()
115+
void LocaleICU::initializeNumberLocalizerData()
116116
{
117117
if (m_didCreateDecimalFormat)
118118
return;
@@ -122,155 +122,21 @@ void LocaleICU::initializeDecimalFormat()
122122
if (!U_SUCCESS(status))
123123
return;
124124

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));
274140
}
275141

276142
bool LocaleICU::initializeShortDateFormat()
@@ -501,13 +367,6 @@ unsigned LocaleICU::firstDayOfWeek()
501367
#endif
502368

503369
#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
504-
505-
String LocaleICU::localizedDecimalSeparator()
506-
{
507-
initializeDecimalFormat();
508-
return m_decimalSymbols[DecimalSeparatorIndex];
509-
}
510-
511370
static PassOwnPtr<Vector<String> > createFallbackAMPMLabels()
512371
{
513372
OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>());

Source/WebCore/platform/text/LocaleICU.h

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define LocaleICU_h
3333

3434
#include "DateComponents.h"
35+
#include "NumberLocalizer.h"
3536
#include <unicode/udat.h>
3637
#include <unicode/unum.h>
3738
#include <wtf/Forward.h>
@@ -43,18 +44,11 @@ namespace WebCore {
4344

4445
// We should use this class only for LocalizedNumberICU.cpp, LocalizedDateICU.cpp,
4546
// and LocalizedNumberICUTest.cpp.
46-
class LocaleICU {
47+
class LocaleICU : public NumberLocalizer {
4748
public:
4849
static PassOwnPtr<LocaleICU> create(const char* localeString);
4950
static LocaleICU* currentLocale();
50-
~LocaleICU();
51-
52-
// For LocalizedNumber
53-
String convertToLocalizedNumber(const String&);
54-
String convertFromLocalizedNumber(const String&);
55-
#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
56-
String localizedDecimalSeparator();
57-
#endif
51+
virtual ~LocaleICU();
5852

5953
// For LocalizedDate
6054
double parseLocalizedDate(const String&);
@@ -76,9 +70,9 @@ class LocaleICU {
7670
private:
7771
static PassOwnPtr<LocaleICU> createForCurrentLocale();
7872
explicit LocaleICU(const char*);
79-
void setDecimalSymbol(unsigned index, UNumberFormatSymbol);
80-
void setDecimalTextAttribute(String&, UNumberFormatTextAttribute);
81-
void initializeDecimalFormat();
73+
String decimalSymbol(UNumberFormatSymbol);
74+
String decimalTextAttribute(UNumberFormatTextAttribute);
75+
virtual void initializeNumberLocalizerData() OVERRIDE;
8276

8377
bool detectSignAndGetDigitRange(const String& input, bool& isNegative, unsigned& startIndex, unsigned& endIndex);
8478
unsigned matchedDecimalSymbolIndex(const String& input, unsigned& position);
@@ -102,17 +96,6 @@ class LocaleICU {
10296
CString m_locale;
10397
UNumberFormat* m_numberFormat;
10498
UDateFormat* m_shortDateFormat;
105-
enum {
106-
// 0-9 for digits.
107-
DecimalSeparatorIndex = 10,
108-
GroupSeparatorIndex = 11,
109-
DecimalSymbolsSize
110-
};
111-
String m_decimalSymbols[DecimalSymbolsSize];
112-
String m_positivePrefix;
113-
String m_positiveSuffix;
114-
String m_negativePrefix;
115-
String m_negativeSuffix;
11699
bool m_didCreateDecimalFormat;
117100
bool m_didCreateShortDateFormat;
118101

0 commit comments

Comments
 (0)