~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/tools/qlocale.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

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