1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "config.h"
32 #include "LocalizedNumber.h"
33 
34 #include <limits>
35 #include <unicode/numfmt.h>
36 #include <unicode/parsepos.h>
37 #include <wtf/MathExtras.h>
38 #include <wtf/PassOwnPtr.h>
39 
40 using namespace icu;
41 using namespace std;
42 
43 namespace WebCore {
44 
createFormatterForCurrentLocale()45 static PassOwnPtr<NumberFormat> createFormatterForCurrentLocale()
46 {
47     UErrorCode status = U_ZERO_ERROR;
48     OwnPtr<NumberFormat> formatter = adoptPtr(NumberFormat::createInstance(status));
49     return U_SUCCESS(status) ? formatter.release() : nullptr;
50 }
51 
52 // This might return 0.
numberFormatter()53 static NumberFormat* numberFormatter()
54 {
55     ASSERT(isMainThread());
56     static NumberFormat* formatter = createFormatterForCurrentLocale().leakPtr();
57     return formatter;
58 }
59 
parseLocalizedNumber(const String & numberString)60 double parseLocalizedNumber(const String& numberString)
61 {
62     if (numberString.isEmpty())
63         return numeric_limits<double>::quiet_NaN();
64     NumberFormat* formatter = numberFormatter();
65     if (!formatter)
66         return numeric_limits<double>::quiet_NaN();
67     UnicodeString numberUnicodeString(numberString.characters(), numberString.length());
68     Formattable result;
69     ParsePosition position(0);
70     formatter->parse(numberUnicodeString, result, position);
71     if (position.getIndex() != numberUnicodeString.length())
72         return numeric_limits<double>::quiet_NaN();
73     UErrorCode status = U_ZERO_ERROR;
74     double numericResult = result.getDouble(status);
75     return U_SUCCESS(status) ? numericResult : numeric_limits<double>::quiet_NaN();
76 }
77 
formatLocalizedNumber(double number,unsigned fractionDigits)78 String formatLocalizedNumber(double number, unsigned fractionDigits)
79 {
80     NumberFormat* formatter = numberFormatter();
81     if (!formatter)
82         return String();
83     UnicodeString result;
84     formatter->setMaximumFractionDigits(clampToInteger(fractionDigits));
85     formatter->format(number, result);
86     return String(result.getBuffer(), result.length());
87 }
88 
89 } // namespace WebCore
90