source: vendor/trolltech/current/src/tools/qlocale.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 136.2 KB
Line 
1/****************************************************************************
2** $Id: qlocale.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Implementation of the QLocale class
5**
6** Copyright (C) 1992-2003 Trolltech AS. All rights reserved.
7**
8** This file is part of the tools module of the Qt GUI Toolkit.
9**
10** This file may be distributed under the terms of the Q Public License
11** as defined by Trolltech AS of Norway and appearing in the file
12** LICENSE.QPL included in the packaging of this file.
13**
14** This file may be distributed and/or modified under the terms of the
15** GNU General Public License version 2 as published by the Free Software
16** Foundation and appearing in the file LICENSE.GPL included in the
17** packaging of this file.
18**
19** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
20** licenses may use this file in accordance with the Qt Commercial License
21** Agreement provided with the Software.
22**
23** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25**
26** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
27** information about Qt Commercial License Agreements.
28** See http://www.trolltech.com/qpl/ for QPL licensing information.
29** See http://www.trolltech.com/gpl/ for GPL licensing information.
30**
31** Contact info@trolltech.com if any conditions of this licensing are
32** not clear to you.
33**
34**********************************************************************/
35
36#include <sys/types.h>
37#include <ctype.h>
38#include <float.h>
39#include <limits.h>
40#include <math.h>
41#include <stdlib.h>
42
43#include "qlocale.h"
44#include "qlocale_p.h"
45
46#if defined (Q_WS_WIN)
47# include <windows.h>
48// mingw defines NAN and INFINITY to 0/0 and x/0
49# if defined(Q_CC_GNU)
50# undef NAN
51# undef INFINITY
52# else
53# define isnan(d) _isnan(d)
54# endif
55#endif
56
57#if !defined( QWS ) && defined( Q_OS_MAC )
58# include <Carbon/Carbon.h>
59#endif
60
61
62
63#if defined (Q_OS_SOLARIS)
64# include <ieeefp.h>
65#endif
66
67enum {
68 LittleEndian,
69 BigEndian
70
71#ifdef Q_BYTE_ORDER
72# if Q_BYTE_ORDER == Q_BIG_ENDIAN
73 , ByteOrder = BigEndian
74# elif Q_BYTE_ORDER == Q_LITTLE_ENDIAN
75 , ByteOrder = LittleEndian
76# else
77# error "undefined byte order"
78# endif
79};
80#else
81};
82static const unsigned int one = 1;
83static const bool ByteOrder = ((*((unsigned char *) &one) == 0) ? BigEndian : LittleEndian);
84#endif
85
86#if !defined(INFINITY)
87static const unsigned char be_inf_bytes[] = { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 };
88static const unsigned char le_inf_bytes[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f };
89static inline double inf()
90{
91 return (ByteOrder == BigEndian ?
92 *((const double *) be_inf_bytes) :
93 *((const double *) le_inf_bytes));
94}
95# define INFINITY (::inf())
96#endif
97
98#if !defined(NAN)
99static const unsigned char be_nan_bytes[] = { 0x7f, 0xf8, 0, 0, 0, 0, 0, 0 };
100static const unsigned char le_nan_bytes[] = { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f };
101static inline double nan()
102{
103 return (ByteOrder == BigEndian ?
104 *((const double *) be_nan_bytes) :
105 *((const double *) le_nan_bytes));
106}
107# define NAN (::nan())
108#endif
109
110// Sizes as defined by the ISO C99 standard - fallback
111#ifndef LLONG_MAX
112# define LLONG_MAX Q_INT64_C(9223372036854775807)
113#endif
114#ifndef LLONG_MIN
115# define LLONG_MIN (-LLONG_MAX - Q_INT64_C(1))
116#endif
117#ifndef ULLONG_MAX
118# define ULLONG_MAX Q_UINT64_C(0xffffffffffffffff)
119#endif
120
121static char *qdtoa(double d, int mode, int ndigits, int *decpt,
122 int *sign, char **rve, char **digits_str);
123static double qstrtod(const char *s00, char const **se, bool *ok);
124static Q_LLONG qstrtoll(const char *nptr, const char **endptr, register int base, bool *ok);
125static Q_ULLONG qstrtoull(const char *nptr, const char **endptr, register int base, bool *ok);
126
127static const uint locale_index[] = {
128 0, // unused
129 0, // C
130 0, // Abkhazian
131 0, // Afan
132 0, // Afar
133 1, // Afrikaans
134 2, // Albanian
135 0, // Amharic
136 3, // Arabic
137 19, // Armenian
138 0, // Assamese
139 0, // Aymara
140 20, // Azerbaijani
141 0, // Bashkir
142 21, // Basque
143 0, // Bengali
144 0, // Bhutani
145 0, // Bihari
146 0, // Bislama
147 0, // Breton
148 22, // Bulgarian
149 0, // Burmese
150 23, // Byelorussian
151 0, // Cambodian
152 24, // Catalan
153 25, // Chinese
154 0, // Corsican
155 30, // Croatian
156 31, // Czech
157 32, // Danish
158 33, // Dutch
159 35, // English
160 0, // Esperanto
161 47, // Estonian
162 48, // Faroese
163 0, // Fiji
164 49, // Finnish
165 50, // French
166 0, // Frisian
167 0, // Gaelic
168 56, // Galician
169 57, // Georgian
170 58, // German
171 63, // Greek
172 0, // Greenlandic
173 0, // Guarani
174 64, // Gujarati
175 0, // Hausa
176 65, // Hebrew
177 66, // Hindi
178 67, // Hungarian
179 68, // Icelandic
180 69, // Indonesian
181 0, // Interlingua
182 0, // Interlingue
183 0, // Inuktitut
184 0, // Inupiak
185 0, // Irish
186 70, // Italian
187 72, // Japanese
188 0, // Javanese
189 73, // Kannada
190 0, // Kashmiri
191 74, // Kazakh
192 0, // Kinyarwanda
193 75, // Kirghiz
194 76, // Korean
195 0, // Kurdish
196 0, // Kurundi
197 0, // Laothian
198 0, // Latin
199 77, // Latvian
200 0, // Lingala
201 78, // Lithuanian
202 79, // Macedonian
203 0, // Malagasy
204 80, // Malay
205 0, // Malayalam
206 0, // Maltese
207 0, // Maori
208 82, // Marathi
209 0, // Moldavian
210 83, // Mongolian
211 0, // Nauru
212 0, // Nepali
213 84, // Norwegian
214 0, // Occitan
215 0, // Oriya
216 0, // Pashto
217 85, // Persian
218 86, // Polish
219 87, // Portuguese
220 89, // Punjabi
221 0, // Quechua
222 0, // RhaetoRomance
223 90, // Romanian
224 91, // Russian
225 0, // Samoan
226 0, // Sangho
227 92, // Sanskrit
228 0, // Serbian
229 0, // SerboCroatian
230 0, // Sesotho
231 0, // Setswana
232 0, // Shona
233 0, // Sindhi
234 0, // Singhalese
235 0, // Siswati
236 93, // Slovak
237 94, // Slovenian
238 0, // Somali
239 95, // Spanish
240 0, // Sundanese
241 114, // Swahili
242 115, // Swedish
243 0, // Tagalog
244 0, // Tajik
245 117, // Tamil
246 0, // Tatar
247 118, // Telugu
248 119, // Thai
249 0, // Tibetan
250 0, // Tigrinya
251 0, // Tonga
252 0, // Tsonga
253 120, // Turkish
254 0, // Turkmen
255 0, // Twi
256 0, // Uigur
257 121, // Ukrainian
258 122, // Urdu
259 123, // Uzbek
260 124, // Vietnamese
261 0, // Volapuk
262 0, // Welsh
263 0, // Wolof
264 0, // Xhosa
265 0, // Yiddish
266 0, // Yoruba
267 0, // Zhuang
268 0, // Zulu
269 0 // trailing 0
270};
271
272static const QLocalePrivate locale_data[] = {
273// lang terr dec group list prcnt zero minus exp
274 { 1, 0, 46, 44, 59, 37, 48, 45, 101 }, // C/AnyCountry
275 { 5, 195, 46, 44, 44, 37, 48, 45, 101 }, // Afrikaans/SouthAfrica
276 { 6, 2, 44, 46, 59, 37, 48, 45, 101 }, // Albanian/Albania
277 { 8, 186, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/SaudiArabia
278 { 8, 3, 46, 44, 59, 37, 48, 45, 101 }, // Arabic/Algeria
279 { 8, 17, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Bahrain
280 { 8, 64, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Egypt
281 { 8, 103, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Iraq
282 { 8, 109, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Jordan
283 { 8, 115, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Kuwait
284 { 8, 119, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Lebanon
285 { 8, 122, 46, 44, 59, 37, 48, 45, 101 }, // Arabic/LibyanArabJamahiriya
286 { 8, 145, 46, 44, 59, 37, 48, 45, 101 }, // Arabic/Morocco
287 { 8, 162, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Oman
288 { 8, 175, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Qatar
289 { 8, 207, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/SyrianArabRepublic
290 { 8, 216, 46, 44, 59, 37, 48, 45, 101 }, // Arabic/Tunisia
291 { 8, 223, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/UnitedArabEmirates
292 { 8, 237, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Yemen
293 { 9, 11, 46, 44, 44, 37, 48, 45, 101 }, // Armenian/Armenia
294 { 12, 15, 44, 160, 59, 37, 48, 45, 101 }, // Azerbaijani/Azerbaijan
295 { 14, 197, 44, 46, 59, 37, 48, 45, 101 }, // Basque/Spain
296 { 20, 33, 44, 160, 59, 37, 48, 45, 101 }, // Bulgarian/Bulgaria
297 { 22, 20, 44, 160, 59, 37, 48, 45, 101 }, // Byelorussian/Belarus
298 { 24, 197, 44, 46, 59, 37, 48, 45, 101 }, // Catalan/Spain
299 { 25, 44, 46, 44, 44, 37, 48, 45, 101 }, // Chinese/China
300 { 25, 97, 46, 44, 44, 37, 48, 45, 101 }, // Chinese/HongKong
301 { 25, 126, 46, 44, 44, 37, 48, 45, 101 }, // Chinese/Macau
302 { 25, 190, 46, 44, 44, 37, 48, 45, 101 }, // Chinese/Singapore
303 { 25, 208, 46, 44, 44, 37, 48, 45, 101 }, // Chinese/Taiwan
304 { 27, 54, 44, 46, 59, 37, 48, 45, 101 }, // Croatian/Croatia
305 { 28, 57, 44, 160, 59, 37, 48, 45, 101 }, // Czech/CzechRepublic
306 { 29, 58, 44, 46, 59, 37, 48, 45, 101 }, // Danish/Denmark
307 { 30, 151, 44, 46, 59, 37, 48, 45, 101 }, // Dutch/Netherlands
308 { 30, 21, 44, 46, 59, 37, 48, 45, 101 }, // Dutch/Belgium
309 { 31, 225, 46, 44, 44, 37, 48, 45, 101 }, // English/UnitedStates
310 { 31, 13, 46, 44, 44, 37, 48, 45, 101 }, // English/Australia
311 { 31, 22, 46, 44, 59, 37, 48, 45, 101 }, // English/Belize
312 { 31, 38, 46, 44, 44, 37, 48, 45, 101 }, // English/Canada
313 { 31, 104, 46, 44, 44, 37, 48, 45, 101 }, // English/Ireland
314 { 31, 107, 46, 44, 44, 37, 48, 45, 101 }, // English/Jamaica
315 { 31, 154, 46, 44, 44, 37, 48, 45, 101 }, // English/NewZealand
316 { 31, 170, 46, 44, 44, 37, 48, 45, 101 }, // English/Philippines
317 { 31, 195, 46, 44, 44, 37, 48, 45, 101 }, // English/SouthAfrica
318 { 31, 215, 46, 44, 59, 37, 48, 45, 101 }, // English/TrinidadAndTobago
319 { 31, 224, 46, 44, 44, 37, 48, 45, 101 }, // English/UnitedKingdom
320 { 31, 240, 46, 44, 44, 37, 48, 45, 101 }, // English/Zimbabwe
321 { 33, 68, 44, 160, 59, 37, 48, 45, 101 }, // Estonian/Estonia
322 { 34, 71, 44, 46, 59, 37, 48, 45, 101 }, // Faroese/FaroeIslands
323 { 36, 73, 44, 160, 59, 37, 48, 45, 101 }, // Finnish/Finland
324 { 37, 74, 44, 160, 59, 37, 48, 45, 101 }, // French/France
325 { 37, 21, 44, 46, 59, 37, 48, 45, 101 }, // French/Belgium
326 { 37, 38, 44, 160, 59, 37, 48, 45, 101 }, // French/Canada
327 { 37, 125, 44, 160, 59, 37, 48, 45, 101 }, // French/Luxembourg
328 { 37, 142, 44, 160, 59, 37, 48, 45, 101 }, // French/Monaco
329 { 37, 206, 46, 39, 59, 37, 48, 45, 101 }, // French/Switzerland
330 { 40, 197, 44, 46, 44, 37, 48, 45, 101 }, // Galician/Spain
331 { 41, 81, 44, 160, 59, 37, 48, 45, 101 }, // Georgian/Georgia
332 { 42, 82, 44, 46, 59, 37, 48, 45, 101 }, // German/Germany
333 { 42, 14, 44, 46, 59, 37, 48, 45, 101 }, // German/Austria
334 { 42, 123, 46, 39, 59, 37, 48, 45, 101 }, // German/Liechtenstein
335 { 42, 125, 44, 46, 59, 37, 48, 45, 101 }, // German/Luxembourg
336 { 42, 206, 46, 39, 59, 37, 48, 45, 101 }, // German/Switzerland
337 { 43, 85, 44, 46, 59, 37, 48, 45, 101 }, // Greek/Greece
338 { 46, 100, 46, 44, 44, 37, 2790, 45, 101 }, // Gujarati/India
339 { 48, 105, 46, 44, 44, 37, 48, 45, 101 }, // Hebrew/Israel
340 { 49, 100, 46, 44, 44, 37, 48, 45, 101 }, // Hindi/India
341 { 50, 98, 44, 160, 59, 37, 48, 45, 101 }, // Hungarian/Hungary
342 { 51, 99, 44, 46, 59, 37, 48, 45, 101 }, // Icelandic/Iceland
343 { 52, 101, 44, 46, 59, 37, 48, 45, 101 }, // Indonesian/Indonesia
344 { 58, 106, 44, 46, 59, 37, 48, 45, 101 }, // Italian/Italy
345 { 58, 206, 46, 39, 59, 37, 48, 45, 101 }, // Italian/Switzerland
346 { 59, 108, 46, 44, 44, 37, 48, 45, 101 }, // Japanese/Japan
347 { 61, 100, 46, 44, 44, 37, 3302, 45, 101 }, // Kannada/India
348 { 63, 110, 44, 160, 59, 37, 48, 45, 101 }, // Kazakh/Kazakhstan
349 { 65, 116, 44, 160, 59, 37, 48, 45, 101 }, // Kirghiz/Kyrgyzstan
350 { 66, 114, 46, 44, 44, 37, 48, 45, 101 }, // Korean/RepublicOfKorea
351 { 71, 118, 44, 160, 59, 37, 48, 45, 101 }, // Latvian/Latvia
352 { 73, 124, 44, 46, 59, 37, 48, 45, 101 }, // Lithuanian/Lithuania
353 { 74, 127, 44, 46, 59, 37, 48, 45, 101 }, // Macedonian/Macedonia
354 { 76, 130, 44, 46, 59, 37, 48, 45, 101 }, // Malay/Malaysia
355 { 76, 32, 44, 46, 59, 37, 48, 45, 101 }, // Malay/BruneiDarussalam
356 { 80, 100, 46, 44, 44, 37, 2406, 45, 101 }, // Marathi/India
357 { 82, 143, 44, 160, 59, 37, 48, 45, 101 }, // Mongolian/Mongolia
358 { 85, 161, 44, 160, 59, 37, 48, 45, 101 }, // Norwegian/Norway
359 { 89, 102, 46, 44, 59, 37, 1776, 45, 101 }, // Persian/Iran
360 { 90, 172, 44, 160, 59, 37, 48, 45, 101 }, // Polish/Poland
361 { 91, 173, 44, 46, 59, 37, 48, 45, 101 }, // Portuguese/Portugal
362 { 91, 30, 44, 46, 59, 37, 48, 45, 101 }, // Portuguese/Brazil
363 { 92, 100, 46, 44, 44, 37, 2662, 45, 101 }, // Punjabi/India
364 { 95, 177, 44, 46, 59, 37, 48, 45, 101 }, // Romanian/Romania
365 { 96, 178, 44, 160, 59, 37, 48, 45, 101 }, // Russian/RussianFederation
366 { 99, 100, 46, 44, 44, 37, 2406, 45, 101 }, // Sanskrit/India
367 { 108, 191, 44, 160, 59, 37, 48, 45, 101 }, // Slovak/Slovakia
368 { 109, 192, 44, 46, 59, 37, 48, 45, 101 }, // Slovenian/Slovenia
369 { 111, 197, 44, 46, 59, 37, 48, 45, 101 }, // Spanish/Spain
370 { 111, 10, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Argentina
371 { 111, 26, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Bolivia
372 { 111, 43, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Chile
373 { 111, 47, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Colombia
374 { 111, 52, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/CostaRica
375 { 111, 61, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/DominicanRepublic
376 { 111, 63, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Ecuador
377 { 111, 65, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/ElSalvador
378 { 111, 90, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Guatemala
379 { 111, 96, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Honduras
380 { 111, 139, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Mexico
381 { 111, 155, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Nicaragua
382 { 111, 166, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Panama
383 { 111, 168, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Paraguay
384 { 111, 169, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Peru
385 { 111, 174, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/PuertoRico
386 { 111, 227, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Uruguay
387 { 111, 231, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Venezuela
388 { 113, 111, 46, 44, 44, 37, 48, 45, 101 }, // Swahili/Kenya
389 { 114, 205, 44, 160, 59, 37, 48, 45, 101 }, // Swedish/Sweden
390 { 114, 73, 44, 160, 59, 37, 48, 45, 101 }, // Swedish/Finland
391 { 117, 100, 46, 44, 44, 37, 48, 45, 101 }, // Tamil/India
392 { 119, 100, 46, 44, 44, 37, 3174, 45, 101 }, // Telugu/India
393 { 120, 211, 46, 44, 44, 37, 3664, 45, 101 }, // Thai/Thailand
394 { 125, 217, 44, 46, 59, 37, 48, 45, 101 }, // Turkish/Turkey
395 { 129, 222, 44, 160, 59, 37, 48, 45, 101 }, // Ukrainian/Ukraine
396 { 130, 163, 46, 44, 59, 37, 1776, 45, 101 }, // Urdu/Pakistan
397 { 131, 228, 44, 160, 59, 37, 48, 45, 101 }, // Uzbek/Uzbekistan
398 { 132, 232, 44, 46, 44, 37, 48, 45, 101 }, // Vietnamese/VietNam
399 { 0, 0, 0, 0, 0, 0, 0, 0, 0 } // trailing 0s
400};
401
402static const char language_name_list[] =
403"Default\0"
404"C\0"
405"Abkhazian\0"
406"Afan\0"
407"Afar\0"
408"Afrikaans\0"
409"Albanian\0"
410"Amharic\0"
411"Arabic\0"
412"Armenian\0"
413"Assamese\0"
414"Aymara\0"
415"Azerbaijani\0"
416"Bashkir\0"
417"Basque\0"
418"Bengali\0"
419"Bhutani\0"
420"Bihari\0"
421"Bislama\0"
422"Breton\0"
423"Bulgarian\0"
424"Burmese\0"
425"Byelorussian\0"
426"Cambodian\0"
427"Catalan\0"
428"Chinese\0"
429"Corsican\0"
430"Croatian\0"
431"Czech\0"
432"Danish\0"
433"Dutch\0"
434"English\0"
435"Esperanto\0"
436"Estonian\0"
437"Faroese\0"
438"Fiji\0"
439"Finnish\0"
440"French\0"
441"Frisian\0"
442"Gaelic\0"
443"Galician\0"
444"Georgian\0"
445"German\0"
446"Greek\0"
447"Greenlandic\0"
448"Guarani\0"
449"Gujarati\0"
450"Hausa\0"
451"Hebrew\0"
452"Hindi\0"
453"Hungarian\0"
454"Icelandic\0"
455"Indonesian\0"
456"Interlingua\0"
457"Interlingue\0"
458"Inuktitut\0"
459"Inupiak\0"
460"Irish\0"
461"Italian\0"
462"Japanese\0"
463"Javanese\0"
464"Kannada\0"
465"Kashmiri\0"
466"Kazakh\0"
467"Kinyarwanda\0"
468"Kirghiz\0"
469"Korean\0"
470"Kurdish\0"
471"Kurundi\0"
472"Laothian\0"
473"Latin\0"
474"Latvian\0"
475"Lingala\0"
476"Lithuanian\0"
477"Macedonian\0"
478"Malagasy\0"
479"Malay\0"
480"Malayalam\0"
481"Maltese\0"
482"Maori\0"
483"Marathi\0"
484"Moldavian\0"
485"Mongolian\0"
486"Nauru\0"
487"Nepali\0"
488"Norwegian\0"
489"Occitan\0"
490"Oriya\0"
491"Pashto\0"
492"Persian\0"
493"Polish\0"
494"Portuguese\0"
495"Punjabi\0"
496"Quechua\0"
497"RhaetoRomance\0"
498"Romanian\0"
499"Russian\0"
500"Samoan\0"
501"Sangho\0"
502"Sanskrit\0"
503"Serbian\0"
504"SerboCroatian\0"
505"Sesotho\0"
506"Setswana\0"
507"Shona\0"
508"Sindhi\0"
509"Singhalese\0"
510"Siswati\0"
511"Slovak\0"
512"Slovenian\0"
513"Somali\0"
514"Spanish\0"
515"Sundanese\0"
516"Swahili\0"
517"Swedish\0"
518"Tagalog\0"
519"Tajik\0"
520"Tamil\0"
521"Tatar\0"
522"Telugu\0"
523"Thai\0"
524"Tibetan\0"
525"Tigrinya\0"
526"Tonga\0"
527"Tsonga\0"
528"Turkish\0"
529"Turkmen\0"
530"Twi\0"
531"Uigur\0"
532"Ukrainian\0"
533"Urdu\0"
534"Uzbek\0"
535"Vietnamese\0"
536"Volapuk\0"
537"Welsh\0"
538"Wolof\0"
539"Xhosa\0"
540"Yiddish\0"
541"Yoruba\0"
542"Zhuang\0"
543"Zulu\0";
544
545static const uint language_name_index[] = {
546 0, // Unused
547 8, // C
548 10, // Abkhazian
549 20, // Afan
550 25, // Afar
551 30, // Afrikaans
552 40, // Albanian
553 49, // Amharic
554 57, // Arabic
555 64, // Armenian
556 73, // Assamese
557 82, // Aymara
558 89, // Azerbaijani
559 101, // Bashkir
560 109, // Basque
561 116, // Bengali
562 124, // Bhutani
563 132, // Bihari
564 139, // Bislama
565 147, // Breton
566 154, // Bulgarian
567 164, // Burmese
568 172, // Byelorussian
569 185, // Cambodian
570 195, // Catalan
571 203, // Chinese
572 211, // Corsican
573 220, // Croatian
574 229, // Czech
575 235, // Danish
576 242, // Dutch
577 248, // English
578 256, // Esperanto
579 266, // Estonian
580 275, // Faroese
581 283, // Fiji
582 288, // Finnish
583 296, // French
584 303, // Frisian
585 311, // Gaelic
586 318, // Galician
587 327, // Georgian
588 336, // German
589 343, // Greek
590 349, // Greenlandic
591 361, // Guarani
592 369, // Gujarati
593 378, // Hausa
594 384, // Hebrew
595 391, // Hindi
596 397, // Hungarian
597 407, // Icelandic
598 417, // Indonesian
599 428, // Interlingua
600 440, // Interlingue
601 452, // Inuktitut
602 462, // Inupiak
603 470, // Irish
604 476, // Italian
605 484, // Japanese
606 493, // Javanese
607 502, // Kannada
608 510, // Kashmiri
609 519, // Kazakh
610 526, // Kinyarwanda
611 538, // Kirghiz
612 546, // Korean
613 553, // Kurdish
614 561, // Kurundi
615 569, // Laothian
616 578, // Latin
617 584, // Latvian
618 592, // Lingala
619 600, // Lithuanian
620 611, // Macedonian
621 622, // Malagasy
622 631, // Malay
623 637, // Malayalam
624 647, // Maltese
625 655, // Maori
626 661, // Marathi
627 669, // Moldavian
628 679, // Mongolian
629 689, // Nauru
630 695, // Nepali
631 702, // Norwegian
632 712, // Occitan
633 720, // Oriya
634 726, // Pashto
635 733, // Persian
636 741, // Polish
637 748, // Portuguese
638 759, // Punjabi
639 767, // Quechua
640 775, // RhaetoRomance
641 789, // Romanian
642 798, // Russian
643 806, // Samoan
644 813, // Sangho
645 820, // Sanskrit
646 829, // Serbian
647 837, // SerboCroatian
648 851, // Sesotho
649 859, // Setswana
650 868, // Shona
651 874, // Sindhi
652 881, // Singhalese
653 892, // Siswati
654 900, // Slovak
655 907, // Slovenian
656 917, // Somali
657 924, // Spanish
658 932, // Sundanese
659 942, // Swahili
660 950, // Swedish
661 958, // Tagalog
662 966, // Tajik
663 972, // Tamil
664 978, // Tatar
665 984, // Telugu
666 991, // Thai
667 996, // Tibetan
668 1004, // Tigrinya
669 1013, // Tonga
670 1019, // Tsonga
671 1026, // Turkish
672 1034, // Turkmen
673 1042, // Twi
674 1046, // Uigur
675 1052, // Ukrainian
676 1062, // Urdu
677 1067, // Uzbek
678 1073, // Vietnamese
679 1084, // Volapuk
680 1092, // Welsh
681 1098, // Wolof
682 1104, // Xhosa
683 1110, // Yiddish
684 1118, // Yoruba
685 1125, // Zhuang
686 1132 // Zulu
687};
688
689static const char country_name_list[] =
690"Default\0"
691"Afghanistan\0"
692"Albania\0"
693"Algeria\0"
694"AmericanSamoa\0"
695"Andorra\0"
696"Angola\0"
697"Anguilla\0"
698"Antarctica\0"
699"AntiguaAndBarbuda\0"
700"Argentina\0"
701"Armenia\0"
702"Aruba\0"
703"Australia\0"
704"Austria\0"
705"Azerbaijan\0"
706"Bahamas\0"
707"Bahrain\0"
708"Bangladesh\0"
709"Barbados\0"
710"Belarus\0"
711"Belgium\0"
712"Belize\0"
713"Benin\0"
714"Bermuda\0"
715"Bhutan\0"
716"Bolivia\0"
717"BosniaAndHerzegowina\0"
718"Botswana\0"
719"BouvetIsland\0"
720"Brazil\0"
721"BritishIndianOceanTerritory\0"
722"BruneiDarussalam\0"
723"Bulgaria\0"
724"BurkinaFaso\0"
725"Burundi\0"
726"Cambodia\0"
727"Cameroon\0"
728"Canada\0"
729"CapeVerde\0"
730"CaymanIslands\0"
731"CentralAfricanRepublic\0"
732"Chad\0"
733"Chile\0"
734"China\0"
735"ChristmasIsland\0"
736"CocosIslands\0"
737"Colombia\0"
738"Comoros\0"
739"DemocraticRepublicOfCongo\0"
740"PeoplesRepublicOfCongo\0"
741"CookIslands\0"
742"CostaRica\0"
743"IvoryCoast\0"
744"Croatia\0"
745"Cuba\0"
746"Cyprus\0"
747"CzechRepublic\0"
748"Denmark\0"
749"Djibouti\0"
750"Dominica\0"
751"DominicanRepublic\0"
752"EastTimor\0"
753"Ecuador\0"
754"Egypt\0"
755"ElSalvador\0"
756"EquatorialGuinea\0"
757"Eritrea\0"
758"Estonia\0"
759"Ethiopia\0"
760"FalklandIslands\0"
761"FaroeIslands\0"
762"Fiji\0"
763"Finland\0"
764"France\0"
765"MetropolitanFrance\0"
766"FrenchGuiana\0"
767"FrenchPolynesia\0"
768"FrenchSouthernTerritories\0"
769"Gabon\0"
770"Gambia\0"
771"Georgia\0"
772"Germany\0"
773"Ghana\0"
774"Gibraltar\0"
775"Greece\0"
776"Greenland\0"
777"Grenada\0"
778"Guadeloupe\0"
779"Guam\0"
780"Guatemala\0"
781"Guinea\0"
782"GuineaBissau\0"
783"Guyana\0"
784"Haiti\0"
785"HeardAndMcDonaldIslands\0"
786"Honduras\0"
787"HongKong\0"
788"Hungary\0"
789"Iceland\0"
790"India\0"
791"Indonesia\0"
792"Iran\0"
793"Iraq\0"
794"Ireland\0"
795"Israel\0"
796"Italy\0"
797"Jamaica\0"
798"Japan\0"
799"Jordan\0"
800"Kazakhstan\0"
801"Kenya\0"
802"Kiribati\0"
803"DemocraticRepublicOfKorea\0"
804"RepublicOfKorea\0"
805"Kuwait\0"
806"Kyrgyzstan\0"
807"Lao\0"
808"Latvia\0"
809"Lebanon\0"
810"Lesotho\0"
811"Liberia\0"
812"LibyanArabJamahiriya\0"
813"Liechtenstein\0"
814"Lithuania\0"
815"Luxembourg\0"
816"Macau\0"
817"Macedonia\0"
818"Madagascar\0"
819"Malawi\0"
820"Malaysia\0"
821"Maldives\0"
822"Mali\0"
823"Malta\0"
824"MarshallIslands\0"
825"Martinique\0"
826"Mauritania\0"
827"Mauritius\0"
828"Mayotte\0"
829"Mexico\0"
830"Micronesia\0"
831"Moldova\0"
832"Monaco\0"
833"Mongolia\0"
834"Montserrat\0"
835"Morocco\0"
836"Mozambique\0"
837"Myanmar\0"
838"Namibia\0"
839"Nauru\0"
840"Nepal\0"
841"Netherlands\0"
842"NetherlandsAntilles\0"
843"NewCaledonia\0"
844"NewZealand\0"
845"Nicaragua\0"
846"Niger\0"
847"Nigeria\0"
848"Niue\0"
849"NorfolkIsland\0"
850"NorthernMarianaIslands\0"
851"Norway\0"
852"Oman\0"
853"Pakistan\0"
854"Palau\0"
855"PalestinianTerritory\0"
856"Panama\0"
857"PapuaNewGuinea\0"
858"Paraguay\0"
859"Peru\0"
860"Philippines\0"
861"Pitcairn\0"
862"Poland\0"
863"Portugal\0"
864"PuertoRico\0"
865"Qatar\0"
866"Reunion\0"
867"Romania\0"
868"RussianFederation\0"
869"Rwanda\0"
870"SaintKittsAndNevis\0"
871"StLucia\0"
872"StVincentAndTheGrenadines\0"
873"Samoa\0"
874"SanMarino\0"
875"SaoTomeAndPrincipe\0"
876"SaudiArabia\0"
877"Senegal\0"
878"Seychelles\0"
879"SierraLeone\0"
880"Singapore\0"
881"Slovakia\0"
882"Slovenia\0"
883"SolomonIslands\0"
884"Somalia\0"
885"SouthAfrica\0"
886"SouthGeorgiaAndTheSouthSandwichIslands\0"
887"Spain\0"
888"SriLanka\0"
889"StHelena\0"
890"StPierreAndMiquelon\0"
891"Sudan\0"
892"Suriname\0"
893"SvalbardAndJanMayenIslands\0"
894"Swaziland\0"
895"Sweden\0"
896"Switzerland\0"
897"SyrianArabRepublic\0"
898"Taiwan\0"
899"Tajikistan\0"
900"Tanzania\0"
901"Thailand\0"
902"Togo\0"
903"Tokelau\0"
904"Tonga\0"
905"TrinidadAndTobago\0"
906"Tunisia\0"
907"Turkey\0"
908"Turkmenistan\0"
909"TurksAndCaicosIslands\0"
910"Tuvalu\0"
911"Uganda\0"
912"Ukraine\0"
913"UnitedArabEmirates\0"
914"UnitedKingdom\0"
915"UnitedStates\0"
916"UnitedStatesMinorOutlyingIslands\0"
917"Uruguay\0"
918"Uzbekistan\0"
919"Vanuatu\0"
920"VaticanCityState\0"
921"Venezuela\0"
922"VietNam\0"
923"BritishVirginIslands\0"
924"USVirginIslands\0"
925"WallisAndFutunaIslands\0"
926"WesternSahara\0"
927"Yemen\0"
928"Yugoslavia\0"
929"Zambia\0"
930"Zimbabwe\0";
931
932static const uint country_name_index[] = {
933 0, // AnyCountry
934 8, // Afghanistan
935 20, // Albania
936 28, // Algeria
937 36, // AmericanSamoa
938 50, // Andorra
939 58, // Angola
940 65, // Anguilla
941 74, // Antarctica
942 85, // AntiguaAndBarbuda
943 103, // Argentina
944 113, // Armenia
945 121, // Aruba
946 127, // Australia
947 137, // Austria
948 145, // Azerbaijan
949 156, // Bahamas
950 164, // Bahrain
951 172, // Bangladesh
952 183, // Barbados
953 192, // Belarus
954 200, // Belgium
955 208, // Belize
956 215, // Benin
957 221, // Bermuda
958 229, // Bhutan
959 236, // Bolivia
960 244, // BosniaAndHerzegowina
961 265, // Botswana
962 274, // BouvetIsland
963 287, // Brazil
964 294, // BritishIndianOceanTerritory
965 322, // BruneiDarussalam
966 339, // Bulgaria
967 348, // BurkinaFaso
968 360, // Burundi
969 368, // Cambodia
970 377, // Cameroon
971 386, // Canada
972 393, // CapeVerde
973 403, // CaymanIslands
974 417, // CentralAfricanRepublic
975 440, // Chad
976 445, // Chile
977 451, // China
978 457, // ChristmasIsland
979 473, // CocosIslands
980 486, // Colombia
981 495, // Comoros
982 503, // DemocraticRepublicOfCongo
983 529, // PeoplesRepublicOfCongo
984 552, // CookIslands
985 564, // CostaRica
986 574, // IvoryCoast
987 585, // Croatia
988 593, // Cuba
989 598, // Cyprus
990 605, // CzechRepublic
991 619, // Denmark
992 627, // Djibouti
993 636, // Dominica
994 645, // DominicanRepublic
995 663, // EastTimor
996 673, // Ecuador
997 681, // Egypt
998 687, // ElSalvador
999 698, // EquatorialGuinea
1000 715, // Eritrea
1001 723, // Estonia
1002 731, // Ethiopia
1003 740, // FalklandIslands
1004 756, // FaroeIslands
1005 769, // Fiji
1006 774, // Finland
1007 782, // France
1008 789, // MetropolitanFrance
1009 808, // FrenchGuiana
1010 821, // FrenchPolynesia
1011 837, // FrenchSouthernTerritories
1012 863, // Gabon
1013 869, // Gambia
1014 876, // Georgia
1015 884, // Germany
1016 892, // Ghana
1017 898, // Gibraltar
1018 908, // Greece
1019 915, // Greenland
1020 925, // Grenada
1021 933, // Guadeloupe
1022 944, // Guam
1023 949, // Guatemala
1024 959, // Guinea
1025 966, // GuineaBissau
1026 979, // Guyana
1027 986, // Haiti
1028 992, // HeardAndMcDonaldIslands
1029 1016, // Honduras
1030 1025, // HongKong
1031 1034, // Hungary
1032 1042, // Iceland
1033 1050, // India
1034 1056, // Indonesia
1035 1066, // Iran
1036 1071, // Iraq
1037 1076, // Ireland
1038 1084, // Israel
1039 1091, // Italy
1040 1097, // Jamaica
1041 1105, // Japan
1042 1111, // Jordan
1043 1118, // Kazakhstan
1044 1129, // Kenya
1045 1135, // Kiribati
1046 1144, // DemocraticRepublicOfKorea
1047 1170, // RepublicOfKorea
1048 1186, // Kuwait
1049 1193, // Kyrgyzstan
1050 1204, // Lao
1051 1208, // Latvia
1052 1215, // Lebanon
1053 1223, // Lesotho
1054 1231, // Liberia
1055 1239, // LibyanArabJamahiriya
1056 1260, // Liechtenstein
1057 1274, // Lithuania
1058 1284, // Luxembourg
1059 1295, // Macau
1060 1301, // Macedonia
1061 1311, // Madagascar
1062 1322, // Malawi
1063 1329, // Malaysia
1064 1338, // Maldives
1065 1347, // Mali
1066 1352, // Malta
1067 1358, // MarshallIslands
1068 1374, // Martinique
1069 1385, // Mauritania
1070 1396, // Mauritius
1071 1406, // Mayotte
1072 1414, // Mexico
1073 1421, // Micronesia
1074 1432, // Moldova
1075 1440, // Monaco
1076 1447, // Mongolia
1077 1456, // Montserrat
1078 1467, // Morocco
1079 1475, // Mozambique
1080 1486, // Myanmar
1081 1494, // Namibia
1082 1502, // Nauru
1083 1508, // Nepal
1084 1514, // Netherlands
1085 1526, // NetherlandsAntilles
1086 1546, // NewCaledonia
1087 1559, // NewZealand
1088 1570, // Nicaragua
1089 1580, // Niger
1090 1586, // Nigeria
1091 1594, // Niue
1092 1599, // NorfolkIsland
1093 1613, // NorthernMarianaIslands
1094 1636, // Norway
1095 1643, // Oman
1096 1648, // Pakistan
1097 1657, // Palau
1098 1663, // PalestinianTerritory
1099 1684, // Panama
1100 1691, // PapuaNewGuinea
1101 1706, // Paraguay
1102 1715, // Peru
1103 1720, // Philippines
1104 1732, // Pitcairn
1105 1741, // Poland
1106 1748, // Portugal
1107 1757, // PuertoRico
1108 1768, // Qatar
1109 1774, // Reunion
1110 1782, // Romania
1111 1790, // RussianFederation
1112 1808, // Rwanda
1113 1815, // SaintKittsAndNevis
1114 1834, // StLucia
1115 1842, // StVincentAndTheGrenadines
1116 1868, // Samoa
1117 1874, // SanMarino
1118 1884, // SaoTomeAndPrincipe
1119 1903, // SaudiArabia
1120 1915, // Senegal
1121 1923, // Seychelles
1122 1934, // SierraLeone
1123 1946, // Singapore
1124 1956, // Slovakia
1125 1965, // Slovenia
1126 1974, // SolomonIslands
1127 1989, // Somalia
1128 1997, // SouthAfrica
1129 2009, // SouthGeorgiaAndTheSouthSandwichIslands
1130 2048, // Spain
1131 2054, // SriLanka
1132 2063, // StHelena
1133 2072, // StPierreAndMiquelon
1134 2092, // Sudan
1135 2098, // Suriname
1136 2107, // SvalbardAndJanMayenIslands
1137 2134, // Swaziland
1138 2144, // Sweden
1139 2151, // Switzerland
1140 2163, // SyrianArabRepublic
1141 2182, // Taiwan
1142 2189, // Tajikistan
1143 2200, // Tanzania
1144 2209, // Thailand
1145 2218, // Togo
1146 2223, // Tokelau
1147 2231, // Tonga
1148 2237, // TrinidadAndTobago
1149 2255, // Tunisia
1150 2263, // Turkey
1151 2270, // Turkmenistan
1152 2283, // TurksAndCaicosIslands
1153 2305, // Tuvalu
1154 2312, // Uganda
1155 2319, // Ukraine
1156 2327, // UnitedArabEmirates
1157 2346, // UnitedKingdom
1158 2360, // UnitedStates
1159 2373, // UnitedStatesMinorOutlyingIslands
1160 2406, // Uruguay
1161 2414, // Uzbekistan
1162 2425, // Vanuatu
1163 2433, // VaticanCityState
1164 2450, // Venezuela
1165 2460, // VietNam
1166 2468, // BritishVirginIslands
1167 2489, // USVirginIslands
1168 2505, // WallisAndFutunaIslands
1169 2528, // WesternSahara
1170 2542, // Yemen
1171 2548, // Yugoslavia
1172 2559, // Zambia
1173 2566 // Zimbabwe
1174};
1175
1176static const char language_code_list[] =
1177" " // Unused
1178" " // C
1179"ab" // Abkhazian
1180"om" // Afan
1181"aa" // Afar
1182"af" // Afrikaans
1183"sq" // Albanian
1184"am" // Amharic
1185"ar" // Arabic
1186"hy" // Armenian
1187"as" // Assamese
1188"ay" // Aymara
1189"az" // Azerbaijani
1190"ba" // Bashkir
1191"eu" // Basque
1192"bn" // Bengali
1193"dz" // Bhutani
1194"bh" // Bihari
1195"bi" // Bislama
1196"br" // Breton
1197"bg" // Bulgarian
1198"my" // Burmese
1199"be" // Byelorussian
1200"km" // Cambodian
1201"ca" // Catalan
1202"zh" // Chinese
1203"co" // Corsican
1204"hr" // Croatian
1205"cs" // Czech
1206"da" // Danish
1207"nl" // Dutch
1208"en" // English
1209"eo" // Esperanto
1210"et" // Estonian
1211"fo" // Faroese
1212"fj" // Fiji
1213"fi" // Finnish
1214"fr" // French
1215"fy" // Frisian
1216"gd" // Gaelic
1217"gl" // Galician
1218"ka" // Georgian
1219"de" // German
1220"el" // Greek
1221"kl" // Greenlandic
1222"gn" // Guarani
1223"gu" // Gujarati
1224"ha" // Hausa
1225"he" // Hebrew
1226"hi" // Hindi
1227"hu" // Hungarian
1228"is" // Icelandic
1229"id" // Indonesian
1230"ia" // Interlingua
1231"ie" // Interlingue
1232"iu" // Inuktitut
1233"ik" // Inupiak
1234"ga" // Irish
1235"it" // Italian
1236"ja" // Japanese
1237"jv" // Javanese
1238"kn" // Kannada
1239"ks" // Kashmiri
1240"kk" // Kazakh
1241"rw" // Kinyarwanda
1242"ky" // Kirghiz
1243"ko" // Korean
1244"ku" // Kurdish
1245"rn" // Kurundi
1246"lo" // Laothian
1247"la" // Latin
1248"lv" // Latvian
1249"ln" // Lingala
1250"lt" // Lithuanian
1251"mk" // Macedonian
1252"mg" // Malagasy
1253"ms" // Malay
1254"ml" // Malayalam
1255"mt" // Maltese
1256"mi" // Maori
1257"mr" // Marathi
1258"mo" // Moldavian
1259"mn" // Mongolian
1260"na" // Nauru
1261"ne" // Nepali
1262"no" // Norwegian
1263"oc" // Occitan
1264"or" // Oriya
1265"ps" // Pashto
1266"fa" // Persian
1267"pl" // Polish
1268"pt" // Portuguese
1269"pa" // Punjabi
1270"qu" // Quechua
1271"rm" // RhaetoRomance
1272"ro" // Romanian
1273"ru" // Russian
1274"sm" // Samoan
1275"sg" // Sangho
1276"sa" // Sanskrit
1277"sr" // Serbian
1278"sh" // SerboCroatian
1279"st" // Sesotho
1280"tn" // Setswana
1281"sn" // Shona
1282"sd" // Sindhi
1283"si" // Singhalese
1284"ss" // Siswati
1285"sk" // Slovak
1286"sl" // Slovenian
1287"so" // Somali
1288"es" // Spanish
1289"su" // Sundanese
1290"sw" // Swahili
1291"sv" // Swedish
1292"tl" // Tagalog
1293"tg" // Tajik
1294"ta" // Tamil
1295"tt" // Tatar
1296"te" // Telugu
1297"th" // Thai
1298"bo" // Tibetan
1299"ti" // Tigrinya
1300"to" // Tonga
1301"ts" // Tsonga
1302"tr" // Turkish
1303"tk" // Turkmen
1304"tw" // Twi
1305"ug" // Uigur
1306"uk" // Ukrainian
1307"ur" // Urdu
1308"uz" // Uzbek
1309"vi" // Vietnamese
1310"vo" // Volapuk
1311"cy" // Welsh
1312"wo" // Wolof
1313"xh" // Xhosa
1314"yi" // Yiddish
1315"yo" // Yoruba
1316"za" // Zhuang
1317"zu" // Zulu
1318;
1319
1320static const char country_code_list[] =
1321" " // AnyLanguage
1322"AF" // Afghanistan
1323"AL" // Albania
1324"DZ" // Algeria
1325"AS" // AmericanSamoa
1326"AD" // Andorra
1327"AO" // Angola
1328"AI" // Anguilla
1329"AQ" // Antarctica
1330"AG" // AntiguaAndBarbuda
1331"AR" // Argentina
1332"AM" // Armenia
1333"AW" // Aruba
1334"AU" // Australia
1335"AT" // Austria
1336"AZ" // Azerbaijan
1337"BS" // Bahamas
1338"BH" // Bahrain
1339"BD" // Bangladesh
1340"BB" // Barbados
1341"BY" // Belarus
1342"BE" // Belgium
1343"BZ" // Belize
1344"BJ" // Benin
1345"BM" // Bermuda
1346"BT" // Bhutan
1347"BO" // Bolivia
1348"BA" // BosniaAndHerzegowina
1349"BW" // Botswana
1350"BV" // BouvetIsland
1351"BR" // Brazil
1352"IO" // BritishIndianOceanTerritory
1353"BN" // BruneiDarussalam
1354"BG" // Bulgaria
1355"BF" // BurkinaFaso
1356"BI" // Burundi
1357"KH" // Cambodia
1358"CM" // Cameroon
1359"CA" // Canada
1360"CV" // CapeVerde
1361"KY" // CaymanIslands
1362"CF" // CentralAfricanRepublic
1363"TD" // Chad
1364"CL" // Chile
1365"CN" // China
1366"CX" // ChristmasIsland
1367"CC" // CocosIslands
1368"CO" // Colombia
1369"KM" // Comoros
1370"CD" // DemocraticRepublicOfCongo
1371"CG" // PeoplesRepublicOfCongo
1372"CK" // CookIslands
1373"CR" // CostaRica
1374"CI" // IvoryCoast
1375"HR" // Croatia
1376"CU" // Cuba
1377"CY" // Cyprus
1378"CZ" // CzechRepublic
1379"DK" // Denmark
1380"DJ" // Djibouti
1381"DM" // Dominica
1382"DO" // DominicanRepublic
1383"TL" // EastTimor
1384"EC" // Ecuador
1385"EG" // Egypt
1386"SV" // ElSalvador
1387"GQ" // EquatorialGuinea
1388"ER" // Eritrea
1389"EE" // Estonia
1390"ET" // Ethiopia
1391"FK" // FalklandIslands
1392"FO" // FaroeIslands
1393"FJ" // Fiji
1394"FI" // Finland
1395"FR" // France
1396"FX" // MetropolitanFrance
1397"GF" // FrenchGuiana
1398"PF" // FrenchPolynesia
1399"TF" // FrenchSouthernTerritories
1400"GA" // Gabon
1401"GM" // Gambia
1402"GE" // Georgia
1403"DE" // Germany
1404"GH" // Ghana
1405"GI" // Gibraltar
1406"GR" // Greece
1407"GL" // Greenland
1408"GD" // Grenada
1409"GP" // Guadeloupe
1410"GU" // Guam
1411"GT" // Guatemala
1412"GN" // Guinea
1413"GW" // GuineaBissau
1414"GY" // Guyana
1415"HT" // Haiti
1416"HM" // HeardAndMcDonaldIslands
1417"HN" // Honduras
1418"HK" // HongKong
1419"HU" // Hungary
1420"IS" // Iceland
1421"IN" // India
1422"ID" // Indonesia
1423"IR" // Iran
1424"IQ" // Iraq
1425"IE" // Ireland
1426"IL" // Israel
1427"IT" // Italy
1428"JM" // Jamaica
1429"JP" // Japan
1430"JO" // Jordan
1431"KZ" // Kazakhstan
1432"KE" // Kenya
1433"KI" // Kiribati
1434"KP" // DemocraticRepublicOfKorea
1435"KR" // RepublicOfKorea
1436"KW" // Kuwait
1437"KG" // Kyrgyzstan
1438"LA" // Lao
1439"LV" // Latvia
1440"LB" // Lebanon
1441"LS" // Lesotho
1442"LR" // Liberia
1443"LY" // LibyanArabJamahiriya
1444"LI" // Liechtenstein
1445"LT" // Lithuania
1446"LU" // Luxembourg
1447"MO" // Macau
1448"MK" // Macedonia
1449"MG" // Madagascar
1450"MW" // Malawi
1451"MY" // Malaysia
1452"MV" // Maldives
1453"ML" // Mali
1454"MT" // Malta
1455"MH" // MarshallIslands
1456"MQ" // Martinique
1457"MR" // Mauritania
1458"MU" // Mauritius
1459"YT" // Mayotte
1460"MX" // Mexico
1461"FM" // Micronesia
1462"MD" // Moldova
1463"MC" // Monaco
1464"MN" // Mongolia
1465"MS" // Montserrat
1466"MA" // Morocco
1467"MZ" // Mozambique
1468"MM" // Myanmar
1469"NA" // Namibia
1470"NR" // Nauru
1471"NP" // Nepal
1472"NL" // Netherlands
1473"AN" // NetherlandsAntilles
1474"NC" // NewCaledonia
1475"NZ" // NewZealand
1476"NI" // Nicaragua
1477"NE" // Niger
1478"NG" // Nigeria
1479"NU" // Niue
1480"NF" // NorfolkIsland
1481"MP" // NorthernMarianaIslands
1482"NO" // Norway
1483"OM" // Oman
1484"PK" // Pakistan
1485"PW" // Palau
1486"PS" // PalestinianTerritory
1487"PA" // Panama
1488"PG" // PapuaNewGuinea
1489"PY" // Paraguay
1490"PE" // Peru
1491"PH" // Philippines
1492"PN" // Pitcairn
1493"PL" // Poland
1494"PT" // Portugal
1495"PR" // PuertoRico
1496"QA" // Qatar
1497"RE" // Reunion
1498"RO" // Romania
1499"RU" // RussianFederation
1500"RW" // Rwanda
1501"KN" // SaintKittsAndNevis
1502"LC" // StLucia
1503"VC" // StVincentAndTheGrenadines
1504"WS" // Samoa
1505"SM" // SanMarino
1506"ST" // SaoTomeAndPrincipe
1507"SA" // SaudiArabia
1508"SN" // Senegal
1509"SC" // Seychelles
1510"SL" // SierraLeone
1511"SG" // Singapore
1512"SK" // Slovakia
1513"SI" // Slovenia
1514"SB" // SolomonIslands
1515"SO" // Somalia
1516"ZA" // SouthAfrica
1517"GS" // SouthGeorgiaAndTheSouthSandwichIslands
1518"ES" // Spain
1519"LK" // SriLanka
1520"SH" // StHelena
1521"PM" // StPierreAndMiquelon
1522"SD" // Sudan
1523"SR" // Suriname
1524"SJ" // SvalbardAndJanMayenIslands
1525"SZ" // Swaziland
1526"SE" // Sweden
1527"CH" // Switzerland
1528"SY" // SyrianArabRepublic
1529"TW" // Taiwan
1530"TJ" // Tajikistan
1531"TZ" // Tanzania
1532"TH" // Thailand
1533"TG" // Togo
1534"TK" // Tokelau
1535"TO" // Tonga
1536"TT" // TrinidadAndTobago
1537"TN" // Tunisia
1538"TR" // Turkey
1539"TM" // Turkmenistan
1540"TC" // TurksAndCaicosIslands
1541"TV" // Tuvalu
1542"UG" // Uganda
1543"UA" // Ukraine
1544"AE" // UnitedArabEmirates
1545"GB" // UnitedKingdom
1546"US" // UnitedStates
1547"UM" // UnitedStatesMinorOutlyingIslands
1548"UY" // Uruguay
1549"UZ" // Uzbekistan
1550"VU" // Vanuatu
1551"VA" // VaticanCityState
1552"VE" // Venezuela
1553"VN" // VietNam
1554"VG" // BritishVirginIslands
1555"VI" // USVirginIslands
1556"WF" // WallisAndFutunaIslands
1557"EH" // WesternSahara
1558"YE" // Yemen
1559"YU" // Yugoslavia
1560"ZM" // Zambia
1561"ZW" // Zimbabwe
1562;
1563
1564static QLocale::Language codeToLanguage(const QString &code)
1565{
1566 if (code.length() != 2)
1567 return QLocale::C;
1568
1569 ushort uc1 = code.unicode()[0].unicode();
1570 ushort uc2 = code.unicode()[1].unicode();
1571
1572 const char *c = language_code_list;
1573 for (; *c != 0; c += 2) {
1574 if (uc1 == (unsigned char)c[0] && uc2 == (unsigned char)c[1])
1575 return (QLocale::Language) ((c - language_code_list)/2);
1576 }
1577
1578 return QLocale::C;
1579}
1580
1581static QLocale::Country codeToCountry(const QString &code)
1582{
1583 if (code.length() != 2)
1584 return QLocale::AnyCountry;
1585
1586 ushort uc1 = code.unicode()[0].unicode();
1587 ushort uc2 = code.unicode()[1].unicode();
1588
1589 const char *c = country_code_list;
1590 for (; *c != 0; c += 2) {
1591 if (uc1 == (unsigned char)c[0] && uc2 == (unsigned char)c[1])
1592 return (QLocale::Country) ((c - country_code_list)/2);
1593 }
1594
1595 return QLocale::AnyCountry;
1596}
1597
1598static QString languageToCode(QLocale::Language language)
1599{
1600 if (language == QLocale::C)
1601 return "C";
1602
1603 QString code;
1604 code.setLength(2);
1605 const char *c = language_code_list + 2*(uint)language;
1606 code[0] = c[0];
1607 code[1] = c[1];
1608 return code;
1609}
1610
1611static QString countryToCode(QLocale::Country country)
1612{
1613 if (country == QLocale::AnyCountry)
1614 return QString::null;
1615
1616 QString code;
1617 code.setLength(2);
1618 const char *c = country_code_list + 2*(uint)country;
1619 code[0] = c[0];
1620 code[1] = c[1];
1621 return code;
1622}
1623
1624const QLocalePrivate *QLocale::default_d = 0;
1625
1626QString QLocalePrivate::infinity() const
1627{
1628 return QString::fromLatin1("inf");
1629}
1630
1631QString QLocalePrivate::nan() const
1632{
1633 return QString::fromLatin1("nan");
1634}
1635
1636const char* QLocalePrivate::systemLocaleName()
1637{
1638 static QCString lang;
1639 lang = getenv( "LANG" );
1640
1641#if !defined( QWS ) && defined( Q_OS_MAC )
1642 if ( !lang.isEmpty() )
1643 return lang;
1644
1645 char mac_ret[255];
1646 if(!LocaleRefGetPartString(NULL, kLocaleLanguageMask | kLocaleRegionMask, 255, mac_ret))
1647 lang = mac_ret;
1648#endif
1649
1650#if defined(Q_WS_WIN)
1651 if ( !lang.isEmpty() )
1652 return lang;
1653
1654 QT_WA( {
1655 wchar_t out[256];
1656 QString language;
1657 QString sublanguage;
1658 if ( GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME , out, 255 ) )
1659 language = QString::fromUcs2( (ushort*)out );
1660 if ( GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, out, 255 ) )
1661 sublanguage = QString::fromUcs2( (ushort*)out ).lower();
1662 lang = language;
1663 if ( sublanguage != language && !sublanguage.isEmpty() )
1664 lang += "_" + sublanguage;
1665 } , {
1666 char out[256];
1667 QString language;
1668 QString sublanguage;
1669 if ( GetLocaleInfoA( LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, out, 255 ) )
1670 language = QString::fromLocal8Bit( out );
1671 if ( GetLocaleInfoA( LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, out, 255 ) )
1672 sublanguage = QString::fromLocal8Bit( out ).lower();
1673 lang = language;
1674 if ( sublanguage != language && !sublanguage.isEmpty() )
1675 lang += "_" + sublanguage;
1676 } );
1677#endif
1678 if ( lang.isEmpty() )
1679 lang = "C";
1680
1681 return lang;
1682}
1683
1684static const QLocalePrivate *findLocale(QLocale::Language language,
1685 QLocale::Country country)
1686{
1687 unsigned language_id = (unsigned)language;
1688 unsigned country_id = (unsigned)country;
1689
1690 uint idx = locale_index[language_id];
1691
1692 const QLocalePrivate *d = locale_data + idx;
1693
1694 if (idx == 0) // default language has no associated country
1695 return d;
1696
1697 if (country == QLocale::AnyCountry)
1698 return d;
1699
1700 Q_ASSERT(d->languageId() == language_id);
1701
1702 while (d->languageId() == language_id
1703 && d->countryId() != country_id)
1704 ++d;
1705
1706 if (d->countryId() == country_id
1707 && d->languageId() == language_id)
1708 return d;
1709
1710 return locale_data + idx;
1711}
1712
1713/*!
1714 \class QLocale
1715 \brief The QLocale class converts between numbers and their
1716 string representations in various languages.
1717
1718 \reentrant
1719 \ingroup text
1720
1721 It is initialized with a country/language pair in its constructor
1722 and offers number-to-string and string-to-number conversion
1723 functions simmilar to those in QString.
1724
1725 \code
1726 QLocale egyptian(QLocale::Arabic, QLocale::Egypt);
1727 QString s1 = egyptian.toString(1.571429E+07, 'e');
1728 QString s2 = egyptian.toString(10);
1729
1730 double d = egyptian.toDouble(s1);
1731 int s2 = egyptian.toInt(s2);
1732 \endcode
1733
1734 Additionally, QLocale supports the concept of a default locale,
1735 which can be set with the static member setDefault(). This
1736 allows a locale to be set globally for the entire application. If
1737 setDefault() is not called, the default locale is determined from
1738 the system's locale settings.
1739
1740 \code
1741 QLocale::setDefault(QLocale::Hebrew, QLocale::Israel);
1742
1743 QLocale hebrew; // Constructs a default QLocale
1744 QString s1 = hebrew.toString(15714.3, 'e');
1745 \endcode
1746
1747 When a language/country pair is specified in the constructor, one
1748 of three things can happen:
1749
1750 \list
1751 \i If the language/country pair is found in the database, it is used.
1752 \i If the language is found but the country is not, or if the country
1753 is \c AnyCountry, the language is used with the most
1754 appropriate available country (for example, Germany for German),
1755 \i If neither the language nor the country are found, QLocale
1756 defaults to the default locale (see setDefault()).
1757 \endlist
1758
1759 The "C" locale is identical to English/UnitedStates.
1760
1761 Use language() and country() to determine the actual language and
1762 country values used.
1763
1764 An alternative method for constructing a QLocale object is by
1765 specifying the locale name.
1766
1767 \code
1768 QLocale korean("ko");
1769 QLocale swiss("de_CH");
1770 \endcode
1771
1772 This constructor converts the locale name to a language/country
1773 pair; it does not use the system locale database.
1774
1775 All the methods in QLocale, with the exception of setDefault(),
1776 are reentrant.
1777
1778 The double-to-string and string-to-double conversion functions are
1779 covered by the following licenses:
1780
1781 \legalese
1782
1783 Copyright (c) 1991 by AT&T.
1784
1785 Permission to use, copy, modify, and distribute this software for any
1786 purpose without fee is hereby granted, provided that this entire notice
1787 is included in all copies of any software which is or includes a copy
1788 or modification of this software and in all copies of the supporting
1789 documentation for such software.
1790
1791 THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
1792 WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
1793 REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
1794 OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
1795
1796 This product includes software developed by the University of
1797 California, Berkeley and its contributors.
1798*/
1799
1800/*!
1801 \enum QLocale::Language
1802
1803 This enumerated type is used to specify a language.
1804
1805 \value C Identical to English/UnitedStates
1806 \value Abkhazian
1807 \value Afan
1808 \value Afar
1809 \value Afrikaans
1810 \value Albanian
1811 \value Amharic
1812 \value Arabic
1813 \value Armenian
1814 \value Assamese
1815 \value Aymara
1816 \value Azerbaijani
1817 \value Bashkir
1818 \value Basque
1819 \value Bengali
1820 \value Bhutani
1821 \value Bihari
1822 \value Bislama
1823 \value Breton
1824 \value Bulgarian
1825 \value Burmese
1826 \value Byelorussian
1827 \value Cambodian
1828 \value Catalan
1829 \value Chinese
1830 \value Corsican
1831 \value Croatian
1832 \value Czech
1833 \value Danish
1834 \value Dutch
1835 \value English
1836 \value Esperanto
1837 \value Estonian
1838 \value Faroese
1839 \value FijiLanguage
1840 \value Finnish
1841 \value French
1842 \value Frisian
1843 \value Gaelic
1844 \value Galician
1845 \value Georgian
1846 \value German
1847 \value Greek
1848 \value Greenlandic
1849 \value Guarani
1850 \value Gujarati
1851 \value Hausa
1852 \value Hebrew
1853 \value Hindi
1854 \value Hungarian
1855 \value Icelandic
1856 \value Indonesian
1857 \value Interlingua
1858 \value Interlingue
1859 \value Inuktitut
1860 \value Inupiak
1861 \value Irish
1862 \value Italian
1863 \value Japanese
1864 \value Javanese
1865 \value Kannada
1866 \value Kashmiri
1867 \value Kazakh
1868 \value Kinyarwanda
1869 \value Kirghiz
1870 \value Korean
1871 \value Kurdish
1872 \value Kurundi
1873 \value Laothian
1874 \value Latin
1875 \value Latvian
1876 \value Lingala
1877 \value Lithuanian
1878 \value Macedonian
1879 \value Malagasy
1880 \value Malay
1881 \value Malayalam
1882 \value Maltese
1883 \value Maori
1884 \value Marathi
1885 \value Moldavian
1886 \value Mongolian
1887 \value NauruLanguage
1888 \value Nepali
1889 \value Norwegian
1890 \value Occitan
1891 \value Oriya
1892 \value Pashto
1893 \value Persian
1894 \value Polish
1895 \value Portuguese
1896 \value Punjabi
1897 \value Quechua
1898 \value RhaetoRomance
1899 \value Romanian
1900 \value Russian
1901 \value Samoan
1902 \value Sangho
1903 \value Sanskrit
1904 \value Serbian
1905 \value SerboCroatian
1906 \value Sesotho
1907 \value Setswana
1908 \value Shona
1909 \value Sindhi
1910 \value Singhalese
1911 \value Siswati
1912 \value Slovak
1913 \value Slovenian
1914 \value Somali
1915 \value Spanish
1916 \value Sundanese
1917 \value Swahili
1918 \value Swedish
1919 \value Tagalog
1920 \value Tajik
1921 \value Tamil
1922 \value Tatar
1923 \value Telugu
1924 \value Thai
1925 \value Tibetan
1926 \value Tigrinya
1927 \value TongaLanguage
1928 \value Tsonga
1929 \value Turkish
1930 \value Turkmen
1931 \value Twi
1932 \value Uigur
1933 \value Ukrainian
1934 \value Urdu
1935 \value Uzbek
1936 \value Vietnamese
1937 \value Volapuk
1938 \value Welsh
1939 \value Wolof
1940 \value Xhosa
1941 \value Yiddish
1942 \value Yoruba
1943 \value Zhuang
1944 \value Zulu
1945*/
1946
1947/*!
1948 \enum QLocale::Country
1949
1950 This enumerated type is used to specify a country.
1951
1952 \value AnyCountry
1953 \value Afghanistan
1954 \value Albania
1955 \value Algeria
1956 \value AmericanSamoa
1957 \value Andorra
1958 \value Angola
1959 \value Anguilla
1960 \value Antarctica
1961 \value AntiguaAndBarbuda
1962 \value Argentina
1963 \value Armenia
1964 \value Aruba
1965 \value Australia
1966 \value Austria
1967 \value Azerbaijan
1968 \value Bahamas
1969 \value Bahrain
1970 \value Bangladesh
1971 \value Barbados
1972 \value Belarus
1973 \value Belgium
1974 \value Belize
1975 \value Benin
1976 \value Bermuda
1977 \value Bhutan
1978 \value Bolivia
1979 \value BosniaAndHerzegowina
1980 \value Botswana
1981 \value BouvetIsland
1982 \value Brazil
1983 \value BritishIndianOceanTerritory
1984 \value BruneiDarussalam
1985 \value Bulgaria
1986 \value BurkinaFaso
1987 \value Burundi
1988 \value Cambodia
1989 \value Cameroon
1990 \value Canada
1991 \value CapeVerde
1992 \value CaymanIslands
1993 \value CentralAfricanRepublic
1994 \value Chad
1995 \value Chile
1996 \value China
1997 \value ChristmasIsland
1998 \value CocosIslands
1999 \value Colombia
2000 \value Comoros
2001 \value DemocraticRepublicOfCongo
2002 \value PeoplesRepublicOfCongo
2003 \value CookIslands
2004 \value CostaRica
2005 \value IvoryCoast
2006 \value Croatia
2007 \value Cuba
2008 \value Cyprus
2009 \value CzechRepublic
2010 \value Denmark
2011 \value Djibouti
2012 \value Dominica
2013 \value DominicanRepublic
2014 \value EastTimor
2015 \value Ecuador
2016 \value Egypt
2017 \value ElSalvador
2018 \value EquatorialGuinea
2019 \value Eritrea
2020 \value Estonia
2021 \value Ethiopia
2022 \value FalklandIslands
2023 \value FaroeIslands
2024 \value FijiCountry
2025 \value Finland
2026 \value France
2027 \value MetropolitanFrance
2028 \value FrenchGuiana
2029 \value FrenchPolynesia
2030 \value FrenchSouthernTerritories
2031 \value Gabon
2032 \value Gambia
2033 \value Georgia
2034 \value Germany
2035 \value Ghana
2036 \value Gibraltar
2037 \value Greece
2038 \value Greenland
2039 \value Grenada
2040 \value Guadeloupe
2041 \value Guam
2042 \value Guatemala
2043 \value Guinea
2044 \value GuineaBissau
2045 \value Guyana
2046 \value Haiti
2047 \value HeardAndMcDonaldIslands
2048 \value Honduras
2049 \value HongKong
2050 \value Hungary
2051 \value Iceland
2052 \value India
2053 \value Indonesia
2054 \value Iran
2055 \value Iraq
2056 \value Ireland
2057 \value Israel
2058 \value Italy
2059 \value Jamaica
2060 \value Japan
2061 \value Jordan
2062 \value Kazakhstan
2063 \value Kenya
2064 \value Kiribati
2065 \value DemocraticRepublicOfKorea
2066 \value RepublicOfKorea
2067 \value Kuwait
2068 \value Kyrgyzstan
2069 \value Lao
2070 \value Latvia
2071 \value Lebanon
2072 \value Lesotho
2073 \value Liberia
2074 \value LibyanArabJamahiriya
2075 \value Liechtenstein
2076 \value Lithuania
2077 \value Luxembourg
2078 \value Macau
2079 \value Macedonia
2080 \value Madagascar
2081 \value Malawi
2082 \value Malaysia
2083 \value Maldives
2084 \value Mali
2085 \value Malta
2086 \value MarshallIslands
2087 \value Martinique
2088 \value Mauritania
2089 \value Mauritius
2090 \value Mayotte
2091 \value Mexico
2092 \value Micronesia
2093 \value Moldova
2094 \value Monaco
2095 \value Mongolia
2096 \value Montserrat
2097 \value Morocco
2098 \value Mozambique
2099 \value Myanmar
2100 \value Namibia
2101 \value NauruCountry
2102 \value Nepal
2103 \value Netherlands
2104 \value NetherlandsAntilles
2105 \value NewCaledonia
2106 \value NewZealand
2107 \value Nicaragua
2108 \value Niger
2109 \value Nigeria
2110 \value Niue
2111 \value NorfolkIsland
2112 \value NorthernMarianaIslands
2113 \value Norway
2114 \value Oman
2115 \value Pakistan
2116 \value Palau
2117 \value PalestinianTerritory
2118 \value Panama
2119 \value PapuaNewGuinea
2120 \value Paraguay
2121 \value Peru
2122 \value Philippines
2123 \value Pitcairn
2124 \value Poland
2125 \value Portugal
2126 \value PuertoRico
2127 \value Qatar
2128 \value Reunion
2129 \value Romania
2130 \value RussianFederation
2131 \value Rwanda
2132 \value SaintKittsAndNevis
2133 \value StLucia
2134 \value StVincentAndTheGrenadines
2135 \value Samoa
2136 \value SanMarino
2137 \value SaoTomeAndPrincipe
2138 \value SaudiArabia
2139 \value Senegal
2140 \value Seychelles
2141 \value SierraLeone
2142 \value Singapore
2143 \value Slovakia
2144 \value Slovenia
2145 \value SolomonIslands
2146 \value Somalia
2147 \value SouthAfrica
2148 \value SouthGeorgiaAndTheSouthSandwichIslands
2149 \value Spain
2150 \value SriLanka
2151 \value StHelena
2152 \value StPierreAndMiquelon
2153 \value Sudan
2154 \value Suriname
2155 \value SvalbardAndJanMayenIslands
2156 \value Swaziland
2157 \value Sweden
2158 \value Switzerland
2159 \value SyrianArabRepublic
2160 \value Taiwan
2161 \value Tajikistan
2162 \value Tanzania
2163 \value Thailand
2164 \value Togo
2165 \value Tokelau
2166 \value TongaCountry
2167 \value TrinidadAndTobago
2168 \value Tunisia
2169 \value Turkey
2170 \value Turkmenistan
2171 \value TurksAndCaicosIslands
2172 \value Tuvalu
2173 \value Uganda
2174 \value Ukraine
2175 \value UnitedArabEmirates
2176 \value UnitedKingdom
2177 \value UnitedStates
2178 \value UnitedStatesMinorOutlyingIslands
2179 \value Uruguay
2180 \value Uzbekistan
2181 \value Vanuatu
2182 \value VaticanCityState
2183 \value Venezuela
2184 \value VietNam
2185 \value BritishVirginIslands
2186 \value USVirginIslands
2187 \value WallisAndFutunaIslands
2188 \value WesternSahara
2189 \value Yemen
2190 \value Yugoslavia
2191 \value Zambia
2192 \value Zimbabwe
2193*/
2194
2195/*!
2196 Constructs a QLocale object with the specified \a name,
2197 which has the format
2198 "language[_country][.codeset][@modifier]" or "C", where:
2199
2200 \list
2201 \i language is a lowercase, two-letter, ISO 639 language code,
2202 \i territory is an uppercase, two-letter, ISO 3166 country code,
2203 \i and codeset and modifier are ignored.
2204 \endlist
2205
2206 If the string violates the locale format, or language is not
2207 a valid ISO 369 code, the "C" locale is used instead. If country
2208 is not present, or is not a valid ISO 3166 code, the most
2209 appropriate country is chosen for the specified language.
2210
2211 The language and country codes are converted to their respective
2212 \c Language and \c Country enums. After this conversion is
2213 performed the constructor behaves exactly like QLocale(Country,
2214 Language).
2215
2216 This constructor is much slower than QLocale(Country, Language).
2217
2218 \sa name()
2219*/
2220
2221QLocale::QLocale(const QString &name)
2222{
2223 Language lang = C;
2224 Country cntry = AnyCountry;
2225
2226 uint l = name.length();
2227
2228 do {
2229 if (l < 2)
2230 break;
2231
2232 const QChar *uc = name.unicode();
2233 if (l > 2
2234 && uc[2] != '_'
2235 && uc[2] != '.'
2236 && uc[2] != '@')
2237 break;
2238
2239 lang = codeToLanguage(name.mid(0, 2));
2240 if (lang == C)
2241 break;
2242
2243 if (l == 2 || uc[2] == '.' || uc[2] == '@')
2244 break;
2245
2246 // we have uc[2] == '_'
2247 if (l < 5)
2248 break;
2249
2250 if (l > 5 && uc[5] != '.' && uc[5] != '@')
2251 break;
2252
2253 cntry = codeToCountry(name.mid(3, 2));
2254 } while (false);
2255
2256 d = findLocale(lang, cntry);
2257}
2258
2259/*!
2260 Constructs a QLocale object initialized with the default locale.
2261
2262 \sa setDefault()
2263*/
2264
2265QLocale::QLocale()
2266{
2267 if (default_d == 0)
2268 default_d = system().d;
2269
2270 d = default_d;
2271}
2272
2273/*!
2274 Constructs a QLocale object with the specified \a language and \a
2275 country.
2276
2277 \list
2278 \i If the language/country pair is found in the database, it is used.
2279 \i If the language is found but the country is not, or if the country
2280 is \c AnyCountry, the language is used with the most
2281 appropriate available country (for example, Germany for German),
2282 \i If neither the language nor the country are found, QLocale
2283 defaults to the default locale (see setDefault()).
2284 \endlist
2285
2286 The language and country that are actually used can be queried
2287 using language() and country().
2288
2289 \sa setDefault() language() country()
2290*/
2291
2292QLocale::QLocale(Language language, Country country)
2293{
2294 d = findLocale(language, country);
2295
2296 // If not found, should default to system
2297 if (d->languageId() == QLocale::C && language != QLocale::C) {
2298 if (default_d == 0)
2299 default_d = system().d;
2300
2301 d = default_d;
2302 }
2303}
2304
2305/*!
2306 Constructs a QLocale object as a copy of \a other.
2307*/
2308
2309QLocale::QLocale(const QLocale &other)
2310{
2311 d = other.d;
2312}
2313
2314/*!
2315 Assigns \a other to this QLocale object and returns a reference
2316 to this QLocale object.
2317*/
2318
2319QLocale &QLocale::operator=(const QLocale &other)
2320{
2321 d = other.d;
2322 return *this;
2323}
2324
2325/*!
2326 \nonreentrant
2327
2328 Sets the global default locale to \a locale. These
2329 values are used when a QLocale object is constructed with
2330 no arguments. If this function is not called, the system's
2331 locale is used.
2332
2333 \warning In a multithreaded application, the default locale
2334 should be set at application startup, before any non-GUI threads
2335 are created.
2336
2337 \sa system() c()
2338*/
2339
2340void QLocale::setDefault(const QLocale &locale)
2341{
2342 default_d = locale.d;
2343}
2344
2345/*!
2346 Returns the language of this locale.
2347
2348 \sa QLocale()
2349*/
2350QLocale::Language QLocale::language() const
2351{
2352 return (Language)d->languageId();
2353}
2354
2355/*!
2356 Returns the country of this locale.
2357
2358 \sa QLocale()
2359*/
2360QLocale::Country QLocale::country() const
2361{
2362 return (Country)d->countryId();
2363}
2364
2365/*!
2366 Returns the language and country of this locale as a
2367 string of the form "language_country", where
2368 language is a lowercase, two-letter ISO 639 language code,
2369 and country is an uppercase, two-letter ISO 3166 country code.
2370
2371 \sa QLocale()
2372*/
2373
2374QString QLocale::name() const
2375{
2376 Language l = language();
2377
2378 QString result = languageToCode(l);
2379
2380 if (l == C)
2381 return result;
2382
2383 Country c = country();
2384 if (c == AnyCountry)
2385 return result;
2386
2387 result.append('_');
2388 result.append(countryToCode(c));
2389
2390 return result;
2391}
2392
2393/*!
2394 Returns a QString containing the name of \a language.
2395*/
2396
2397QString QLocale::languageToString(Language language)
2398{
2399 if ((uint)language > (uint)QLocale::LastLanguage)
2400 return "Unknown";
2401 return language_name_list + language_name_index[(uint)language];
2402}
2403
2404/*!
2405 Returns a QString containing the name of \a country.
2406*/
2407
2408QString QLocale::countryToString(Country country)
2409{
2410 if ((uint)country > (uint)QLocale::LastCountry)
2411 return "Unknown";
2412 return country_name_list + country_name_index[(uint)country];
2413}
2414
2415/*!
2416 Returns the short int represented by the localized string \a s, or
2417 0 if the conversion failed.
2418
2419 If \a ok is not 0, reports failure by setting
2420 *ok to false and success by setting *ok to true.
2421
2422 \sa toString()
2423*/
2424
2425short QLocale::toShort(const QString &s, bool *ok) const
2426{
2427 Q_LLONG i = toLongLong(s, ok);
2428 if (i < SHRT_MIN || i > SHRT_MAX) {
2429 if (ok != 0)
2430 *ok = false;
2431 return 0;
2432 }
2433 return (short) i;
2434}
2435
2436/*!
2437 Returns the unsigned short int represented by the localized string
2438 \a s, or 0 if the conversion failed.
2439
2440 If \a ok is not 0, reports failure by setting
2441 *ok to false and success by setting *ok to true.
2442
2443 \sa toString()
2444*/
2445
2446ushort QLocale::toUShort(const QString &s, bool *ok) const
2447{
2448 Q_ULLONG i = toULongLong(s, ok);
2449 if (i > USHRT_MAX) {
2450 if (ok != 0)
2451 *ok = false;
2452 return 0;
2453 }
2454 return (ushort) i;
2455}
2456
2457/*!
2458 Returns the int represented by the localized string \a s, or 0 if
2459 the conversion failed.
2460
2461 If \a ok is not 0, reports failure by setting *ok to false and
2462 success by setting *ok to true.
2463
2464 \sa toString()
2465*/
2466
2467int QLocale::toInt(const QString &s, bool *ok) const
2468{
2469 Q_LLONG i = toLongLong(s, ok);
2470 if (i < INT_MIN || i > INT_MAX) {
2471 if (ok != 0)
2472 *ok = false;
2473 return 0;
2474 }
2475 return (int) i;
2476}
2477
2478/*!
2479 Returns the unsigned int represented by the localized string \a s,
2480 or 0 if the conversion failed.
2481
2482 If \a ok is not 0, reports failure by setting
2483 *ok to false and success by setting *ok to true.
2484
2485 \sa toString()
2486*/
2487
2488uint QLocale::toUInt(const QString &s, bool *ok) const
2489{
2490 Q_ULLONG i = toULongLong(s, ok);
2491 if (i > UINT_MAX) {
2492 if (ok != 0)
2493 *ok = false;
2494 return 0;
2495 }
2496 return (uint) i;
2497}
2498
2499/*!
2500 Returns the long int represented by the localized string \a s, or
2501 0 if the conversion failed.
2502
2503 If \a ok is not 0, reports failure by setting
2504 *ok to false and success by setting *ok to true.
2505
2506 \sa toString()
2507*/
2508
2509Q_LONG QLocale::toLong(const QString &s, bool *ok) const
2510{
2511 Q_LLONG i = toLongLong(s, ok);
2512 if (i < LONG_MIN || i > LONG_MAX) {
2513 if (ok != 0)
2514 *ok = false;
2515 return 0;
2516 }
2517 return (Q_LONG) i;
2518}
2519
2520/*!
2521 Returns the unsigned long int represented by the localized string
2522 \a s, or 0 if the conversion failed.
2523
2524 If \a ok is not 0, reports failure by setting
2525 *ok to false and success by setting *ok to true.
2526
2527 \sa toString()
2528*/
2529
2530Q_ULONG QLocale::toULong(const QString &s, bool *ok) const
2531{
2532 Q_ULLONG i = toULongLong(s, ok);
2533 if (i > ULONG_MAX) {
2534 if (ok != 0)
2535 *ok = false;
2536 return 0;
2537 }
2538 return (Q_ULONG) i;
2539}
2540
2541/*!
2542 Returns the long long int represented by the localized string \a
2543 s, or 0 if the conversion failed.
2544
2545 If \a ok is not 0, reports failure by setting
2546 *ok to false and success by setting *ok to true.
2547
2548 \sa toString()
2549*/
2550
2551
2552Q_LLONG QLocale::toLongLong(const QString &s, bool *ok) const
2553{
2554 return d->stringToLongLong(s, 0, ok);
2555}
2556
2557/*!
2558 Returns the unsigned long long int represented by the localized
2559 string \a s, or 0 if the conversion failed.
2560
2561 If \a ok is not 0, reports failure by setting
2562 *ok to false and success by setting *ok to true.
2563
2564 \sa toString()
2565*/
2566
2567
2568Q_ULLONG QLocale::toULongLong(const QString &s, bool *ok) const
2569{
2570 return d->stringToUnsLongLong(s, 0, ok);
2571}
2572
2573/*!
2574 Returns the float represented by the localized string \a s, or 0.0
2575 if the conversion failed.
2576
2577 If \a ok is not 0, reports failure by setting
2578 *ok to false and success by setting *ok to true.
2579
2580 \sa toString()
2581*/
2582
2583float QLocale::toFloat(const QString &s, bool *ok) const
2584{
2585 return (float) toDouble(s, ok);
2586}
2587
2588/*!
2589 Returns the double represented by the localized string \a s, or
2590 0.0 if the conversion failed.
2591
2592 If \a ok is not 0, reports failure by setting
2593 *ok to false and success by setting *ok to true.
2594
2595 \sa toString()
2596*/
2597
2598double QLocale::toDouble(const QString &s, bool *ok) const
2599{
2600 return d->stringToDouble(s, ok);
2601}
2602
2603/*!
2604 Returns a localized string representation of \a i.
2605
2606 \sa toLongLong()
2607*/
2608
2609QString QLocale::toString(Q_LLONG i) const
2610{
2611 return d->longLongToString(i, -1, 10, QLocalePrivate::ThousandsGroup);
2612}
2613
2614/*!
2615 \overload
2616
2617 \sa toULongLong()
2618*/
2619
2620QString QLocale::toString(Q_ULLONG i) const
2621{
2622 return d->unsLongLongToString(i, -1, 10, QLocalePrivate::ThousandsGroup);
2623}
2624
2625static bool qIsUpper(char c)
2626{
2627 return c >= 'A' && c <= 'Z';
2628}
2629
2630static char qToLower(char c)
2631{
2632 if (c >= 'A' && c <= 'Z')
2633 return c - 'A' + 'a';
2634 else
2635 return c;
2636}
2637
2638/*!
2639 \overload
2640
2641 \a f and \a prec have the same meaning as in QString::number(double, char, int).
2642
2643 \sa toDouble()
2644*/
2645
2646QString QLocale::toString(double i, char f, int prec) const
2647{
2648 QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
2649 uint flags = 0;
2650
2651 if (qIsUpper(f))
2652 flags = QLocalePrivate::CapitalEorX;
2653 f = qToLower(f);
2654
2655 switch (f) {
2656 case 'f':
2657 form = QLocalePrivate::DFDecimal;
2658 break;
2659 case 'e':
2660 form = QLocalePrivate::DFExponent;
2661 break;
2662 case 'g':
2663 form = QLocalePrivate::DFSignificantDigits;
2664 break;
2665 default:
2666 break;
2667 }
2668
2669 flags |= QLocalePrivate::ThousandsGroup;
2670 return d->doubleToString(i, prec, form, -1, flags);
2671}
2672
2673/*!
2674 \fn QLocale QLocale::c()
2675
2676 Returns a QLocale object initialized to the "C" locale.
2677
2678 \sa system()
2679*/
2680
2681/*!
2682 Returns a QLocale object initialized to the system locale.
2683*/
2684
2685QLocale QLocale::system()
2686{
2687 return QLocale(QLocalePrivate::systemLocaleName());
2688}
2689
2690/*!
2691\fn QString QLocale::toString(short i) const
2692
2693\overload
2694
2695\sa toShort()
2696*/
2697
2698/*!
2699\fn QString QLocale::toString(ushort i) const
2700
2701\overload
2702
2703\sa toUShort()
2704*/
2705
2706/*!
2707\fn QString QLocale::toString(int i) const
2708
2709\overload
2710
2711\sa toInt()
2712*/
2713
2714/*!
2715\fn QString QLocale::toString(uint i) const
2716
2717\overload
2718
2719\sa toUInt()
2720*/
2721
2722/*!
2723\fn QString QLocale::toString(Q_LONG i) const
2724
2725\overload
2726
2727\sa toLong()
2728*/
2729
2730/*!
2731\fn QString QLocale::toString(Q_ULONG i) const
2732
2733\overload
2734
2735\sa toULong()
2736*/
2737
2738/*!
2739\fn QString QLocale::toString(float i, char f = 'g', int prec = 6) const
2740
2741\overload
2742
2743\a f and \a prec have the same meaning as in QString::number(double, char, int).
2744
2745\sa toDouble()
2746*/
2747
2748
2749bool QLocalePrivate::isDigit(QChar d) const
2750{
2751 return zero().unicode() <= d.unicode()
2752 && zero().unicode() + 10 > d.unicode();
2753}
2754
2755static char digitToCLocale(QChar zero, QChar d)
2756{
2757 if (zero.unicode() <= d.unicode()
2758 && zero.unicode() + 10 > d.unicode())
2759 return '0' + d.unicode() - zero.unicode();
2760
2761 qWarning("QLocalePrivate::digitToCLocale(): bad digit: row=%d, cell=%d", d.row(), d.cell());
2762 return QChar(0);
2763}
2764
2765static QString qulltoa(Q_ULLONG l, int base, const QLocalePrivate &locale)
2766{
2767 QChar buff[65]; // length of MAX_ULLONG in base 2
2768 QChar *p = buff + 65;
2769
2770 if (base != 10 || locale.zero().unicode() == '0') {
2771 while (l != 0) {
2772 int c = l % base;
2773
2774 --p;
2775
2776 if (c < 10)
2777 *p = '0' + c;
2778 else
2779 *p = c - 10 + 'a';
2780
2781 l /= base;
2782 }
2783 }
2784 else {
2785 while (l != 0) {
2786 int c = l % base;
2787
2788 *(--p) = locale.zero().unicode() + c;
2789
2790 l /= base;
2791 }
2792 }
2793
2794 return QString(p, 65 - (p - buff));
2795}
2796
2797static QString qlltoa(Q_LLONG l, int base, const QLocalePrivate &locale)
2798{
2799 return qulltoa(l < 0 ? -l : l, base, locale);
2800}
2801
2802enum PrecisionMode {
2803 PMDecimalDigits = 0x01,
2804 PMSignificantDigits = 0x02,
2805 PMChopTrailingZeros = 0x03
2806};
2807
2808static QString &decimalForm(QString &digits, int decpt, uint precision,
2809 PrecisionMode pm,
2810 bool always_show_decpt,
2811 bool thousands_group,
2812 const QLocalePrivate &locale)
2813{
2814 if (decpt < 0) {
2815 for (int i = 0; i < -decpt; ++i)
2816 digits.prepend(locale.zero());
2817 decpt = 0;
2818 }
2819 else if ((uint)decpt > digits.length()) {
2820 for (uint i = digits.length(); i < (uint)decpt; ++i)
2821 digits.append(locale.zero());
2822 }
2823
2824 if (pm == PMDecimalDigits) {
2825 uint decimal_digits = digits.length() - decpt;
2826 for (uint i = decimal_digits; i < precision; ++i)
2827 digits.append(locale.zero());
2828 }
2829 else if (pm == PMSignificantDigits) {
2830 for (uint i = digits.length(); i < precision; ++i)
2831 digits.append(locale.zero());
2832 }
2833 else { // pm == PMChopTrailingZeros
2834 }
2835
2836 if (always_show_decpt || (uint)decpt < digits.length())
2837 digits.insert(decpt, locale.decimal());
2838
2839 if (thousands_group) {
2840 for (int i = decpt - 3; i > 0; i -= 3)
2841 digits.insert(i, locale.group());
2842 }
2843
2844 if (decpt == 0)
2845 digits.prepend(locale.zero());
2846
2847 return digits;
2848}
2849
2850static QString &exponentForm(QString &digits, int decpt, uint precision,
2851 PrecisionMode pm,
2852 bool always_show_decpt,
2853 const QLocalePrivate &locale)
2854{
2855 int exp = decpt - 1;
2856
2857 if (pm == PMDecimalDigits) {
2858 for (uint i = digits.length(); i < precision + 1; ++i)
2859 digits.append(locale.zero());
2860 }
2861 else if (pm == PMSignificantDigits) {
2862 for (uint i = digits.length(); i < precision; ++i)
2863 digits.append(locale.zero());
2864 }
2865 else { // pm == PMChopTrailingZeros
2866 }
2867
2868 if (always_show_decpt || digits.length() > 1)
2869 digits.insert(1, locale.decimal());
2870
2871 digits.append(locale.exponential());
2872 digits.append(locale.longLongToString(exp, 2, 10,
2873 -1, QLocalePrivate::AlwaysShowSign));
2874
2875 return digits;
2876}
2877
2878QString QLocalePrivate::doubleToString(double d,
2879 int precision,
2880 DoubleForm form,
2881 int width,
2882 unsigned flags) const
2883{
2884 if (precision == -1)
2885 precision = 6;
2886 if (width == -1)
2887 width = 0;
2888
2889 bool negative = false;
2890 bool special_number = false; // nan, +/-inf
2891 QString num_str;
2892
2893 // Detect special numbers (nan, +/-inf)
2894 if (d == INFINITY || d == -INFINITY) {
2895 num_str = infinity();
2896 special_number = true;
2897 negative = d < 0;
2898 } else if (isnan(d)) {
2899 num_str = nan();
2900 special_number = true;
2901 }
2902
2903 // Handle normal numbers
2904 if (!special_number) {
2905 char *buff = 0;
2906 char *rve = 0;
2907 int decpt, sign;
2908
2909 int mode;
2910 if (form == DFDecimal)
2911 mode = 3;
2912 else
2913 mode = 2;
2914
2915 /* This next bit is a bit quirky. In DFExponent form, the precision
2916 is the number of digits after decpt. So that would suggest using
2917 mode=3 for qdtoa. But qdtoa behaves strangely when mode=3 and
2918 precision=0. So we get around this by using mode=2 and reasoning
2919 that we want precision+1 significant digits, since the decimal
2920 point in this mode is always after the first digit. */
2921 int pr = precision;
2922 if (form == DFExponent)
2923 ++pr;
2924
2925 QString digits = qdtoa(d, mode, pr, &decpt, &sign, &rve, &buff);
2926
2927 if (zero().unicode() != '0') {
2928 for (uint i = 0; i < digits.length(); ++i)
2929 digits.ref(i).unicode() += zero().unicode() - '0';
2930 }
2931
2932 if (buff != 0)
2933 free(buff);
2934
2935 bool always_show_decpt = flags & Alternate;
2936 switch (form) {
2937 case DFExponent: {
2938 num_str = exponentForm(digits, decpt, precision, PMDecimalDigits,
2939 always_show_decpt, *this);
2940 break;
2941 }
2942 case DFDecimal: {
2943 num_str = decimalForm(digits, decpt, precision, PMDecimalDigits,
2944 always_show_decpt, flags & ThousandsGroup,
2945 *this);
2946 break;
2947 }
2948 case DFSignificantDigits: {
2949 PrecisionMode mode = (flags & Alternate) ?
2950 PMSignificantDigits : PMChopTrailingZeros;
2951
2952 if (decpt != (int)digits.length() && (decpt <= -4 || decpt > (int)precision))
2953 num_str = exponentForm(digits, decpt, precision, mode,
2954 always_show_decpt, *this);
2955 else
2956 num_str = decimalForm(digits, decpt, precision, mode,
2957 always_show_decpt, flags & ThousandsGroup,
2958 *this);
2959 break;
2960 }
2961 }
2962
2963 negative = sign != 0;
2964 }
2965
2966 // pad with zeros. LeftAdjusted overrides this flag). Also, we don't
2967 // pad special numbers
2968 if (flags & QLocalePrivate::ZeroPadded
2969 && !(flags & QLocalePrivate::LeftAdjusted)
2970 && !special_number) {
2971 int num_pad_chars = width - (int)num_str.length();
2972 // leave space for the sign
2973 if (negative
2974 || flags & QLocalePrivate::AlwaysShowSign
2975 || flags & QLocalePrivate::BlankBeforePositive)
2976 --num_pad_chars;
2977
2978 for (int i = 0; i < num_pad_chars; ++i)
2979 num_str.prepend(zero());
2980 }
2981
2982 // add sign
2983 if (negative)
2984 num_str.prepend(minus());
2985 else if (flags & QLocalePrivate::AlwaysShowSign)
2986 num_str.prepend(plus());
2987 else if (flags & QLocalePrivate::BlankBeforePositive)
2988 num_str.prepend(' ');
2989
2990 if (flags & QLocalePrivate::CapitalEorX)
2991 num_str = num_str.upper();
2992
2993 return num_str;
2994}
2995
2996QString QLocalePrivate::longLongToString(Q_LLONG l, int precision,
2997 int base, int width,
2998 unsigned flags) const
2999{
3000 bool precision_not_specified = false;
3001 if (precision == -1) {
3002 precision_not_specified = true;
3003 precision = 1;
3004 }
3005
3006 bool negative = l < 0;
3007 if (base != 10) {
3008 // these are not suported by sprintf for octal and hex
3009 flags &= ~AlwaysShowSign;
3010 flags &= ~BlankBeforePositive;
3011 negative = false; // neither are negative numbers
3012 }
3013
3014 QString num_str;
3015 if (base == 10)
3016 num_str = qlltoa(l, base, *this);
3017 else
3018 num_str = qulltoa(l, base, *this);
3019
3020 uint cnt_thousand_sep = 0;
3021 if (flags & ThousandsGroup && base == 10) {
3022 for (int i = (int)num_str.length() - 3; i > 0; i -= 3) {
3023 num_str.insert(i, group());
3024 ++cnt_thousand_sep;
3025 }
3026 }
3027
3028 for (int i = num_str.length()/* - cnt_thousand_sep*/; i < precision; ++i)
3029 num_str.prepend(base == 10 ? zero() : QChar('0'));
3030
3031 if (flags & Alternate
3032 && base == 8
3033 && (num_str.isEmpty()
3034 || num_str[0].unicode() != '0'))
3035 num_str.prepend('0');
3036
3037 // LeftAdjusted overrides this flag ZeroPadded. sprintf only padds
3038 // when precision is not specified in the format string
3039 bool zero_padded = flags & ZeroPadded
3040 && !(flags & LeftAdjusted)
3041 && precision_not_specified;
3042
3043 if (zero_padded) {
3044 int num_pad_chars = width - (int)num_str.length();
3045
3046 // leave space for the sign
3047 if (negative
3048 || flags & AlwaysShowSign
3049 || flags & BlankBeforePositive)
3050 --num_pad_chars;
3051
3052 // leave space for optional '0x' in hex form
3053 if (base == 16
3054 && flags & Alternate
3055 && l != 0)
3056 num_pad_chars -= 2;
3057
3058 for (int i = 0; i < num_pad_chars; ++i)
3059 num_str.prepend(base == 10 ? zero() : QChar('0'));
3060 }
3061
3062 if (base == 16
3063 && flags & Alternate
3064 && l != 0)
3065 num_str.prepend("0x");
3066
3067 // add sign
3068 if (negative)
3069 num_str.prepend(minus());
3070 else if (flags & AlwaysShowSign)
3071 num_str.prepend(base == 10 ? plus() : QChar('+'));
3072 else if (flags & BlankBeforePositive)
3073 num_str.prepend(' ');
3074
3075 if (flags & CapitalEorX)
3076 num_str = num_str.upper();
3077
3078 return num_str;
3079}
3080
3081QString QLocalePrivate::unsLongLongToString(Q_ULLONG l, int precision,
3082 int base, int width,
3083 unsigned flags) const
3084{
3085 bool precision_not_specified = false;
3086 if (precision == -1) {
3087 precision_not_specified = true;
3088 precision = 1;
3089 }
3090
3091 QString num_str = qulltoa(l, base, *this);
3092
3093 uint cnt_thousand_sep = 0;
3094 if (flags & ThousandsGroup && base == 10) {
3095 for (int i = (int)num_str.length() - 3; i > 0; i -=3) {
3096 num_str.insert(i, group());
3097 ++cnt_thousand_sep;
3098 }
3099 }
3100
3101 for (int i = num_str.length()/* - cnt_thousand_sep*/; i < precision; ++i)
3102 num_str.prepend(base == 10 ? zero() : QChar('0'));
3103
3104 if (flags & Alternate
3105 && base == 8
3106 && (num_str.isEmpty()
3107 || num_str[0].unicode() != '0'))
3108 num_str.prepend('0');
3109
3110 // LeftAdjusted overrides this flag ZeroPadded. sprintf only padds
3111 // when precision is not specified in the format string
3112 bool zero_padded = flags & ZeroPadded
3113 && !(flags & LeftAdjusted)
3114 && precision_not_specified;
3115
3116 if (zero_padded) {
3117 int num_pad_chars = width - (int)num_str.length();
3118
3119 // leave space for optional '0x' in hex form
3120 if (base == 16
3121 && flags & Alternate
3122 && l != 0)
3123 num_pad_chars -= 2;
3124
3125 for (int i = 0; i < num_pad_chars; ++i)
3126 num_str.prepend(base == 10 ? zero() : QChar('0'));
3127 }
3128
3129 if (base == 16
3130 && flags & Alternate
3131 && l != 0)
3132 num_str.prepend("0x");
3133
3134 if (flags & CapitalEorX)
3135 num_str = num_str.upper();
3136
3137 return num_str;
3138}
3139
3140static bool compareSubstr(const QString &s1, uint idx, const QString &s2)
3141{
3142 uint i = 0;
3143 for (; i + idx < s1.length() && i < s2.length(); ++i) {
3144 if (s1.unicode()[i + idx] != s2.unicode()[i])
3145 return false;
3146 }
3147
3148 return i == s2.length();
3149}
3150
3151// Converts a number in locale to its representation in the C locale.
3152// If it fails, returns number unconverted.
3153QString &QLocalePrivate::numberToCLocale(QString &l_num) const
3154{
3155 if (languageId() == QLocale::C)
3156 return l_num;
3157
3158 uint idx = 0;
3159
3160 // skip leading white space
3161 while (idx < l_num.length() && l_num.unicode()[idx].isSpace())
3162 ++idx;
3163
3164 do {
3165
3166 if (compareSubstr(l_num, idx, nan())) {
3167 idx += nan().length();
3168 break;
3169 }
3170 else if (compareSubstr(l_num, idx, nan().upper())) {
3171 for (uint i = idx; i < idx + nan().length(); ++i)
3172 l_num.ref(i) = l_num.unicode()[i].lower();
3173 idx += nan().length();
3174 break;
3175 }
3176 QChar &c = l_num.ref(idx);
3177
3178 if (c == plus()) {
3179 c.unicode() = '+';
3180 ++idx;
3181 }
3182 else if (c == minus()) {
3183 c.unicode() = '-';
3184 ++idx;
3185 }
3186
3187 if (compareSubstr(l_num, idx, infinity())) {
3188 idx += infinity().length();
3189 break;
3190 }
3191 else if (compareSubstr(l_num, idx, infinity().upper())) {
3192 for (uint i = idx; i < idx + infinity().length(); ++i)
3193 l_num.ref(i) = l_num.unicode()[i].lower();
3194 idx += infinity().length();
3195 break;
3196 }
3197
3198 while (idx < l_num.length()) {
3199 QChar &c = l_num.ref(idx);
3200
3201 if (isDigit(c))
3202 c = digitToCLocale(zero(), c);
3203 else if (c == plus())
3204 c = '+';
3205 else if (c == minus())
3206 c = '-';
3207 else if (c == decimal())
3208 c = '.';
3209 else if (c == group())
3210 c = ',';
3211 else if (c == exponential() || c == exponential().upper())
3212 c = 'e';
3213 else if (c.unicode() == 'x' || c.unicode() == 'X') // hex number
3214 c = 'x';
3215 else if (c == list())
3216 c = ';';
3217 else if (c == percent())
3218 c = '%';
3219 else
3220 break;
3221
3222 ++idx;
3223 }
3224
3225 } while (false);
3226
3227 return l_num;
3228}
3229
3230double QLocalePrivate::stringToDouble(QString num,
3231 bool *ok) const
3232{
3233// num = num.stripWhiteSpace();
3234 if (num.isEmpty()) {
3235 if (ok != 0)
3236 *ok = false;
3237 return 0.0;
3238 }
3239
3240 if (ok != 0)
3241 *ok = true;
3242
3243 num = numberToCLocale(num);
3244
3245 if (num == "nan")
3246 return NAN;
3247
3248 if (num == "+inf"
3249 || num == "inf")
3250 return INFINITY;
3251
3252 if (num == "-inf")
3253 return -INFINITY;
3254
3255 bool _ok;
3256 const char *endptr;
3257 const char *num_buff = num.latin1();
3258 double d = qstrtod(num_buff, &endptr, &_ok);
3259
3260 if (!_ok || *endptr != '\0') {
3261 if (ok != 0)
3262 *ok = false;
3263 return 0.0;
3264 }
3265 else
3266 return d;
3267}
3268
3269Q_LLONG QLocalePrivate::stringToLongLong(QString num, int base,
3270 bool *ok) const
3271{
3272 num = num.stripWhiteSpace();
3273 if (num.isEmpty()) {
3274 if (ok != 0)
3275 *ok = false;
3276 return 0;
3277 }
3278
3279 num = numberToCLocale(num);
3280
3281 bool _ok;
3282 const char *endptr;
3283 const char *num_buff = num.latin1();
3284 Q_LLONG l = qstrtoll(num_buff, &endptr, base, &_ok);
3285
3286 if (!_ok || *endptr != '\0') {
3287 if (ok != 0)
3288 *ok = false;
3289 return 0;
3290 }
3291
3292 if (ok != 0)
3293 *ok = true;
3294 return l;
3295}
3296
3297Q_ULLONG QLocalePrivate::stringToUnsLongLong(QString num, int base,
3298 bool *ok) const
3299{
3300 num = num.stripWhiteSpace();
3301 if (num.isEmpty()) {
3302 if (ok != 0)
3303 *ok = false;
3304 return 0;
3305 }
3306
3307 num = numberToCLocale(num);
3308
3309 bool _ok;
3310 const char *endptr;
3311 const char *num_buff = num.latin1();
3312 Q_ULLONG l = qstrtoull(num_buff, &endptr, base, &_ok);
3313
3314 if (!_ok || *endptr != '\0') {
3315 if (ok != 0)
3316 *ok = false;
3317 return 0;
3318 }
3319
3320 if (ok != 0)
3321 *ok = true;
3322 return l;
3323}
3324
3325/*-
3326 * Copyright (c) 1992, 1993
3327 * The Regents of the University of California. All rights reserved.
3328 *
3329 * Redistribution and use in source and binary forms, with or without
3330 * modification, are permitted provided that the following conditions
3331 * are met:
3332 * 1. Redistributions of source code must retain the above copyright
3333 * notice, this list of conditions and the following disclaimer.
3334 * 2. Redistributions in binary form must reproduce the above copyright
3335 * notice, this list of conditions and the following disclaimer in the
3336 * documentation and/or other materials provided with the distribution.
3337 * 3. All advertising materials mentioning features or use of this software
3338 * must display the following acknowledgement:
3339 * This product includes software developed by the University of
3340 * California, Berkeley and its contributors.
3341 * 4. Neither the name of the University nor the names of its contributors
3342 * may be used to endorse or promote products derived from this software
3343 * without specific prior written permission.
3344 *
3345 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3346 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3347 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3348 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3349 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3350 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3351 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3352 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3353 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3354 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3355 * SUCH DAMAGE.
3356 */
3357
3358// static char sccsid[] = "@(#)strtouq.c 8.1 (Berkeley) 6/4/93";
3359// "$FreeBSD: src/lib/libc/stdlib/strtoull.c,v 1.5.2.1 2001/03/02 09:45:20 obrien Exp $";
3360
3361/*
3362 * Convert a string to an Q_ULLONG integer.
3363 *
3364 * Ignores `locale' stuff. Assumes that the upper and lower case
3365 * alphabets and digits are each contiguous.
3366 */
3367static Q_ULLONG qstrtoull(const char *nptr, const char **endptr, register int base, bool *ok)
3368{
3369 register const char *s = nptr;
3370 register Q_ULLONG acc;
3371 register unsigned char c;
3372 register Q_ULLONG qbase, cutoff;
3373 register int neg, any, cutlim;
3374
3375 if (ok != 0)
3376 *ok = true;
3377
3378 /*
3379 * See strtoq for comments as to the logic used.
3380 */
3381 s = nptr;
3382 do {
3383 c = *s++;
3384 } while (isspace(c));
3385 if (c == '-') {
3386 if (ok != 0)
3387 *ok = false;
3388 if (endptr != 0)
3389 *endptr = s - 1;
3390 return 0;
3391 } else {
3392 neg = 0;
3393 if (c == '+')
3394 c = *s++;
3395 }
3396 if ((base == 0 || base == 16) &&
3397 c == '0' && (*s == 'x' || *s == 'X')) {
3398 c = s[1];
3399 s += 2;
3400 base = 16;
3401 }
3402 if (base == 0)
3403 base = c == '0' ? 8 : 10;
3404 qbase = (unsigned)base;
3405 cutoff = (Q_ULLONG)ULLONG_MAX / qbase;
3406 cutlim = (Q_ULLONG)ULLONG_MAX % qbase;
3407 for (acc = 0, any = 0;; c = *s++) {
3408 if (!isascii(c))
3409 break;
3410 if (isdigit(c))
3411 c -= '0';
3412 else if (isalpha(c))
3413 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
3414 else
3415 break;
3416 if (c >= base)
3417 break;
3418 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
3419 any = -1;
3420 else {
3421 any = 1;
3422 acc *= qbase;
3423 acc += c;
3424 }
3425 }
3426 if (any < 0) {
3427 acc = ULLONG_MAX;
3428 if (ok != 0)
3429 *ok = false;
3430 }
3431 else if (neg)
3432 acc = (~acc) + 1;
3433 if (endptr != 0)
3434 *endptr = (char *)(any ? s - 1 : nptr);
3435 return (acc);
3436}
3437
3438
3439// "$FreeBSD: src/lib/libc/stdlib/strtoll.c,v 1.5.2.1 2001/03/02 09:45:20 obrien Exp $";
3440
3441
3442/*
3443 * Convert a string to a Q_LLONG integer.
3444 *
3445 * Ignores `locale' stuff. Assumes that the upper and lower case
3446 * alphabets and digits are each contiguous.
3447 */
3448static Q_LLONG qstrtoll(const char *nptr, const char **endptr, register int base, bool *ok)
3449{
3450 register const char *s;
3451 register Q_ULLONG acc;
3452 register unsigned char c;
3453 register Q_ULLONG qbase, cutoff;
3454 register int neg, any, cutlim;
3455
3456 if (ok != 0)
3457 *ok = true;
3458
3459 /*
3460 * Skip white space and pick up leading +/- sign if any.
3461 * If base is 0, allow 0x for hex and 0 for octal, else
3462 * assume decimal; if base is already 16, allow 0x.
3463 */
3464 s = nptr;
3465 do {
3466 c = *s++;
3467 } while (isspace(c));
3468 if (c == '-') {
3469 neg = 1;
3470 c = *s++;
3471 } else {
3472 neg = 0;
3473 if (c == '+')
3474 c = *s++;
3475 }
3476 if ((base == 0 || base == 16) &&
3477 c == '0' && (*s == 'x' || *s == 'X')) {
3478 c = s[1];
3479 s += 2;
3480 base = 16;
3481 }
3482 if (base == 0)
3483 base = c == '0' ? 8 : 10;
3484
3485 /*
3486 * Compute the cutoff value between legal numbers and illegal
3487 * numbers. That is the largest legal value, divided by the
3488 * base. An input number that is greater than this value, if
3489 * followed by a legal input character, is too big. One that
3490 * is equal to this value may be valid or not; the limit
3491 * between valid and invalid numbers is then based on the last
3492 * digit. For instance, if the range for quads is
3493 * [-9223372036854775808..9223372036854775807] and the input base
3494 * is 10, cutoff will be set to 922337203685477580 and cutlim to
3495 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
3496 * accumulated a value > 922337203685477580, or equal but the
3497 * next digit is > 7 (or 8), the number is too big, and we will
3498 * return a range error.
3499 *
3500 * Set any if any `digits' consumed; make it negative to indicate
3501 * overflow.
3502 */
3503 qbase = (unsigned)base;
3504 cutoff = neg ? (Q_ULLONG)-(LLONG_MIN + LLONG_MAX) + LLONG_MAX
3505 : LLONG_MAX;
3506 cutlim = cutoff % qbase;
3507 cutoff /= qbase;
3508 for (acc = 0, any = 0;; c = *s++) {
3509 if (!isascii(c))
3510 break;
3511 if (isdigit(c))
3512 c -= '0';
3513 else if (isalpha(c))
3514 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
3515 else
3516 break;
3517 if (c >= base)
3518 break;
3519 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
3520 any = -1;
3521 else {
3522 any = 1;
3523 acc *= qbase;
3524 acc += c;
3525 }
3526 }
3527 if (any < 0) {
3528 acc = neg ? LLONG_MIN : LLONG_MAX;
3529 if (ok != 0)
3530 *ok = false;
3531 } else if (neg) {
3532 acc = (~acc) + 1;
3533 }
3534 if (endptr != 0)
3535 *endptr = (char *)(any ? s - 1 : nptr);
3536 return (acc);
3537}
3538
3539/* From: NetBSD: strtod.c,v 1.26 1998/02/03 18:44:21 perry Exp */
3540/* $FreeBSD: src/lib/libc/stdlib/netbsd_strtod.c,v 1.2.2.2 2001/03/02 17:14:15 tegge Exp $ */
3541
3542/* Please send bug reports to
3543 David M. Gay
3544 AT&T Bell Laboratories, Room 2C-463
3545 600 Mountain Avenue
3546 Murray Hill, NJ 07974-2070
3547 U.S.A.
3548 dmg@research.att.com or research!dmg
3549 */
3550
3551/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
3552 *
3553 * This strtod returns a nearest machine number to the input decimal
3554 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
3555 * broken by the IEEE round-even rule. Otherwise ties are broken by
3556 * biased rounding (add half and chop).
3557 *
3558 * Inspired loosely by William D. Clinger's paper "How to Read Floating
3559 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
3560 *
3561 * Modifications:
3562 *
3563 * 1. We only require IEEE, IBM, or VAX double-precision
3564 * arithmetic (not IEEE double-extended).
3565 * 2. We get by with floating-point arithmetic in a case that
3566 * Clinger missed -- when we're computing d * 10^n
3567 * for a small integer d and the integer n is not too
3568 * much larger than 22 (the maximum integer k for which
3569 * we can represent 10^k exactly), we may be able to
3570 * compute (d*10^k) * 10^(e-k) with just one roundoff.
3571 * 3. Rather than a bit-at-a-time adjustment of the binary
3572 * result in the hard case, we use floating-point
3573 * arithmetic to determine the adjustment to within
3574 * one bit; only in really hard cases do we need to
3575 * compute a second residual.
3576 * 4. Because of 3., we don't need a large table of powers of 10
3577 * for ten-to-e (just some small tables, e.g. of 10^k
3578 * for 0 <= k <= 22).
3579 */
3580
3581/*
3582 * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
3583 * significant byte has the lowest address.
3584 * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
3585 * significant byte has the lowest address.
3586 * #define Long int on machines with 32-bit ints and 64-bit longs.
3587 * #define Sudden_Underflow for IEEE-format machines without gradual
3588 * underflow (i.e., that flush to zero on underflow).
3589 * #define IBM for IBM mainframe-style floating-point arithmetic.
3590 * #define VAX for VAX-style floating-point arithmetic.
3591 * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
3592 * #define No_leftright to omit left-right logic in fast floating-point
3593 * computation of dtoa.
3594 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
3595 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
3596 * that use extended-precision instructions to compute rounded
3597 * products and quotients) with IBM.
3598 * #define ROUND_BIASED for IEEE-format with biased rounding.
3599 * #define Inaccurate_Divide for IEEE-format with correctly rounded
3600 * products but inaccurate quotients, e.g., for Intel i860.
3601 * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
3602 * integer arithmetic. Whether this speeds things up or slows things
3603 * down depends on the machine and the number being converted.
3604 * #define KR_headers for old-style C function headers.
3605 * #define Bad_float_h if your system lacks a float.h or if it does not
3606 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
3607 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
3608 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
3609 * if memory is available and otherwise does something you deem
3610 * appropriate. If MALLOC is undefined, malloc will be invoked
3611 * directly -- and assumed always to succeed.
3612 */
3613
3614#if defined(LIBC_SCCS) && !defined(lint)
3615__RCSID("$NetBSD: strtod.c,v 1.26 1998/02/03 18:44:21 perry Exp $");
3616#endif /* LIBC_SCCS and not lint */
3617
3618/*
3619#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
3620 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
3621 defined(__powerpc__) || defined(Q_OS_WIN) || defined(Q_OS_DARWIN) || defined(Q_OS_MACX) || \
3622 defined(mips) || defined(Q_OS_AIX) || defined(Q_OS_SOLARIS)
3623# define IEEE_BIG_OR_LITTLE_ENDIAN 1
3624#endif
3625*/
3626
3627// *All* of our architectures have IEEE arithmetic, don't they?
3628#define IEEE_BIG_OR_LITTLE_ENDIAN 1
3629
3630#ifdef __arm32__
3631/*
3632 * Although the CPU is little endian the FP has different
3633 * byte and word endianness. The byte order is still little endian
3634 * but the word order is big endian.
3635 */
3636#define IEEE_BIG_OR_LITTLE_ENDIAN
3637#endif
3638
3639#ifdef vax
3640#define VAX
3641#endif
3642
3643#define Long Q_INT32
3644#define ULong Q_UINT32
3645
3646#define MALLOC malloc
3647#define CONST const
3648
3649#ifdef BSD_QDTOA_DEBUG
3650#include <stdio.h>
3651#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
3652#endif
3653
3654#ifdef Unsigned_Shifts
3655#define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
3656#else
3657#define Sign_Extend(a,b) /*no-op*/
3658#endif
3659
3660#if (defined(IEEE_BIG_OR_LITTLE_ENDIAN) + defined(VAX) + defined(IBM)) != 1
3661#error Exactly one of IEEE_BIG_OR_LITTLE_ENDIAN, VAX, or IBM should be defined.
3662#endif
3663
3664
3665#define word0(x) ((volatile ULong *)&x)[ByteOrder == BigEndian ? 0 : 1]
3666#define word1(x) ((volatile ULong *)&x)[ByteOrder == BigEndian ? 1 : 0]
3667
3668
3669/* The following definition of Storeinc is appropriate for MIPS processors.
3670 * An alternative that might be better on some machines is
3671 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
3672 */
3673
3674/*
3675#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm32__)
3676#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
3677((unsigned short *)a)[0] = (unsigned short)c, a++)
3678#else
3679#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
3680((unsigned short *)a)[1] = (unsigned short)c, a++)
3681#endif
3682*/
3683
3684static inline void Storeinc(ULong *&a, const ULong &b, const ULong &c)
3685{
3686
3687# if defined(VAX) + defined(__arm32__)
3688# define USE_LITTLE_ENDIAN 1
3689# else
3690# define USE_LITTLE_ENDIAN 0
3691# endif
3692
3693# if defined(IEEE_BIG_OR_LITTLE_ENDIAN)
3694# define USE_IEEE 1
3695# else
3696# define USE_IEEE 0
3697# endif
3698
3699 if (ByteOrder == LittleEndian && USE_IEEE || USE_LITTLE_ENDIAN) {
3700 ((unsigned short *)a)[1] = (unsigned short)b;
3701 ((unsigned short *)a)[0] = (unsigned short)c;
3702 } else {
3703 ((unsigned short *)a)[0] = (unsigned short)b;
3704 ((unsigned short *)a)[1] = (unsigned short)c;
3705 }
3706
3707 ++a;
3708
3709# undef USE_LITTLE_ENDIAN
3710# undef USE_IEEE
3711}
3712
3713/* #define P DBL_MANT_DIG */
3714/* Ten_pmax = floor(P*log(2)/log(5)) */
3715/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
3716/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
3717/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
3718
3719#if defined(IEEE_BIG_OR_LITTLE_ENDIAN)
3720#define Exp_shift 20
3721#define Exp_shift1 20
3722#define Exp_msk1 0x100000
3723#define Exp_msk11 0x100000
3724#define Exp_mask 0x7ff00000
3725#define P 53
3726#define Bias 1023
3727#define IEEE_Arith
3728#define Emin (-1022)
3729#define Exp_1 0x3ff00000
3730#define Exp_11 0x3ff00000
3731#define Ebits 11
3732#define Frac_mask 0xfffff
3733#define Frac_mask1 0xfffff
3734#define Ten_pmax 22
3735#define Bletch 0x10
3736#define Bndry_mask 0xfffff
3737#define Bndry_mask1 0xfffff
3738#define LSB 1
3739#define Sign_bit 0x80000000
3740#define Log2P 1
3741#define Tiny0 0
3742#define Tiny1 1
3743#define Quick_max 14
3744#define Int_max 14
3745#define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
3746#else
3747#undef Sudden_Underflow
3748#define Sudden_Underflow
3749#ifdef IBM
3750#define Exp_shift 24
3751#define Exp_shift1 24
3752#define Exp_msk1 0x1000000
3753#define Exp_msk11 0x1000000
3754#define Exp_mask 0x7f000000
3755#define P 14
3756#define Bias 65
3757#define Exp_1 0x41000000
3758#define Exp_11 0x41000000
3759#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
3760#define Frac_mask 0xffffff
3761#define Frac_mask1 0xffffff
3762#define Bletch 4
3763#define Ten_pmax 22
3764#define Bndry_mask 0xefffff
3765#define Bndry_mask1 0xffffff
3766#define LSB 1
3767#define Sign_bit 0x80000000
3768#define Log2P 4
3769#define Tiny0 0x100000
3770#define Tiny1 0
3771#define Quick_max 14
3772#define Int_max 15
3773#else /* VAX */
3774#define Exp_shift 23
3775#define Exp_shift1 7
3776#define Exp_msk1 0x80
3777#define Exp_msk11 0x800000
3778#define Exp_mask 0x7f80
3779#define P 56
3780#define Bias 129
3781#define Exp_1 0x40800000
3782#define Exp_11 0x4080
3783#define Ebits 8
3784#define Frac_mask 0x7fffff
3785#define Frac_mask1 0xffff007f
3786#define Ten_pmax 24
3787#define Bletch 2
3788#define Bndry_mask 0xffff007f
3789#define Bndry_mask1 0xffff007f
3790#define LSB 0x10000
3791#define Sign_bit 0x8000
3792#define Log2P 1
3793#define Tiny0 0x80
3794#define Tiny1 0
3795#define Quick_max 15
3796#define Int_max 15
3797#endif
3798#endif
3799
3800#ifndef IEEE_Arith
3801#define ROUND_BIASED
3802#endif
3803
3804#ifdef RND_PRODQUOT
3805#define rounded_product(a,b) a = rnd_prod(a, b)
3806#define rounded_quotient(a,b) a = rnd_quot(a, b)
3807#ifdef KR_headers
3808extern double rnd_prod(), rnd_quot();
3809#else
3810extern double rnd_prod(double, double), rnd_quot(double, double);
3811#endif
3812#else
3813#define rounded_product(a,b) a *= b
3814#define rounded_quotient(a,b) a /= b
3815#endif
3816
3817#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
3818#define Big1 0xffffffff
3819
3820#ifndef Just_16
3821/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
3822 * This makes some inner loops simpler and sometimes saves work
3823 * during multiplications, but it often seems to make things slightly
3824 * slower. Hence the default is now to store 32 bits per Long.
3825 */
3826#ifndef Pack_32
3827#define Pack_32
3828#endif
3829#endif
3830
3831#define Kmax 15
3832
3833struct
3834Bigint {
3835 struct Bigint *next;
3836 int k, maxwds, sign, wds;
3837 ULong x[1];
3838};
3839
3840 typedef struct Bigint Bigint;
3841
3842static Bigint *Balloc(int k)
3843{
3844 int x;
3845 Bigint *rv;
3846
3847 x = 1 << k;
3848 rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
3849 rv->k = k;
3850 rv->maxwds = x;
3851 rv->sign = rv->wds = 0;
3852 return rv;
3853}
3854
3855static void Bfree(Bigint *v)
3856{
3857 free(v);
3858}
3859
3860#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
3861y->wds*sizeof(Long) + 2*sizeof(int))
3862
3863/* multiply by m and add a */
3864static Bigint *multadd(Bigint *b, int m, int a)
3865{
3866 int i, wds;
3867 ULong *x, y;
3868#ifdef Pack_32
3869 ULong xi, z;
3870#endif
3871 Bigint *b1;
3872
3873 wds = b->wds;
3874 x = b->x;
3875 i = 0;
3876 do {
3877#ifdef Pack_32
3878 xi = *x;
3879 y = (xi & 0xffff) * m + a;
3880 z = (xi >> 16) * m + (y >> 16);
3881 a = (int)(z >> 16);
3882 *x++ = (z << 16) + (y & 0xffff);
3883#else
3884 y = *x * m + a;
3885 a = (int)(y >> 16);
3886 *x++ = y & 0xffff;
3887#endif
3888 }
3889 while(++i < wds);
3890 if (a) {
3891 if (wds >= b->maxwds) {
3892 b1 = Balloc(b->k+1);
3893 Bcopy(b1, b);
3894 Bfree(b);
3895 b = b1;
3896 }
3897 b->x[wds++] = a;
3898 b->wds = wds;
3899 }
3900 return b;
3901}
3902
3903static Bigint *s2b(CONST char *s, int nd0, int nd, ULong y9)
3904{
3905 Bigint *b;
3906 int i, k;
3907 Long x, y;
3908
3909 x = (nd + 8) / 9;
3910 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
3911#ifdef Pack_32
3912 b = Balloc(k);
3913 b->x[0] = y9;
3914 b->wds = 1;
3915#else
3916 b = Balloc(k+1);
3917 b->x[0] = y9 & 0xffff;
3918 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
3919#endif
3920
3921 i = 9;
3922 if (9 < nd0) {
3923 s += 9;
3924 do b = multadd(b, 10, *s++ - '0');
3925 while(++i < nd0);
3926 s++;
3927 }
3928 else
3929 s += 10;
3930 for(; i < nd; i++)
3931 b = multadd(b, 10, *s++ - '0');
3932 return b;
3933}
3934
3935static int hi0bits(ULong x)
3936{
3937 int k = 0;
3938
3939 if (!(x & 0xffff0000)) {
3940 k = 16;
3941 x <<= 16;
3942 }
3943 if (!(x & 0xff000000)) {
3944 k += 8;
3945 x <<= 8;
3946 }
3947 if (!(x & 0xf0000000)) {
3948 k += 4;
3949 x <<= 4;
3950 }
3951 if (!(x & 0xc0000000)) {
3952 k += 2;
3953 x <<= 2;
3954 }
3955 if (!(x & 0x80000000)) {
3956 k++;
3957 if (!(x & 0x40000000))
3958 return 32;
3959 }
3960 return k;
3961}
3962
3963static int lo0bits(ULong *y)
3964{
3965 int k;
3966 ULong x = *y;
3967
3968 if (x & 7) {
3969 if (x & 1)
3970 return 0;
3971 if (x & 2) {
3972 *y = x >> 1;
3973 return 1;
3974 }
3975 *y = x >> 2;
3976 return 2;
3977 }
3978 k = 0;
3979 if (!(x & 0xffff)) {
3980 k = 16;
3981 x >>= 16;
3982 }
3983 if (!(x & 0xff)) {
3984 k += 8;
3985 x >>= 8;
3986 }
3987 if (!(x & 0xf)) {
3988 k += 4;
3989 x >>= 4;
3990 }
3991 if (!(x & 0x3)) {
3992 k += 2;
3993 x >>= 2;
3994 }
3995 if (!(x & 1)) {
3996 k++;
3997 x >>= 1;
3998 if (!x & 1)
3999 return 32;
4000 }
4001 *y = x;
4002 return k;
4003}
4004
4005static Bigint *i2b(int i)
4006{
4007 Bigint *b;
4008
4009 b = Balloc(1);
4010 b->x[0] = i;
4011 b->wds = 1;
4012 return b;
4013}
4014
4015static Bigint *mult(Bigint *a, Bigint *b)
4016{
4017 Bigint *c;
4018 int k, wa, wb, wc;
4019 ULong carry, y, z;
4020 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
4021#ifdef Pack_32
4022 ULong z2;
4023#endif
4024
4025 if (a->wds < b->wds) {
4026 c = a;
4027 a = b;
4028 b = c;
4029 }
4030 k = a->k;
4031 wa = a->wds;
4032 wb = b->wds;
4033 wc = wa + wb;
4034 if (wc > a->maxwds)
4035 k++;
4036 c = Balloc(k);
4037 for(x = c->x, xa = x + wc; x < xa; x++)
4038 *x = 0;
4039 xa = a->x;
4040 xae = xa + wa;
4041 xb = b->x;
4042 xbe = xb + wb;
4043 xc0 = c->x;
4044#ifdef Pack_32
4045 for(; xb < xbe; xb++, xc0++) {
4046 if ((y = *xb & 0xffff) != 0) {
4047 x = xa;
4048 xc = xc0;
4049 carry = 0;
4050 do {
4051 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
4052 carry = z >> 16;
4053 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
4054 carry = z2 >> 16;
4055 Storeinc(xc, z2, z);
4056 }
4057 while(x < xae);
4058 *xc = carry;
4059 }
4060 if ((y = *xb >> 16) != 0) {
4061 x = xa;
4062 xc = xc0;
4063 carry = 0;
4064 z2 = *xc;
4065 do {
4066 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
4067 carry = z >> 16;
4068 Storeinc(xc, z, z2);
4069 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
4070 carry = z2 >> 16;
4071 }
4072 while(x < xae);
4073 *xc = z2;
4074 }
4075 }
4076#else
4077 for(; xb < xbe; xc0++) {
4078 if (y = *xb++) {
4079 x = xa;
4080 xc = xc0;
4081 carry = 0;
4082 do {
4083 z = *x++ * y + *xc + carry;
4084 carry = z >> 16;
4085 *xc++ = z & 0xffff;
4086 }
4087 while(x < xae);
4088 *xc = carry;
4089 }
4090 }
4091#endif
4092 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
4093 c->wds = wc;
4094 return c;
4095}
4096
4097static Bigint *p5s;
4098
4099static Bigint *pow5mult(Bigint *b, int k)
4100{
4101 Bigint *b1, *p5, *p51;
4102 int i;
4103 static const int p05[3] = { 5, 25, 125 };
4104
4105 if ((i = k & 3) != 0)
4106 b = multadd(b, p05[i-1], 0);
4107
4108 if (!(k >>= 2))
4109 return b;
4110 if (!(p5 = p5s)) {
4111 /* first time */
4112 p5 = p5s = i2b(625);
4113 p5->next = 0;
4114 }
4115 for(;;) {
4116 if (k & 1) {
4117 b1 = mult(b, p5);
4118 Bfree(b);
4119 b = b1;
4120 }
4121 if (!(k >>= 1))
4122 break;
4123 if (!(p51 = p5->next)) {
4124 p51 = p5->next = mult(p5,p5);
4125 p51->next = 0;
4126 }
4127 p5 = p51;
4128 }
4129 return b;
4130}
4131
4132static Bigint *lshift(Bigint *b, int k)
4133{
4134 int i, k1, n, n1;
4135 Bigint *b1;
4136 ULong *x, *x1, *xe, z;
4137
4138#ifdef Pack_32
4139 n = k >> 5;
4140#else
4141 n = k >> 4;
4142#endif
4143 k1 = b->k;
4144 n1 = n + b->wds + 1;
4145 for(i = b->maxwds; n1 > i; i <<= 1)
4146 k1++;
4147 b1 = Balloc(k1);
4148 x1 = b1->x;
4149 for(i = 0; i < n; i++)
4150 *x1++ = 0;
4151 x = b->x;
4152 xe = x + b->wds;
4153#ifdef Pack_32
4154 if (k &= 0x1f) {
4155 k1 = 32 - k;
4156 z = 0;
4157 do {
4158 *x1++ = *x << k | z;
4159 z = *x++ >> k1;
4160 }
4161 while(x < xe);
4162 if ((*x1 = z) != 0)
4163 ++n1;
4164 }
4165#else
4166 if (k &= 0xf) {
4167 k1 = 16 - k;
4168 z = 0;
4169 do {
4170 *x1++ = *x << k & 0xffff | z;
4171 z = *x++ >> k1;
4172 }
4173 while(x < xe);
4174 if (*x1 = z)
4175 ++n1;
4176 }
4177#endif
4178 else do
4179 *x1++ = *x++;
4180 while(x < xe);
4181 b1->wds = n1 - 1;
4182 Bfree(b);
4183 return b1;
4184}
4185
4186static int cmp(Bigint *a, Bigint *b)
4187{
4188 ULong *xa, *xa0, *xb, *xb0;
4189 int i, j;
4190
4191 i = a->wds;
4192 j = b->wds;
4193#ifdef BSD_QDTOA_DEBUG
4194 if (i > 1 && !a->x[i-1])
4195 Bug("cmp called with a->x[a->wds-1] == 0");
4196 if (j > 1 && !b->x[j-1])
4197 Bug("cmp called with b->x[b->wds-1] == 0");
4198#endif
4199 if (i -= j)
4200 return i;
4201 xa0 = a->x;
4202 xa = xa0 + j;
4203 xb0 = b->x;
4204 xb = xb0 + j;
4205 for(;;) {
4206 if (*--xa != *--xb)
4207 return *xa < *xb ? -1 : 1;
4208 if (xa <= xa0)
4209 break;
4210 }
4211 return 0;
4212}
4213
4214static Bigint *diff(Bigint *a, Bigint *b)
4215{
4216 Bigint *c;
4217 int i, wa, wb;
4218 Long borrow, y; /* We need signed shifts here. */
4219 ULong *xa, *xae, *xb, *xbe, *xc;
4220#ifdef Pack_32
4221 Long z;
4222#endif
4223
4224 i = cmp(a,b);
4225 if (!i) {
4226 c = Balloc(0);
4227 c->wds = 1;
4228 c->x[0] = 0;
4229 return c;
4230 }
4231 if (i < 0) {
4232 c = a;
4233 a = b;
4234 b = c;
4235 i = 1;
4236 }
4237 else
4238 i = 0;
4239 c = Balloc(a->k);
4240 c->sign = i;
4241 wa = a->wds;
4242 xa = a->x;
4243 xae = xa + wa;
4244 wb = b->wds;
4245 xb = b->x;
4246 xbe = xb + wb;
4247 xc = c->x;
4248 borrow = 0;
4249#ifdef Pack_32
4250 do {
4251 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
4252 borrow = y >> 16;
4253 Sign_Extend(borrow, y);
4254 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
4255 borrow = z >> 16;
4256 Sign_Extend(borrow, z);
4257 Storeinc(xc, z, y);
4258 }
4259 while(xb < xbe);
4260 while(xa < xae) {
4261 y = (*xa & 0xffff) + borrow;
4262 borrow = y >> 16;
4263 Sign_Extend(borrow, y);
4264 z = (*xa++ >> 16) + borrow;
4265 borrow = z >> 16;
4266 Sign_Extend(borrow, z);
4267 Storeinc(xc, z, y);
4268 }
4269#else
4270 do {
4271 y = *xa++ - *xb++ + borrow;
4272 borrow = y >> 16;
4273 Sign_Extend(borrow, y);
4274 *xc++ = y & 0xffff;
4275 }
4276 while(xb < xbe);
4277 while(xa < xae) {
4278 y = *xa++ + borrow;
4279 borrow = y >> 16;
4280 Sign_Extend(borrow, y);
4281 *xc++ = y & 0xffff;
4282 }
4283#endif
4284 while(!*--xc)
4285 wa--;
4286 c->wds = wa;
4287 return c;
4288}
4289
4290static double ulp(volatile double x)
4291{
4292 Long L;
4293 double a;
4294
4295 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
4296#ifndef Sudden_Underflow
4297 if (L > 0) {
4298#endif
4299#ifdef IBM
4300 L |= Exp_msk1 >> 4;
4301#endif
4302 word0(a) = L;
4303 word1(a) = 0;
4304#ifndef Sudden_Underflow
4305 }
4306 else {
4307 L = -L >> Exp_shift;
4308 if (L < Exp_shift) {
4309 word0(a) = 0x80000 >> L;
4310 word1(a) = 0;
4311 }
4312 else {
4313 word0(a) = 0;
4314 L -= Exp_shift;
4315 word1(a) = (L >= 31 ? 1U : 1U << (31 - L));
4316 }
4317 }
4318#endif
4319 return a;
4320}
4321
4322static double b2d(Bigint *a, int *e)
4323{
4324 ULong *xa, *xa0, w, y, z;
4325 int k;
4326 double d;
4327#ifdef VAX
4328 ULong d0, d1;
4329#else
4330#define d0 word0(d)
4331#define d1 word1(d)
4332#endif
4333
4334 xa0 = a->x;
4335 xa = xa0 + a->wds;
4336 y = *--xa;
4337#ifdef BSD_QDTOA_DEBUG
4338 if (!y) Bug("zero y in b2d");
4339#endif
4340 k = hi0bits(y);
4341 *e = 32 - k;
4342#ifdef Pack_32
4343 if (k < Ebits) {
4344 d0 = Exp_1 | y >> (Ebits - k);
4345 w = xa > xa0 ? *--xa : 0;
4346 d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
4347 goto ret_d;
4348 }
4349 z = xa > xa0 ? *--xa : 0;
4350 if (k -= Ebits) {
4351 d0 = Exp_1 | y << k | z >> (32 - k);
4352 y = xa > xa0 ? *--xa : 0;
4353 d1 = z << k | y >> (32 - k);
4354 }
4355 else {
4356 d0 = Exp_1 | y;
4357 d1 = z;
4358 }
4359#else
4360 if (k < Ebits + 16) {
4361 z = xa > xa0 ? *--xa : 0;
4362 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
4363 w = xa > xa0 ? *--xa : 0;
4364 y = xa > xa0 ? *--xa : 0;
4365 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
4366 goto ret_d;
4367 }
4368 z = xa > xa0 ? *--xa : 0;
4369 w = xa > xa0 ? *--xa : 0;
4370 k -= Ebits + 16;
4371 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
4372 y = xa > xa0 ? *--xa : 0;
4373 d1 = w << k + 16 | y << k;
4374#endif
4375 ret_d:
4376#ifdef VAX
4377 word0(d) = d0 >> 16 | d0 << 16;
4378 word1(d) = d1 >> 16 | d1 << 16;
4379#else
4380#undef d0
4381#undef d1
4382#endif
4383 return d;
4384}
4385
4386static Bigint *d2b(double d, int *e, int *bits)
4387{
4388 Bigint *b;
4389 int de, i, k;
4390 ULong *x, y, z;
4391#ifdef VAX
4392 ULong d0, d1;
4393 d0 = word0(d) >> 16 | word0(d) << 16;
4394 d1 = word1(d) >> 16 | word1(d) << 16;
4395#else
4396#define d0 word0(d)
4397#define d1 word1(d)
4398#endif
4399
4400#ifdef Pack_32
4401 b = Balloc(1);
4402#else
4403 b = Balloc(2);
4404#endif
4405 x = b->x;
4406
4407 z = d0 & Frac_mask;
4408 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
4409#ifdef Sudden_Underflow
4410 de = (int)(d0 >> Exp_shift);
4411#ifndef IBM
4412 z |= Exp_msk11;
4413#endif
4414#else
4415 if ((de = (int)(d0 >> Exp_shift)) != 0)
4416 z |= Exp_msk1;
4417#endif
4418#ifdef Pack_32
4419 if ((y = d1) != 0) {
4420 if ((k = lo0bits(&y)) != 0) {
4421 x[0] = y | z << (32 - k);
4422 z >>= k;
4423 }
4424 else
4425 x[0] = y;
4426 i = b->wds = (x[1] = z) ? 2 : 1;
4427 }
4428 else {
4429#ifdef BSD_QDTOA_DEBUG
4430 if (!z)
4431 Bug("Zero passed to d2b");
4432#endif
4433 k = lo0bits(&z);
4434 x[0] = z;
4435 i = b->wds = 1;
4436 k += 32;
4437 }
4438#else
4439 if (y = d1) {
4440 if (k = lo0bits(&y))
4441 if (k >= 16) {
4442 x[0] = y | z << 32 - k & 0xffff;
4443 x[1] = z >> k - 16 & 0xffff;
4444 x[2] = z >> k;
4445 i = 2;
4446 }
4447 else {
4448 x[0] = y & 0xffff;
4449 x[1] = y >> 16 | z << 16 - k & 0xffff;
4450 x[2] = z >> k & 0xffff;
4451 x[3] = z >> k+16;
4452 i = 3;
4453 }
4454 else {
4455 x[0] = y & 0xffff;
4456 x[1] = y >> 16;
4457 x[2] = z & 0xffff;
4458 x[3] = z >> 16;
4459 i = 3;
4460 }
4461 }
4462 else {
4463#ifdef BSD_QDTOA_DEBUG
4464 if (!z)
4465 Bug("Zero passed to d2b");
4466#endif
4467 k = lo0bits(&z);
4468 if (k >= 16) {
4469 x[0] = z;
4470 i = 0;
4471 }
4472 else {
4473 x[0] = z & 0xffff;
4474 x[1] = z >> 16;
4475 i = 1;
4476 }
4477 k += 32;
4478 }
4479 while(!x[i])
4480 --i;
4481 b->wds = i + 1;
4482#endif
4483#ifndef Sudden_Underflow
4484 if (de) {
4485#endif
4486#ifdef IBM
4487 *e = (de - Bias - (P-1) << 2) + k;
4488 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
4489#else
4490 *e = de - Bias - (P-1) + k;
4491 *bits = P - k;
4492#endif
4493#ifndef Sudden_Underflow
4494 }
4495 else {
4496 *e = de - Bias - (P-1) + 1 + k;
4497#ifdef Pack_32
4498 *bits = 32*i - hi0bits(x[i-1]);
4499#else
4500 *bits = (i+2)*16 - hi0bits(x[i]);
4501#endif
4502 }
4503#endif
4504 return b;
4505}
4506#undef d0
4507#undef d1
4508
4509static double ratio(Bigint *a, Bigint *b)
4510{
4511 double da, db;
4512 int k, ka, kb;
4513
4514 da = b2d(a, &ka);
4515 db = b2d(b, &kb);
4516#ifdef Pack_32
4517 k = ka - kb + 32*(a->wds - b->wds);
4518#else
4519 k = ka - kb + 16*(a->wds - b->wds);
4520#endif
4521#ifdef IBM
4522 if (k > 0) {
4523 word0(da) += (k >> 2)*Exp_msk1;
4524 if (k &= 3)
4525 da *= 1 << k;
4526 }
4527 else {
4528 k = -k;
4529 word0(db) += (k >> 2)*Exp_msk1;
4530 if (k &= 3)
4531 db *= 1 << k;
4532 }
4533#else
4534 if (k > 0)
4535 word0(da) += k*Exp_msk1;
4536 else {
4537 k = -k;
4538 word0(db) += k*Exp_msk1;
4539 }
4540#endif
4541 return da / db;
4542}
4543
4544static CONST double tens[] = {
4545 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
4546 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
4547 1e20, 1e21, 1e22
4548#ifdef VAX
4549 , 1e23, 1e24
4550#endif
4551};
4552
4553#ifdef IEEE_Arith
4554static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
4555static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
4556#define n_bigtens 5
4557#else
4558#ifdef IBM
4559static CONST double bigtens[] = { 1e16, 1e32, 1e64 };
4560static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
4561#define n_bigtens 3
4562#else
4563static CONST double bigtens[] = { 1e16, 1e32 };
4564static CONST double tinytens[] = { 1e-16, 1e-32 };
4565#define n_bigtens 2
4566#endif
4567#endif
4568
4569/*
4570 The pre-release gcc3.3 shipped with SuSE 8.2 has a bug which causes
4571 the comparison 1e-100 == 0.0 to return true. As a workaround, we
4572 compare it to a global variable containing 0.0, which produces
4573 correct assembler output.
4574
4575 ### consider detecting the broken compilers and using the static
4576 ### double for these, and use a #define for all working compilers
4577*/
4578static double g_double_zero = 0.0;
4579
4580static double qstrtod(CONST char *s00, CONST char **se, bool *ok)
4581{
4582 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
4583 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
4584 CONST char *s, *s0, *s1;
4585 double aadj, aadj1, adj, rv, rv0;
4586 Long L;
4587 ULong y, z;
4588 Bigint *bb1, *bd0;
4589 Bigint *bb = NULL, *bd = NULL, *bs = NULL, *delta = NULL;/* pacify gcc */
4590
4591 /*
4592 #ifndef KR_headers
4593 CONST char decimal_point = localeconv()->decimal_point[0];
4594 #else
4595 CONST char decimal_point = '.';
4596 #endif */
4597 if (ok != 0)
4598 *ok = true;
4599
4600 CONST char decimal_point = '.';
4601
4602 sign = nz0 = nz = 0;
4603 rv = 0.;
4604
4605
4606 for(s = s00; isspace((unsigned char) *s); s++)
4607 ;
4608
4609 if (*s == '-') {
4610 sign = 1;
4611 s++;
4612 } else if (*s == '+') {
4613 s++;
4614 }
4615
4616 if (*s == '\0') {
4617 s = s00;
4618 goto ret;
4619 }
4620
4621 if (*s == '0') {
4622 nz0 = 1;
4623 while(*++s == '0') ;
4624 if (!*s)
4625 goto ret;
4626 }
4627 s0 = s;
4628 y = z = 0;
4629 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
4630 if (nd < 9)
4631 y = 10*y + c - '0';
4632 else if (nd < 16)
4633 z = 10*z + c - '0';
4634 nd0 = nd;
4635 if (c == decimal_point) {
4636 c = *++s;
4637 if (!nd) {
4638 for(; c == '0'; c = *++s)
4639 nz++;
4640 if (c > '0' && c <= '9') {
4641 s0 = s;
4642 nf += nz;
4643 nz = 0;
4644 goto have_dig;
4645 }
4646 goto dig_done;
4647 }
4648 for(; c >= '0' && c <= '9'; c = *++s) {
4649 have_dig:
4650 nz++;
4651 if (c -= '0') {
4652 nf += nz;
4653 for(i = 1; i < nz; i++)
4654 if (nd++ < 9)
4655 y *= 10;
4656 else if (nd <= DBL_DIG + 1)
4657 z *= 10;
4658 if (nd++ < 9)
4659 y = 10*y + c;
4660 else if (nd <= DBL_DIG + 1)
4661 z = 10*z + c;
4662 nz = 0;
4663 }
4664 }
4665 }
4666 dig_done:
4667 e = 0;
4668 if (c == 'e' || c == 'E') {
4669 if (!nd && !nz && !nz0) {
4670 s = s00;
4671 goto ret;
4672 }
4673 s00 = s;
4674 esign = 0;
4675 switch(c = *++s) {
4676 case '-':
4677 esign = 1;
4678 case '+':
4679 c = *++s;
4680 }
4681 if (c >= '0' && c <= '9') {
4682 while(c == '0')
4683 c = *++s;
4684 if (c > '0' && c <= '9') {
4685 L = c - '0';
4686 s1 = s;
4687 while((c = *++s) >= '0' && c <= '9')
4688 L = 10*L + c - '0';
4689 if (s - s1 > 8 || L > 19999)
4690 /* Avoid confusion from exponents
4691 * so large that e might overflow.
4692 */
4693 e = 19999; /* safe for 16 bit ints */
4694 else
4695 e = (int)L;
4696 if (esign)
4697 e = -e;
4698 }
4699 else
4700 e = 0;
4701 }
4702 else
4703 s = s00;
4704 }
4705 if (!nd) {
4706 if (!nz && !nz0)
4707 s = s00;
4708 goto ret;
4709 }
4710 e1 = e -= nf;
4711
4712 /* Now we have nd0 digits, starting at s0, followed by a
4713 * decimal point, followed by nd-nd0 digits. The number we're
4714 * after is the integer represented by those digits times
4715 * 10**e */
4716
4717 if (!nd0)
4718 nd0 = nd;
4719 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
4720 rv = y;
4721 if (k > 9)
4722 rv = tens[k - 9] * rv + z;
4723 bd0 = 0;
4724 if (nd <= DBL_DIG
4725#ifndef RND_PRODQUOT
4726 && FLT_ROUNDS == 1
4727#endif
4728 ) {
4729 if (!e)
4730 goto ret;
4731 if (e > 0) {
4732 if (e <= Ten_pmax) {
4733#ifdef VAX
4734 goto vax_ovfl_check;
4735#else
4736 /* rv = */ rounded_product(rv, tens[e]);
4737 goto ret;
4738#endif
4739 }
4740 i = DBL_DIG - nd;
4741 if (e <= Ten_pmax + i) {
4742 /* A fancier test would sometimes let us do
4743 * this for larger i values.
4744 */
4745 e -= i;
4746 rv *= tens[i];
4747#ifdef VAX
4748 /* VAX exponent range is so narrow we must
4749 * worry about overflow here...
4750 */
4751 vax_ovfl_check:
4752 word0(rv) -= P*Exp_msk1;
4753 /* rv = */ rounded_product(rv, tens[e]);
4754 if ((word0(rv) & Exp_mask)
4755 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
4756 goto ovfl;
4757 word0(rv) += P*Exp_msk1;
4758#else
4759 /* rv = */ rounded_product(rv, tens[e]);
4760#endif
4761 goto ret;
4762 }
4763 }
4764#ifndef Inaccurate_Divide
4765 else if (e >= -Ten_pmax) {
4766 /* rv = */ rounded_quotient(rv, tens[-e]);
4767 goto ret;
4768 }
4769#endif
4770 }
4771 e1 += nd - k;
4772
4773 /* Get starting approximation = rv * 10**e1 */
4774
4775 if (e1 > 0) {
4776 if ((i = e1 & 15) != 0)
4777 rv *= tens[i];
4778 if (e1 &= ~15) {
4779 if (e1 > DBL_MAX_10_EXP) {
4780 ovfl:
4781 // errno = ERANGE;
4782 if (ok != 0)
4783 *ok = false;
4784#ifdef __STDC__
4785 rv = HUGE_VAL;
4786#else
4787 /* Can't trust HUGE_VAL */
4788#ifdef IEEE_Arith
4789 word0(rv) = Exp_mask;
4790 word1(rv) = 0;
4791#else
4792 word0(rv) = Big0;
4793 word1(rv) = Big1;
4794#endif
4795#endif
4796 if (bd0)
4797 goto retfree;
4798 goto ret;
4799 }
4800 if (e1 >>= 4) {
4801 for(j = 0; e1 > 1; j++, e1 >>= 1)
4802 if (e1 & 1)
4803 rv *= bigtens[j];
4804 /* The last multiplication could overflow. */
4805 word0(rv) -= P*Exp_msk1;
4806 rv *= bigtens[j];
4807 if ((z = word0(rv) & Exp_mask)
4808 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
4809 goto ovfl;
4810 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
4811 /* set to largest number */
4812 /* (Can't trust DBL_MAX) */
4813 word0(rv) = Big0;
4814 word1(rv) = Big1;
4815 }
4816 else
4817 word0(rv) += P*Exp_msk1;
4818 }
4819
4820 }
4821 }
4822 else if (e1 < 0) {
4823 e1 = -e1;
4824 if ((i = e1 & 15) != 0)
4825 rv /= tens[i];
4826 if (e1 &= ~15) {
4827 e1 >>= 4;
4828 if (e1 >= 1 << n_bigtens)
4829 goto undfl;
4830 for(j = 0; e1 > 1; j++, e1 >>= 1)
4831 if (e1 & 1)
4832 rv *= tinytens[j];
4833 /* The last multiplication could underflow. */
4834 rv0 = rv;
4835 rv *= tinytens[j];
4836 if (rv == g_double_zero)
4837 {
4838 rv = 2.*rv0;
4839 rv *= tinytens[j];
4840 if (rv == g_double_zero)
4841 {
4842 undfl:
4843 rv = 0.;
4844 // errno = ERANGE;
4845 if (ok != 0)
4846 *ok = false;
4847 if (bd0)
4848 goto retfree;
4849 goto ret;
4850 }
4851 word0(rv) = Tiny0;
4852 word1(rv) = Tiny1;
4853 /* The refinement below will clean
4854 * this approximation up.
4855 */
4856 }
4857 }
4858 }
4859
4860 /* Now the hard part -- adjusting rv to the correct value.*/
4861
4862 /* Put digits into bd: true value = bd * 10^e */
4863
4864 bd0 = s2b(s0, nd0, nd, y);
4865
4866 for(;;) {
4867 bd = Balloc(bd0->k);
4868 Bcopy(bd, bd0);
4869 bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
4870 bs = i2b(1);
4871
4872 if (e >= 0) {
4873 bb2 = bb5 = 0;
4874 bd2 = bd5 = e;
4875 }
4876 else {
4877 bb2 = bb5 = -e;
4878 bd2 = bd5 = 0;
4879 }
4880 if (bbe >= 0)
4881 bb2 += bbe;
4882 else
4883 bd2 -= bbe;
4884 bs2 = bb2;
4885#ifdef Sudden_Underflow
4886#ifdef IBM
4887 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
4888#else
4889 j = P + 1 - bbbits;
4890#endif
4891#else
4892 i = bbe + bbbits - 1; /* logb(rv) */
4893 if (i < Emin) /* denormal */
4894 j = bbe + (P-Emin);
4895 else
4896 j = P + 1 - bbbits;
4897#endif
4898 bb2 += j;
4899 bd2 += j;
4900 i = bb2 < bd2 ? bb2 : bd2;
4901 if (i > bs2)
4902 i = bs2;
4903 if (i > 0) {
4904 bb2 -= i;
4905 bd2 -= i;
4906 bs2 -= i;
4907 }
4908 if (bb5 > 0) {
4909 bs = pow5mult(bs, bb5);
4910 bb1 = mult(bs, bb);
4911 Bfree(bb);
4912 bb = bb1;
4913 }
4914 if (bb2 > 0)
4915 bb = lshift(bb, bb2);
4916 if (bd5 > 0)
4917 bd = pow5mult(bd, bd5);
4918 if (bd2 > 0)
4919 bd = lshift(bd, bd2);
4920 if (bs2 > 0)
4921 bs = lshift(bs, bs2);
4922 delta = diff(bb, bd);
4923 dsign = delta->sign;
4924 delta->sign = 0;
4925 i = cmp(delta, bs);
4926 if (i < 0) {
4927 /* Error is less than half an ulp -- check for
4928 * special case of mantissa a power of two.
4929 */
4930 if (dsign || word1(rv) || word0(rv) & Bndry_mask)
4931 break;
4932 delta = lshift(delta,Log2P);
4933 if (cmp(delta, bs) > 0)
4934 goto drop_down;
4935 break;
4936 }
4937 if (i == 0) {
4938 /* exactly half-way between */
4939 if (dsign) {
4940 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
4941 && word1(rv) == 0xffffffff) {
4942 /*boundary case -- increment exponent*/
4943 word0(rv) = (word0(rv) & Exp_mask)
4944 + Exp_msk1
4945#ifdef IBM
4946 | Exp_msk1 >> 4
4947#endif
4948 ;
4949 word1(rv) = 0;
4950 break;
4951 }
4952 }
4953 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
4954 drop_down:
4955 /* boundary case -- decrement exponent */
4956#ifdef Sudden_Underflow
4957 L = word0(rv) & Exp_mask;
4958#ifdef IBM
4959 if (L < Exp_msk1)
4960#else
4961 if (L <= Exp_msk1)
4962#endif
4963 goto undfl;
4964 L -= Exp_msk1;
4965#else
4966 L = (word0(rv) & Exp_mask) - Exp_msk1;
4967#endif
4968 word0(rv) = L | Bndry_mask1;
4969 word1(rv) = 0xffffffff;
4970#ifdef IBM
4971 goto cont;
4972#else
4973 break;
4974#endif
4975 }
4976#ifndef ROUND_BIASED
4977 if (!(word1(rv) & LSB))
4978 break;
4979#endif
4980 if (dsign)
4981 rv += ulp(rv);
4982#ifndef ROUND_BIASED
4983 else {
4984 rv -= ulp(rv);
4985#ifndef Sudden_Underflow
4986 if (rv == g_double_zero)
4987 goto undfl;
4988#endif
4989 }
4990#endif
4991 break;
4992 }
4993 if ((aadj = ratio(delta, bs)) <= 2.) {
4994 if (dsign)
4995 aadj = aadj1 = 1.;
4996 else if (word1(rv) || word0(rv) & Bndry_mask) {
4997#ifndef Sudden_Underflow
4998 if (word1(rv) == Tiny1 && !word0(rv))
4999 goto undfl;
5000#endif
5001 aadj = 1.;
5002 aadj1 = -1.;
5003 }
5004 else {
5005 /* special case -- power of FLT_RADIX to be */
5006 /* rounded down... */
5007
5008 if (aadj < 2./FLT_RADIX)
5009 aadj = 1./FLT_RADIX;
5010 else
5011 aadj *= 0.5;
5012 aadj1 = -aadj;
5013 }
5014 }
5015 else {
5016 aadj *= 0.5;
5017 aadj1 = dsign ? aadj : -aadj;
5018#ifdef Check_FLT_ROUNDS
5019 switch(FLT_ROUNDS) {
5020 case 2: /* towards +infinity */
5021 aadj1 -= 0.5;
5022 break;
5023 case 0: /* towards 0 */
5024 case 3: /* towards -infinity */
5025 aadj1 += 0.5;
5026 }
5027#else
5028 if (FLT_ROUNDS == 0)
5029 aadj1 += 0.5;
5030#endif
5031 }
5032 y = word0(rv) & Exp_mask;
5033
5034 /* Check for overflow */
5035
5036 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
5037 rv0 = rv;
5038 word0(rv) -= P*Exp_msk1;
5039 adj = aadj1 * ulp(rv);
5040 rv += adj;
5041 if ((word0(rv) & Exp_mask) >=
5042 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
5043 if (word0(rv0) == Big0 && word1(rv0) == Big1)
5044 goto ovfl;
5045 word0(rv) = Big0;
5046 word1(rv) = Big1;
5047 goto cont;
5048 }
5049 else
5050 word0(rv) += P*Exp_msk1;
5051 }
5052 else {
5053#ifdef Sudden_Underflow
5054 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
5055 rv0 = rv;
5056 word0(rv) += P*Exp_msk1;
5057 adj = aadj1 * ulp(rv);
5058 rv += adj;
5059#ifdef IBM
5060 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
5061#else
5062 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
5063#endif
5064 {
5065 if (word0(rv0) == Tiny0
5066 && word1(rv0) == Tiny1)
5067 goto undfl;
5068 word0(rv) = Tiny0;
5069 word1(rv) = Tiny1;
5070 goto cont;
5071 }
5072 else
5073 word0(rv) -= P*Exp_msk1;
5074 }
5075 else {
5076 adj = aadj1 * ulp(rv);
5077 rv += adj;
5078 }
5079#else
5080 /* Compute adj so that the IEEE rounding rules will
5081 * correctly round rv + adj in some half-way cases.
5082 * If rv * ulp(rv) is denormalized (i.e.,
5083 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
5084 * trouble from bits lost to denormalization;
5085 * example: 1.2e-307 .
5086 */
5087 if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
5088 aadj1 = (double)(int)(aadj + 0.5);
5089 if (!dsign)
5090 aadj1 = -aadj1;
5091 }
5092 adj = aadj1 * ulp(rv);
5093 rv += adj;
5094#endif
5095 }
5096 z = word0(rv) & Exp_mask;
5097 if (y == z) {
5098 /* Can we stop now? */
5099 L = (Long) aadj;
5100 aadj -= L;
5101 /* The tolerances below are conservative. */
5102 if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
5103 if (aadj < .4999999 || aadj > .5000001)
5104 break;
5105 }
5106 else if (aadj < .4999999/FLT_RADIX)
5107 break;
5108 }
5109 cont:
5110 Bfree(bb);
5111 Bfree(bd);
5112 Bfree(bs);
5113 Bfree(delta);
5114 }
5115 retfree:
5116 Bfree(bb);
5117 Bfree(bd);
5118 Bfree(bs);
5119 Bfree(bd0);
5120 Bfree(delta);
5121 ret:
5122 if (se)
5123 *se = (char *)s;
5124 return sign ? -rv : rv;
5125}
5126
5127static int quorem(Bigint *b, Bigint *S)
5128{
5129 int n;
5130 Long borrow, y;
5131 ULong carry, q, ys;
5132 ULong *bx, *bxe, *sx, *sxe;
5133#ifdef Pack_32
5134 Long z;
5135 ULong si, zs;
5136#endif
5137
5138 n = S->wds;
5139#ifdef BSD_QDTOA_DEBUG
5140 /*debug*/ if (b->wds > n)
5141 /*debug*/ Bug("oversize b in quorem");
5142#endif
5143 if (b->wds < n)
5144 return 0;
5145 sx = S->x;
5146 sxe = sx + --n;
5147 bx = b->x;
5148 bxe = bx + n;
5149 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
5150#ifdef BSD_QDTOA_DEBUG
5151 /*debug*/ if (q > 9)
5152 /*debug*/ Bug("oversized quotient in quorem");
5153#endif
5154 if (q) {
5155 borrow = 0;
5156 carry = 0;
5157 do {
5158#ifdef Pack_32
5159 si = *sx++;
5160 ys = (si & 0xffff) * q + carry;
5161 zs = (si >> 16) * q + (ys >> 16);
5162 carry = zs >> 16;
5163 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
5164 borrow = y >> 16;
5165 Sign_Extend(borrow, y);
5166 z = (*bx >> 16) - (zs & 0xffff) + borrow;
5167 borrow = z >> 16;
5168 Sign_Extend(borrow, z);
5169 Storeinc(bx, z, y);
5170#else
5171 ys = *sx++ * q + carry;
5172 carry = ys >> 16;
5173 y = *bx - (ys & 0xffff) + borrow;
5174 borrow = y >> 16;
5175 Sign_Extend(borrow, y);
5176 *bx++ = y & 0xffff;
5177#endif
5178 }
5179 while(sx <= sxe);
5180 if (!*bxe) {
5181 bx = b->x;
5182 while(--bxe > bx && !*bxe)
5183 --n;
5184 b->wds = n;
5185 }
5186 }
5187 if (cmp(b, S) >= 0) {
5188 q++;
5189 borrow = 0;
5190 carry = 0;
5191 bx = b->x;
5192 sx = S->x;
5193 do {
5194#ifdef Pack_32
5195 si = *sx++;
5196 ys = (si & 0xffff) + carry;
5197 zs = (si >> 16) + (ys >> 16);
5198 carry = zs >> 16;
5199 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
5200 borrow = y >> 16;
5201 Sign_Extend(borrow, y);
5202 z = (*bx >> 16) - (zs & 0xffff) + borrow;
5203 borrow = z >> 16;
5204 Sign_Extend(borrow, z);
5205 Storeinc(bx, z, y);
5206#else
5207 ys = *sx++ + carry;
5208 carry = ys >> 16;
5209 y = *bx - (ys & 0xffff) + borrow;
5210 borrow = y >> 16;
5211 Sign_Extend(borrow, y);
5212 *bx++ = y & 0xffff;
5213#endif
5214 }
5215 while(sx <= sxe);
5216 bx = b->x;
5217 bxe = bx + n;
5218 if (!*bxe) {
5219 while(--bxe > bx && !*bxe)
5220 --n;
5221 b->wds = n;
5222 }
5223 }
5224 return q;
5225}
5226
5227/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
5228 *
5229 * Inspired by "How to Print Floating-Point Numbers Accurately" by
5230 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
5231 *
5232 * Modifications:
5233 * 1. Rather than iterating, we use a simple numeric overestimate
5234 * to determine k = floor(log10(d)). We scale relevant
5235 * quantities using O(log2(k)) rather than O(k) multiplications.
5236 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
5237 * try to generate digits strictly left to right. Instead, we
5238 * compute with fewer bits and propagate the carry if necessary
5239 * when rounding the final digit up. This is often faster.
5240 * 3. Under the assumption that input will be rounded nearest,
5241 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
5242 * That is, we allow equality in stopping tests when the
5243 * round-nearest rule will give the same floating-point value
5244 * as would satisfaction of the stopping test with strict
5245 * inequality.
5246 * 4. We remove common factors of powers of 2 from relevant
5247 * quantities.
5248 * 5. When converting floating-point integers less than 1e16,
5249 * we use floating-point arithmetic rather than resorting
5250 * to multiple-precision integers.
5251 * 6. When asked to produce fewer than 15 digits, we first try
5252 * to get by with floating-point arithmetic; we resort to
5253 * multiple-precision integer arithmetic only if we cannot
5254 * guarantee that the floating-point calculation has given
5255 * the correctly rounded result. For k requested digits and
5256 * "uniformly" distributed input, the probability is
5257 * something like 10^(k-15) that we must resort to the Long
5258 * calculation.
5259 */
5260
5261
5262/* This actually sometimes returns a pointer to a string literal
5263 cast to a char*. Do NOT try to modify the return value. */
5264
5265static char *qdtoa (volatile double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **resultp)
5266{
5267 /*
5268 Arguments ndigits, decpt, sign are similar to those
5269 of ecvt and fcvt; trailing zeros are suppressed from
5270 the returned string. If not null, *rve is set to point
5271 to the end of the return value. If d is +-Infinity or NaN,
5272 then *decpt is set to 9999.
5273
5274 mode:
5275 0 ==> shortest string that yields d when read in
5276 and rounded to nearest.
5277 1 ==> like 0, but with Steele & White stopping rule;
5278 e.g. with IEEE P754 arithmetic , mode 0 gives
5279 1e23 whereas mode 1 gives 9.999999999999999e22.
5280 2 ==> max(1,ndigits) significant digits. This gives a
5281 return value similar to that of ecvt, except
5282 that trailing zeros are suppressed.
5283 3 ==> through ndigits past the decimal point. This
5284 gives a return value similar to that from fcvt,
5285 except that trailing zeros are suppressed, and
5286 ndigits can be negative.
5287 4-9 should give the same return values as 2-3, i.e.,
5288 4 <= mode <= 9 ==> same return as mode
5289 2 + (mode & 1). These modes are mainly for
5290 debugging; often they run slower but sometimes
5291 faster than modes 2-3.
5292 4,5,8,9 ==> left-to-right digit generation.
5293 6-9 ==> don't try fast floating-point estimate
5294 (if applicable).
5295
5296 Values of mode other than 0-9 are treated as mode 0.
5297
5298 Sufficient space is allocated to the return value
5299 to hold the suppressed trailing zeros.
5300 */
5301
5302 int bbits, b2, b5, be, dig, i, ieps, ilim0,
5303 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
5304 try_quick;
5305 int ilim = 0, ilim1 = 0, spec_case = 0; /* pacify gcc */
5306 Long L;
5307#ifndef Sudden_Underflow
5308 int denorm;
5309 ULong x;
5310#endif
5311 Bigint *b, *b1, *delta, *mhi, *S;
5312 Bigint *mlo = NULL; /* pacify gcc */
5313 volatile double d2;
5314 double ds, eps;
5315 char *s, *s0;
5316
5317 if (word0(d) & Sign_bit) {
5318 /* set sign for everything, including 0's and NaNs */
5319 *sign = 1;
5320 word0(d) &= ~Sign_bit; /* clear sign bit */
5321 }
5322 else
5323 *sign = 0;
5324
5325#if defined(IEEE_Arith) + defined(VAX)
5326#ifdef IEEE_Arith
5327 if ((word0(d) & Exp_mask) == Exp_mask)
5328#else
5329 if (word0(d) == 0x8000)
5330#endif
5331 {
5332 /* Infinity or NaN */
5333 *decpt = 9999;
5334 s =
5335#ifdef IEEE_Arith
5336 !word1(d) && !(word0(d) & 0xfffff) ? (char*)"Infinity" :
5337#endif
5338 (char*)"NaN";
5339 if (rve)
5340 *rve =
5341#ifdef IEEE_Arith
5342 s[3] ? s + 8 :
5343#endif
5344 s + 3;
5345 return s;
5346 }
5347#endif
5348#ifdef IBM
5349 d += 0; /* normalize */
5350#endif
5351 if (d == g_double_zero)
5352 {
5353 *decpt = 1;
5354 s = (char*) "0";
5355 if (rve)
5356 *rve = s + 1;
5357 return s;
5358 }
5359
5360 b = d2b(d, &be, &bbits);
5361#ifdef Sudden_Underflow
5362 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
5363#else
5364 if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) != 0) {
5365#endif
5366 d2 = d;
5367 word0(d2) &= Frac_mask1;
5368 word0(d2) |= Exp_11;
5369#ifdef IBM
5370 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
5371 d2 /= 1 << j;
5372#endif
5373
5374 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
5375 * log10(x) = log(x) / log(10)
5376 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
5377 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
5378 *
5379 * This suggests computing an approximation k to log10(d) by
5380 *
5381 * k = (i - Bias)*0.301029995663981
5382 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
5383 *
5384 * We want k to be too large rather than too small.
5385 * The error in the first-order Taylor series approximation
5386 * is in our favor, so we just round up the constant enough
5387 * to compensate for any error in the multiplication of
5388 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
5389 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
5390 * adding 1e-13 to the constant term more than suffices.
5391 * Hence we adjust the constant term to 0.1760912590558.
5392 * (We could get a more accurate k by invoking log10,
5393 * but this is probably not worthwhile.)
5394 */
5395
5396 i -= Bias;
5397#ifdef IBM
5398 i <<= 2;
5399 i += j;
5400#endif
5401#ifndef Sudden_Underflow
5402 denorm = 0;
5403 }
5404 else {
5405 /* d is denormalized */
5406
5407 i = bbits + be + (Bias + (P-1) - 1);
5408 x = i > 32 ? word0(d) << (64 - i) | word1(d) >> (i - 32)
5409 : word1(d) << (32 - i);
5410 d2 = x;
5411 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
5412 i -= (Bias + (P-1) - 1) + 1;
5413 denorm = 1;
5414 }
5415#endif
5416 ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
5417 k = (int)ds;
5418 if (ds < 0. && ds != k)
5419 k--; /* want k = floor(ds) */
5420 k_check = 1;
5421 if (k >= 0 && k <= Ten_pmax) {
5422 if (d < tens[k])
5423 k--;
5424 k_check = 0;
5425 }
5426 j = bbits - i - 1;
5427 if (j >= 0) {
5428 b2 = 0;
5429 s2 = j;
5430 }
5431 else {
5432 b2 = -j;
5433 s2 = 0;
5434 }
5435 if (k >= 0) {
5436 b5 = 0;
5437 s5 = k;
5438 s2 += k;
5439 }
5440 else {
5441 b2 -= k;
5442 b5 = -k;
5443 s5 = 0;
5444 }
5445 if (mode < 0 || mode > 9)
5446 mode = 0;
5447 try_quick = 1;
5448 if (mode > 5) {
5449 mode -= 4;
5450 try_quick = 0;
5451 }
5452 leftright = 1;
5453 switch(mode) {
5454 case 0:
5455 case 1:
5456 ilim = ilim1 = -1;
5457 i = 18;
5458 ndigits = 0;
5459 break;
5460 case 2:
5461 leftright = 0;
5462 /* no break */
5463 case 4:
5464 if (ndigits <= 0)
5465 ndigits = 1;
5466 ilim = ilim1 = i = ndigits;
5467 break;
5468 case 3:
5469 leftright = 0;
5470 /* no break */
5471 case 5:
5472 i = ndigits + k + 1;
5473 ilim = i;
5474 ilim1 = i - 1;
5475 if (i <= 0)
5476 i = 1;
5477 }
5478 *resultp = (char *) malloc(i + 1);
5479 s = s0 = *resultp;
5480
5481 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
5482
5483 /* Try to get by with floating-point arithmetic. */
5484
5485 i = 0;
5486 d2 = d;
5487 k0 = k;
5488 ilim0 = ilim;
5489 ieps = 2; /* conservative */
5490 if (k > 0) {
5491 ds = tens[k&0xf];
5492 j = k >> 4;
5493 if (j & Bletch) {
5494 /* prevent overflows */
5495 j &= Bletch - 1;
5496 d /= bigtens[n_bigtens-1];
5497 ieps++;
5498 }
5499 for(; j; j >>= 1, i++)
5500 if (j & 1) {
5501 ieps++;
5502 ds *= bigtens[i];
5503 }
5504 d /= ds;
5505 }
5506 else if ((j1 = -k) != 0) {
5507 d *= tens[j1 & 0xf];
5508 for(j = j1 >> 4; j; j >>= 1, i++)
5509 if (j & 1) {
5510 ieps++;
5511 d *= bigtens[i];
5512 }
5513 }
5514 if (k_check && d < 1. && ilim > 0) {
5515 if (ilim1 <= 0)
5516 goto fast_failed;
5517 ilim = ilim1;
5518 k--;
5519 d *= 10.;
5520 ieps++;
5521 }
5522 eps = ieps*d + 7.;
5523 word0(eps) -= (P-1)*Exp_msk1;
5524 if (ilim == 0) {
5525 S = mhi = 0;
5526 d -= 5.;
5527 if (d > eps)
5528 goto one_digit;
5529 if (d < -eps)
5530 goto no_digits;
5531 goto fast_failed;
5532 }
5533#ifndef No_leftright
5534 if (leftright) {
5535 /* Use Steele & White method of only
5536 * generating digits needed.
5537 */
5538 eps = 0.5/tens[ilim-1] - eps;
5539 for(i = 0;;) {
5540 L = (Long)d;
5541 d -= L;
5542 *s++ = '0' + (int)L;
5543 if (d < eps)
5544 goto ret1;
5545 if (1. - d < eps)
5546 goto bump_up;
5547 if (++i >= ilim)
5548 break;
5549 eps *= 10.;
5550 d *= 10.;
5551 }
5552 }
5553 else {
5554#endif
5555 /* Generate ilim digits, then fix them up. */
5556 eps *= tens[ilim-1];
5557 for(i = 1;; i++, d *= 10.) {
5558 L = (Long)d;
5559 d -= L;
5560 *s++ = '0' + (int)L;
5561 if (i == ilim) {
5562 if (d > 0.5 + eps)
5563 goto bump_up;
5564 else if (d < 0.5 - eps) {
5565 while(*--s == '0');
5566 s++;
5567 goto ret1;
5568 }
5569 break;
5570 }
5571 }
5572#ifndef No_leftright
5573 }
5574#endif
5575 fast_failed:
5576 s = s0;
5577 d = d2;
5578 k = k0;
5579 ilim = ilim0;
5580 }
5581
5582 /* Do we have a "small" integer? */
5583
5584 if (be >= 0 && k <= Int_max) {
5585 /* Yes. */
5586 ds = tens[k];
5587 if (ndigits < 0 && ilim <= 0) {
5588 S = mhi = 0;
5589 if (ilim < 0 || d <= 5*ds)
5590 goto no_digits;
5591 goto one_digit;
5592 }
5593 for(i = 1;; i++) {
5594 L = (Long)(d / ds);
5595 d -= L*ds;
5596#ifdef Check_FLT_ROUNDS
5597 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
5598 if (d < 0) {
5599 L--;
5600 d += ds;
5601 }
5602#endif
5603 *s++ = '0' + (int)L;
5604 if (i == ilim) {
5605 d += d;
5606 if (d > ds || (d == ds && L & 1)) {
5607 bump_up:
5608 while(*--s == '9')
5609 if (s == s0) {
5610 k++;
5611 *s = '0';
5612 break;
5613 }
5614 ++*s++;
5615 }
5616 break;
5617 }
5618 if ((d *= 10.) == g_double_zero)
5619 break;
5620 }
5621 goto ret1;
5622 }
5623
5624 m2 = b2;
5625 m5 = b5;
5626 mhi = mlo = 0;
5627 if (leftright) {
5628 if (mode < 2) {
5629 i =
5630#ifndef Sudden_Underflow
5631 denorm ? be + (Bias + (P-1) - 1 + 1) :
5632#endif
5633#ifdef IBM
5634 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
5635#else
5636 1 + P - bbits;
5637#endif
5638 }
5639 else {
5640 j = ilim - 1;
5641 if (m5 >= j)
5642 m5 -= j;
5643 else {
5644 s5 += j -= m5;
5645 b5 += j;
5646 m5 = 0;
5647 }
5648 if ((i = ilim) < 0) {
5649 m2 -= i;
5650 i = 0;
5651 }
5652 }
5653 b2 += i;
5654 s2 += i;
5655 mhi = i2b(1);
5656 }
5657 if (m2 > 0 && s2 > 0) {
5658 i = m2 < s2 ? m2 : s2;
5659 b2 -= i;
5660 m2 -= i;
5661 s2 -= i;
5662 }
5663 if (b5 > 0) {
5664 if (leftright) {
5665 if (m5 > 0) {
5666 mhi = pow5mult(mhi, m5);
5667 b1 = mult(mhi, b);
5668 Bfree(b);
5669 b = b1;
5670 }
5671 if ((j = b5 - m5) != 0)
5672 b = pow5mult(b, j);
5673 }
5674 else
5675 b = pow5mult(b, b5);
5676 }
5677 S = i2b(1);
5678 if (s5 > 0)
5679 S = pow5mult(S, s5);
5680
5681 /* Check for special case that d is a normalized power of 2. */
5682
5683 if (mode < 2) {
5684 if (!word1(d) && !(word0(d) & Bndry_mask)
5685#ifndef Sudden_Underflow
5686 && word0(d) & Exp_mask
5687#endif
5688 ) {
5689 /* The special case */
5690 b2 += Log2P;
5691 s2 += Log2P;
5692 spec_case = 1;
5693 }
5694 else
5695 spec_case = 0;
5696 }
5697
5698 /* Arrange for convenient computation of quotients:
5699 * shift left if necessary so divisor has 4 leading 0 bits.
5700 *
5701 * Perhaps we should just compute leading 28 bits of S once
5702 * and for all and pass them and a shift to quorem, so it
5703 * can do shifts and ors to compute the numerator for q.
5704 */
5705#ifdef Pack_32
5706 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0)
5707 i = 32 - i;
5708#else
5709 if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
5710 i = 16 - i;
5711#endif
5712 if (i > 4) {
5713 i -= 4;
5714 b2 += i;
5715 m2 += i;
5716 s2 += i;
5717 }
5718 else if (i < 4) {
5719 i += 28;
5720 b2 += i;
5721 m2 += i;
5722 s2 += i;
5723 }
5724 if (b2 > 0)
5725 b = lshift(b, b2);
5726 if (s2 > 0)
5727 S = lshift(S, s2);
5728 if (k_check) {
5729 if (cmp(b,S) < 0) {
5730 k--;
5731 b = multadd(b, 10, 0); /* we botched the k estimate */
5732 if (leftright)
5733 mhi = multadd(mhi, 10, 0);
5734 ilim = ilim1;
5735 }
5736 }
5737 if (ilim <= 0 && mode > 2) {
5738 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
5739 /* no digits, fcvt style */
5740 no_digits:
5741 k = -1 - ndigits;
5742 goto ret;
5743 }
5744 one_digit:
5745 *s++ = '1';
5746 k++;
5747 goto ret;
5748 }
5749 if (leftright) {
5750 if (m2 > 0)
5751 mhi = lshift(mhi, m2);
5752
5753 /* Compute mlo -- check for special case
5754 * that d is a normalized power of 2.
5755 */
5756
5757 mlo = mhi;
5758 if (spec_case) {
5759 mhi = Balloc(mhi->k);
5760 Bcopy(mhi, mlo);
5761 mhi = lshift(mhi, Log2P);
5762 }
5763
5764 for(i = 1;;i++) {
5765 dig = quorem(b,S) + '0';
5766 /* Do we yet have the shortest decimal string
5767 * that will round to d?
5768 */
5769 j = cmp(b, mlo);
5770 delta = diff(S, mhi);
5771 j1 = delta->sign ? 1 : cmp(b, delta);
5772 Bfree(delta);
5773#ifndef ROUND_BIASED
5774 if (j1 == 0 && !mode && !(word1(d) & 1)) {
5775 if (dig == '9')
5776 goto round_9_up;
5777 if (j > 0)
5778 dig++;
5779 *s++ = dig;
5780 goto ret;
5781 }
5782#endif
5783 if (j < 0 || (j == 0 && !mode
5784#ifndef ROUND_BIASED
5785 && !(word1(d) & 1)
5786#endif
5787 )) {
5788 if (j1 > 0) {
5789 b = lshift(b, 1);
5790 j1 = cmp(b, S);
5791 if ((j1 > 0 || (j1 == 0 && dig & 1))
5792 && dig++ == '9')
5793 goto round_9_up;
5794 }
5795 *s++ = dig;
5796 goto ret;
5797 }
5798 if (j1 > 0) {
5799 if (dig == '9') { /* possible if i == 1 */
5800 round_9_up:
5801 *s++ = '9';
5802 goto roundoff;
5803 }
5804 *s++ = dig + 1;
5805 goto ret;
5806 }
5807 *s++ = dig;
5808 if (i == ilim)
5809 break;
5810 b = multadd(b, 10, 0);
5811 if (mlo == mhi)
5812 mlo = mhi = multadd(mhi, 10, 0);
5813 else {
5814 mlo = multadd(mlo, 10, 0);
5815 mhi = multadd(mhi, 10, 0);
5816 }
5817 }
5818 }
5819 else
5820 for(i = 1;; i++) {
5821 *s++ = dig = quorem(b,S) + '0';
5822 if (i >= ilim)
5823 break;
5824 b = multadd(b, 10, 0);
5825 }
5826
5827 /* Round off last digit */
5828
5829 b = lshift(b, 1);
5830 j = cmp(b, S);
5831 if (j > 0 || (j == 0 && dig & 1)) {
5832 roundoff:
5833 while(*--s == '9')
5834 if (s == s0) {
5835 k++;
5836 *s++ = '1';
5837 goto ret;
5838 }
5839 ++*s++;
5840 }
5841 else {
5842 while(*--s == '0');
5843 s++;
5844 }
5845 ret:
5846 Bfree(S);
5847 if (mhi) {
5848 if (mlo && mlo != mhi)
5849 Bfree(mlo);
5850 Bfree(mhi);
5851 }
5852 ret1:
5853 Bfree(b);
5854 if (s == s0) { /* don't return empty string */
5855 *s++ = '0';
5856 k = 0;
5857 }
5858 *s = 0;
5859 *decpt = k + 1;
5860 if (rve)
5861 *rve = s;
5862 return s0;
5863}
Note: See TracBrowser for help on using the repository browser.