WebKit Bugzilla
New
Browse
Search+
Log In
×
Sign in with GitHub
or
Remember my login
Create Account
·
Forgot Password
Forgotten password account recovery
[patch]
Patch
bug-92976-20120802205530.patch (text/plain), 18.50 KB, created by
Kent Tamura
on 2012-08-02 04:55:49 PDT
(
hide
)
Description:
Patch
Filename:
MIME Type:
Creator:
Kent Tamura
Created:
2012-08-02 04:55:49 PDT
Size:
18.50 KB
patch
obsolete
>Subversion Revision: 124437 >diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog >index 3cfb1e2f99d3abb002ae2d94ddffbead4662cbe7..41c230427899fdb19a6d48ec04b701d63c0738b1 100644 >--- a/Source/WebCore/ChangeLog >+++ b/Source/WebCore/ChangeLog >@@ -1,3 +1,36 @@ >+2012-08-02 Kent Tamura <tkent@chromium.org> >+ >+ Move number localization code in LocaleICU.cpp to new class >+ https://bugs.webkit.org/show_bug.cgi?id=92976 >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ The number localization code by character mapping is usefull for non-ICU >+ platforms. >+ >+ No new tests. This is just a refactoring. >+ >+ * WebCore.gypi: Add NumberLocalizer.{cpp,h}. >+ * platform/text/LocaleICU.cpp: >+ (WebCore::LocaleICU::initializeNumberLocalizerData): >+ Renamed from initializeDecimalFormat. >+ (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::convertToLocalizedNumber): >+ (WebCore::matches): >+ (WebCore::NumberLocalizer::detectSignAndGetDigitRange): >+ (WebCore::NumberLocalizer::matchedDecimalSymbolIndex): >+ (WebCore::NumberLocalizer::convertFromLocalizedNumber): >+ * platform/text/NumberLocalizer.h: Added. >+ (WebCore): >+ (NumberLocalizer): >+ (WebCore::NumberLocalizer::NumberLocalizer): >+ > 2012-08-02 Yury Semikhatsky <yurys@chromium.org> > > Web Inspector: rename reportMemoryUsage to reportDescendantMemoryUsage in StyleRuleBase descendants >diff --git a/Source/WebCore/WebCore.gypi b/Source/WebCore/WebCore.gypi >index 900516ec5bc3081fa6f696a267f287ee791c2a58..a89fd251a794451ba67e688dd81db934fc096776 100644 >--- a/Source/WebCore/WebCore.gypi >+++ b/Source/WebCore/WebCore.gypi >@@ -4431,6 +4431,8 @@ > 'platform/text/LocalizedNumber.h', > 'platform/text/LocalizedNumberICU.cpp', > 'platform/text/LocalizedNumberNone.cpp', >+ 'platform/text/NumberLocalizer.cpp', >+ 'platform/text/NumberLocalizer.h', > 'platform/text/ParserUtilities.h', > 'platform/text/QuotedPrintable.h', > 'platform/text/QuotedPrintable.cpp', >diff --git a/Source/WebCore/platform/text/LocaleICU.cpp b/Source/WebCore/platform/text/LocaleICU.cpp >index 51ba4aa35eec454d61f94d1a66ba8883e9079b79..ba402c888490fbf37c86b4f0a969606a17c9aa4c 100644 >--- a/Source/WebCore/platform/text/LocaleICU.cpp >+++ b/Source/WebCore/platform/text/LocaleICU.cpp >@@ -112,7 +112,7 @@ void LocaleICU::setDecimalTextAttribute(String& destination, UNumberFormatTextAt > destination = String::adopt(buffer); > } > >-void LocaleICU::initializeDecimalFormat() >+void LocaleICU::initializeNumberLocalizerData() > { > if (m_didCreateDecimalFormat) > return; >@@ -139,138 +139,7 @@ void LocaleICU::initializeDecimalFormat() > setDecimalTextAttribute(m_negativePrefix, UNUM_NEGATIVE_PREFIX); > setDecimalTextAttribute(m_negativeSuffix, UNUM_NEGATIVE_SUFFIX); > ASSERT(!m_positivePrefix.isEmpty() || !m_positiveSuffix.isEmpty() || !m_negativePrefix.isEmpty() || !m_negativeSuffix.isEmpty()); >-} >- >-String LocaleICU::convertToLocalizedNumber(const String& input) >-{ >- initializeDecimalFormat(); >- if (!m_numberFormat || input.isEmpty()) >- return input; >- >- unsigned i = 0; >- bool isNegative = false; >- UnicodeString ustring; >- StringBuilder builder; >- builder.reserveCapacity(input.length()); >- >- if (input[0] == '-') { >- ++i; >- isNegative = true; >- builder.append(m_negativePrefix); >- } else >- builder.append(m_positivePrefix); >- >- for (; i < input.length(); ++i) { >- switch (input[i]) { >- case '0': >- case '1': >- case '2': >- case '3': >- case '4': >- case '5': >- case '6': >- case '7': >- case '8': >- case '9': >- builder.append(m_decimalSymbols[input[i] - '0']); >- break; >- case '.': >- builder.append(m_decimalSymbols[DecimalSeparatorIndex]); >- break; >- default: >- ASSERT_NOT_REACHED(); >- } >- } >- >- builder.append(isNegative ? m_negativeSuffix : m_positiveSuffix); >- >- return builder.toString(); >-} >- >-static bool matches(const String& text, unsigned position, const String& part) >-{ >- if (part.isEmpty()) >- return true; >- if (position + part.length() > text.length()) >- return false; >- for (unsigned i = 0; i < part.length(); ++i) { >- if (text[position + i] != part[i]) >- return false; >- } >- return true; >-} >- >-bool LocaleICU::detectSignAndGetDigitRange(const String& input, bool& isNegative, unsigned& startIndex, unsigned& endIndex) >-{ >- startIndex = 0; >- endIndex = input.length(); >- if (m_negativePrefix.isEmpty() && m_negativeSuffix.isEmpty()) { >- if (input.startsWith(m_positivePrefix) && input.endsWith(m_positiveSuffix)) { >- isNegative = false; >- startIndex = m_positivePrefix.length(); >- endIndex -= m_positiveSuffix.length(); >- } else >- isNegative = true; >- } else { >- if (input.startsWith(m_negativePrefix) && input.endsWith(m_negativeSuffix)) { >- isNegative = true; >- startIndex = m_negativePrefix.length(); >- endIndex -= m_negativeSuffix.length(); >- } else { >- isNegative = false; >- if (input.startsWith(m_positivePrefix) && input.endsWith(m_positiveSuffix)) { >- startIndex = m_positivePrefix.length(); >- endIndex -= m_positiveSuffix.length(); >- } else >- return false; >- } >- } >- return true; >-} >- >-unsigned LocaleICU::matchedDecimalSymbolIndex(const String& input, unsigned& position) >-{ >- for (unsigned symbolIndex = 0; symbolIndex < DecimalSymbolsSize; ++symbolIndex) { >- if (m_decimalSymbols[symbolIndex].length() && matches(input, position, m_decimalSymbols[symbolIndex])) { >- position += m_decimalSymbols[symbolIndex].length(); >- return symbolIndex; >- } >- } >- return DecimalSymbolsSize; >-} >- >-String LocaleICU::convertFromLocalizedNumber(const String& localized) >-{ >- initializeDecimalFormat(); >- String input = localized.stripWhiteSpace(); >- if (!m_numberFormat || input.isEmpty()) >- return input; >- >- bool isNegative; >- unsigned startIndex; >- unsigned endIndex; >- if (!detectSignAndGetDigitRange(input, isNegative, startIndex, endIndex)) { >- // Input is broken. Returning an invalid number string. >- return "*"; >- } >- >- StringBuilder builder; >- builder.reserveCapacity(input.length()); >- if (isNegative) >- builder.append("-"); >- for (unsigned i = startIndex; i < endIndex;) { >- unsigned symbolIndex = matchedDecimalSymbolIndex(input, i); >- if (symbolIndex >= DecimalSymbolsSize) >- return "*"; >- if (symbolIndex == DecimalSeparatorIndex) >- builder.append('.'); >- else if (symbolIndex == GroupSeparatorIndex) { >- // Ignore group separators. >- >- } else >- builder.append(static_cast<UChar>('0' + symbolIndex)); >- } >- return builder.toString(); >+ m_hasNumberLocalizerData = true; > } > > bool LocaleICU::initializeShortDateFormat() >@@ -504,7 +373,7 @@ unsigned LocaleICU::firstDayOfWeek() > > String LocaleICU::localizedDecimalSeparator() > { >- initializeDecimalFormat(); >+ initializeNumberLocalizerData(); > return m_decimalSymbols[DecimalSeparatorIndex]; > } > >diff --git a/Source/WebCore/platform/text/LocaleICU.h b/Source/WebCore/platform/text/LocaleICU.h >index b50cf44cf9e590b4e2ddb6d183d8eaa575e6a522..0a77480bfd8181802aa2daf292cd78ca40ff4026 100644 >--- a/Source/WebCore/platform/text/LocaleICU.h >+++ b/Source/WebCore/platform/text/LocaleICU.h >@@ -32,6 +32,7 @@ > #define LocaleICU_h > > #include "DateComponents.h" >+#include "NumberLocalizer.h" > #include <unicode/udat.h> > #include <unicode/unum.h> > #include <wtf/Forward.h> >@@ -43,15 +44,12 @@ namespace WebCore { > > // We should use this class only for LocalizedNumberICU.cpp, LocalizedDateICU.cpp, > // and LocalizedNumberICUTest.cpp. >-class LocaleICU { >+class LocaleICU : public NumberLocalizer { > public: > static PassOwnPtr<LocaleICU> create(const char* localeString); > static LocaleICU* currentLocale(); >- ~LocaleICU(); >+ virtual ~LocaleICU(); > >- // For LocalizedNumber >- String convertToLocalizedNumber(const String&); >- String convertFromLocalizedNumber(const String&); > #if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS) > String localizedDecimalSeparator(); > #endif >@@ -78,7 +76,7 @@ private: > explicit LocaleICU(const char*); > void setDecimalSymbol(unsigned index, UNumberFormatSymbol); > void setDecimalTextAttribute(String&, UNumberFormatTextAttribute); >- void initializeDecimalFormat(); >+ virtual void initializeNumberLocalizerData() OVERRIDE; > > bool detectSignAndGetDigitRange(const String& input, bool& isNegative, unsigned& startIndex, unsigned& endIndex); > unsigned matchedDecimalSymbolIndex(const String& input, unsigned& position); >@@ -102,17 +100,6 @@ private: > CString m_locale; > UNumberFormat* m_numberFormat; > UDateFormat* m_shortDateFormat; >- enum { >- // 0-9 for digits. >- DecimalSeparatorIndex = 10, >- GroupSeparatorIndex = 11, >- DecimalSymbolsSize >- }; >- String m_decimalSymbols[DecimalSymbolsSize]; >- String m_positivePrefix; >- String m_positiveSuffix; >- String m_negativePrefix; >- String m_negativeSuffix; > bool m_didCreateDecimalFormat; > bool m_didCreateShortDateFormat; > >diff --git a/Source/WebCore/platform/text/NumberLocalizer.cpp b/Source/WebCore/platform/text/NumberLocalizer.cpp >new file mode 100644 >index 0000000000000000000000000000000000000000..fdd87dd8e6e0db6411190cf96e91fccb29614009 >--- /dev/null >+++ b/Source/WebCore/platform/text/NumberLocalizer.cpp >@@ -0,0 +1,173 @@ >+/* >+ * Copyright (C) 2011 Google Inc. All rights reserved. >+ * >+ * Redistribution and use in source and binary forms, with or without >+ * modification, are permitted provided that the following conditions are >+ * met: >+ * >+ * * Redistributions of source code must retain the above copyright >+ * notice, this list of conditions and the following disclaimer. >+ * * Redistributions in binary form must reproduce the above >+ * copyright notice, this list of conditions and the following disclaimer >+ * in the documentation and/or other materials provided with the >+ * distribution. >+ * * Neither the name of Google Inc. nor the names of its >+ * contributors may be used to endorse or promote products derived from >+ * this software without specific prior written permission. >+ * >+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS >+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT >+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR >+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT >+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, >+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT >+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, >+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY >+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT >+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE >+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. >+ */ >+ >+#include "config.h" >+#include "NumberLocalizer.h" >+ >+#include <wtf/text/StringBuilder.h> >+ >+namespace WebCore { >+ >+NumberLocalizer::~NumberLocalizer() >+{ >+} >+ >+String NumberLocalizer::convertToLocalizedNumber(const String& input) >+{ >+ initializeNumberLocalizerData(); >+ if (!m_hasNumberLocalizerData || input.isEmpty()) >+ return input; >+ >+ unsigned i = 0; >+ bool isNegative = false; >+ StringBuilder builder; >+ builder.reserveCapacity(input.length()); >+ >+ if (input[0] == '-') { >+ ++i; >+ isNegative = true; >+ builder.append(m_negativePrefix); >+ } else >+ builder.append(m_positivePrefix); >+ >+ for (; i < input.length(); ++i) { >+ switch (input[i]) { >+ case '0': >+ case '1': >+ case '2': >+ case '3': >+ case '4': >+ case '5': >+ case '6': >+ case '7': >+ case '8': >+ case '9': >+ builder.append(m_decimalSymbols[input[i] - '0']); >+ break; >+ case '.': >+ builder.append(m_decimalSymbols[DecimalSeparatorIndex]); >+ break; >+ default: >+ ASSERT_NOT_REACHED(); >+ } >+ } >+ >+ builder.append(isNegative ? m_negativeSuffix : m_positiveSuffix); >+ >+ return builder.toString(); >+} >+ >+static bool matches(const String& text, unsigned position, const String& part) >+{ >+ if (part.isEmpty()) >+ return true; >+ if (position + part.length() > text.length()) >+ return false; >+ for (unsigned i = 0; i < part.length(); ++i) { >+ if (text[position + i] != part[i]) >+ return false; >+ } >+ return true; >+} >+ >+bool NumberLocalizer::detectSignAndGetDigitRange(const String& input, bool& isNegative, unsigned& startIndex, unsigned& endIndex) >+{ >+ startIndex = 0; >+ endIndex = input.length(); >+ if (m_negativePrefix.isEmpty() && m_negativeSuffix.isEmpty()) { >+ if (input.startsWith(m_positivePrefix) && input.endsWith(m_positiveSuffix)) { >+ isNegative = false; >+ startIndex = m_positivePrefix.length(); >+ endIndex -= m_positiveSuffix.length(); >+ } else >+ isNegative = true; >+ } else { >+ if (input.startsWith(m_negativePrefix) && input.endsWith(m_negativeSuffix)) { >+ isNegative = true; >+ startIndex = m_negativePrefix.length(); >+ endIndex -= m_negativeSuffix.length(); >+ } else { >+ isNegative = false; >+ if (input.startsWith(m_positivePrefix) && input.endsWith(m_positiveSuffix)) { >+ startIndex = m_positivePrefix.length(); >+ endIndex -= m_positiveSuffix.length(); >+ } else >+ return false; >+ } >+ } >+ return true; >+} >+ >+unsigned NumberLocalizer::matchedDecimalSymbolIndex(const String& input, unsigned& position) >+{ >+ for (unsigned symbolIndex = 0; symbolIndex < DecimalSymbolsSize; ++symbolIndex) { >+ if (m_decimalSymbols[symbolIndex].length() && matches(input, position, m_decimalSymbols[symbolIndex])) { >+ position += m_decimalSymbols[symbolIndex].length(); >+ return symbolIndex; >+ } >+ } >+ return DecimalSymbolsSize; >+} >+ >+String NumberLocalizer::convertFromLocalizedNumber(const String& localized) >+{ >+ initializeNumberLocalizerData(); >+ String input = localized.stripWhiteSpace(); >+ if (!m_hasNumberLocalizerData || input.isEmpty()) >+ return input; >+ >+ bool isNegative; >+ unsigned startIndex; >+ unsigned endIndex; >+ if (!detectSignAndGetDigitRange(input, isNegative, startIndex, endIndex)) { >+ // Input is broken. Returning an invalid number string. >+ return "*"; >+ } >+ >+ StringBuilder builder; >+ builder.reserveCapacity(input.length()); >+ if (isNegative) >+ builder.append("-"); >+ for (unsigned i = startIndex; i < endIndex;) { >+ unsigned symbolIndex = matchedDecimalSymbolIndex(input, i); >+ if (symbolIndex >= DecimalSymbolsSize) >+ return "*"; >+ if (symbolIndex == DecimalSeparatorIndex) >+ builder.append('.'); >+ else if (symbolIndex == GroupSeparatorIndex) { >+ // Ignore group separators. >+ >+ } else >+ builder.append(static_cast<UChar>('0' + symbolIndex)); >+ } >+ return builder.toString(); >+} >+ >+} >diff --git a/Source/WebCore/platform/text/NumberLocalizer.h b/Source/WebCore/platform/text/NumberLocalizer.h >new file mode 100644 >index 0000000000000000000000000000000000000000..eb04b58f08d2b82ec2509daaddf60ab58a6dd469 >--- /dev/null >+++ b/Source/WebCore/platform/text/NumberLocalizer.h >@@ -0,0 +1,62 @@ >+/* >+ * Copyright (C) 2012 Google Inc. All rights reserved. >+ * >+ * Redistribution and use in source and binary forms, with or without >+ * modification, are permitted provided that the following conditions >+ * are met: >+ * 1. Redistributions of source code must retain the above copyright >+ * notice, this list of conditions and the following disclaimer. >+ * 2. Redistributions in binary form must reproduce the above copyright >+ * notice, this list of conditions and the following disclaimer in the >+ * documentation and/or other materials provided with the distribution. >+ * >+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND >+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE >+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE >+ * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE >+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL >+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR >+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER >+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT >+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY >+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH >+ * DAMAGE. >+ */ >+ >+#ifndef NumberLocalizer_h >+#define NumberLocalizer_h >+ >+#include <wtf/text/WTFString.h> >+ >+namespace WebCore { >+ >+class NumberLocalizer { >+public: >+ String convertToLocalizedNumber(const String&); >+ String convertFromLocalizedNumber(const String&); >+ virtual ~NumberLocalizer(); >+ >+protected: >+ NumberLocalizer() : m_hasNumberLocalizerData(false) { } >+ virtual void initializeNumberLocalizerData() = 0; >+ >+ enum { >+ // 0-9 for digits. >+ DecimalSeparatorIndex = 10, >+ GroupSeparatorIndex = 11, >+ DecimalSymbolsSize >+ }; >+ String m_decimalSymbols[DecimalSymbolsSize]; >+ String m_positivePrefix; >+ String m_positiveSuffix; >+ String m_negativePrefix; >+ String m_negativeSuffix; >+ bool m_hasNumberLocalizerData; >+ >+private: >+ bool detectSignAndGetDigitRange(const String& input, bool& isNegative, unsigned& startIndex, unsigned& endIndex); >+ unsigned matchedDecimalSymbolIndex(const String& input, unsigned& position); >+}; >+ >+} >+#endif
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Formatted Diff
|
Diff
Attachments on
bug 92976
:
156041
|
156059