summaryrefslogtreecommitdiff
blob: 5a1bd63ca03589f656a1adad9d0570223a83a0a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
commit f41e6b7f2a1c2f016e29ea440f781a7d90680b4f
Author: Petteri Räty <petsku@petteriraty.eu>
Date:   Mon May 30 23:33:08 2011 +0300

    locale.cpp improved linux support
    
    Use type_traits to get the type of locale_t underlying struct and use
    posix versions of functions. What is left to compile fully on Linux is
    figuring out how to support _DefaultRuneLocale.

diff --git a/src/locale.cpp b/src/locale.cpp
index f32ce96..138dbc2 100644
--- a/src/locale.cpp
+++ b/src/locale.cpp
@@ -14,6 +14,7 @@
 #include "algorithm"
 #include "algorithm"
 #include "typeinfo"
+#include "type_traits"
 #include "clocale"
 #include "cstring"
 #include "cwctype"
@@ -21,6 +22,97 @@
 #include <langinfo.h>
 #include <stdlib.h>
 
+namespace {
+  typedef std::remove_pointer<locale_t>::type locale_struct;
+  typedef std::unique_ptr<locale_struct, decltype(&freelocale)> locale_unique_ptr;
+  typedef std::unique_ptr<locale_struct, decltype(&uselocale)> locale_raii;
+}
+
+namespace with_locale { namespace {
+#ifdef __APPLE__
+  using ::btowc_l;
+  using ::wctob_l;
+  using ::wcsnrtombs_l;
+  using ::wcrtomb_l;
+  using ::mbsnrtowcs_l;
+  using ::mbrtowc_l;
+  using ::mbtowc_l;
+  using ::mbrlen_l;
+  using ::localeconv_l;
+  using ::mbsrtowcs_l;
+
+  decltype(MB_CUR_MAX_L)
+  mb_cur_max_l(locale_t loc)
+  {
+    return MB_CUR_MAX_L(loc);
+  }
+#else
+  template
+  <typename Function, typename ...Args>
+  auto using_locale(Function f, locale_t loc, Args&&... params) -> decltype(f(std::forward<Args>(params)...))
+  {
+    locale_raii current(uselocale(loc), uselocale);
+    return f(std::forward<Args>(params)...);
+  }
+
+  decltype(MB_CUR_MAX)
+  mb_cur_max_l(locale_t loc)
+  {
+    locale_raii current(uselocale(loc), uselocale);
+    return MB_CUR_MAX;
+  }
+
+  wint_t btowc_l(int c, locale_t l) { return using_locale(&btowc, l, c); }
+  int wctob_l(wint_t c, locale_t l) { return using_locale(&wctob, l, c); }
+  size_t wcsnrtombs_l(char * dest,
+                      const wchar_t * * src,
+                      size_t nwc,
+                      size_t len,
+                      mbstate_t * ps,
+                      locale_t l)
+  {
+    return using_locale(&wcsnrtombs, l, dest, src, nwc, len, ps);
+  }
+  size_t wcrtomb_l(char *s, wchar_t wc, mbstate_t *ps, locale_t l)
+  {
+    return using_locale(&wcrtomb, l, s, wc, ps);
+  }
+  size_t mbsnrtowcs_l(wchar_t * dest,
+                      const char * * src,
+                      size_t nms,
+                      size_t len,
+                      mbstate_t * ps,
+                      locale_t l)
+  {
+    return using_locale(&mbsnrtowcs, l, dest, src, nms, len, ps);
+  }
+  size_t mbrtowc_l(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps, locale_t l)
+  {
+    return using_locale(&mbrtowc, l, pwc, s, n, ps);
+  }
+  int mbtowc_l(wchar_t * pwc, const char * pmb, size_t max, locale_t l)
+  {
+    return using_locale(&mbtowc, l, pwc, pmb, max);
+  }
+  size_t mbrlen_l(const char *s, size_t n, mbstate_t *ps, locale_t l)
+  {
+    return using_locale(&mbrlen, l, s, n, ps);
+  }
+  struct lconv *localeconv_l(locale_t l)
+  {
+    return using_locale(&localeconv, l);
+  }
+  size_t mbsrtowcs_l(wchar_t * dest,
+                     const char * * src,
+                     size_t len,
+                     mbstate_t * ps,
+                     locale_t l)
+  {
+    return using_locale(&mbsrtowcs, l, dest, src, len, ps);
+  }
+#endif
+} }
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 namespace {
@@ -1082,49 +1174,33 @@ ctype_byname<wchar_t>::do_tolower(char_type* low, const char_type* high) const
 wchar_t
 ctype_byname<wchar_t>::do_widen(char c) const
 {
-#ifdef __APPLE__
-    return btowc_l(c, __l);
-#else
-    return 0;
-#endif
+    return with_locale::btowc_l(c, __l);
 }
 
 const char*
 ctype_byname<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const
 {
-#ifdef __APPLE__
     for (; low != high; ++low, ++dest)
-        *dest = btowc_l(*low, __l);
+        *dest = with_locale::btowc_l(*low, __l);
     return low;
-#else
-    return NULL;
-#endif
 }
 
 char
 ctype_byname<wchar_t>::do_narrow(char_type c, char dfault) const
 {
-#ifdef __APPLE__
-    int r = wctob_l(c, __l);
+    int r = with_locale::wctob_l(c, __l);
     return r != WEOF ? static_cast<char>(r) : dfault;
-#else
-    return 0;
-#endif
 }
 
 const wchar_t*
 ctype_byname<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
 {
-#ifdef __APPLE__
     for (; low != high; ++low, ++dest)
     {
-        int r = wctob_l(*low, __l);
+        int r = with_locale::wctob_l(*low, __l);
         *dest = r != WEOF ? static_cast<char>(r) : dfault;
     }
     return low;
-#else
-    return NULL;
-#endif
 }
 
 // template <> class codecvt<char, char, mbstate_t>
@@ -1220,7 +1296,6 @@ codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st,
     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
 {
-#ifdef __APPLE__
     // look for first internal null in frm
     const intern_type* fend = frm;
     for (; fend != frm_end; ++fend)
@@ -1232,13 +1307,13 @@ codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st,
     {
         // save state in case needed to reover to_nxt on error
         mbstate_t save_state = st;
-        size_t n = wcsnrtombs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l);
+        size_t n = with_locale::wcsnrtombs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l);
         if (n == size_t(-1))
         {
             // need to recover to_nxt
             for (to_nxt = to; frm != frm_nxt; ++frm)
             {
-                n = wcrtomb_l(to_nxt, *frm, &save_state, __l);
+                n = with_locale::wcrtomb_l(to_nxt, *frm, &save_state, __l);
                 if (n == size_t(-1))
                     break;
                 to_nxt += n;
@@ -1255,7 +1330,7 @@ codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st,
         {
             // Try to write the terminating null
             extern_type tmp[MB_LEN_MAX];
-            n = wcrtomb_l(tmp, intern_type(), &st, __l);
+            n = with_locale::wcrtomb_l(tmp, intern_type(), &st, __l);
             if (n == size_t(-1))  // on error
                 return error;
             if (n > to_end-to_nxt)  // is there room?
@@ -1270,9 +1345,6 @@ codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st,
         }
     }
     return frm_nxt == frm_end ? ok : partial;
-#else
-    return error;
-#endif
 }
 
 codecvt<wchar_t, char, mbstate_t>::result
@@ -1280,7 +1352,6 @@ codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st,
     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
 {
-#ifdef __APPLE__
     // look for first internal null in frm
     const extern_type* fend = frm;
     for (; fend != frm_end; ++fend)
@@ -1292,13 +1363,13 @@ codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st,
     {
         // save state in case needed to reover to_nxt on error
         mbstate_t save_state = st;
-        size_t n = mbsnrtowcs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l);
+        size_t n = with_locale::mbsnrtowcs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l);
         if (n == size_t(-1))
         {
             // need to recover to_nxt
             for (to_nxt = to; frm != frm_nxt; ++to_nxt)
             {
-                n = mbrtowc_l(to_nxt, frm, fend-frm, &save_state, __l);
+                n = with_locale::mbrtowc_l(to_nxt, frm, fend-frm, &save_state, __l);
                 switch (n)
                 {
                 case 0:
@@ -1326,7 +1397,7 @@ codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st,
         if (fend != frm_end)  // set up next null terminated sequence
         {
             // Try to write the terminating null
-            n = mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l);
+            n = with_locale::mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l);
             if (n != 0)  // on error
                 return error;
             ++to_nxt;
@@ -1338,19 +1409,15 @@ codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st,
         }
     }
     return frm_nxt == frm_end ? ok : partial;
-#else
-    return error;
-#endif
 }
 
 codecvt<wchar_t, char, mbstate_t>::result
 codecvt<wchar_t, char, mbstate_t>::do_unshift(state_type& st,
     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
 {
-#ifdef __APPLE__
     to_nxt = to;
     extern_type tmp[MB_LEN_MAX];
-    size_t n = wcrtomb_l(tmp, intern_type(), &st, __l);
+    size_t n = with_locale::wcrtomb_l(tmp, intern_type(), &st, __l);
     if (n == size_t(-1) || n == 0)  // on error
         return error;
     --n;
@@ -1359,26 +1426,19 @@ codecvt<wchar_t, char, mbstate_t>::do_unshift(state_type& st,
     for (extern_type* p = tmp; n; --n)  // write it
         *to_nxt++ = *p++;
     return ok;
-#else
-    return error;
-#endif
 }
 
 int
 codecvt<wchar_t, char, mbstate_t>::do_encoding() const  _NOEXCEPT
 {
-#ifdef __APPLE__
-    if (mbtowc_l(0, 0, MB_LEN_MAX, __l) == 0)
+    if (with_locale::mbtowc_l((wchar_t*) 0, (const char*) 0, MB_LEN_MAX, __l) == 0)
     {
         // stateless encoding
-        if (__l == 0 || MB_CUR_MAX_L(__l) == 1)  // there are no known constant length encodings
+        if (__l == 0 || with_locale::mb_cur_max_l(__l) == 1)  // there are no known constant length encodings
             return 1;                // which take more than 1 char to form a wchar_t
          return 0;
     }
     return -1;
-#else
-    return 0;
-#endif
 }
 
 bool
@@ -1391,11 +1451,10 @@ int
 codecvt<wchar_t, char, mbstate_t>::do_length(state_type& st,
     const extern_type* frm, const extern_type* frm_end, size_t mx) const
 {
-#ifdef __APPLE__
     int nbytes = 0;
     for (size_t nwchar_t = 0; nwchar_t < mx && frm != frm_end; ++nwchar_t)
     {
-        size_t n = mbrlen_l(frm, frm_end-frm, &st, __l);
+        size_t n = with_locale::mbrlen_l(frm, frm_end-frm, &st, __l);
         switch (n)
         {
         case 0:
@@ -1412,19 +1471,12 @@ codecvt<wchar_t, char, mbstate_t>::do_length(state_type& st,
         }
     }
     return nbytes;
-#else
-    return 0;
-#endif
 }
 
 int
 codecvt<wchar_t, char, mbstate_t>::do_max_length() const  _NOEXCEPT
 {
-#ifdef __APPLE__
-    return __l == 0 ? 1 : MB_CUR_MAX_L(__l);
-#else
-    return 0;
-#endif
+    return __l == 0 ? 1 : with_locale::mb_cur_max_l(__l);
 }
 
 //                                     Valid UTF ranges
@@ -3965,16 +4017,15 @@ numpunct_byname<char>::~numpunct_byname()
 void
 numpunct_byname<char>::__init(const char* nm)
 {
-#ifdef __APPLE__
     if (strcmp(nm, "C") != 0)
     {
-        unique_ptr<_xlocale, int(*)(locale_t)>  loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
+        locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
 #ifndef _LIBCPP_NO_EXCEPTIONS
         if (loc == 0)
             throw runtime_error("numpunct_byname<char>::numpunct_byname"
                                 " failed to construct for " + string(nm));
 #endif  // _LIBCPP_NO_EXCEPTIONS
-        lconv* lc = localeconv_l(loc.get());
+        lconv* lc = with_locale::localeconv_l(loc.get());
         if (*lc->decimal_point)
             __decimal_point_ = *lc->decimal_point;
         if (*lc->thousands_sep)
@@ -3982,7 +4033,6 @@ numpunct_byname<char>::__init(const char* nm)
         __grouping_ = lc->grouping;
         // locallization for truename and falsename is not available
     }
-#endif
 }
 
 // numpunct_byname<wchar_t>
@@ -4006,16 +4056,15 @@ numpunct_byname<wchar_t>::~numpunct_byname()
 void
 numpunct_byname<wchar_t>::__init(const char* nm)
 {
-#ifdef __APPLE__
     if (strcmp(nm, "C") != 0)
     {
-        unique_ptr<_xlocale, int(*)(locale_t)>  loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
+        locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
 #ifndef _LIBCPP_NO_EXCEPTIONS
         if (loc == 0)
             throw runtime_error("numpunct_byname<char>::numpunct_byname"
                                 " failed to construct for " + string(nm));
 #endif  // _LIBCPP_NO_EXCEPTIONS
-        lconv* lc = localeconv_l(loc.get());
+        lconv* lc = with_locale::localeconv_l(loc.get());
         if (*lc->decimal_point)
             __decimal_point_ = *lc->decimal_point;
         if (*lc->thousands_sep)
@@ -4023,7 +4072,6 @@ numpunct_byname<wchar_t>::__init(const char* nm)
         __grouping_ = lc->grouping;
         // locallization for truename and falsename is not available
     }
-#endif
 }
 
 // num_get helpers
@@ -4588,7 +4636,6 @@ template <>
 wstring
 __time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& ct)
 {
-#ifdef __APPLE__
     tm t;
     t.tm_sec = 59;
     t.tm_min = 55;
@@ -4608,7 +4655,7 @@ __time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& ct)
     wchar_t* wbb = wbuf;
     mbstate_t mb = {0};
     const char* bb = buf;
-    size_t i = mbsrtowcs_l(wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    size_t i = with_locale::mbsrtowcs_l( wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
     if (i == -1)
         __throw_runtime_error("locale not supported");
     wchar_t* wbe = wbb + i;
@@ -4733,9 +4780,6 @@ __time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& ct)
         ++wbb;
     }
     return result;
-#else
-    return wstring();
-#endif
 }
 
 template <>
@@ -4779,7 +4823,6 @@ template <>
 void
 __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
 {
-#ifdef __APPLE__
     tm t = {0};
     char buf[100];
     size_t be;
@@ -4793,7 +4836,7 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
         be = strftime_l(buf, 100, "%A", &t, __loc_);
         mb = mbstate_t();
         const char* bb = buf;
-        size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        size_t j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
         if (j == -1)
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
@@ -4801,7 +4844,7 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
         be = strftime_l(buf, 100, "%a", &t, __loc_);
         mb = mbstate_t();
         bb = buf;
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
         if (j == -1)
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
@@ -4814,7 +4857,7 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
         be = strftime_l(buf, 100, "%B", &t, __loc_);
         mb = mbstate_t();
         const char* bb = buf;
-        size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        size_t j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
         if (j == -1)
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
@@ -4822,7 +4865,7 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
         be = strftime_l(buf, 100, "%b", &t, __loc_);
         mb = mbstate_t();
         bb = buf;
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
         if (j == -1)
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
@@ -4833,7 +4876,7 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
     be = strftime_l(buf, 100, "%p", &t, __loc_);
     mb = mbstate_t();
     const char* bb = buf;
-    size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    size_t j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
     if (j == -1)
         __throw_runtime_error("locale not supported");
     wbe = wbuf + j;
@@ -4842,7 +4885,7 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
     be = strftime_l(buf, 100, "%p", &t, __loc_);
     mb = mbstate_t();
     bb = buf;
-    j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
     if (j == -1)
         __throw_runtime_error("locale not supported");
     wbe = wbuf + j;
@@ -4851,7 +4894,6 @@ __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
     __r_ = __analyze('r', ct);
     __x_ = __analyze('x', ct);
     __X_ = __analyze('X', ct);
-#endif
 }
 
 template <class CharT>
@@ -5113,17 +5155,15 @@ void
 __time_put::__do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
                      char __fmt, char __mod) const
 {
-#ifdef __APPLE__
     char __nar[100];
     char* __ne = __nar + 100;
     __do_put(__nar, __ne, __tm, __fmt, __mod);
     mbstate_t mb = {0};
     const char* __nb = __nar;
-    size_t j = mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_);
+    size_t j = with_locale::mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_);
     if (j == -1)
         __throw_runtime_error("locale not supported");
     __we = __wb + j;
-#endif
 }
 
 // moneypunct_byname
@@ -5368,15 +5408,14 @@ template<>
 void
 moneypunct_byname<char, false>::init(const char* nm)
 {
-#ifdef __APPLE__
     typedef moneypunct<char, false> base;
-    unique_ptr<_xlocale, int(*)(locale_t)>  loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
+    locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
 #ifndef _LIBCPP_NO_EXCEPTIONS
     if (loc == 0)
         throw runtime_error("moneypunct_byname"
                             " failed to construct for " + string(nm));
 #endif  // _LIBCPP_NO_EXCEPTIONS
-    lconv* lc = localeconv_l(loc.get());
+    lconv* lc = with_locale::localeconv_l(loc.get());
     if (*lc->mon_decimal_point)
         __decimal_point_ = *lc->mon_decimal_point;
     else
@@ -5401,22 +5440,20 @@ moneypunct_byname<char, false>::init(const char* nm)
         __negative_sign_ = lc->negative_sign;
     __init_pat(__pos_format_, lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn);
     __init_pat(__neg_format_, lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn);
-#endif
 }
 
 template<>
 void
 moneypunct_byname<char, true>::init(const char* nm)
 {
-#ifdef __APPLE__
     typedef moneypunct<char, true> base;
-    unique_ptr<_xlocale, int(*)(locale_t)>  loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
+    locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
 #ifndef _LIBCPP_NO_EXCEPTIONS
     if (loc == 0)
         throw runtime_error("moneypunct_byname"
                             " failed to construct for " + string(nm));
 #endif  // _LIBCPP_NO_EXCEPTIONS
-    lconv* lc = localeconv_l(loc.get());
+    lconv* lc = with_locale::localeconv_l(loc.get());
     if (*lc->mon_decimal_point)
         __decimal_point_ = *lc->mon_decimal_point;
     else
@@ -5441,22 +5478,20 @@ moneypunct_byname<char, true>::init(const char* nm)
         __negative_sign_ = lc->negative_sign;
     __init_pat(__pos_format_, lc->int_p_cs_precedes, lc->int_p_sep_by_space, lc->int_p_sign_posn);
     __init_pat(__neg_format_, lc->int_n_cs_precedes, lc->int_n_sep_by_space, lc->int_n_sign_posn);
-#endif
 }
 
 template<>
 void
 moneypunct_byname<wchar_t, false>::init(const char* nm)
 {
-#ifdef __APPLE__
     typedef moneypunct<wchar_t, false> base;
-    unique_ptr<_xlocale, int(*)(locale_t)>  loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
+    locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
 #ifndef _LIBCPP_NO_EXCEPTIONS
     if (loc == 0)
         throw runtime_error("moneypunct_byname"
                             " failed to construct for " + string(nm));
 #endif  // _LIBCPP_NO_EXCEPTIONS
-    lconv* lc = localeconv_l(loc.get());
+    lconv* lc = with_locale::localeconv_l(loc.get());
     if (*lc->mon_decimal_point)
         __decimal_point_ = static_cast<wchar_t>(*lc->mon_decimal_point);
     else
@@ -5469,7 +5504,7 @@ moneypunct_byname<wchar_t, false>::init(const char* nm)
     wchar_t wbuf[100];
     mbstate_t mb = {0};
     const char* bb = lc->currency_symbol;
-    size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+    size_t j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
     if (j == -1)
         __throw_runtime_error("locale not supported");
     wchar_t* wbe = wbuf + j;
@@ -5484,7 +5519,7 @@ moneypunct_byname<wchar_t, false>::init(const char* nm)
     {
         mb = mbstate_t();
         bb = lc->positive_sign;
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
         if (j == -1)
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
@@ -5496,7 +5531,7 @@ moneypunct_byname<wchar_t, false>::init(const char* nm)
     {
         mb = mbstate_t();
         bb = lc->negative_sign;
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
         if (j == -1)
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
@@ -5504,22 +5539,20 @@ moneypunct_byname<wchar_t, false>::init(const char* nm)
     }
     __init_pat(__pos_format_, lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn);
     __init_pat(__neg_format_, lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn);
-#endif
 }
 
 template<>
 void
 moneypunct_byname<wchar_t, true>::init(const char* nm)
 {
-#ifdef __APPLE__
     typedef moneypunct<wchar_t, true> base;
-    unique_ptr<_xlocale, int(*)(locale_t)>  loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
+    locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
 #ifndef _LIBCPP_NO_EXCEPTIONS
     if (loc == 0)
         throw runtime_error("moneypunct_byname"
                             " failed to construct for " + string(nm));
 #endif  // _LIBCPP_NO_EXCEPTIONS
-    lconv* lc = localeconv_l(loc.get());
+    lconv* lc = with_locale::localeconv_l(loc.get());
     if (*lc->mon_decimal_point)
         __decimal_point_ = static_cast<wchar_t>(*lc->mon_decimal_point);
     else
@@ -5532,7 +5565,7 @@ moneypunct_byname<wchar_t, true>::init(const char* nm)
     wchar_t wbuf[100];
     mbstate_t mb = {0};
     const char* bb = lc->int_curr_symbol;
-    size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+    size_t j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
     if (j == -1)
         __throw_runtime_error("locale not supported");
     wchar_t* wbe = wbuf + j;
@@ -5547,7 +5580,7 @@ moneypunct_byname<wchar_t, true>::init(const char* nm)
     {
         mb = mbstate_t();
         bb = lc->positive_sign;
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
         if (j == -1)
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
@@ -5559,7 +5592,7 @@ moneypunct_byname<wchar_t, true>::init(const char* nm)
     {
         mb = mbstate_t();
         bb = lc->negative_sign;
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = with_locale::mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
         if (j == -1)
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
@@ -5567,7 +5600,6 @@ moneypunct_byname<wchar_t, true>::init(const char* nm)
     }
     __init_pat(__pos_format_, lc->int_p_cs_precedes, lc->int_p_sep_by_space, lc->int_p_sign_posn);
     __init_pat(__neg_format_, lc->int_n_cs_precedes, lc->int_n_sep_by_space, lc->int_n_sign_posn);
-#endif
 }
 
 void __do_nothing(void*) {}