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