]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/src/c++98/locale.cc
Remove trailing whitespace from libstdc++-v3 files
[thirdparty/gcc.git] / libstdc++-v3 / src / c++98 / locale.cc
CommitLineData
818ab71a 1// Copyright (C) 1997-2016 Free Software Foundation, Inc.
b2dad0e3
BK
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
748086b7 6// Free Software Foundation; either version 3, or (at your option)
b2dad0e3
BK
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
748086b7
JJ
14// Under Section 7 of GPL version 3, you are granted additional
15// permissions described in the GCC Runtime Library Exception, version
16// 3.1, as published by the Free Software Foundation.
b2dad0e3 17
748086b7
JJ
18// You should have received a copy of the GNU General Public License and
19// a copy of the GCC Runtime Library Exception along with this program;
20// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21// <http://www.gnu.org/licenses/>.
b2dad0e3 22
34a2b755 23#define _GLIBCXX_USE_CXX11_ABI 1
54c1bf78
BK
24#include <clocale>
25#include <cstring>
54c1bf78 26#include <cctype>
d3a193e3 27#include <cwctype> // For towupper, etc.
54c1bf78 28#include <locale>
2e362c74 29#include <ext/concurrence.h>
18c75543 30
b82f782b 31namespace
18c75543 32{
99827523
BK
33 __gnu_cxx::__mutex&
34 get_locale_cache_mutex()
35 {
36 static __gnu_cxx::__mutex locale_cache_mutex;
37 return locale_cache_mutex;
38 }
2e362c74 39} // anonymous namespace
17325050 40
6defecc2
JJ
41// XXX GLIBCXX_ABI Deprecated
42#ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
43# define _GLIBCXX_LOC_ID(mangled) extern std::locale::id mangled
44_GLIBCXX_LOC_ID (_ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
45_GLIBCXX_LOC_ID (_ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
46_GLIBCXX_LOC_ID (_ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
47_GLIBCXX_LOC_ID (_ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
48# ifdef _GLIBCXX_USE_WCHAR_T
49_GLIBCXX_LOC_ID (_ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
50_GLIBCXX_LOC_ID (_ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
51_GLIBCXX_LOC_ID (_ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
52_GLIBCXX_LOC_ID (_ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
53# endif
54#endif
55
12ffa228
BK
56namespace std _GLIBCXX_VISIBILITY(default)
57{
58_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 59
0479a462 60 // Definitions for static const data members of locale.
b75bedeb 61 const locale::category locale::none;
b75bedeb 62 const locale::category locale::ctype;
b75bedeb 63 const locale::category locale::numeric;
0a31609b 64 const locale::category locale::collate;
b75bedeb 65 const locale::category locale::time;
0a31609b 66 const locale::category locale::monetary;
b75bedeb
BK
67 const locale::category locale::messages;
68 const locale::category locale::all;
69
8099b2ae 70 // These are no longer exported.
8ae81136 71 locale::_Impl* locale::_S_classic;
f92ab29f 72 locale::_Impl* locale::_S_global;
0479a462 73
8ae81136
BK
74#ifdef __GTHREADS
75 __gthread_once_t locale::_S_once = __GTHREAD_ONCE_INIT;
76#endif
77
0214010c 78 locale::locale(const locale& __other) throw()
26c691a8
BK
79 : _M_impl(__other._M_impl)
80 { _M_impl->_M_add_reference(); }
0214010c 81
1f46fc8e
BK
82 // This is used to initialize global and classic locales, and
83 // assumes that the _Impl objects are constructed correctly.
402a402c 84 // The lack of a reference increment is intentional.
1f46fc8e
BK
85 locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
86 { }
0214010c 87
402a402c
BK
88 locale::~locale() throw()
89 { _M_impl->_M_remove_reference(); }
90
0214010c
BK
91 bool
92 locale::operator==(const locale& __rhs) const throw()
93 {
4df9c41d
PC
94 // Deal first with the common cases, fast to process: refcopies,
95 // unnamed (i.e., !_M_names[0]), "simple" (!_M_names[1] => all the
96 // categories same name, i.e., _M_names[0]). Otherwise fall back
97 // to the general locale::name().
98 bool __ret;
d7ed521b 99 if (_M_impl == __rhs._M_impl)
4df9c41d
PC
100 __ret = true;
101 else if (!_M_impl->_M_names[0] || !__rhs._M_impl->_M_names[0]
102 || std::strcmp(_M_impl->_M_names[0],
103 __rhs._M_impl->_M_names[0]) != 0)
59639106 104 __ret = false;
4df9c41d
PC
105 else if (!_M_impl->_M_names[1] && !__rhs._M_impl->_M_names[1])
106 __ret = true;
d7ed521b 107 else
4df9c41d 108 __ret = this->name() == __rhs.name();
d7ed521b 109 return __ret;
0214010c
BK
110 }
111
112 const locale&
113 locale::operator=(const locale& __other) throw()
114 {
115 __other._M_impl->_M_add_reference();
116 _M_impl->_M_remove_reference();
117 _M_impl = __other._M_impl;
118 return *this;
119 }
120
34a2b755 121 _GLIBCXX_DEFAULT_ABI_TAG
0214010c
BK
122 string
123 locale::name() const
124 {
d3a193e3 125 string __ret;
4df9c41d
PC
126 if (!_M_impl->_M_names[0])
127 __ret = '*';
128 else if (_M_impl->_M_check_same_name())
0214010c
BK
129 __ret = _M_impl->_M_names[0];
130 else
131 {
4df9c41d 132 __ret.reserve(128);
aa53f832 133 __ret += _S_categories[0];
f815521c 134 __ret += '=';
f92ab29f 135 __ret += _M_impl->_M_names[0];
73c4dcc6 136 for (size_t __i = 1; __i < _S_categories_size; ++__i)
d3a193e3 137 {
f815521c 138 __ret += ';';
aa53f832 139 __ret += _S_categories[__i];
f815521c 140 __ret += '=';
aa53f832 141 __ret += _M_impl->_M_names[__i];
d3a193e3 142 }
0214010c
BK
143 }
144 return __ret;
145 }
146
0214010c 147 locale::category
f92ab29f 148 locale::_S_normalize_category(category __cat)
0214010c 149 {
758c46c9 150 int __ret = 0;
2a67bec2 151 if (__cat == none || ((__cat & all) && !(__cat & ~all)))
0214010c
BK
152 __ret = __cat;
153 else
154 {
155 // NB: May be a C-style "LC_ALL" category; convert.
156 switch (__cat)
157 {
f92ab29f
CG
158 case LC_COLLATE:
159 __ret = collate;
0214010c 160 break;
f92ab29f 161 case LC_CTYPE:
0214010c
BK
162 __ret = ctype;
163 break;
f92ab29f 164 case LC_MONETARY:
0214010c
BK
165 __ret = monetary;
166 break;
f92ab29f 167 case LC_NUMERIC:
0214010c
BK
168 __ret = numeric;
169 break;
f92ab29f
CG
170 case LC_TIME:
171 __ret = time;
0214010c 172 break;
3d7c150e 173#ifdef _GLIBCXX_HAVE_LC_MESSAGES
f92ab29f 174 case LC_MESSAGES:
0214010c
BK
175 __ret = messages;
176 break;
f92ab29f
CG
177#endif
178 case LC_ALL:
0214010c
BK
179 __ret = all;
180 break;
181 default:
ba9119ec
PC
182 __throw_runtime_error(__N("locale::_S_normalize_category "
183 "category not found"));
0214010c
BK
184 }
185 }
186 return __ret;
187 }
188
c755e77d 189 // locale::facet
8ae81136
BK
190 __c_locale locale::facet::_S_c_locale;
191
192 const char locale::facet::_S_c_name[2] = "C";
193
194#ifdef __GTHREADS
195 __gthread_once_t locale::facet::_S_once = __GTHREAD_ONCE_INIT;
196#endif
1c26d8fd 197
8ae81136
BK
198 void
199 locale::facet::_S_initialize_once()
200 {
201 // Initialize the underlying locale model.
202 _S_create_c_locale(_S_c_locale, _S_c_name);
203 }
204
205 __c_locale
206 locale::facet::_S_get_c_locale()
207 {
b87bc3aa 208#ifdef __GTHREADS
5c109a17
BK
209 if (__gthread_active_p())
210 __gthread_once(&_S_once, _S_initialize_once);
211 else
8ae81136 212#endif
5c109a17
BK
213 {
214 if (!_S_c_locale)
215 _S_initialize_once();
216 }
8ae81136
BK
217 return _S_c_locale;
218 }
219
bb1b12ec 220 const char*
32ade559 221 locale::facet::_S_get_c_name() throw()
bb1b12ec
BK
222 { return _S_c_name; }
223
c755e77d
BK
224 locale::facet::
225 ~facet() { }
bf3b866e 226
c755e77d
BK
227 // locale::_Impl
228 locale::_Impl::
229 ~_Impl() throw()
230 {
231 if (_M_facets)
232 for (size_t __i = 0; __i < _M_facets_size; ++__i)
233 if (_M_facets[__i])
234 _M_facets[__i]->_M_remove_reference();
235 delete [] _M_facets;
236
237 if (_M_caches)
238 for (size_t __i = 0; __i < _M_facets_size; ++__i)
239 if (_M_caches[__i])
f92ab29f 240 _M_caches[__i]->_M_remove_reference();
c755e77d
BK
241 delete [] _M_caches;
242
243 if (_M_names)
244 for (size_t __i = 0; __i < _S_categories_size; ++__i)
f92ab29f 245 delete [] _M_names[__i];
c755e77d
BK
246 delete [] _M_names;
247 }
bf3b866e 248
c755e77d
BK
249 // Clone existing _Impl object.
250 locale::_Impl::
251 _Impl(const _Impl& __imp, size_t __refs)
26c691a8
BK
252 : _M_refcount(__refs), _M_facets(0), _M_facets_size(__imp._M_facets_size),
253 _M_caches(0), _M_names(0)
c755e77d 254 {
bc2631e0 255 __try
c755e77d
BK
256 {
257 _M_facets = new const facet*[_M_facets_size];
258 for (size_t __i = 0; __i < _M_facets_size; ++__i)
259 {
260 _M_facets[__i] = __imp._M_facets[__i];
261 if (_M_facets[__i])
262 _M_facets[__i]->_M_add_reference();
263 }
264 _M_caches = new const facet*[_M_facets_size];
95b147fe 265 for (size_t __j = 0; __j < _M_facets_size; ++__j)
c755e77d 266 {
95b147fe
SM
267 _M_caches[__j] = __imp._M_caches[__j];
268 if (_M_caches[__j])
f92ab29f 269 _M_caches[__j]->_M_add_reference();
c755e77d
BK
270 }
271 _M_names = new char*[_S_categories_size];
95b147fe
SM
272 for (size_t __k = 0; __k < _S_categories_size; ++__k)
273 _M_names[__k] = 0;
c755e77d 274
4df9c41d 275 // Name the categories.
95b147fe
SM
276 for (size_t __l = 0; (__l < _S_categories_size
277 && __imp._M_names[__l]); ++__l)
c755e77d 278 {
95b147fe
SM
279 const size_t __len = std::strlen(__imp._M_names[__l]) + 1;
280 _M_names[__l] = new char[__len];
281 std::memcpy(_M_names[__l], __imp._M_names[__l], __len);
c755e77d
BK
282 }
283 }
bc2631e0 284 __catch(...)
c755e77d
BK
285 {
286 this->~_Impl();
287 __throw_exception_again;
288 }
289 }
bf3b866e 290
5dc91152 291 void
c755e77d 292 locale::_Impl::
f92ab29f 293 _M_replace_category(const _Impl* __imp,
95b147fe 294 const locale::id* const* __idpp)
bf3b866e 295 {
c755e77d
BK
296 for (; *__idpp; ++__idpp)
297 _M_replace_facet(__imp, *__idpp);
298 }
f92ab29f 299
c755e77d
BK
300 void
301 locale::_Impl::
302 _M_replace_facet(const _Impl* __imp, const locale::id* __idp)
303 {
304 size_t __index = __idp->_M_id();
f92ab29f 305 if ((__index > (__imp->_M_facets_size - 1))
95b147fe 306 || !__imp->_M_facets[__index])
ba9119ec 307 __throw_runtime_error(__N("locale::_Impl::_M_replace_facet"));
f92ab29f 308 _M_install_facet(__idp, __imp->_M_facets[__index]);
c755e77d
BK
309 }
310
311 void
312 locale::_Impl::
313 _M_install_facet(const locale::id* __idp, const facet* __fp)
314 {
315 if (__fp)
316 {
317 size_t __index = __idp->_M_id();
318
319 // Check size of facet vector to ensure adequate room.
320 if (__index > _M_facets_size - 1)
321 {
322 const size_t __new_size = __index + 4;
323
324 // New facet array.
325 const facet** __oldf = _M_facets;
326 const facet** __newf;
f92ab29f 327 __newf = new const facet*[__new_size];
c755e77d
BK
328 for (size_t __i = 0; __i < _M_facets_size; ++__i)
329 __newf[__i] = _M_facets[__i];
95b147fe
SM
330 for (size_t __l = _M_facets_size; __l < __new_size; ++__l)
331 __newf[__l] = 0;
c755e77d
BK
332
333 // New cache array.
334 const facet** __oldc = _M_caches;
335 const facet** __newc;
bc2631e0 336 __try
c755e77d
BK
337 {
338 __newc = new const facet*[__new_size];
339 }
bc2631e0 340 __catch(...)
c755e77d
BK
341 {
342 delete [] __newf;
343 __throw_exception_again;
344 }
95b147fe
SM
345 for (size_t __j = 0; __j < _M_facets_size; ++__j)
346 __newc[__j] = _M_caches[__j];
347 for (size_t __k = _M_facets_size; __k < __new_size; ++__k)
348 __newc[__k] = 0;
c755e77d
BK
349
350 _M_facets_size = __new_size;
351 _M_facets = __newf;
352 _M_caches = __newc;
353 delete [] __oldf;
354 delete [] __oldc;
355 }
356
357 __fp->_M_add_reference();
358 const facet*& __fpr = _M_facets[__index];
359 if (__fpr)
360 {
34a2b755
JW
361#if _GLIBCXX_USE_DUAL_ABI
362 // If this is a twinned facet replace its twin with a shim.
363 for (const id* const* p = _S_twinned_facets; *p != 0; p += 2)
364 {
365 if (p[0]->_M_id() == __index)
366 {
367 // replacing the old ABI facet, also replace new ABI twin
368 const facet*& __fpr2 = _M_facets[p[1]->_M_id()];
369 if (__fpr2)
370 {
371 const facet* __fp2 = __fp->_M_sso_shim(p[1]);
372 __fp2->_M_add_reference();
373 __fpr2->_M_remove_reference();
374 __fpr2 = __fp2;
375 }
376 break;
377 }
378 else if (p[1]->_M_id() == __index)
379 {
380 // replacing the new ABI facet, also replace old ABI twin
381 const facet*& __fpr2 = _M_facets[p[0]->_M_id()];
382 if (__fpr2)
383 {
384 const facet* __fp2 = __fp->_M_cow_shim(p[0]);
385 __fp2->_M_add_reference();
386 __fpr2->_M_remove_reference();
387 __fpr2 = __fp2;
388 }
389 break;
390 }
391 }
392#endif
c755e77d
BK
393 // Replacing an existing facet. Order matters.
394 __fpr->_M_remove_reference();
395 __fpr = __fp;
396 }
397 else
398 {
399 // Installing a newly created facet into an empty
400 // _M_facets container, say a newly-constructed,
401 // swanky-fresh _Impl.
402 _M_facets[__index] = __fp;
403 }
404
405 // Ideally, it would be nice to only remove the caches that
406 // are now incorrect. However, some of the caches depend on
407 // multiple facets, and we only know about one facet
408 // here. It's no great loss: the first use of the new facet
409 // will create a new, correctly cached facet anyway.
410 for (size_t __i = 0; __i < _M_facets_size; ++__i)
411 {
412 const facet* __cpr = _M_caches[__i];
413 if (__cpr)
414 {
415 __cpr->_M_remove_reference();
416 _M_caches[__i] = 0;
417 }
418 }
419 }
420 }
421
18c75543
ILT
422 void
423 locale::_Impl::
424 _M_install_cache(const facet* __cache, size_t __index)
425 {
99827523 426 __gnu_cxx::__scoped_lock sentry(get_locale_cache_mutex());
34a2b755
JW
427#if _GLIBCXX_USE_DUAL_ABI
428 // If this cache is for one of the facets that is instantiated twice,
429 // for old and new std::string ABI, install it in both slots.
430 size_t __index2 = -1;
431 for (const id* const* p = _S_twinned_facets; *p != 0; p += 2)
432 {
433 if (p[0]->_M_id() == __index)
434 {
435 __index2 = p[1]->_M_id();
436 break;
437 }
438 else if (p[1]->_M_id() == __index)
439 {
440 __index2 = __index;
441 __index = p[0]->_M_id();
442 break;
443 }
444 }
445#endif
18c75543
ILT
446 if (_M_caches[__index] != 0)
447 {
448 // Some other thread got in first.
449 delete __cache;
450 }
451 else
452 {
453 __cache->_M_add_reference();
454 _M_caches[__index] = __cache;
34a2b755
JW
455#if _GLIBCXX_USE_DUAL_ABI
456 if (__index2 != size_t(-1))
457 {
458 __cache->_M_add_reference();
459 _M_caches[__index2] = __cache;
460 }
461#endif
18c75543
ILT
462 }
463 }
464
c755e77d
BK
465 // locale::id
466 // Definitions for static const data members of locale::id
1313d87f 467 _Atomic_word locale::id::_S_refcount; // init'd to 0 by linker
c755e77d
BK
468
469 size_t
32ade559 470 locale::id::_M_id() const throw()
c755e77d
BK
471 {
472 if (!_M_index)
6defecc2
JJ
473 {
474 // XXX GLIBCXX_ABI Deprecated
475#ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
476 locale::id *f = 0;
477# define _GLIBCXX_SYNC_ID(facet, mangled) \
478 if (this == &::mangled) \
479 f = &facet::id
480 _GLIBCXX_SYNC_ID (num_get<char>, _ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
481 _GLIBCXX_SYNC_ID (num_put<char>, _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
482 _GLIBCXX_SYNC_ID (money_get<char>, _ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
483 _GLIBCXX_SYNC_ID (money_put<char>, _ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
484# ifdef _GLIBCXX_USE_WCHAR_T
485 _GLIBCXX_SYNC_ID (num_get<wchar_t>, _ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
486 _GLIBCXX_SYNC_ID (num_put<wchar_t>, _ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
487 _GLIBCXX_SYNC_ID (money_get<wchar_t>, _ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
488 _GLIBCXX_SYNC_ID (money_put<wchar_t>, _ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
489# endif
490 if (f)
491 _M_index = 1 + f->_M_id();
492 else
493#endif
b7ee72de
PC
494 _M_index = 1 + __gnu_cxx::__exchange_and_add_dispatch(&_S_refcount,
495 1);
6defecc2 496 }
c755e77d 497 return _M_index - 1;
bf3b866e 498 }
3cbc7af0 499
12ffa228
BK
500_GLIBCXX_END_NAMESPACE_VERSION
501} // namespace