• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From c6092cc576174fcbd433f3b34288255ef30f6510 Mon Sep 17 00:00:00 2001
2From: Andrew Hsieh <andrewhsieh@google.com>
3Date: Wed, 12 Feb 2014 20:07:01 +0800
4Subject: [PATCH 03/12] Fallback to locale("C")
5
6Android's newlocale() return null for anything other than "", "C",
7and "POSIX", fallback to "C" to allow more tests to run and uncover
8other issues.
9---
10 src/locale.cpp | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
11 1 file changed, 118 insertions(+), 6 deletions(-)
12
13diff --git a/src/locale.cpp b/src/locale.cpp
14index 66aaca9..0fba54d 100644
15--- a/src/locale.cpp
16+++ b/src/locale.cpp
17@@ -54,6 +54,10 @@ locale_t __cloc() {
18 }
19 #endif // __cloc_defined
20
21+inline locale_t __new_cloc() {
22+  return newlocale(LC_ALL_MASK, "C", 0);
23+}
24+
25 namespace {
26
27 struct release
28@@ -657,8 +661,14 @@ collate_byname<char>::collate_byname(const char* n, size_t refs)
29 {
30 #ifndef _LIBCPP_NO_EXCEPTIONS
31     if (__l == 0)
32+    {
33+#if !defined(__ANDROID__)
34         throw runtime_error("collate_byname<char>::collate_byname"
35                             " failed to construct for " + string(n));
36+#else
37+        __l = __new_cloc();
38+#endif
39+    }
40 #endif  // _LIBCPP_NO_EXCEPTIONS
41 }
42
43@@ -668,8 +678,14 @@ collate_byname<char>::collate_byname(const string& name, size_t refs)
44 {
45 #ifndef _LIBCPP_NO_EXCEPTIONS
46     if (__l == 0)
47+    {
48+#if !defined(__ANDROID__)
49         throw runtime_error("collate_byname<char>::collate_byname"
50                             " failed to construct for " + name);
51+#else
52+        __l = __new_cloc();
53+#endif
54+    }
55 #endif  // _LIBCPP_NO_EXCEPTIONS
56 }
57
58@@ -709,8 +725,14 @@ collate_byname<wchar_t>::collate_byname(const char* n, size_t refs)
59 {
60 #ifndef _LIBCPP_NO_EXCEPTIONS
61     if (__l == 0)
62+    {
63+#if !defined(__ANDROID__)
64         throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)"
65                             " failed to construct for " + string(n));
66+#else
67+        __l = __new_cloc();
68+#endif
69+    }
70 #endif  // _LIBCPP_NO_EXCEPTIONS
71 }
72
73@@ -720,8 +742,14 @@ collate_byname<wchar_t>::collate_byname(const string& name, size_t refs)
74 {
75 #ifndef _LIBCPP_NO_EXCEPTIONS
76     if (__l == 0)
77+    {
78+#if !defined(__ANDROID__)
79         throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)"
80                             " failed to construct for " + name);
81+#else
82+        __l = __new_cloc();
83+#endif
84+    }
85 #endif  // _LIBCPP_NO_EXCEPTIONS
86 }
87
88@@ -1117,8 +1145,14 @@ ctype_byname<char>::ctype_byname(const char* name, size_t refs)
89 {
90 #ifndef _LIBCPP_NO_EXCEPTIONS
91     if (__l == 0)
92+    {
93+#if !defined(__ANDROID__)
94         throw runtime_error("ctype_byname<char>::ctype_byname"
95                             " failed to construct for " + string(name));
96+#else
97+        __l = __new_cloc();
98+#endif
99+    }
100 #endif  // _LIBCPP_NO_EXCEPTIONS
101 }
102
103@@ -1128,8 +1162,14 @@ ctype_byname<char>::ctype_byname(const string& name, size_t refs)
104 {
105 #ifndef _LIBCPP_NO_EXCEPTIONS
106     if (__l == 0)
107+    {
108+#if !defined(__ANDROID__)
109         throw runtime_error("ctype_byname<char>::ctype_byname"
110                             " failed to construct for " + name);
111+#else
112+        __l = __new_cloc();
113+#endif
114+    }
115 #endif  // _LIBCPP_NO_EXCEPTIONS
116 }
117
118@@ -1174,8 +1214,14 @@ ctype_byname<wchar_t>::ctype_byname(const char* name, size_t refs)
119 {
120 #ifndef _LIBCPP_NO_EXCEPTIONS
121     if (__l == 0)
122+    {
123+#if !defined(__ANDROID__)
124         throw runtime_error("ctype_byname<wchar_t>::ctype_byname"
125                             " failed to construct for " + string(name));
126+#else
127+        __l = __new_cloc();
128+#endif
129+    }
130 #endif  // _LIBCPP_NO_EXCEPTIONS
131 }
132
133@@ -1185,8 +1231,14 @@ ctype_byname<wchar_t>::ctype_byname(const string& name, size_t refs)
134 {
135 #ifndef _LIBCPP_NO_EXCEPTIONS
136     if (__l == 0)
137+    {
138+#if !defined(__ANDROID__)
139         throw runtime_error("ctype_byname<wchar_t>::ctype_byname"
140                             " failed to construct for " + name);
141+#else
142+        __l = __new_cloc();
143+#endif
144+    }
145 #endif  // _LIBCPP_NO_EXCEPTIONS
146 }
147
148@@ -1455,8 +1507,14 @@ codecvt<wchar_t, char, mbstate_t>::codecvt(const char* nm, size_t refs)
149 {
150 #ifndef _LIBCPP_NO_EXCEPTIONS
151     if (__l == 0)
152+    {
153+#if !defined(__ANDROID__)
154         throw runtime_error("codecvt_byname<wchar_t, char, mbstate_t>::codecvt_byname"
155                             " failed to construct for " + string(nm));
156+#else
157+        __l = __new_cloc();
158+#endif
159+    }
160 #endif  // _LIBCPP_NO_EXCEPTIONS
161 }
162
163@@ -4250,7 +4308,12 @@ numpunct_byname<char>::__init(const char* nm)
164 {
165     if (strcmp(nm, "C") != 0)
166     {
167-        __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
168+        locale_t l = newlocale(LC_ALL_MASK, nm, 0);
169+#if defined(__ANDROID__)
170+        if (l == 0)
171+            l = __new_cloc();
172+#endif
173+        __locale_unique_ptr loc(l, freelocale);
174 #ifndef _LIBCPP_NO_EXCEPTIONS
175         if (loc == nullptr)
176             throw runtime_error("numpunct_byname<char>::numpunct_byname"
177@@ -4293,7 +4356,12 @@ numpunct_byname<wchar_t>::__init(const char* nm)
178 {
179     if (strcmp(nm, "C") != 0)
180     {
181-        __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
182+        locale_t l = newlocale(LC_ALL_MASK, nm, 0);
183+#if defined(__ANDROID__)
184+        if (l == 0)
185+            l = __new_cloc();
186+#endif
187+        __locale_unique_ptr loc(l, freelocale);
188 #ifndef _LIBCPP_NO_EXCEPTIONS
189         if (loc == nullptr)
190             throw runtime_error("numpunct_byname<char>::numpunct_byname"
191@@ -4707,8 +4775,14 @@ __time_get::__time_get(const char* nm)
192 {
193 #ifndef _LIBCPP_NO_EXCEPTIONS
194     if (__loc_ == 0)
195+    {
196+#if !defined(__ANDROID__)
197         throw runtime_error("time_get_byname"
198                             " failed to construct for " + string(nm));
199+#else
200+        __loc_ = __new_cloc();
201+#endif
202+    }
203 #endif  // _LIBCPP_NO_EXCEPTIONS
204 }
205
206@@ -4717,8 +4791,14 @@ __time_get::__time_get(const string& nm)
207 {
208 #ifndef _LIBCPP_NO_EXCEPTIONS
209     if (__loc_ == 0)
210+    {
211+# if !defined(__ANDROID__)
212         throw runtime_error("time_get_byname"
213                             " failed to construct for " + nm);
214+#else
215+        __loc_ = __new_cloc();
216+#endif
217+    }
218 #endif  // _LIBCPP_NO_EXCEPTIONS
219 }
220
221@@ -5395,8 +5475,14 @@ __time_put::__time_put(const char* nm)
222 {
223 #ifndef _LIBCPP_NO_EXCEPTIONS
224     if (__loc_ == 0)
225+    {
226+# if !defined(__ANDROID__)
227         throw runtime_error("time_put_byname"
228                             " failed to construct for " + string(nm));
229+#else
230+        __loc_ = __new_cloc();
231+#endif
232+    }
233 #endif  // _LIBCPP_NO_EXCEPTIONS
234 }
235
236@@ -5405,8 +5491,14 @@ __time_put::__time_put(const string& nm)
237 {
238 #ifndef _LIBCPP_NO_EXCEPTIONS
239     if (__loc_ == 0)
240+    {
241+# if !defined(__ANDROID__)
242         throw runtime_error("time_put_byname"
243                             " failed to construct for " + nm);
244+#else
245+        __loc_ = __new_cloc();
246+#endif
247+    }
248 #endif  // _LIBCPP_NO_EXCEPTIONS
249 }
250
251@@ -5825,7 +5917,12 @@ void
252 moneypunct_byname<char, false>::init(const char* nm)
253 {
254     typedef moneypunct<char, false> base;
255-    __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
256+    locale_t l = newlocale(LC_ALL_MASK, nm, 0);
257+#if defined(__ANDROID__)
258+    if (l == 0)
259+        l = __new_cloc();
260+#endif
261+    __locale_unique_ptr loc(l, freelocale);
262 #ifndef _LIBCPP_NO_EXCEPTIONS
263     if (loc == nullptr)
264         throw runtime_error("moneypunct_byname"
265@@ -5873,7 +5970,12 @@ void
266 moneypunct_byname<char, true>::init(const char* nm)
267 {
268     typedef moneypunct<char, true> base;
269-    __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
270+    locale_t l = newlocale(LC_ALL_MASK, nm, 0);
271+#if defined(__ANDROID__)
272+    if (l == 0)
273+        l = __new_cloc();
274+#endif
275+    __locale_unique_ptr loc(l, freelocale);
276 #ifndef _LIBCPP_NO_EXCEPTIONS
277     if (loc == nullptr)
278         throw runtime_error("moneypunct_byname"
279@@ -5938,7 +6040,12 @@ void
280 moneypunct_byname<wchar_t, false>::init(const char* nm)
281 {
282     typedef moneypunct<wchar_t, false> base;
283-    __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
284+    locale_t l = newlocale(LC_ALL_MASK, nm, 0);
285+#if defined(__ANDROID__)
286+    if (l == 0)
287+        l = __new_cloc();
288+#endif
289+    __locale_unique_ptr loc(l, freelocale);
290 #ifndef _LIBCPP_NO_EXCEPTIONS
291     if (loc == nullptr)
292         throw runtime_error("moneypunct_byname"
293@@ -6021,7 +6128,12 @@ void
294 moneypunct_byname<wchar_t, true>::init(const char* nm)
295 {
296     typedef moneypunct<wchar_t, true> base;
297-    __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
298+    locale_t l = newlocale(LC_ALL_MASK, nm, 0);
299+#if defined(__ANDROID__)
300+    if (l == 0)
301+        l = __new_cloc();
302+#endif
303+    __locale_unique_ptr loc(l, freelocale);
304 #ifndef _LIBCPP_NO_EXCEPTIONS
305     if (loc == nullptr)
306         throw runtime_error("moneypunct_byname"
307--
3081.9.1.423.g4596e3a
309
310