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