1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qplatformdefs.h"
31
#include "qdatastream.h"
34
#include "qlocale_p.h"
35
#include "qnamespace.h"
44
# include "qt_windows.h"
47
#if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
51
#if !defined(QWS) && defined(Q_OS_MAC)
52
# include <private/qcore_mac_p.h>
55
#include <private/qnumeric_p.h>
57
#if !defined(QT_QLOCALE_NEEDS_VOLATILE)
58
# if defined(Q_CC_GNU)
59
# if __GNUC__ == 4 && __GNUC_MINOR__ == 0
60
# define QT_QLOCALE_NEEDS_VOLATILE
61
# elif defined(Q_OS_WIN)
62
# define QT_QLOCALE_NEEDS_VOLATILE
67
#if defined(QT_QLOCALE_NEEDS_VOLATILE)
68
# define NEEDS_VOLATILE volatile
70
# define NEEDS_VOLATILE
73
// Sizes as defined by the ISO C99 standard - fallback
75
# define LLONG_MAX Q_INT64_C(0x7fffffffffffffff)
78
# define LLONG_MIN (-LLONG_MAX - Q_INT64_C(1))
81
# define ULLONG_MAX Q_UINT64_C(0xffffffffffffffff)
84
#define CONVERSION_BUFF_SIZE 255
86
#ifndef QT_QLOCALE_USES_FCVT
87
static char *qdtoa(double d, int mode, int ndigits, int *decpt,
88
int *sign, char **rve, char **digits_str);
89
static char *_qdtoa( NEEDS_VOLATILE double d, int mode, int ndigits, int *decpt,
90
int *sign, char **rve, char **digits_str);
91
static double qstrtod(const char *s00, char const **se, bool *ok);
93
static qlonglong qstrtoll(const char *nptr, const char **endptr, register int base, bool *ok);
94
static qulonglong qstrtoull(const char *nptr, const char **endptr, register int base, bool *ok);
96
static const uint locale_index[] = {
242
static const QLocalePrivate locale_data[] = {
243
// lang terr dec group list prcnt zero minus exp
244
{ 1, 0, 46, 44, 59, 37, 48, 45, 101 }, // C/AnyCountry
245
{ 5, 195, 46, 44, 44, 37, 48, 45, 101 }, // Afrikaans/SouthAfrica
246
{ 6, 2, 44, 46, 59, 37, 48, 45, 101 }, // Albanian/Albania
247
{ 8, 186, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/SaudiArabia
248
{ 8, 3, 46, 44, 59, 37, 48, 45, 101 }, // Arabic/Algeria
249
{ 8, 17, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Bahrain
250
{ 8, 64, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Egypt
251
{ 8, 103, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Iraq
252
{ 8, 109, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Jordan
253
{ 8, 115, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Kuwait
254
{ 8, 119, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Lebanon
255
{ 8, 122, 46, 44, 59, 37, 48, 45, 101 }, // Arabic/LibyanArabJamahiriya
256
{ 8, 145, 46, 44, 59, 37, 48, 45, 101 }, // Arabic/Morocco
257
{ 8, 162, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Oman
258
{ 8, 175, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Qatar
259
{ 8, 207, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/SyrianArabRepublic
260
{ 8, 216, 46, 44, 59, 37, 48, 45, 101 }, // Arabic/Tunisia
261
{ 8, 223, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/UnitedArabEmirates
262
{ 8, 237, 46, 44, 59, 37, 1632, 45, 101 }, // Arabic/Yemen
263
{ 9, 11, 46, 44, 44, 37, 48, 45, 101 }, // Armenian/Armenia
264
{ 12, 15, 44, 160, 59, 37, 48, 45, 101 }, // Azerbaijani/Azerbaijan
265
{ 14, 197, 44, 46, 59, 37, 48, 45, 101 }, // Basque/Spain
266
{ 20, 33, 44, 160, 59, 37, 48, 45, 101 }, // Bulgarian/Bulgaria
267
{ 22, 20, 44, 160, 59, 37, 48, 45, 101 }, // Byelorussian/Belarus
268
{ 24, 197, 44, 46, 59, 37, 48, 45, 101 }, // Catalan/Spain
269
{ 25, 44, 46, 44, 44, 37, 48, 45, 101 }, // Chinese/China
270
{ 25, 97, 46, 44, 44, 37, 48, 45, 101 }, // Chinese/HongKong
271
{ 25, 126, 46, 44, 44, 37, 48, 45, 101 }, // Chinese/Macau
272
{ 25, 190, 46, 44, 44, 37, 48, 45, 101 }, // Chinese/Singapore
273
{ 25, 208, 46, 44, 44, 37, 48, 45, 101 }, // Chinese/Taiwan
274
{ 27, 54, 44, 46, 59, 37, 48, 45, 101 }, // Croatian/Croatia
275
{ 28, 57, 44, 160, 59, 37, 48, 45, 101 }, // Czech/CzechRepublic
276
{ 29, 58, 44, 46, 59, 37, 48, 45, 101 }, // Danish/Denmark
277
{ 30, 151, 44, 46, 59, 37, 48, 45, 101 }, // Dutch/Netherlands
278
{ 30, 21, 44, 46, 59, 37, 48, 45, 101 }, // Dutch/Belgium
279
{ 31, 225, 46, 44, 44, 37, 48, 45, 101 }, // English/UnitedStates
280
{ 31, 13, 46, 44, 44, 37, 48, 45, 101 }, // English/Australia
281
{ 31, 22, 46, 44, 59, 37, 48, 45, 101 }, // English/Belize
282
{ 31, 38, 46, 44, 44, 37, 48, 45, 101 }, // English/Canada
283
{ 31, 104, 46, 44, 44, 37, 48, 45, 101 }, // English/Ireland
284
{ 31, 107, 46, 44, 44, 37, 48, 45, 101 }, // English/Jamaica
285
{ 31, 154, 46, 44, 44, 37, 48, 45, 101 }, // English/NewZealand
286
{ 31, 170, 46, 44, 44, 37, 48, 45, 101 }, // English/Philippines
287
{ 31, 195, 46, 44, 44, 37, 48, 45, 101 }, // English/SouthAfrica
288
{ 31, 215, 46, 44, 59, 37, 48, 45, 101 }, // English/TrinidadAndTobago
289
{ 31, 224, 46, 44, 44, 37, 48, 45, 101 }, // English/UnitedKingdom
290
{ 31, 240, 46, 44, 44, 37, 48, 45, 101 }, // English/Zimbabwe
291
{ 33, 68, 44, 160, 59, 37, 48, 45, 101 }, // Estonian/Estonia
292
{ 34, 71, 44, 46, 59, 37, 48, 45, 101 }, // Faroese/FaroeIslands
293
{ 36, 73, 44, 160, 59, 37, 48, 45, 101 }, // Finnish/Finland
294
{ 37, 74, 44, 160, 59, 37, 48, 45, 101 }, // French/France
295
{ 37, 21, 44, 46, 59, 37, 48, 45, 101 }, // French/Belgium
296
{ 37, 38, 44, 160, 59, 37, 48, 45, 101 }, // French/Canada
297
{ 37, 125, 44, 160, 59, 37, 48, 45, 101 }, // French/Luxembourg
298
{ 37, 142, 44, 160, 59, 37, 48, 45, 101 }, // French/Monaco
299
{ 37, 206, 46, 39, 59, 37, 48, 45, 101 }, // French/Switzerland
300
{ 40, 197, 44, 46, 44, 37, 48, 45, 101 }, // Galician/Spain
301
{ 41, 81, 44, 160, 59, 37, 48, 45, 101 }, // Georgian/Georgia
302
{ 42, 82, 44, 46, 59, 37, 48, 45, 101 }, // German/Germany
303
{ 42, 14, 44, 46, 59, 37, 48, 45, 101 }, // German/Austria
304
{ 42, 123, 46, 39, 59, 37, 48, 45, 101 }, // German/Liechtenstein
305
{ 42, 125, 44, 46, 59, 37, 48, 45, 101 }, // German/Luxembourg
306
{ 42, 206, 46, 39, 59, 37, 48, 45, 101 }, // German/Switzerland
307
{ 43, 85, 44, 46, 59, 37, 48, 45, 101 }, // Greek/Greece
308
{ 46, 100, 46, 44, 44, 37, 2790, 45, 101 }, // Gujarati/India
309
{ 48, 105, 46, 44, 44, 37, 48, 45, 101 }, // Hebrew/Israel
310
{ 49, 100, 46, 44, 44, 37, 48, 45, 101 }, // Hindi/India
311
{ 50, 98, 44, 160, 59, 37, 48, 45, 101 }, // Hungarian/Hungary
312
{ 51, 99, 44, 46, 59, 37, 48, 45, 101 }, // Icelandic/Iceland
313
{ 52, 101, 44, 46, 59, 37, 48, 45, 101 }, // Indonesian/Indonesia
314
{ 58, 106, 44, 46, 59, 37, 48, 45, 101 }, // Italian/Italy
315
{ 58, 206, 46, 39, 59, 37, 48, 45, 101 }, // Italian/Switzerland
316
{ 59, 108, 46, 44, 44, 37, 48, 45, 101 }, // Japanese/Japan
317
{ 61, 100, 46, 44, 44, 37, 3302, 45, 101 }, // Kannada/India
318
{ 63, 110, 44, 160, 59, 37, 48, 45, 101 }, // Kazakh/Kazakhstan
319
{ 65, 116, 44, 160, 59, 37, 48, 45, 101 }, // Kirghiz/Kyrgyzstan
320
{ 66, 114, 46, 44, 44, 37, 48, 45, 101 }, // Korean/RepublicOfKorea
321
{ 71, 118, 44, 160, 59, 37, 48, 45, 101 }, // Latvian/Latvia
322
{ 73, 124, 44, 46, 59, 37, 48, 45, 101 }, // Lithuanian/Lithuania
323
{ 74, 127, 44, 46, 59, 37, 48, 45, 101 }, // Macedonian/Macedonia
324
{ 76, 130, 44, 46, 59, 37, 48, 45, 101 }, // Malay/Malaysia
325
{ 76, 32, 44, 46, 59, 37, 48, 45, 101 }, // Malay/BruneiDarussalam
326
{ 80, 100, 46, 44, 44, 37, 2406, 45, 101 }, // Marathi/India
327
{ 82, 143, 44, 160, 59, 37, 48, 45, 101 }, // Mongolian/Mongolia
328
{ 85, 161, 44, 160, 59, 37, 48, 45, 101 }, // Norwegian/Norway
329
{ 89, 102, 46, 44, 59, 37, 1776, 45, 101 }, // Persian/Iran
330
{ 90, 172, 44, 160, 59, 37, 48, 45, 101 }, // Polish/Poland
331
{ 91, 173, 44, 46, 59, 37, 48, 45, 101 }, // Portuguese/Portugal
332
{ 91, 30, 44, 46, 59, 37, 48, 45, 101 }, // Portuguese/Brazil
333
{ 92, 100, 46, 44, 44, 37, 2662, 45, 101 }, // Punjabi/India
334
{ 95, 177, 44, 46, 59, 37, 48, 45, 101 }, // Romanian/Romania
335
{ 96, 178, 44, 160, 59, 37, 48, 45, 101 }, // Russian/RussianFederation
336
{ 99, 100, 46, 44, 44, 37, 2406, 45, 101 }, // Sanskrit/India
337
{ 108, 191, 44, 160, 59, 37, 48, 45, 101 }, // Slovak/Slovakia
338
{ 109, 192, 44, 46, 59, 37, 48, 45, 101 }, // Slovenian/Slovenia
339
{ 111, 197, 44, 46, 59, 37, 48, 45, 101 }, // Spanish/Spain
340
{ 111, 10, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Argentina
341
{ 111, 26, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Bolivia
342
{ 111, 43, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Chile
343
{ 111, 47, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Colombia
344
{ 111, 52, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/CostaRica
345
{ 111, 61, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/DominicanRepublic
346
{ 111, 63, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Ecuador
347
{ 111, 65, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/ElSalvador
348
{ 111, 90, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Guatemala
349
{ 111, 96, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Honduras
350
{ 111, 139, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Mexico
351
{ 111, 155, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Nicaragua
352
{ 111, 166, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Panama
353
{ 111, 168, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Paraguay
354
{ 111, 169, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/Peru
355
{ 111, 174, 46, 44, 44, 37, 48, 45, 101 }, // Spanish/PuertoRico
356
{ 111, 227, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Uruguay
357
{ 111, 231, 44, 46, 44, 37, 48, 45, 101 }, // Spanish/Venezuela
358
{ 113, 111, 46, 44, 44, 37, 48, 45, 101 }, // Swahili/Kenya
359
{ 114, 205, 44, 160, 59, 37, 48, 45, 101 }, // Swedish/Sweden
360
{ 114, 73, 44, 160, 59, 37, 48, 45, 101 }, // Swedish/Finland
361
{ 117, 100, 46, 44, 44, 37, 48, 45, 101 }, // Tamil/India
362
{ 119, 100, 46, 44, 44, 37, 3174, 45, 101 }, // Telugu/India
363
{ 120, 211, 46, 44, 44, 37, 3664, 45, 101 }, // Thai/Thailand
364
{ 125, 217, 44, 46, 59, 37, 48, 45, 101 }, // Turkish/Turkey
365
{ 129, 222, 44, 160, 59, 37, 48, 45, 101 }, // Ukrainian/Ukraine
366
{ 130, 163, 46, 44, 59, 37, 1776, 45, 101 }, // Urdu/Pakistan
367
{ 131, 228, 44, 160, 59, 37, 48, 45, 101 }, // Uzbek/Uzbekistan
368
{ 132, 232, 44, 46, 44, 37, 48, 45, 101 }, // Vietnamese/VietNam
369
{ 141, 161, 44, 160, 59, 37, 48, 45, 101 }, // Nynorsk/Norway
370
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 } // trailing 0s
373
static const char language_name_list[] =
517
static const uint language_name_index[] = {
612
775, // RhaetoRomance
619
837, // SerboCroatian
662
static const char country_name_list[] =
672
"AntiguaAndBarbuda\0"
690
"BosniaAndHerzegowina\0"
694
"BritishIndianOceanTerritory\0"
704
"CentralAfricanRepublic\0"
712
"DemocraticRepublicOfCongo\0"
713
"PeoplesRepublicOfCongo\0"
724
"DominicanRepublic\0"
738
"MetropolitanFrance\0"
741
"FrenchSouthernTerritories\0"
758
"HeardAndMcDonaldIslands\0"
776
"DemocraticRepublicOfKorea\0"
785
"LibyanArabJamahiriya\0"
815
"NetherlandsAntilles\0"
823
"NorthernMarianaIslands\0"
828
"PalestinianTerritory\0"
841
"RussianFederation\0"
843
"SaintKittsAndNevis\0"
845
"StVincentAndTheGrenadines\0"
848
"SaoTomeAndPrincipe\0"
859
"SouthGeorgiaAndTheSouthSandwichIslands\0"
863
"StPierreAndMiquelon\0"
866
"SvalbardAndJanMayenIslands\0"
870
"SyrianArabRepublic\0"
878
"TrinidadAndTobago\0"
882
"TurksAndCaicosIslands\0"
886
"UnitedArabEmirates\0"
889
"UnitedStatesMinorOutlyingIslands\0"
896
"BritishVirginIslands\0"
898
"WallisAndFutunaIslands\0"
905
static const uint country_name_index[] = {
915
85, // AntiguaAndBarbuda
933
244, // BosniaAndHerzegowina
937
294, // BritishIndianOceanTerritory
938
322, // BruneiDarussalam
946
403, // CaymanIslands
947
417, // CentralAfricanRepublic
951
457, // ChristmasIsland
955
503, // DemocraticRepublicOfCongo
956
529, // PeoplesRepublicOfCongo
963
605, // CzechRepublic
967
645, // DominicanRepublic
972
698, // EquatorialGuinea
976
740, // FalklandIslands
981
789, // MetropolitanFrance
983
821, // FrenchPolynesia
984
837, // FrenchSouthernTerritories
1001
992, // HeardAndMcDonaldIslands
1019
1144, // DemocraticRepublicOfKorea
1020
1170, // RepublicOfKorea
1028
1239, // LibyanArabJamahiriya
1029
1260, // Liechtenstein
1040
1358, // MarshallIslands
1057
1514, // Netherlands
1058
1526, // NetherlandsAntilles
1059
1546, // NewCaledonia
1065
1599, // NorfolkIsland
1066
1613, // NorthernMarianaIslands
1071
1663, // PalestinianTerritory
1073
1691, // PapuaNewGuinea
1076
1720, // Philippines
1084
1790, // RussianFederation
1086
1815, // SaintKittsAndNevis
1088
1842, // StVincentAndTheGrenadines
1091
1884, // SaoTomeAndPrincipe
1092
1903, // SaudiArabia
1095
1934, // SierraLeone
1099
1974, // SolomonIslands
1101
1997, // SouthAfrica
1102
2009, // SouthGeorgiaAndTheSouthSandwichIslands
1106
2072, // StPierreAndMiquelon
1109
2107, // SvalbardAndJanMayenIslands
1112
2151, // Switzerland
1113
2163, // SyrianArabRepublic
1121
2237, // TrinidadAndTobago
1124
2270, // Turkmenistan
1125
2283, // TurksAndCaicosIslands
1129
2327, // UnitedArabEmirates
1130
2346, // UnitedKingdom
1131
2360, // UnitedStates
1132
2373, // UnitedStatesMinorOutlyingIslands
1136
2433, // VaticanCityState
1139
2468, // BritishVirginIslands
1140
2489, // USVirginIslands
1141
2505, // WallisAndFutunaIslands
1142
2528, // WesternSahara
1149
static const unsigned char language_code_list[] =
1172
"be" // Byelorussian
1244
"rm" // RhaetoRomance
1251
"sh" // SerboCroatian
1293
static const unsigned char country_code_list[] =
1298
"AS" // AmericanSamoa
1303
"AG" // AntiguaAndBarbuda
1321
"BA" // BosniaAndHerzegowina
1323
"BV" // BouvetIsland
1325
"IO" // BritishIndianOceanTerritory
1326
"BN" // BruneiDarussalam
1334
"KY" // CaymanIslands
1335
"CF" // CentralAfricanRepublic
1339
"CX" // ChristmasIsland
1340
"CC" // CocosIslands
1343
"CD" // DemocraticRepublicOfCongo
1344
"CG" // PeoplesRepublicOfCongo
1351
"CZ" // CzechRepublic
1355
"DO" // DominicanRepublic
1360
"GQ" // EquatorialGuinea
1364
"FK" // FalklandIslands
1365
"FO" // FaroeIslands
1369
"FX" // MetropolitanFrance
1370
"GF" // FrenchGuiana
1371
"PF" // FrenchPolynesia
1372
"TF" // FrenchSouthernTerritories
1386
"GW" // GuineaBissau
1389
"HM" // HeardAndMcDonaldIslands
1407
"KP" // DemocraticRepublicOfKorea
1408
"KR" // RepublicOfKorea
1416
"LY" // LibyanArabJamahiriya
1417
"LI" // Liechtenstein
1428
"MH" // MarshallIslands
1446
"AN" // NetherlandsAntilles
1447
"NC" // NewCaledonia
1453
"NF" // NorfolkIsland
1454
"MP" // NorthernMarianaIslands
1459
"PS" // PalestinianTerritory
1461
"PG" // PapuaNewGuinea
1472
"RU" // RussianFederation
1474
"KN" // SaintKittsAndNevis
1476
"VC" // StVincentAndTheGrenadines
1479
"ST" // SaoTomeAndPrincipe
1487
"SB" // SolomonIslands
1490
"GS" // SouthGeorgiaAndTheSouthSandwichIslands
1494
"PM" // StPierreAndMiquelon
1497
"SJ" // SvalbardAndJanMayenIslands
1501
"SY" // SyrianArabRepublic
1509
"TT" // TrinidadAndTobago
1512
"TM" // Turkmenistan
1513
"TC" // TurksAndCaicosIslands
1517
"AE" // UnitedArabEmirates
1518
"GB" // UnitedKingdom
1519
"US" // UnitedStates
1520
"UM" // UnitedStatesMinorOutlyingIslands
1524
"VA" // VaticanCityState
1527
"VG" // BritishVirginIslands
1528
"VI" // USVirginIslands
1529
"WF" // WallisAndFutunaIslands
1530
"EH" // WesternSahara
1536
static QLocale::Language codeToLanguage(const QString &code)
1538
if (code.length() != 2)
1541
ushort uc1 = code.unicode()[0].unicode();
1542
ushort uc2 = code.unicode()[1].unicode();
1544
const unsigned char *c = language_code_list;
1545
for (; *c != 0; c += 2) {
1546
if (uc1 == c[0] && uc2 == c[1])
1547
return QLocale::Language((c - language_code_list)/2);
1553
static QLocale::Country codeToCountry(const QString &code)
1555
if (code.length() != 2)
1556
return QLocale::AnyCountry;
1558
ushort uc1 = code.unicode()[0].unicode();
1559
ushort uc2 = code.unicode()[1].unicode();
1561
const unsigned char *c = country_code_list;
1562
for (; *c != 0; c += 2) {
1563
if (uc1 == c[0] && uc2 == c[1])
1564
return QLocale::Country((c - country_code_list)/2);
1567
return QLocale::AnyCountry;
1570
static QString languageToCode(QLocale::Language language)
1572
if (language == QLocale::C)
1573
return QLatin1String("C");
1577
const unsigned char *c = language_code_list + 2*(uint(language));
1578
code[0] = ushort(c[0]);
1579
code[1] = ushort(c[1]);
1583
static QString countryToCode(QLocale::Country country)
1585
if (country == QLocale::AnyCountry)
1590
const unsigned char *c = country_code_list + 2*(uint(country));
1591
code[0] = ushort(c[0]);
1592
code[1] = ushort(c[1]);
1596
const QLocalePrivate *QLocale::default_d = 0;
1598
QString QLocalePrivate::infinity() const
1600
return QString::fromLatin1("inf");
1603
QString QLocalePrivate::nan() const
1605
return QString::fromLatin1("nan");
1608
#ifndef QT_NO_DATASTREAM
1609
QDataStream &operator<<(QDataStream &ds, const QLocale &l)
1615
QDataStream &operator>>(QDataStream &ds, QLocale &l)
1624
#if defined(Q_OS_WIN)
1625
/* Win95 doesn't have a function to return the ISO lang/country name of the user's locale.
1626
Instead it can return a "Windows code". This maps windows codes to ISO country names. */
1628
struct WindowsToISOListElt {
1633
static const WindowsToISOListElt windows_to_iso_list[] = {
1634
{ 0x0401, "ar_SA" },
1635
{ 0x0402, "bg\0 " },
1636
{ 0x0403, "ca\0 " },
1637
{ 0x0404, "zh_TW" },
1638
{ 0x0405, "cs\0 " },
1639
{ 0x0406, "da\0 " },
1640
{ 0x0407, "de\0 " },
1641
{ 0x0408, "el\0 " },
1642
{ 0x0409, "en_US" },
1643
{ 0x040a, "es\0 " },
1644
{ 0x040b, "fi\0 " },
1645
{ 0x040c, "fr\0 " },
1646
{ 0x040d, "he\0 " },
1647
{ 0x040e, "hu\0 " },
1648
{ 0x040f, "is\0 " },
1649
{ 0x0410, "it\0 " },
1650
{ 0x0411, "ja\0 " },
1651
{ 0x0412, "ko\0 " },
1652
{ 0x0413, "nl\0 " },
1653
{ 0x0414, "no\0 " },
1654
{ 0x0415, "pl\0 " },
1655
{ 0x0416, "pt_BR" },
1656
{ 0x0418, "ro\0 " },
1657
{ 0x0419, "ru\0 " },
1658
{ 0x041a, "hr\0 " },
1659
{ 0x041c, "sq\0 " },
1660
{ 0x041d, "sv\0 " },
1661
{ 0x041e, "th\0 " },
1662
{ 0x041f, "tr\0 " },
1663
{ 0x0420, "ur\0 " },
1664
{ 0x0421, "in\0 " },
1665
{ 0x0422, "uk\0 " },
1666
{ 0x0423, "be\0 " },
1667
{ 0x0425, "et\0 " },
1668
{ 0x0426, "lv\0 " },
1669
{ 0x0427, "lt\0 " },
1670
{ 0x0429, "fa\0 " },
1671
{ 0x042a, "vi\0 " },
1672
{ 0x042d, "eu\0 " },
1673
{ 0x042f, "mk\0 " },
1674
{ 0x0436, "af\0 " },
1675
{ 0x0438, "fo\0 " },
1676
{ 0x0439, "hi\0 " },
1677
{ 0x043e, "ms\0 " },
1678
{ 0x0458, "mt\0 " },
1679
{ 0x0801, "ar_IQ" },
1680
{ 0x0804, "zh_CN" },
1681
{ 0x0807, "de_CH" },
1682
{ 0x0809, "en_GB" },
1683
{ 0x080a, "es_MX" },
1684
{ 0x080c, "fr_BE" },
1685
{ 0x0810, "it_CH" },
1686
{ 0x0812, "ko\0 " },
1687
{ 0x0813, "nl_BE" },
1688
{ 0x0814, "no\0 " },
1689
{ 0x0816, "pt\0 " },
1690
{ 0x081a, "sr\0 " },
1691
{ 0x081d, "sv_FI" },
1692
{ 0x0c01, "ar_EG" },
1693
{ 0x0c04, "zh_HK" },
1694
{ 0x0c07, "de_AT" },
1695
{ 0x0c09, "en_AU" },
1696
{ 0x0c0a, "es\0 " },
1697
{ 0x0c0c, "fr_CA" },
1698
{ 0x0c1a, "sr\0 " },
1699
{ 0x1001, "ar_LY" },
1700
{ 0x1004, "zh_SG" },
1701
{ 0x1007, "de_LU" },
1702
{ 0x1009, "en_CA" },
1703
{ 0x100a, "es_GT" },
1704
{ 0x100c, "fr_CH" },
1705
{ 0x1401, "ar_DZ" },
1706
{ 0x1407, "de_LI" },
1707
{ 0x1409, "en_NZ" },
1708
{ 0x140a, "es_CR" },
1709
{ 0x140c, "fr_LU" },
1710
{ 0x1801, "ar_MA" },
1711
{ 0x1809, "en_IE" },
1712
{ 0x180a, "es_PA" },
1713
{ 0x1c01, "ar_TN" },
1714
{ 0x1c09, "en_ZA" },
1715
{ 0x1c0a, "es_DO" },
1716
{ 0x2001, "ar_OM" },
1717
{ 0x2009, "en_JM" },
1718
{ 0x200a, "es_VE" },
1719
{ 0x2401, "ar_YE" },
1720
{ 0x2409, "en\0 " },
1721
{ 0x240a, "es_CO" },
1722
{ 0x2801, "ar_SY" },
1723
{ 0x2809, "en_BZ" },
1724
{ 0x280a, "es_PE" },
1725
{ 0x2c01, "ar_JO" },
1726
{ 0x2c09, "en_TT" },
1727
{ 0x2c0a, "es_AR" },
1728
{ 0x3001, "ar_LB" },
1729
{ 0x300a, "es_EC" },
1730
{ 0x3401, "ar_KW" },
1731
{ 0x340a, "es_CL" },
1732
{ 0x3801, "ar_AE" },
1733
{ 0x380a, "es_UY" },
1734
{ 0x3c01, "ar_BH" },
1735
{ 0x3c0a, "es_PY" },
1736
{ 0x4001, "ar_QA" },
1737
{ 0x400a, "es_BO" },
1738
{ 0x440a, "es_SV" },
1739
{ 0x480a, "es_HN" },
1740
{ 0x4c0a, "es_NI" },
1744
static const int windows_to_iso_count
1745
= sizeof(windows_to_iso_list)/sizeof(WindowsToISOListElt);
1747
static const char *winLangCodeToIsoName(int code)
1749
int cmp = code - windows_to_iso_list[0].windows_code;
1754
return windows_to_iso_list[0].iso_name;
1757
int end = windows_to_iso_count;
1759
while (end - begin > 1) {
1760
uint mid = (begin + end)/2;
1762
const WindowsToISOListElt *elt = windows_to_iso_list + mid;
1763
int cmp = code - elt->windows_code;
1769
return elt->iso_name;
1776
static QString winIso639LangName()
1780
// Windows returns the wrong ISO639 for some languages, we need to detect them here using
1781
// the language code
1785
if (GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_ILANGUAGE, out, 255))
1786
lang_code = QString::fromUtf16((ushort*)out);
1789
if (GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ILANGUAGE, out, 255))
1790
lang_code = QString::fromLocal8Bit(out);
1793
if (!lang_code.isEmpty()) {
1796
QByteArray latin1_lang_code = lang_code.toLatin1();
1797
int i = qstrtoull(latin1_lang_code, &endptr, 16, &ok);
1798
if (ok && *endptr == '\0') {
1801
result = QLatin1String("nn"); // Nynorsk
1809
if (!result.isEmpty())
1812
// not one of the problematic languages - do the usual lookup
1815
if (GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME , out, 255))
1816
result = QString::fromUtf16((ushort*)out);
1819
if (GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, out, 255))
1820
result = QString::fromLocal8Bit(out);
1826
static QString winIso3116CtryName()
1832
if (GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, out, 255))
1833
result = QString::fromUtf16((ushort*)out);
1836
if (GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, out, 255))
1837
result = QString::fromLocal8Bit(out);
1847
QByteArray QLocalePrivate::systemLocaleName()
1849
static QByteArray lang;
1850
lang = qgetenv("LANG");
1852
#if !defined(QWS) && defined(Q_OS_MAC)
1853
if (!lang.isEmpty())
1857
if(!LocaleRefGetPartString(NULL, kLocaleLanguageMask | kLocaleRegionMask, 255, mac_ret))
1861
#if defined(Q_WS_WIN)
1862
if (!lang.isEmpty())
1865
if (QSysInfo::WindowsVersion == QSysInfo::WV_95) {
1866
lang = winLangCodeToIsoName(GetUserDefaultLangID());
1868
QString language = winIso639LangName();
1869
QString country = winIso3116CtryName();
1870
lang += language.toLatin1();
1871
if (!country.isEmpty()) {
1873
lang += country.toLatin1();
1883
static const QLocalePrivate *findLocale(QLocale::Language language,
1884
QLocale::Country country)
1886
unsigned language_id = language;
1887
unsigned country_id = country;
1889
uint idx = locale_index[language_id];
1891
const QLocalePrivate *d = locale_data + idx;
1893
if (idx == 0) // default language has no associated country
1896
if (country == QLocale::AnyCountry)
1899
Q_ASSERT(d->languageId() == language_id);
1901
while (d->languageId() == language_id
1902
&& d->countryId() != country_id)
1905
if (d->countryId() == country_id
1906
&& d->languageId() == language_id)
1909
return locale_data + idx;
1914
\brief The QLocale class converts between numbers and their
1915
string representations in various languages.
1922
QLocale is initialized with a language/country pair in its
1923
constructor and offers number-to-string and string-to-number
1924
conversion functions similar to those in QString.
1929
QLocale egyptian(QLocale::Arabic, QLocale::Egypt);
1930
QString s1 = egyptian.toString(1.571429E+07, 'e');
1931
QString s2 = egyptian.toString(10);
1933
double d = egyptian.toDouble(s1);
1934
int i = egyptian.toInt(s2);
1937
QLocale supports the concept of a default locale, which is
1938
determined from the system's locale settings at application
1939
startup. The default locale can be changed by calling the
1940
static member setDefault(). The default locale has the
1944
\i If a QLocale object is constructed with the default constructor,
1945
it will use the default locale's settings.
1946
\i QString::toInt(), QString::toDouble(), etc., interpret the
1947
string according to the default locale. If this fails, it
1948
falls back on the "C" locale.
1949
\i QString::arg() uses the default locale to format a number when
1950
its position specifier in the format string contains an 'L',
1954
The following example illustrates how to use QLocale directly:
1957
QLocale::setDefault(QLocale::Hebrew, QLocale::Israel);
1958
QLocale hebrew; // Constructs a default QLocale
1959
QString s1 = hebrew.toString(15714.3, 'e');
1964
QLocale::setDefault(QLocale::C);
1965
d = QString("1234,56").toDouble(&ok); // ok == false
1966
d = QString("1234.56").toDouble(&ok); // ok == true, d == 1234.56
1968
QLocale::setDefault(QLocale::German);
1969
d = QString("1234,56").toDouble(&ok); // ok == true, d == 1234.56
1970
d = QString("1234.56").toDouble(&ok); // ok == true, d == 1234.56
1972
QLocale::setDefault(QLocale::English, QLocale::UnitedStates);
1973
str = QString("%1 %L2 %L3")
1974
.arg(12345).arg(12345).arg(12345, 0, 16);
1975
// str == "12345 12,345 3039"
1978
When a language/country pair is specified in the constructor, one
1979
of three things can happen:
1982
\i If the language/country pair is found in the database, it is used.
1983
\i If the language is found but the country is not, or if the country
1984
is \c AnyCountry, the language is used with the most
1985
appropriate available country (for example, Germany for German),
1986
\i If neither the language nor the country are found, QLocale
1987
defaults to the default locale (see setDefault()).
1990
The "C" locale is identical to \l{English}/\l{UnitedStates}.
1992
Use language() and country() to determine the actual language and
1993
country values used.
1995
An alternative method for constructing a QLocale object is by
1996
specifying the locale name.
1999
QLocale korean("ko");
2000
QLocale swiss("de_CH");
2003
This constructor converts the locale name to a language/country
2004
pair; it does not use the system locale database.
2006
The double-to-string and string-to-double conversion functions are
2007
covered by the following licenses:
2012
Copyright (c) 1991 by AT&T.
2014
Permission to use, copy, modify, and distribute this software for any
2015
purpose without fee is hereby granted, provided that this entire notice
2016
is included in all copies of any software which is or includes a copy
2017
or modification of this software and in all copies of the supporting
2018
documentation for such software.
2020
THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
2021
WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
2022
REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
2023
OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
2025
This product includes software developed by the University of
2026
California, Berkeley and its contributors.
2029
\sa QString::arg(), QString::toInt(), QString::toDouble()
2033
\enum QLocale::Language
2035
This enumerated type is used to specify a language.
2119
\value NauruLanguage
2131
\value RhaetoRomance
2138
\value SerboCroatian
2160
\value TongaLanguage
2178
\omitvalue LastLanguage
2184
\enum QLocale::Country
2186
This enumerated type is used to specify a country.
2192
\value AmericanSamoa
2197
\value AntiguaAndBarbuda
2215
\value BosniaAndHerzegowina
2219
\value BritishIndianOceanTerritory
2220
\value BruneiDarussalam
2228
\value CaymanIslands
2229
\value CentralAfricanRepublic
2233
\value ChristmasIsland
2237
\value DemocraticRepublicOfCongo
2238
\value PeoplesRepublicOfCongo
2245
\value CzechRepublic
2249
\value DominicanRepublic
2254
\value EquatorialGuinea
2258
\value FalklandIslands
2263
\value MetropolitanFrance
2265
\value FrenchPolynesia
2266
\value FrenchSouthernTerritories
2283
\value HeardAndMcDonaldIslands
2301
\value DemocraticRepublicOfKorea
2302
\value RepublicOfKorea
2310
\value LibyanArabJamahiriya
2311
\value Liechtenstein
2322
\value MarshallIslands
2340
\value NetherlandsAntilles
2347
\value NorfolkIsland
2348
\value NorthernMarianaIslands
2353
\value PalestinianTerritory
2355
\value PapuaNewGuinea
2366
\value RussianFederation
2368
\value SaintKittsAndNevis
2370
\value StVincentAndTheGrenadines
2373
\value SaoTomeAndPrincipe
2381
\value SolomonIslands
2384
\value SouthGeorgiaAndTheSouthSandwichIslands
2388
\value StPierreAndMiquelon
2391
\value SvalbardAndJanMayenIslands
2395
\value SyrianArabRepublic
2403
\value TrinidadAndTobago
2407
\value TurksAndCaicosIslands
2411
\value UnitedArabEmirates
2412
\value UnitedKingdom
2414
\value UnitedStatesMinorOutlyingIslands
2418
\value VaticanCityState
2421
\value BritishVirginIslands
2422
\value USVirginIslands
2423
\value WallisAndFutunaIslands
2424
\value WesternSahara
2429
\omitvalue LastCountry
2435
\fn bool QLocale::operator==(const QLocale &other) const
2437
Returns true if the QLocale object is the same as the \a other
2438
locale specified; otherwise returns false.
2442
\fn bool QLocale::operator!=(const QLocale &other) const
2444
Returns true if the QLocale object is not the same as the \a other
2445
locale specified; otherwise returns false.
2449
Constructs a QLocale object with the specified \a name,
2450
which has the format
2451
"language[_country][.codeset][@modifier]" or "C", where:
2454
\i language is a lowercase, two-letter, ISO 639 language code,
2455
\i territory is an uppercase, two-letter, ISO 3166 country code,
2456
\i and codeset and modifier are ignored.
2459
If the string violates the locale format, or language is not
2460
a valid ISO 369 code, the "C" locale is used instead. If country
2461
is not present, or is not a valid ISO 3166 code, the most
2462
appropriate country is chosen for the specified language.
2464
The language and country codes are converted to their respective
2465
\c Language and \c Country enums. After this conversion is
2466
performed the constructor behaves exactly like QLocale(Country,
2469
This constructor is much slower than QLocale(Country, Language).
2474
QLocale::QLocale(const QString &name)
2477
Country cntry = AnyCountry;
2479
uint l = name.length();
2485
const QChar *uc = name.unicode();
2487
&& uc[2] != QLatin1Char('_')
2488
&& uc[2] != QLatin1Char('.')
2489
&& uc[2] != QLatin1Char('@'))
2492
lang = codeToLanguage(name.mid(0, 2));
2496
if (l == 2 || uc[2] == QLatin1Char('.') || uc[2] == QLatin1Char('@'))
2499
// we have uc[2] == '_'
2503
if (l > 5 && uc[5] != QLatin1Char('.') && uc[5] != QLatin1Char('@'))
2506
cntry = codeToCountry(name.mid(3, 2));
2509
d = findLocale(lang, cntry);
2513
Constructs a QLocale object initialized with the default locale.
2521
default_d = system().d;
2527
Constructs a QLocale object with the specified \a language and \a
2531
\i If the language/country pair is found in the database, it is used.
2532
\i If the language is found but the country is not, or if the country
2533
is \c AnyCountry, the language is used with the most
2534
appropriate available country (for example, Germany for German),
2535
\i If neither the language nor the country are found, QLocale
2536
defaults to the default locale (see setDefault()).
2539
The language and country that are actually used can be queried
2540
using language() and country().
2542
\sa setDefault() language() country()
2545
QLocale::QLocale(Language language, Country country)
2547
d = findLocale(language, country);
2549
// If not found, should default to system
2550
if (d->languageId() == QLocale::C && language != QLocale::C) {
2552
default_d = system().d;
2559
Constructs a QLocale object as a copy of \a other.
2562
QLocale::QLocale(const QLocale &other)
2568
Assigns \a other to this QLocale object and returns a reference
2569
to this QLocale object.
2572
QLocale &QLocale::operator=(const QLocale &other)
2581
Sets the global default locale to \a locale. These
2582
values are used when a QLocale object is constructed with
2583
no arguments. If this function is not called, the system's
2586
\warning In a multithreaded application, the default locale
2587
should be set at application startup, before any non-GUI threads
2593
void QLocale::setDefault(const QLocale &locale)
2595
default_d = locale.d;
2599
Returns the language of this locale.
2601
\sa country(), languageToString(), name()
2603
QLocale::Language QLocale::language() const
2605
return Language(d->languageId());
2609
Returns the country of this locale.
2611
\sa language(), countryToString(), name()
2613
QLocale::Country QLocale::country() const
2615
return Country(d->countryId());
2619
Returns the language and country of this locale as a
2620
string of the form "language_country", where
2621
language is a lowercase, two-letter ISO 639 language code,
2622
and country is an uppercase, two-letter ISO 3166 country code.
2624
\sa language(), country()
2627
QString QLocale::name() const
2629
Language l = language();
2631
QString result = languageToCode(l);
2636
Country c = country();
2637
if (c == AnyCountry)
2640
result.append(QLatin1Char('_'));
2641
result.append(countryToCode(c));
2647
Returns a QString containing the name of \a language.
2649
\sa countryToString(), name()
2652
QString QLocale::languageToString(Language language)
2654
if (uint(language) > uint(QLocale::LastLanguage))
2655
return QLatin1String("Unknown");
2656
return QLatin1String(language_name_list + language_name_index[language]);
2660
Returns a QString containing the name of \a country.
2662
\sa country(), name()
2665
QString QLocale::countryToString(Country country)
2667
if (uint(country) > uint(QLocale::LastCountry))
2668
return QLatin1String("Unknown");
2669
return QLatin1String(country_name_list + country_name_index[country]);
2673
Returns the short int represented by the localized string \a s,
2674
using base \a base. If \a base is 0 the base is determined
2675
automatically using the following rules: If the string begins with
2676
"0x", it is assumed to be hexadecimal; if it begins with "0", it
2677
is assumed to be octal; otherwise it is assumed to be decimal.
2679
If the conversion fails the function returns 0.
2681
If \a ok is not 0, failure is reported by setting *ok to false, and
2682
success by setting *ok to true.
2684
This function ignores leading and trailing whitespace.
2686
\sa toUShort(), toString()
2689
short QLocale::toShort(const QString &s, bool *ok, int base) const
2691
qlonglong i = toLongLong(s, ok, base);
2692
if (i < SHRT_MIN || i > SHRT_MAX) {
2701
Returns the unsigned short int represented by the localized string
2702
\a s, using base \a base. If \a base is 0 the base is determined
2703
automatically using the following rules: If the string begins with
2704
"0x", it is assumed to be hexadecimal; if it begins with "0", it
2705
is assumed to be octal; otherwise it is assumed to be decimal.
2707
If the conversion fails the function returns 0.
2709
If \a ok is not 0, failure is reported by setting *ok to false, and
2710
success by setting *ok to true.
2712
This function ignores leading and trailing whitespace.
2714
\sa toShort(), toString()
2717
ushort QLocale::toUShort(const QString &s, bool *ok, int base) const
2719
qulonglong i = toULongLong(s, ok, base);
2720
if (i > USHRT_MAX) {
2729
Returns the int represented by the localized string \a s, using
2730
base \a base. If \a base is 0 the base is determined automatically
2731
using the following rules: If the string begins with "0x", it is
2732
assumed to be hexadecimal; if it begins with "0", it is assumed to
2733
be octal; otherwise it is assumed to be decimal.
2735
If the conversion fails the function returns 0.
2737
If \a ok is not 0, failure is reported by setting *ok to false, and
2738
success by setting *ok to true.
2740
This function ignores leading and trailing whitespace.
2742
\sa toUInt(), toString()
2745
int QLocale::toInt(const QString &s, bool *ok, int base) const
2747
qlonglong i = toLongLong(s, ok, base);
2748
if (i < INT_MIN || i > INT_MAX) {
2757
Returns the unsigned int represented by the localized string \a s,
2758
using base \a base. If \a base is 0 the base is determined
2759
automatically using the following rules: If the string begins with
2760
"0x", it is assumed to be hexadecimal; if it begins with "0", it
2761
is assumed to be octal; otherwise it is assumed to be decimal.
2763
If the conversion fails the function returns 0.
2765
If \a ok is not 0, failure is reported by setting *ok to false, and
2766
success by setting *ok to true.
2768
This function ignores leading and trailing whitespace.
2770
\sa toInt(), toString()
2773
uint QLocale::toUInt(const QString &s, bool *ok, int base) const
2775
qulonglong i = toULongLong(s, ok, base);
2785
Returns the long long int represented by the localized string \a
2786
s, using base \a base. If \a base is 0 the base is determined
2787
automatically using the following rules: If the string begins with
2788
"0x", it is assumed to be hexadecimal; if it begins with "0", it
2789
is assumed to be octal; otherwise it is assumed to be decimal.
2791
If the conversion fails the function returns 0.
2793
If \a ok is not 0, failure is reported by setting *ok to false, and
2794
success by setting *ok to true.
2796
This function ignores leading and trailing whitespace.
2798
\sa toInt(), toULongLong(), toDouble(), toString()
2802
qlonglong QLocale::toLongLong(const QString &s, bool *ok, int base) const
2804
return d->stringToLongLong(s, base, ok, QLocalePrivate::ParseGroupSeparators);
2808
Returns the unsigned long long int represented by the localized
2809
string \a s, using base \a base. If \a base is 0 the base is
2810
determined automatically using the following rules: If the string
2811
begins with "0x", it is assumed to be hexadecimal; if it begins
2812
with "0", it is assumed to be octal; otherwise it is assumed to be
2815
If the conversion fails the function returns 0.
2817
If \a ok is not 0, failure is reported by setting *ok to false, and
2818
success by setting *ok to true.
2820
This function ignores leading and trailing whitespace.
2822
\sa toLongLong(), toInt(), toDouble(), toString()
2825
qlonglong QLocale::toULongLong(const QString &s, bool *ok, int base) const
2827
return d->stringToUnsLongLong(s, base, ok, QLocalePrivate::ParseGroupSeparators);
2831
Returns the float represented by the localized string \a s, or 0.0
2832
if the conversion failed.
2834
If \a ok is not 0, reports failure by setting
2835
*ok to false and success by setting *ok to true.
2837
This function ignores leading and trailing whitespace.
2839
\sa toDouble(), toInt(), toString()
2842
#define QT_MAX_FLOAT 3.4028234663852886e+38
2844
float QLocale::toFloat(const QString &s, bool *ok) const
2847
double d = toDouble(s, &myOk);
2848
if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
2859
Returns the double represented by the localized string \a s, or
2860
0.0 if the conversion failed.
2862
If \a ok is not 0, reports failure by setting
2863
*ok to false and success by setting *ok to true.
2865
Unlike QString::toDouble(), this function does not fall back to
2866
the "C" locale if the string cannot be interpreted in this
2873
QLocale c(QLocale::C);
2874
d = c.toDouble( "1234.56", &ok ); // ok == true, d == 1234.56
2875
d = c.toDouble( "1,234.56", &ok ); // ok == true, d == 1234.56
2876
d = c.toDouble( "1234,56", &ok ); // ok == false
2878
QLocale german(QLocale::German);
2879
d = german.toDouble( "1234,56", &ok ); // ok == true, d == 1234.56
2880
d = german.toDouble( "1.234,56", &ok ); // ok == true, d == 1234.56
2881
d = german.toDouble( "1234.56", &ok ); // ok == false
2883
d = german.toDouble( "1.234", &ok ); // ok == true, d == 1234.0
2886
Notice that the last conversion returns 1234.0, because '.' is the
2887
thousands group separator in the German locale.
2889
This function ignores leading and trailing whitespace.
2891
\sa toFloat(), toInt(), toString()
2894
double QLocale::toDouble(const QString &s, bool *ok) const
2896
return d->stringToDouble(s, ok, QLocalePrivate::ParseGroupSeparators);
2900
Returns a localized string representation of \a i.
2905
QString QLocale::toString(qlonglong i) const
2907
return d->longLongToString(i, -1, 10, -1, QLocalePrivate::ThousandsGroup);
2916
QString QLocale::toString(qulonglong i) const
2918
return d->unsLongLongToString(i, -1, 10, -1, QLocalePrivate::ThousandsGroup);
2921
static bool qIsUpper(char c)
2923
return c >= 'A' && c <= 'Z';
2926
static char qToLower(char c)
2928
if (c >= 'A' && c <= 'Z')
2929
return c - 'A' + 'a';
2937
\a f and \a prec have the same meaning as in QString::number(double, char, int).
2942
QString QLocale::toString(double i, char f, int prec) const
2944
QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
2948
flags = QLocalePrivate::CapitalEorX;
2953
form = QLocalePrivate::DFDecimal;
2956
form = QLocalePrivate::DFExponent;
2959
form = QLocalePrivate::DFSignificantDigits;
2965
flags |= QLocalePrivate::ThousandsGroup;
2966
return d->doubleToString(i, prec, form, -1, flags);
2970
\fn QLocale QLocale::c()
2972
Returns a QLocale object initialized to the "C" locale.
2978
Returns a QLocale object initialized to the system locale.
2980
\sa QTextCodec::locale() c()
2983
QLocale QLocale::system()
2987
s = qgetenv("LC_ALL");
2989
s = qgetenv("LC_NUMERIC");
2992
s = QLocalePrivate::systemLocaleName();
2993
return QLocale(QString::fromLocal8Bit(s));
2997
\fn QString QLocale::toString(short i) const
3005
\fn QString QLocale::toString(ushort i) const
3013
\fn QString QLocale::toString(int i) const
3021
\fn QString QLocale::toString(uint i) const
3029
\fn QString QLocale::toString(long i) const
3037
\fn QString QLocale::toString(ulong i) const
3045
\fn QString QLocale::toString(float i, char f = 'g', int prec = 6) const
3049
\a f and \a prec have the same meaning as in QString::number(double, char, int).
3055
static inline char digitToCLocale(QChar zero, QChar d)
3057
if (zero.unicode() <= d.unicode()
3058
&& zero.unicode() + 10 > d.unicode())
3059
return '0' + d.unicode() - zero.unicode();
3061
qWarning("QLocalePrivate::digitToCLocale(): bad digit: row=%d, cell=%d", d.row(), d.cell());
3065
static QString qulltoa(qulonglong l, int base, const QLocalePrivate &locale)
3067
QChar buff[65]; // length of MAX_ULLONG in base 2
3068
QChar *p = buff + 65;
3070
if (base != 10 || locale.zero().unicode() == '0') {
3088
*(--p) = locale.zero().unicode() + c;
3094
return QString(p, 65 - (p - buff));
3097
static QString qlltoa(qlonglong l, int base, const QLocalePrivate &locale)
3099
return qulltoa(l < 0 ? -l : l, base, locale);
3102
enum PrecisionMode {
3103
PMDecimalDigits = 0x01,
3104
PMSignificantDigits = 0x02,
3105
PMChopTrailingZeros = 0x03
3108
static QString &decimalForm(QString &digits, int decpt, uint precision,
3110
bool always_show_decpt,
3111
bool thousands_group,
3112
const QLocalePrivate &locale)
3115
for (int i = 0; i < -decpt; ++i)
3116
digits.prepend(locale.zero());
3119
else if (decpt > digits.length()) {
3120
for (int i = digits.length(); i < decpt; ++i)
3121
digits.append(locale.zero());
3124
if (pm == PMDecimalDigits) {
3125
uint decimal_digits = digits.length() - decpt;
3126
for (uint i = decimal_digits; i < precision; ++i)
3127
digits.append(locale.zero());
3129
else if (pm == PMSignificantDigits) {
3130
for (uint i = digits.length(); i < precision; ++i)
3131
digits.append(locale.zero());
3133
else { // pm == PMChopTrailingZeros
3136
if (always_show_decpt || decpt < digits.length())
3137
digits.insert(decpt, locale.decimal());
3139
if (thousands_group) {
3140
for (int i = decpt - 3; i > 0; i -= 3)
3141
digits.insert(i, locale.group());
3145
digits.prepend(locale.zero());
3150
static QString &exponentForm(QString &digits, int decpt, uint precision,
3152
bool always_show_decpt,
3153
const QLocalePrivate &locale)
3155
int exp = decpt - 1;
3157
if (pm == PMDecimalDigits) {
3158
for (uint i = digits.length(); i < precision + 1; ++i)
3159
digits.append(locale.zero());
3161
else if (pm == PMSignificantDigits) {
3162
for (uint i = digits.length(); i < precision; ++i)
3163
digits.append(locale.zero());
3165
else { // pm == PMChopTrailingZeros
3168
if (always_show_decpt || digits.length() > 1)
3169
digits.insert(1, locale.decimal());
3171
digits.append(locale.exponential());
3172
digits.append(locale.longLongToString(exp, 2, 10,
3173
-1, QLocalePrivate::AlwaysShowSign));
3178
static bool isZero(double d)
3180
uchar *ch = (uchar *)&d;
3181
if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
3182
return !(ch[0] & 0x7F || ch[1] || ch[2] || ch[3] || ch[4] || ch[5] || ch[6] || ch[7]);
3184
return !(ch[7] & 0x7F || ch[6] || ch[5] || ch[4] || ch[3] || ch[2] || ch[1] || ch[0]);
3188
QString QLocalePrivate::doubleToString(double d,
3192
unsigned flags) const
3194
if (precision == -1)
3199
bool negative = false;
3200
bool special_number = false; // nan, +/-inf
3203
// Detect special numbers (nan, +/-inf)
3205
num_str = infinity();
3206
special_number = true;
3208
} else if (qIsNan(d)) {
3210
special_number = true;
3213
// Handle normal numbers
3214
if (!special_number) {
3218
#ifdef QT_QLOCALE_USES_FCVT
3220
if (form == DFDecimal) {
3221
digits = QLatin1String(fcvt(d, precision, &decpt, &sign));
3224
if (form == DFExponent)
3226
else if (form == DFSignificantDigits && pr == 0)
3228
digits = QLatin1String(ecvt(d, pr, &decpt, &sign));
3230
// Chop trailing zeros
3231
if (digits.length() > 0) {
3232
int last_nonzero_idx = digits.length() - 1;
3233
while (last_nonzero_idx > 0
3234
&& digits.unicode()[last_nonzero_idx] == '0')
3236
digits.truncate(last_nonzero_idx + 1);
3243
if (form == DFDecimal)
3248
/* This next bit is a bit quirky. In DFExponent form, the precision
3249
is the number of digits after decpt. So that would suggest using
3250
mode=3 for qdtoa. But qdtoa behaves strangely when mode=3 and
3251
precision=0. So we get around this by using mode=2 and reasoning
3252
that we want precision+1 significant digits, since the decimal
3253
point in this mode is always after the first digit. */
3255
if (form == DFExponent)
3260
digits = QLatin1String(qdtoa(d, mode, pr, &decpt, &sign, &rve, &buff));
3263
#endif // QT_QLOCALE_USES_FCVT
3265
if (zero().unicode() != '0') {
3266
ushort z = zero().unicode() - '0';
3267
for (int i = 0; i < digits.length(); ++i)
3268
reinterpret_cast<ushort *>(digits.data())[i] += z;
3271
bool always_show_decpt = flags & Alternate;
3274
num_str = exponentForm(digits, decpt, precision, PMDecimalDigits,
3275
always_show_decpt, *this);
3279
num_str = decimalForm(digits, decpt, precision, PMDecimalDigits,
3280
always_show_decpt, flags & ThousandsGroup,
3284
case DFSignificantDigits: {
3285
PrecisionMode mode = (flags & Alternate) ?
3286
PMSignificantDigits : PMChopTrailingZeros;
3288
if (decpt != digits.length() && (decpt <= -4 || decpt > precision))
3289
num_str = exponentForm(digits, decpt, precision, mode,
3290
always_show_decpt, *this);
3292
num_str = decimalForm(digits, decpt, precision, mode,
3293
always_show_decpt, flags & ThousandsGroup,
3299
negative = sign != 0 && !isZero(d);
3302
// pad with zeros. LeftAdjusted overrides this flag). Also, we don't
3303
// pad special numbers
3304
if (flags & QLocalePrivate::ZeroPadded
3305
&& !(flags & QLocalePrivate::LeftAdjusted)
3306
&& !special_number) {
3307
int num_pad_chars = width - num_str.length();
3308
// leave space for the sign
3310
|| flags & QLocalePrivate::AlwaysShowSign
3311
|| flags & QLocalePrivate::BlankBeforePositive)
3314
for (int i = 0; i < num_pad_chars; ++i)
3315
num_str.prepend(zero());
3320
num_str.prepend(minus());
3321
else if (flags & QLocalePrivate::AlwaysShowSign)
3322
num_str.prepend(plus());
3323
else if (flags & QLocalePrivate::BlankBeforePositive)
3324
num_str.prepend(QLatin1Char(' '));
3326
if (flags & QLocalePrivate::CapitalEorX)
3327
num_str = num_str.toUpper();
3332
QString QLocalePrivate::longLongToString(qlonglong l, int precision,
3333
int base, int width,
3334
unsigned flags) const
3336
bool precision_not_specified = false;
3337
if (precision == -1) {
3338
precision_not_specified = true;
3342
bool negative = l < 0;
3344
// these are not suported by sprintf for octal and hex
3345
flags &= ~AlwaysShowSign;
3346
flags &= ~BlankBeforePositive;
3347
negative = false; // neither are negative numbers
3352
num_str = qlltoa(l, base, *this);
3354
num_str = qulltoa(l, base, *this);
3356
uint cnt_thousand_sep = 0;
3357
if (flags & ThousandsGroup && base == 10) {
3358
for (int i = num_str.length() - 3; i > 0; i -= 3) {
3359
num_str.insert(i, group());
3364
for (int i = num_str.length()/* - cnt_thousand_sep*/; i < precision; ++i)
3365
num_str.prepend(base == 10 ? zero() : QChar::fromLatin1('0'));
3367
if (flags & Alternate
3369
&& (num_str.isEmpty() || num_str[0].unicode() != QLatin1Char('0')))
3370
num_str.prepend(QLatin1Char('0'));
3372
// LeftAdjusted overrides this flag ZeroPadded. sprintf only padds
3373
// when precision is not specified in the format string
3374
bool zero_padded = flags & ZeroPadded
3375
&& !(flags & LeftAdjusted)
3376
&& precision_not_specified;
3379
int num_pad_chars = width - num_str.length();
3381
// leave space for the sign
3383
|| flags & AlwaysShowSign
3384
|| flags & BlankBeforePositive)
3387
// leave space for optional '0x' in hex form
3389
&& flags & Alternate
3393
for (int i = 0; i < num_pad_chars; ++i)
3394
num_str.prepend(base == 10 ? zero() : QChar::fromLatin1('0'));
3398
&& flags & Alternate
3400
num_str.prepend(QLatin1String("0x"));
3404
num_str.prepend(minus());
3405
else if (flags & AlwaysShowSign)
3406
num_str.prepend(base == 10 ? plus() : QChar::fromLatin1('+'));
3407
else if (flags & BlankBeforePositive)
3408
num_str.prepend(QLatin1Char(' '));
3410
if (flags & CapitalEorX)
3411
num_str = num_str.toUpper();
3416
QString QLocalePrivate::unsLongLongToString(qulonglong l, int precision,
3417
int base, int width,
3418
unsigned flags) const
3420
bool precision_not_specified = false;
3421
if (precision == -1) {
3422
precision_not_specified = true;
3426
QString num_str = qulltoa(l, base, *this);
3428
uint cnt_thousand_sep = 0;
3429
if (flags & ThousandsGroup && base == 10) {
3430
for (int i = num_str.length() - 3; i > 0; i -=3) {
3431
num_str.insert(i, group());
3436
for (int i = num_str.length()/* - cnt_thousand_sep*/; i < precision; ++i)
3437
num_str.prepend(base == 10 ? zero() : QChar::fromLatin1('0'));
3439
if (flags & Alternate
3441
&& (num_str.isEmpty() || num_str[0].unicode() != QLatin1Char('0')))
3442
num_str.prepend(QLatin1Char('0'));
3444
// LeftAdjusted overrides this flag ZeroPadded. sprintf only padds
3445
// when precision is not specified in the format string
3446
bool zero_padded = flags & ZeroPadded
3447
&& !(flags & LeftAdjusted)
3448
&& precision_not_specified;
3451
int num_pad_chars = width - num_str.length();
3453
// leave space for optional '0x' in hex form
3455
&& flags & Alternate
3459
for (int i = 0; i < num_pad_chars; ++i)
3460
num_str.prepend(base == 10 ? zero() : QChar::fromLatin1('0'));
3464
&& flags & Alternate
3466
num_str.prepend(QLatin1String("0x"));
3468
if (flags & CapitalEorX)
3469
num_str = num_str.toUpper();
3474
// Removes thousand-group separators in "C" locale.
3475
static bool removeGroupSeparators(QLocalePrivate::CharBuff *num)
3477
int group_cnt = 0; // counts number of group chars
3480
char *data = num->data();
3481
int l = qstrlen(data);
3483
// Find the decimal point and check if there are any group chars
3485
for (; i < l; ++i) {
3489
if (i == 0 || data[i - 1] < '0' || data[i - 1] > '9')
3491
if (i == l || data[i + 1] < '0' || data[i + 1] > '9')
3495
else if (c == '.') {
3496
// Fail if more than one decimal points
3497
if (decpt_idx != -1)
3500
} else if (c == 'e' || c == 'E') {
3501
// an 'e' or 'E' - if we have not encountered a decimal
3502
// point, this is where it "is".
3503
if (decpt_idx == -1)
3508
// If no group chars, we're done
3512
// No decimal point means that it "is" at the end of the string
3513
if (decpt_idx == -1)
3517
while (i < l && group_cnt > 0) {
3521
// Don't allow group chars after the decimal point
3525
// Check that it is placed correctly relative to the decpt
3526
if ((decpt_idx - i) % 4 != 0)
3530
memmove(data + i, data + i + 1, l - i - 1);
3536
// Check that we are not missing a separator
3537
if (i < decpt_idx && (decpt_idx - i) % 4 == 0)
3547
Converts a number in locale to its representation in the C locale.
3548
Only has to guarantee that a string that is a correct representation of
3549
a number will be converted. If junk is passed in, junk will be passed
3550
out and the error will be detected during the actual conversion to a
3551
number. We can't detect junk here, since we don't even know the base
3554
bool QLocalePrivate::numberToCLocale(const QString &num,
3555
GroupSeparatorMode group_sep_mode,
3556
CharBuff *result) const
3558
const QChar *uc = num.unicode();
3559
int l = num.length();
3563
while (idx < l && uc[idx].isSpace())
3569
const QChar &in = uc[idx];
3572
if (in.unicode() >= zero().unicode() && in.unicode() < zero().unicode() + 10)
3573
out = digitToCLocale(zero(), in);
3574
else if (in == plus())
3576
else if (in == minus())
3578
else if (in == decimal())
3580
else if (in == group())
3582
// In several languages group() is the char 0xA0, which looks like a space.
3583
// People use a regular space instead of it and complain it doesn't work.
3584
else if (group().unicode() == 0xA0 && in.unicode() == ' ')
3586
else if (in == exponential() || in == exponential().toUpper())
3588
else if (in == list())
3590
else if (in == percent())
3592
else if (in.unicode() >= 'A' && in.unicode() <= 'Z')
3593
out = in.toLower().toLatin1();
3594
else if (in.unicode() >= 'a' && in.unicode() <= 'z')
3595
out = in.toLatin1();
3599
result->append(out);
3604
// Check trailing whitespace
3605
for (; idx < l; ++idx) {
3606
if (!uc[idx].isSpace())
3610
result->append('\0');
3613
if (group_sep_mode == ParseGroupSeparators
3614
&& !removeGroupSeparators(result))
3621
double QLocalePrivate::stringToDouble(const QString &number, bool *ok,
3622
GroupSeparatorMode group_sep_mode) const
3625
if (!numberToCLocale(number, group_sep_mode, &buff)) {
3631
return bytearrayToDouble(buff.constData(), ok);
3634
qlonglong QLocalePrivate::stringToLongLong(const QString &number, int base,
3635
bool *ok, GroupSeparatorMode group_sep_mode) const
3638
if (!numberToCLocale(number, group_sep_mode, &buff)) {
3644
return bytearrayToLongLong(buff.constData(), base, ok);
3647
qulonglong QLocalePrivate::stringToUnsLongLong(const QString &number, int base,
3648
bool *ok, GroupSeparatorMode group_sep_mode) const
3651
if (!numberToCLocale(number, group_sep_mode, &buff)) {
3657
return bytearrayToUnsLongLong(buff.constData(), base, ok);
3661
double QLocalePrivate::bytearrayToDouble(const char *num, bool *ok)
3666
if (qstrcmp(num, "nan") == 0)
3669
if (qstrcmp(num, "+inf") == 0 || qstrcmp(num, "inf") == 0)
3672
if (qstrcmp(num, "-inf") == 0)
3676
#ifdef QT_QLOCALE_USES_FCVT
3678
double d = strtod(num, &endptr);
3682
double d = qstrtod(num, &endptr, &_ok);
3685
if (!_ok || *endptr != '\0') {
3694
qlonglong QLocalePrivate::bytearrayToLongLong(const char *num, int base, bool *ok)
3698
qlonglong l = qstrtoll(num, &endptr, base, &_ok);
3700
if (!_ok || *endptr != '\0') {
3711
qulonglong QLocalePrivate::bytearrayToUnsLongLong(const char *num, int base, bool *ok)
3715
qulonglong l = qstrtoull(num, &endptr, base, &_ok);
3717
if (!_ok || *endptr != '\0') {
3729
* Copyright (c) 1992, 1993
3730
* The Regents of the University of California. All rights reserved.
3732
* Redistribution and use in source and binary forms, with or without
3733
* modification, are permitted provided that the following conditions
3735
* 1. Redistributions of source code must retain the above copyright
3736
* notice, this list of conditions and the following disclaimer.
3737
* 2. Redistributions in binary form must reproduce the above copyright
3738
* notice, this list of conditions and the following disclaimer in the
3739
* documentation and/or other materials provided with the distribution.
3740
* 3. All advertising materials mentioning features or use of this software
3741
* must display the following acknowledgement:
3742
* This product includes software developed by the University of
3743
* California, Berkeley and its contributors.
3744
* 4. Neither the name of the University nor the names of its contributors
3745
* may be used to endorse or promote products derived from this software
3746
* without specific prior written permission.
3748
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3749
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3750
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3751
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3752
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3753
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3754
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3755
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3756
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3757
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3761
// static char sccsid[] = "@(#)strtouq.c 8.1 (Berkeley) 6/4/93";
3762
// "$FreeBSD: src/lib/libc/stdlib/strtoull.c,v 1.5.2.1 2001/03/02 09:45:20 obrien Exp $";
3765
* Convert a string to an unsigned long long integer.
3767
* Ignores `locale' stuff. Assumes that the upper and lower case
3768
* alphabets and digits are each contiguous.
3770
static qulonglong qstrtoull(const char *nptr, const char **endptr, register int base, bool *ok)
3772
register const char *s = nptr;
3773
register qulonglong acc;
3774
register unsigned char c;
3775
register qulonglong qbase, cutoff;
3776
register int neg, any, cutlim;
3782
* See strtoq for comments as to the logic used.
3787
} while (isspace(c));
3799
if ((base == 0 || base == 16) &&
3800
c == '0' && (*s == 'x' || *s == 'X')) {
3806
base = c == '0' ? 8 : 10;
3807
qbase = unsigned(base);
3808
cutoff = qulonglong(ULLONG_MAX) / qbase;
3809
cutlim = qulonglong(ULLONG_MAX) % qbase;
3810
for (acc = 0, any = 0;; c = *s++) {
3815
else if (isalpha(c))
3816
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
3821
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
3837
*endptr = (any ? s - 1 : nptr);
3842
// "$FreeBSD: src/lib/libc/stdlib/strtoll.c,v 1.5.2.1 2001/03/02 09:45:20 obrien Exp $";
3846
* Convert a string to a long long integer.
3848
* Ignores `locale' stuff. Assumes that the upper and lower case
3849
* alphabets and digits are each contiguous.
3851
static qlonglong qstrtoll(const char *nptr, const char **endptr, register int base, bool *ok)
3853
register const char *s;
3854
register qulonglong acc;
3855
register unsigned char c;
3856
register qulonglong qbase, cutoff;
3857
register int neg, any, cutlim;
3863
* Skip white space and pick up leading +/- sign if any.
3864
* If base is 0, allow 0x for hex and 0 for octal, else
3865
* assume decimal; if base is already 16, allow 0x.
3870
} while (isspace(c));
3879
if ((base == 0 || base == 16) &&
3880
c == '0' && (*s == 'x' || *s == 'X')) {
3886
base = c == '0' ? 8 : 10;
3889
* Compute the cutoff value between legal numbers and illegal
3890
* numbers. That is the largest legal value, divided by the
3891
* base. An input number that is greater than this value, if
3892
* followed by a legal input character, is too big. One that
3893
* is equal to this value may be valid or not; the limit
3894
* between valid and invalid numbers is then based on the last
3895
* digit. For instance, if the range for quads is
3896
* [-9223372036854775808..9223372036854775807] and the input base
3897
* is 10, cutoff will be set to 922337203685477580 and cutlim to
3898
* either 7 (neg==0) or 8 (neg==1), meaning that if we have
3899
* accumulated a value > 922337203685477580, or equal but the
3900
* next digit is > 7 (or 8), the number is too big, and we will
3901
* return a range error.
3903
* Set any if any `digits' consumed; make it negative to indicate
3906
qbase = unsigned(base);
3907
cutoff = neg ? qulonglong(0-(LLONG_MIN + LLONG_MAX)) + LLONG_MAX : LLONG_MAX;
3908
cutlim = cutoff % qbase;
3910
for (acc = 0, any = 0;; c = *s++) {
3915
else if (isalpha(c))
3916
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
3921
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
3930
acc = neg ? LLONG_MIN : LLONG_MAX;
3937
*endptr = (any ? s - 1 : nptr);
3941
#ifndef QT_QLOCALE_USES_FCVT
3943
/* From: NetBSD: strtod.c,v 1.26 1998/02/03 18:44:21 perry Exp */
3944
/* $FreeBSD: src/lib/libc/stdlib/netbsd_strtod.c,v 1.2.2.2 2001/03/02 17:14:15 tegge Exp $ */
3946
/* Please send bug reports to
3948
AT&T Bell Laboratories, Room 2C-463
3950
Murray Hill, NJ 07974-2070
3952
dmg@research.att.com or research!dmg
3955
/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
3957
* This strtod returns a nearest machine number to the input decimal
3958
* string (or sets errno to ERANGE). With IEEE arithmetic, ties are
3959
* broken by the IEEE round-even rule. Otherwise ties are broken by
3960
* biased rounding (add half and chop).
3962
* Inspired loosely by William D. Clinger's paper "How to Read Floating
3963
* Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
3967
* 1. We only require IEEE, IBM, or VAX double-precision
3968
* arithmetic (not IEEE double-extended).
3969
* 2. We get by with floating-point arithmetic in a case that
3970
* Clinger missed -- when we're computing d * 10^n
3971
* for a small integer d and the integer n is not too
3972
* much larger than 22 (the maximum integer k for which
3973
* we can represent 10^k exactly), we may be able to
3974
* compute (d*10^k) * 10^(e-k) with just one roundoff.
3975
* 3. Rather than a bit-at-a-time adjustment of the binary
3976
* result in the hard case, we use floating-point
3977
* arithmetic to determine the adjustment to within
3978
* one bit; only in really hard cases do we need to
3979
* compute a second residual.
3980
* 4. Because of 3., we don't need a large table of powers of 10
3981
* for ten-to-e (just some small tables, e.g. of 10^k
3982
* for 0 <= k <= 22).
3986
* #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
3987
* significant byte has the lowest address.
3988
* #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
3989
* significant byte has the lowest address.
3990
* #define Long int on machines with 32-bit ints and 64-bit longs.
3991
* #define Sudden_Underflow for IEEE-format machines without gradual
3992
* underflow (i.e., that flush to zero on underflow).
3993
* #define IBM for IBM mainframe-style floating-point arithmetic.
3994
* #define VAX for VAX-style floating-point arithmetic.
3995
* #define Unsigned_Shifts if >> does treats its left operand as unsigned.
3996
* #define No_leftright to omit left-right logic in fast floating-point
3997
* computation of dtoa.
3998
* #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
3999
* #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
4000
* that use extended-precision instructions to compute rounded
4001
* products and quotients) with IBM.
4002
* #define ROUND_BIASED for IEEE-format with biased rounding.
4003
* #define Inaccurate_Divide for IEEE-format with correctly rounded
4004
* products but inaccurate quotients, e.g., for Intel i860.
4005
* #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
4006
* integer arithmetic. Whether this speeds things up or slows things
4007
* down depends on the machine and the number being converted.
4008
* #define KR_headers for old-style C function headers.
4009
* #define Bad_float_h if your system lacks a float.h or if it does not
4010
* define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
4011
* FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
4012
* #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
4013
* if memory is available and otherwise does something you deem
4014
* appropriate. If MALLOC is undefined, malloc will be invoked
4015
* directly -- and assumed always to succeed.
4018
#if defined(LIBC_SCCS) && !defined(lint)
4019
__RCSID("$NetBSD: strtod.c,v 1.26 1998/02/03 18:44:21 perry Exp $");
4020
#endif /* LIBC_SCCS and not lint */
4023
#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
4024
defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
4025
defined(__powerpc__) || defined(Q_OS_WIN) || defined(Q_OS_DARWIN) || defined(Q_OS_MAC) || \
4026
defined(mips) || defined(Q_OS_AIX) || defined(Q_OS_SOLARIS)
4027
# define IEEE_BIG_OR_LITTLE_ENDIAN 1
4031
// *All* of our architectures have IEEE arithmetic, don't they?
4032
#define IEEE_BIG_OR_LITTLE_ENDIAN 1
4036
* Although the CPU is little endian the FP has different
4037
* byte and word endianness. The byte order is still little endian
4038
* but the word order is big endian.
4040
#define IEEE_BIG_OR_LITTLE_ENDIAN
4048
#define ULong quint32
4050
#define MALLOC malloc
4052
#ifdef BSD_QDTOA_DEBUG
4054
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
4057
#ifdef Unsigned_Shifts
4058
#define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
4060
#define Sign_Extend(a,b) /*no-op*/
4063
#if (defined(IEEE_BIG_OR_LITTLE_ENDIAN) + defined(VAX) + defined(IBM)) != 1
4064
#error Exactly one of IEEE_BIG_OR_LITTLE_ENDIAN, VAX, or IBM should be defined.
4067
inline ULong getWord0(const NEEDS_VOLATILE double x)
4069
const NEEDS_VOLATILE uchar *ptr = reinterpret_cast<const NEEDS_VOLATILE uchar *>(&x);
4070
if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
4071
return (ptr[0]<<24) + (ptr[1]<<16) + (ptr[2]<<8) + ptr[3];
4073
return (ptr[7]<<24) + (ptr[6]<<16) + (ptr[5]<<8) + ptr[4];
4077
inline void setWord0(NEEDS_VOLATILE double *x, ULong l)
4079
NEEDS_VOLATILE uchar *ptr = reinterpret_cast<NEEDS_VOLATILE uchar *>(x);
4080
if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
4081
ptr[0] = uchar(l>>24);
4082
ptr[1] = uchar(l>>16);
4083
ptr[2] = uchar(l>>8);
4086
ptr[7] = uchar(l>>24);
4087
ptr[6] = uchar(l>>16);
4088
ptr[5] = uchar(l>>8);
4093
inline ULong getWord1(const NEEDS_VOLATILE double x)
4095
const NEEDS_VOLATILE uchar *ptr = reinterpret_cast<const NEEDS_VOLATILE uchar *>(&x);
4096
if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
4097
return (ptr[4]<<24) + (ptr[5]<<16) + (ptr[6]<<8) + ptr[7];
4099
return (ptr[3]<<24) + (ptr[2]<<16) + (ptr[1]<<8) + ptr[0];
4102
inline void setWord1(NEEDS_VOLATILE double *x, ULong l)
4104
NEEDS_VOLATILE uchar *ptr = reinterpret_cast<uchar NEEDS_VOLATILE *>(x);
4105
if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
4106
ptr[4] = uchar(l>>24);
4107
ptr[5] = uchar(l>>16);
4108
ptr[6] = uchar(l>>8);
4111
ptr[3] = uchar(l>>24);
4112
ptr[2] = uchar(l>>16);
4113
ptr[1] = uchar(l>>8);
4118
static inline void Storeinc(ULong *&a, const ULong &b, const ULong &c)
4121
*a = (ushort(b) << 16) | ushort(c);
4125
/* #define P DBL_MANT_DIG */
4126
/* Ten_pmax = floor(P*log(2)/log(5)) */
4127
/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
4128
/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
4129
/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
4131
#if defined(IEEE_BIG_OR_LITTLE_ENDIAN)
4132
#define Exp_shift 20
4133
#define Exp_shift1 20
4134
#define Exp_msk1 0x100000
4135
#define Exp_msk11 0x100000
4136
#define Exp_mask 0x7ff00000
4140
#define Emin (-1022)
4141
#define Exp_1 0x3ff00000
4142
#define Exp_11 0x3ff00000
4144
#define Frac_mask 0xfffff
4145
#define Frac_mask1 0xfffff
4148
#define Bndry_mask 0xfffff
4149
#define Bndry_mask1 0xfffff
4151
#define Sign_bit 0x80000000
4155
#define Quick_max 14
4157
#define Infinite(x) (getWord0(x) == 0x7ff00000) /* sufficient test for here */
4159
#undef Sudden_Underflow
4160
#define Sudden_Underflow
4162
#define Exp_shift 24
4163
#define Exp_shift1 24
4164
#define Exp_msk1 0x1000000
4165
#define Exp_msk11 0x1000000
4166
#define Exp_mask 0x7f000000
4169
#define Exp_1 0x41000000
4170
#define Exp_11 0x41000000
4171
#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
4172
#define Frac_mask 0xffffff
4173
#define Frac_mask1 0xffffff
4176
#define Bndry_mask 0xefffff
4177
#define Bndry_mask1 0xffffff
4179
#define Sign_bit 0x80000000
4181
#define Tiny0 0x100000
4183
#define Quick_max 14
4186
#define Exp_shift 23
4187
#define Exp_shift1 7
4188
#define Exp_msk1 0x80
4189
#define Exp_msk11 0x800000
4190
#define Exp_mask 0x7f80
4193
#define Exp_1 0x40800000
4194
#define Exp_11 0x4080
4196
#define Frac_mask 0x7fffff
4197
#define Frac_mask1 0xffff007f
4200
#define Bndry_mask 0xffff007f
4201
#define Bndry_mask1 0xffff007f
4203
#define Sign_bit 0x8000
4207
#define Quick_max 15
4213
#define ROUND_BIASED
4217
#define rounded_product(a,b) a = rnd_prod(a, b)
4218
#define rounded_quotient(a,b) a = rnd_quot(a, b)
4219
extern double rnd_prod(double, double), rnd_quot(double, double);
4221
#define rounded_product(a,b) a *= b
4222
#define rounded_quotient(a,b) a /= b
4225
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
4226
#define Big1 0xffffffff
4229
/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
4230
* This makes some inner loops simpler and sometimes saves work
4231
* during multiplications, but it often seems to make things slightly
4232
* slower. Hence the default is now to store 32 bits per Long.
4243
struct Bigint *next;
4244
int k, maxwds, sign, wds;
4248
typedef struct Bigint Bigint;
4250
static Bigint *Balloc(int k)
4256
rv = static_cast<Bigint *>(MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long)));
4259
rv->sign = rv->wds = 0;
4263
static void Bfree(Bigint *v)
4268
#define Bcopy(x,y) memcpy(reinterpret_cast<char *>(&x->sign), reinterpret_cast<char *>(&y->sign), \
4269
y->wds*sizeof(Long) + 2*sizeof(int))
4271
/* multiply by m and add a */
4272
static Bigint *multadd(Bigint *b, int m, int a)
4287
y = (xi & 0xffff) * m + a;
4288
z = (xi >> 16) * m + (y >> 16);
4290
*x++ = (z << 16) + (y & 0xffff);
4299
if (wds >= b->maxwds) {
4300
b1 = Balloc(b->k+1);
4311
static Bigint *s2b(const char *s, int nd0, int nd, ULong y9)
4318
for(k = 0, y = 1; x > y; y <<= 1, k++) ;
4325
b->x[0] = y9 & 0xffff;
4326
b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
4332
do b = multadd(b, 10, *s++ - '0');
4339
b = multadd(b, 10, *s++ - '0');
4343
static int hi0bits(ULong x)
4347
if (!(x & 0xffff0000)) {
4351
if (!(x & 0xff000000)) {
4355
if (!(x & 0xf0000000)) {
4359
if (!(x & 0xc0000000)) {
4363
if (!(x & 0x80000000)) {
4365
if (!(x & 0x40000000))
4371
static int lo0bits(ULong *y)
4387
if (!(x & 0xffff)) {
4413
static Bigint *i2b(int i)
4423
static Bigint *mult(Bigint *a, Bigint *b)
4428
ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
4433
if (a->wds < b->wds) {
4445
for(x = c->x, xa = x + wc; x < xa; x++)
4453
for(; xb < xbe; xb++, xc0++) {
4454
if ((y = *xb & 0xffff) != 0) {
4459
z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
4461
z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
4463
Storeinc(xc, z2, z);
4468
if ((y = *xb >> 16) != 0) {
4474
z = (*x & 0xffff) * y + (*xc >> 16) + carry;
4476
Storeinc(xc, z, z2);
4477
z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
4485
for(; xb < xbe; xc0++) {
4491
z = *x++ * y + *xc + carry;
4500
for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
4507
static Bigint *pow5mult(Bigint *b, int k)
4509
Bigint *b1, *p5, *p51;
4511
static const int p05[3] = { 5, 25, 125 };
4513
if ((i = k & 3) != 0)
4514
#if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
4516
// work around a bug on 64 bit IRIX gcc
4517
int *p = (int *) p05;
4518
b = multadd(b, p[i-1], 0);
4521
b = multadd(b, p05[i-1], 0);
4528
p5 = p5s = i2b(625);
4539
if (!(p51 = p5->next)) {
4540
p51 = p5->next = mult(p5,p5);
4548
static Bigint *lshift(Bigint *b, int k)
4552
ULong *x, *x1, *xe, z;
4560
n1 = n + b->wds + 1;
4561
for(i = b->maxwds; n1 > i; i <<= 1)
4565
for(i = 0; i < n; i++)
4574
*x1++ = *x << k | z;
4586
*x1++ = *x << k & 0xffff | z;
4602
static int cmp(Bigint *a, Bigint *b)
4604
ULong *xa, *xa0, *xb, *xb0;
4609
#ifdef BSD_QDTOA_DEBUG
4610
if (i > 1 && !a->x[i-1])
4611
Bug("cmp called with a->x[a->wds-1] == 0");
4612
if (j > 1 && !b->x[j-1])
4613
Bug("cmp called with b->x[b->wds-1] == 0");
4623
return *xa < *xb ? -1 : 1;
4630
static Bigint *diff(Bigint *a, Bigint *b)
4634
Long borrow, y; /* We need signed shifts here. */
4635
ULong *xa, *xae, *xb, *xbe, *xc;
4667
y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
4669
Sign_Extend(borrow, y);
4670
z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
4672
Sign_Extend(borrow, z);
4677
y = (*xa & 0xffff) + borrow;
4679
Sign_Extend(borrow, y);
4680
z = (*xa++ >> 16) + borrow;
4682
Sign_Extend(borrow, z);
4687
y = *xa++ - *xb++ + borrow;
4689
Sign_Extend(borrow, y);
4696
Sign_Extend(borrow, y);
4706
static double ulp(double x)
4711
L = (getWord0(x) & Exp_mask) - (P-1)*Exp_msk1;
4712
#ifndef Sudden_Underflow
4720
#ifndef Sudden_Underflow
4723
L = -L >> Exp_shift;
4724
if (L < Exp_shift) {
4725
setWord0(&a, 0x80000 >> L);
4731
setWord1(&a, L >= 31 ? 1U : 1U << (31 - L));
4738
static double b2d(Bigint *a, int *e)
4740
ULong *xa, *xa0, w, y, z;
4747
#ifdef BSD_QDTOA_DEBUG
4748
if (!y) Bug("zero y in b2d");
4754
setWord0(&d, Exp_1 | y >> (Ebits - k));
4755
w = xa > xa0 ? *--xa : 0;
4756
setWord1(&d, y << ((32-Ebits) + k) | w >> (Ebits - k));
4759
z = xa > xa0 ? *--xa : 0;
4761
setWord0(&d, Exp_1 | y << k | z >> (32 - k));
4762
y = xa > xa0 ? *--xa : 0;
4763
setWord1(&d, z << k | y >> (32 - k));
4766
setWord0(&d, Exp_1 | y);
4770
if (k < Ebits + 16) {
4771
z = xa > xa0 ? *--xa : 0;
4772
setWord0(&d, Exp_1 | y << k - Ebits | z >> Ebits + 16 - k);
4773
w = xa > xa0 ? *--xa : 0;
4774
y = xa > xa0 ? *--xa : 0;
4775
setWord1(&d, z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k);
4778
z = xa > xa0 ? *--xa : 0;
4779
w = xa > xa0 ? *--xa : 0;
4781
setWord0(&d, Exp_1 | y << k + 16 | z << k | w >> 16 - k);
4782
y = xa > xa0 ? *--xa : 0;
4783
setWord1(&d, w << k + 16 | y << k);
4789
static Bigint *d2b(double d, int *e, int *bits)
4802
z = getWord0(d) & Frac_mask;
4803
setWord0(&d, getWord0(d) & 0x7fffffff); /* clear sign bit, which we ignore */
4804
#ifdef Sudden_Underflow
4805
de = (int)(getWord0(d) >> Exp_shift);
4810
if ((de = int(getWord0(d) >> Exp_shift)) != 0)
4814
if ((y = getWord1(d)) != 0) {
4815
if ((k = lo0bits(&y)) != 0) {
4816
x[0] = y | z << (32 - k);
4821
i = b->wds = (x[1] = z) ? 2 : 1;
4824
#ifdef BSD_QDTOA_DEBUG
4826
Bug("Zero passed to d2b");
4834
if (y = getWord1(d)) {
4835
if (k = lo0bits(&y))
4837
x[0] = y | z << 32 - k & 0xffff;
4838
x[1] = z >> k - 16 & 0xffff;
4844
x[1] = y >> 16 | z << 16 - k & 0xffff;
4845
x[2] = z >> k & 0xffff;
4858
#ifdef BSD_QDTOA_DEBUG
4860
Bug("Zero passed to d2b");
4878
#ifndef Sudden_Underflow
4882
*e = (de - Bias - (P-1) << 2) + k;
4883
*bits = 4*P + 8 - k - hi0bits(getWord0(d) & Frac_mask);
4885
*e = de - Bias - (P-1) + k;
4888
#ifndef Sudden_Underflow
4891
*e = de - Bias - (P-1) + 1 + k;
4893
*bits = 32*i - hi0bits(x[i-1]);
4895
*bits = (i+2)*16 - hi0bits(x[i]);
4902
static double ratio(Bigint *a, Bigint *b)
4910
k = ka - kb + 32*(a->wds - b->wds);
4912
k = ka - kb + 16*(a->wds - b->wds);
4916
setWord0(&da, getWord0(da) + (k >> 2)*Exp_msk1);
4922
setWord0(&db, getWord0(db) + (k >> 2)*Exp_msk1);
4928
setWord0(&da, getWord0(da) + k*Exp_msk1);
4931
setWord0(&db, getWord0(db) + k*Exp_msk1);
4937
static const double tens[] = {
4938
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
4939
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
4947
static const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
4948
static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
4952
static const double bigtens[] = { 1e16, 1e32, 1e64 };
4953
static const double tinytens[] = { 1e-16, 1e-32, 1e-64 };
4956
static const double bigtens[] = { 1e16, 1e32 };
4957
static const double tinytens[] = { 1e-16, 1e-32 };
4963
The pre-release gcc3.3 shipped with SuSE 8.2 has a bug which causes
4964
the comparison 1e-100 == 0.0 to return true. As a workaround, we
4965
compare it to a global variable containing 0.0, which produces
4966
correct assembler output.
4968
### consider detecting the broken compilers and using the static
4969
### double for these, and use a #define for all working compilers
4971
static double g_double_zero = 0.0;
4973
static double qstrtod(const char *s00, const char **se, bool *ok)
4975
int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
4976
e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
4977
const char *s, *s0, *s1;
4978
double aadj, aadj1, adj, rv, rv0;
4982
Bigint *bb = NULL, *bd = NULL, *bs = NULL, *delta = NULL;/* pacify gcc */
4986
const char decimal_point = localeconv()->decimal_point[0];
4988
const char decimal_point = '.';
4993
const char decimal_point = '.';
4995
sign = nz0 = nz = 0;
4999
for(s = s00; isspace(uchar(*s)); s++)
5005
} else if (*s == '+') {
5016
while(*++s == '0') ;
5022
for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
5028
if (c == decimal_point) {
5031
for(; c == '0'; c = *++s)
5033
if (c > '0' && c <= '9') {
5041
for(; c >= '0' && c <= '9'; c = *++s) {
5046
for(i = 1; i < nz; i++)
5049
else if (nd <= DBL_DIG + 1)
5053
else if (nd <= DBL_DIG + 1)
5061
if (c == 'e' || c == 'E') {
5062
if (!nd && !nz && !nz0) {
5074
if (c >= '0' && c <= '9') {
5077
if (c > '0' && c <= '9') {
5080
while((c = *++s) >= '0' && c <= '9')
5082
if (s - s1 > 8 || L > 19999)
5083
/* Avoid confusion from exponents
5084
* so large that e might overflow.
5086
e = 19999; /* safe for 16 bit ints */
5105
/* Now we have nd0 digits, starting at s0, followed by a
5106
* decimal point, followed by nd-nd0 digits. The number we're
5107
* after is the integer represented by those digits times
5112
k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
5115
#if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
5117
// work around a bug on 64 bit IRIX gcc
5118
double *t = (double *) tens;
5119
rv = t[k - 9] * rv + z;
5122
rv = tens[k - 9] * rv + z;
5127
#ifndef RND_PRODQUOT
5134
if (e <= Ten_pmax) {
5136
goto vax_ovfl_check;
5138
/* rv = */ rounded_product(rv, tens[e]);
5143
if (e <= Ten_pmax + i) {
5144
/* A fancier test would sometimes let us do
5145
* this for larger i values.
5150
/* VAX exponent range is so narrow we must
5151
* worry about overflow here...
5154
setWord0(&rv, getWord0(rv) - P*Exp_msk1);
5155
/* rv = */ rounded_product(rv, tens[e]);
5156
if ((getWord0(rv) & Exp_mask)
5157
> Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
5159
setWord0(&rv, getWord0(rv) + P*Exp_msk1);
5161
/* rv = */ rounded_product(rv, tens[e]);
5166
#ifndef Inaccurate_Divide
5167
else if (e >= -Ten_pmax) {
5168
/* rv = */ rounded_quotient(rv, tens[-e]);
5175
/* Get starting approximation = rv * 10**e1 */
5178
if ((i = e1 & 15) != 0)
5181
if (e1 > DBL_MAX_10_EXP) {
5189
/* Can't trust HUGE_VAL */
5191
setWord0(&rv, Exp_mask);
5194
setWord0(&rv, Big0);
5195
setWord1(&rv, Big1);
5203
for(j = 0; e1 > 1; j++, e1 >>= 1)
5206
/* The last multiplication could overflow. */
5207
setWord0(&rv, getWord0(rv) - P*Exp_msk1);
5209
if ((z = getWord0(rv) & Exp_mask)
5210
> Exp_msk1*(DBL_MAX_EXP+Bias-P))
5212
if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
5213
/* set to largest number */
5214
/* (Can't trust DBL_MAX) */
5215
setWord0(&rv, Big0);
5216
setWord1(&rv, Big1);
5219
setWord0(&rv, getWord0(rv) + P*Exp_msk1);
5226
if ((i = e1 & 15) != 0)
5230
if (e1 >= 1 << n_bigtens)
5232
for(j = 0; e1 > 1; j++, e1 >>= 1)
5235
/* The last multiplication could underflow. */
5238
if (rv == g_double_zero)
5242
if (rv == g_double_zero)
5253
setWord0(&rv, Tiny0);
5254
setWord1(&rv, Tiny1);
5255
/* The refinement below will clean
5256
* this approximation up.
5262
/* Now the hard part -- adjusting rv to the correct value.*/
5264
/* Put digits into bd: true value = bd * 10^e */
5266
bd0 = s2b(s0, nd0, nd, y);
5269
bd = Balloc(bd0->k);
5271
bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
5287
#ifdef Sudden_Underflow
5289
j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
5294
i = bbe + bbbits - 1; /* logb(rv) */
5295
if (i < Emin) /* denormal */
5302
i = bb2 < bd2 ? bb2 : bd2;
5311
bs = pow5mult(bs, bb5);
5317
bb = lshift(bb, bb2);
5319
bd = pow5mult(bd, bd5);
5321
bd = lshift(bd, bd2);
5323
bs = lshift(bs, bs2);
5324
delta = diff(bb, bd);
5325
dsign = delta->sign;
5329
/* Error is less than half an ulp -- check for
5330
* special case of mantissa a power of two.
5332
if (dsign || getWord1(rv) || getWord0(rv) & Bndry_mask)
5334
delta = lshift(delta,Log2P);
5335
if (cmp(delta, bs) > 0)
5340
/* exactly half-way between */
5342
if ((getWord0(rv) & Bndry_mask1) == Bndry_mask1
5343
&& getWord1(rv) == 0xffffffff) {
5344
/*boundary case -- increment exponent*/
5345
setWord0(&rv, (getWord0(rv) & Exp_mask)
5355
else if (!(getWord0(rv) & Bndry_mask) && !getWord1(rv)) {
5357
/* boundary case -- decrement exponent */
5358
#ifdef Sudden_Underflow
5359
L = getWord0(rv) & Exp_mask;
5368
L = (getWord0(rv) & Exp_mask) - Exp_msk1;
5370
setWord0(&rv, L | Bndry_mask1);
5371
setWord1(&rv, 0xffffffff);
5378
#ifndef ROUND_BIASED
5379
if (!(getWord1(rv) & LSB))
5384
#ifndef ROUND_BIASED
5387
#ifndef Sudden_Underflow
5388
if (rv == g_double_zero)
5395
if ((aadj = ratio(delta, bs)) <= 2.) {
5398
else if (getWord1(rv) || getWord0(rv) & Bndry_mask) {
5399
#ifndef Sudden_Underflow
5400
if (getWord1(rv) == Tiny1 && !getWord0(rv))
5407
/* special case -- power of FLT_RADIX to be */
5408
/* rounded down... */
5410
if (aadj < 2./FLT_RADIX)
5411
aadj = 1./FLT_RADIX;
5419
aadj1 = dsign ? aadj : -aadj;
5420
#ifdef Check_FLT_ROUNDS
5421
switch(FLT_ROUNDS) {
5422
case 2: /* towards +infinity */
5425
case 0: /* towards 0 */
5426
case 3: /* towards -infinity */
5430
if (FLT_ROUNDS == 0)
5434
y = getWord0(rv) & Exp_mask;
5436
/* Check for overflow */
5438
if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
5440
setWord0(&rv, getWord0(rv) - P*Exp_msk1);
5441
adj = aadj1 * ulp(rv);
5443
if ((getWord0(rv) & Exp_mask) >=
5444
Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
5445
if (getWord0(rv0) == Big0 && getWord1(rv0) == Big1)
5447
setWord0(&rv, Big0);
5448
setWord1(&rv, Big1);
5452
setWord0(&rv, getWord0(rv) + P*Exp_msk1);
5455
#ifdef Sudden_Underflow
5456
if ((getWord0(rv) & Exp_mask) <= P*Exp_msk1) {
5458
setWord0(&rv, getWord0(rv) + P*Exp_msk1);
5459
adj = aadj1 * ulp(rv);
5462
if ((getWord0(rv) & Exp_mask) < P*Exp_msk1)
5464
if ((getWord0(rv) & Exp_mask) <= P*Exp_msk1)
5467
if (getWord0(rv0) == Tiny0
5468
&& getWord1(rv0) == Tiny1)
5470
setWord0(&rv, Tiny0);
5471
setWord1(&rv, Tiny1);
5475
setWord0(&rv, getWord0(rv) - P*Exp_msk1);
5478
adj = aadj1 * ulp(rv);
5482
/* Compute adj so that the IEEE rounding rules will
5483
* correctly round rv + adj in some half-way cases.
5484
* If rv * ulp(rv) is denormalized (i.e.,
5485
* y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
5486
* trouble from bits lost to denormalization;
5487
* example: 1.2e-307 .
5489
if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
5490
aadj1 = int(aadj + 0.5);
5494
adj = aadj1 * ulp(rv);
5498
z = getWord0(rv) & Exp_mask;
5500
/* Can we stop now? */
5503
/* The tolerances below are conservative. */
5504
if (dsign || getWord1(rv) || getWord0(rv) & Bndry_mask) {
5505
if (aadj < .4999999 || aadj > .5000001)
5508
else if (aadj < .4999999/FLT_RADIX)
5526
return sign ? -rv : rv;
5529
static int quorem(Bigint *b, Bigint *S)
5534
ULong *bx, *bxe, *sx, *sxe;
5541
#ifdef BSD_QDTOA_DEBUG
5542
/*debug*/ if (b->wds > n)
5543
/*debug*/ Bug("oversize b in quorem");
5551
q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
5552
#ifdef BSD_QDTOA_DEBUG
5553
/*debug*/ if (q > 9)
5554
/*debug*/ Bug("oversized quotient in quorem");
5562
ys = (si & 0xffff) * q + carry;
5563
zs = (si >> 16) * q + (ys >> 16);
5565
y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
5567
Sign_Extend(borrow, y);
5568
z = (*bx >> 16) - (zs & 0xffff) + borrow;
5570
Sign_Extend(borrow, z);
5573
ys = *sx++ * q + carry;
5575
y = *bx - (ys & 0xffff) + borrow;
5577
Sign_Extend(borrow, y);
5584
while(--bxe > bx && !*bxe)
5589
if (cmp(b, S) >= 0) {
5598
ys = (si & 0xffff) + carry;
5599
zs = (si >> 16) + (ys >> 16);
5601
y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
5603
Sign_Extend(borrow, y);
5604
z = (*bx >> 16) - (zs & 0xffff) + borrow;
5606
Sign_Extend(borrow, z);
5611
y = *bx - (ys & 0xffff) + borrow;
5613
Sign_Extend(borrow, y);
5621
while(--bxe > bx && !*bxe)
5629
/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
5631
* Inspired by "How to Print Floating-Point Numbers Accurately" by
5632
* Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
5635
* 1. Rather than iterating, we use a simple numeric overestimate
5636
* to determine k = floor(log10(d)). We scale relevant
5637
* quantities using O(log2(k)) rather than O(k) multiplications.
5638
* 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
5639
* try to generate digits strictly left to right. Instead, we
5640
* compute with fewer bits and propagate the carry if necessary
5641
* when rounding the final digit up. This is often faster.
5642
* 3. Under the assumption that input will be rounded nearest,
5643
* mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
5644
* That is, we allow equality in stopping tests when the
5645
* round-nearest rule will give the same floating-point value
5646
* as would satisfaction of the stopping test with strict
5648
* 4. We remove common factors of powers of 2 from relevant
5650
* 5. When converting floating-point integers less than 1e16,
5651
* we use floating-point arithmetic rather than resorting
5652
* to multiple-precision integers.
5653
* 6. When asked to produce fewer than 15 digits, we first try
5654
* to get by with floating-point arithmetic; we resort to
5655
* multiple-precision integer arithmetic only if we cannot
5656
* guarantee that the floating-point calculation has given
5657
* the correctly rounded result. For k requested digits and
5658
* "uniformly" distributed input, the probability is
5659
* something like 10^(k-15) that we must resort to the Long
5664
/* This actually sometimes returns a pointer to a string literal
5665
cast to a char*. Do NOT try to modify the return value. */
5667
static char *qdtoa ( double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **resultp)
5669
// Some values of the floating-point control word can cause _qdtoa to crash with an underflow.
5670
// We set a safe value here.
5673
unsigned int oldbits = _control87(0, 0);
5676
# define MCW_EM _MCW_EM
5678
# define MCW_EM 0x0008001F
5681
_control87(MCW_EM, MCW_EM);
5684
#if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
5686
feholdexcept(&envp);
5689
char *s = _qdtoa(d, mode, ndigits, decpt, sign, rve, resultp);
5694
_control87(oldbits, 0xFFFFF);
5696
_control87(oldbits, _MCW_EM|_MCW_DN|_MCW_RC);
5700
#if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
5707
static char *_qdtoa( NEEDS_VOLATILE double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **resultp)
5710
Arguments ndigits, decpt, sign are similar to those
5711
of ecvt and fcvt; trailing zeros are suppressed from
5712
the returned string. If not null, *rve is set to point
5713
to the end of the return value. If d is +-Infinity or NaN,
5714
then *decpt is set to 9999.
5717
0 ==> shortest string that yields d when read in
5718
and rounded to nearest.
5719
1 ==> like 0, but with Steele & White stopping rule;
5720
e.g. with IEEE P754 arithmetic , mode 0 gives
5721
1e23 whereas mode 1 gives 9.999999999999999e22.
5722
2 ==> max(1,ndigits) significant digits. This gives a
5723
return value similar to that of ecvt, except
5724
that trailing zeros are suppressed.
5725
3 ==> through ndigits past the decimal point. This
5726
gives a return value similar to that from fcvt,
5727
except that trailing zeros are suppressed, and
5728
ndigits can be negative.
5729
4-9 should give the same return values as 2-3, i.e.,
5730
4 <= mode <= 9 ==> same return as mode
5731
2 + (mode & 1). These modes are mainly for
5732
debugging; often they run slower but sometimes
5733
faster than modes 2-3.
5734
4,5,8,9 ==> left-to-right digit generation.
5735
6-9 ==> don't try fast floating-point estimate
5738
Values of mode other than 0-9 are treated as mode 0.
5740
Sufficient space is allocated to the return value
5741
to hold the suppressed trailing zeros.
5744
int bbits, b2, b5, be, dig, i, ieps, ilim0,
5745
j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
5747
int ilim = 0, ilim1 = 0, spec_case = 0; /* pacify gcc */
5749
#ifndef Sudden_Underflow
5753
Bigint *b, *b1, *delta, *mhi, *S;
5754
Bigint *mlo = NULL; /* pacify gcc */
5759
if (getWord0(d) & Sign_bit) {
5760
/* set sign for everything, including 0's and NaNs */
5762
setWord0(&d, getWord0(d) & ~Sign_bit); /* clear sign bit */
5767
#if defined(IEEE_Arith) + defined(VAX)
5769
if ((getWord0(d) & Exp_mask) == Exp_mask)
5771
if (getWord0(d) == 0x8000)
5774
/* Infinity or NaN */
5778
!getWord1(d) && !(getWord0(d) & 0xfffff) ? const_cast<char*>("Infinity") :
5780
const_cast<char*>("NaN");
5791
d += 0; /* normalize */
5793
if (d == g_double_zero)
5796
s = const_cast<char*>("0");
5802
b = d2b(d, &be, &bbits);
5803
#ifdef Sudden_Underflow
5804
i = (int)(getWord0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
5806
if ((i = int(getWord0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) != 0) {
5809
setWord0(&d2, getWord0(d2) & Frac_mask1);
5810
setWord0(&d2, getWord0(d2) | Exp_11);
5812
if (j = 11 - hi0bits(getWord0(d2) & Frac_mask))
5816
/* log(x) ~=~ log(1.5) + (x-1.5)/1.5
5817
* log10(x) = log(x) / log(10)
5818
* ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
5819
* log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
5821
* This suggests computing an approximation k to log10(d) by
5823
* k = (i - Bias)*0.301029995663981
5824
* + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
5826
* We want k to be too large rather than too small.
5827
* The error in the first-order Taylor series approximation
5828
* is in our favor, so we just round up the constant enough
5829
* to compensate for any error in the multiplication of
5830
* (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
5831
* and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
5832
* adding 1e-13 to the constant term more than suffices.
5833
* Hence we adjust the constant term to 0.1760912590558.
5834
* (We could get a more accurate k by invoking log10,
5835
* but this is probably not worthwhile.)
5843
#ifndef Sudden_Underflow
5847
/* d is denormalized */
5849
i = bbits + be + (Bias + (P-1) - 1);
5850
x = i > 32 ? getWord0(d) << (64 - i) | getWord1(d) >> (i - 32)
5851
: getWord1(d) << (32 - i);
5853
setWord0(&d2, getWord0(d2) - 31*Exp_msk1); /* adjust exponent */
5854
i -= (Bias + (P-1) - 1) + 1;
5858
ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
5860
if (ds < 0. && ds != k)
5861
k--; /* want k = floor(ds) */
5863
if (k >= 0 && k <= Ten_pmax) {
5887
if (mode < 0 || mode > 9)
5908
ilim = ilim1 = i = ndigits;
5914
i = ndigits + k + 1;
5920
*resultp = static_cast<char *>(malloc(i + 1));
5923
if (ilim >= 0 && ilim <= Quick_max && try_quick) {
5925
/* Try to get by with floating-point arithmetic. */
5931
ieps = 2; /* conservative */
5936
/* prevent overflows */
5938
d /= bigtens[n_bigtens-1];
5941
for(; j; j >>= 1, i++)
5948
else if ((j1 = -k) != 0) {
5949
d *= tens[j1 & 0xf];
5950
for(j = j1 >> 4; j; j >>= 1, i++)
5956
if (k_check && d < 1. && ilim > 0) {
5965
setWord0(&eps, getWord0(eps) - (P-1)*Exp_msk1);
5975
#ifndef No_leftright
5977
/* Use Steele & White method of only
5978
* generating digits needed.
5980
eps = 0.5/tens[ilim-1] - eps;
5984
*s++ = '0' + int(L);
5997
/* Generate ilim digits, then fix them up. */
5998
#if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
5999
// work around a bug on 64 bit IRIX gcc
6000
double *t = (double *) tens;
6003
eps *= tens[ilim-1];
6005
for(i = 1;; i++, d *= 10.) {
6008
*s++ = '0' + int(L);
6012
else if (d < 0.5 - eps) {
6020
#ifndef No_leftright
6030
/* Do we have a "small" integer? */
6032
if (be >= 0 && k <= Int_max) {
6035
if (ndigits < 0 && ilim <= 0) {
6037
if (ilim < 0 || d <= 5*ds)
6044
#ifdef Check_FLT_ROUNDS
6045
/* If FLT_ROUNDS == 2, L will usually be high by 1 */
6051
*s++ = '0' + int(L);
6054
if (d > ds || (d == ds && L & 1)) {
6066
if ((d *= 10.) == g_double_zero)
6078
#ifndef Sudden_Underflow
6079
denorm ? be + (Bias + (P-1) - 1 + 1) :
6082
1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
6096
if ((i = ilim) < 0) {
6105
if (m2 > 0 && s2 > 0) {
6106
i = m2 < s2 ? m2 : s2;
6114
mhi = pow5mult(mhi, m5);
6119
if ((j = b5 - m5) != 0)
6123
b = pow5mult(b, b5);
6127
S = pow5mult(S, s5);
6129
/* Check for special case that d is a normalized power of 2. */
6132
if (!getWord1(d) && !(getWord0(d) & Bndry_mask)
6133
#ifndef Sudden_Underflow
6134
&& getWord0(d) & Exp_mask
6137
/* The special case */
6146
/* Arrange for convenient computation of quotients:
6147
* shift left if necessary so divisor has 4 leading 0 bits.
6149
* Perhaps we should just compute leading 28 bits of S once
6150
* and for all and pass them and a shift to quorem, so it
6151
* can do shifts and ors to compute the numerator for q.
6154
if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0)
6157
if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
6179
b = multadd(b, 10, 0); /* we botched the k estimate */
6181
mhi = multadd(mhi, 10, 0);
6185
if (ilim <= 0 && mode > 2) {
6186
if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
6187
/* no digits, fcvt style */
6199
mhi = lshift(mhi, m2);
6201
/* Compute mlo -- check for special case
6202
* that d is a normalized power of 2.
6207
mhi = Balloc(mhi->k);
6209
mhi = lshift(mhi, Log2P);
6213
dig = quorem(b,S) + '0';
6214
/* Do we yet have the shortest decimal string
6215
* that will round to d?
6218
delta = diff(S, mhi);
6219
j1 = delta->sign ? 1 : cmp(b, delta);
6221
#ifndef ROUND_BIASED
6222
if (j1 == 0 && !mode && !(getWord1(d) & 1)) {
6231
if (j < 0 || (j == 0 && !mode
6232
#ifndef ROUND_BIASED
6233
&& !(getWord1(d) & 1)
6239
if ((j1 > 0 || (j1 == 0 && dig & 1))
6247
if (dig == '9') { /* possible if i == 1 */
6258
b = multadd(b, 10, 0);
6260
mlo = mhi = multadd(mhi, 10, 0);
6262
mlo = multadd(mlo, 10, 0);
6263
mhi = multadd(mhi, 10, 0);
6269
*s++ = dig = quorem(b,S) + '0';
6272
b = multadd(b, 10, 0);
6275
/* Round off last digit */
6279
if (j > 0 || (j == 0 && dig & 1)) {
6296
if (mlo && mlo != mhi)
6302
if (s == s0) { /* don't return empty string */
6313
#endif // QT_QLOCALE_USES_FCVT