source: trunk/src/tools/qlocale.cpp

Last change on this file was 117, checked in by dmik, 19 years ago

Tools: Fixed double number handling routines: applying word0 and word1 macros to cached double variables could screw up the GCC optimizer (resulting into SIGFPEs in the release build); |volatile| is now used to prevent caching.

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