diff options
Diffstat (limited to 'src/corelib/tools/qlocale_tools.cpp')
-rw-r--r-- | src/corelib/tools/qlocale_tools.cpp | 332 |
1 files changed, 58 insertions, 274 deletions
diff --git a/src/corelib/tools/qlocale_tools.cpp b/src/corelib/tools/qlocale_tools.cpp index 1b246a29de..03b911c4b3 100644 --- a/src/corelib/tools/qlocale_tools.cpp +++ b/src/corelib/tools/qlocale_tools.cpp @@ -1,7 +1,8 @@ /**************************************************************************** ** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal +** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2014 Intel Corporation +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. ** @@ -10,9 +11,9 @@ ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser @@ -23,8 +24,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** $QT_END_LICENSE$ @@ -36,6 +37,7 @@ #include "qstring.h" #include <ctype.h> +#include <errno.h> #include <float.h> #include <limits.h> #include <math.h> @@ -63,10 +65,57 @@ QT_BEGIN_NAMESPACE -#ifndef QT_QLOCALE_USES_FCVT +#include "../../3rdparty/freebsd/strtoull.c" +#include "../../3rdparty/freebsd/strtoll.c" + +unsigned long long +qstrtoull(const char * nptr, const char **endptr, int base, bool *ok) +{ + // strtoull accepts negative numbers. We don't. + // Use a different variable so we pass the original nptr to strtoul + // (we need that so endptr may be nptr in case of failure) + const char *begin = nptr; + while (ascii_isspace(*begin)) + ++begin; + if (*begin == '-') { + *ok = false; + return 0; + } + + *ok = true; + errno = 0; + char *endptr2 = 0; + unsigned long long result = qt_strtoull(nptr, &endptr2, base); + if (endptr) + *endptr = endptr2; + if ((result == 0 || result == std::numeric_limits<unsigned long long>::max()) + && (errno || endptr2 == nptr)) { + *ok = false; + return 0; + } + return result; +} + +long long +qstrtoll(const char * nptr, const char **endptr, int base, bool *ok) +{ + *ok = true; + errno = 0; + char *endptr2 = 0; + long long result = qt_strtoll(nptr, &endptr2, base); + if (endptr) + *endptr = endptr2; + if ((result == 0 || result == std::numeric_limits<long long>::min() + || result == std::numeric_limits<long long>::max()) + && (errno || nptr == endptr2)) { + *ok = false; + return 0; + } + return result; +} + static char *_qdtoa( NEEDS_VOLATILE double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **digits_str); -#endif QString qulltoa(qulonglong l, int base, const QChar _zero) { @@ -176,222 +225,6 @@ QString &exponentForm(QChar zero, QChar decimal, QChar exponential, return digits; } -/*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgment: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -// static char sccsid[] = "@(#)strtouq.c 8.1 (Berkeley) 6/4/93"; -// "$FreeBSD: src/lib/libc/stdlib/strtoull.c,v 1.5.2.1 2001/03/02 09:45:20 obrien Exp $"; - -/* - * Convert a string to an unsigned long long integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -qulonglong qstrtoull(const char *nptr, const char **endptr, int base, bool *ok) -{ - const char *s = nptr; - qulonglong acc; - unsigned char c; - qulonglong qbase, cutoff; - int any, cutlim; - - if (ok != 0) - *ok = true; - - /* - * See strtoq for comments as to the logic used. - */ - s = nptr; - do { - c = *s++; - } while (ascii_isspace(c)); - if (c == '-') { - if (ok != 0) - *ok = false; - if (endptr != 0) - *endptr = s - 1; - return 0; - } else { - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - qbase = unsigned(base); - cutoff = qulonglong(ULLONG_MAX) / qbase; - cutlim = qulonglong(ULLONG_MAX) % qbase; - for (acc = 0, any = 0;; c = *s++) { - if (!isascii(c)) - break; - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) - any = -1; - else { - any = 1; - acc *= qbase; - acc += c; - } - } - if (any == 0) { - if (ok != 0) - *ok = false; - } else if (any < 0) { - acc = ULLONG_MAX; - if (ok != 0) - *ok = false; - } - if (endptr != 0) - *endptr = (any ? s - 1 : nptr); - return acc; -} - - -// "$FreeBSD: src/lib/libc/stdlib/strtoll.c,v 1.5.2.1 2001/03/02 09:45:20 obrien Exp $"; - - -/* - * Convert a string to a long long integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -qlonglong qstrtoll(const char *nptr, const char **endptr, int base, bool *ok) -{ - const char *s; - qulonglong acc; - unsigned char c; - qulonglong qbase, cutoff; - int neg, any, cutlim; - - /* - * Skip white space and pick up leading +/- sign if any. - * If base is 0, allow 0x for hex and 0 for octal, else - * assume decimal; if base is already 16, allow 0x. - */ - s = nptr; - do { - c = *s++; - } while (ascii_isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - /* - * Compute the cutoff value between legal numbers and illegal - * numbers. That is the largest legal value, divided by the - * base. An input number that is greater than this value, if - * followed by a legal input character, is too big. One that - * is equal to this value may be valid or not; the limit - * between valid and invalid numbers is then based on the last - * digit. For instance, if the range for quads is - * [-9223372036854775808..9223372036854775807] and the input base - * is 10, cutoff will be set to 922337203685477580 and cutlim to - * either 7 (neg==0) or 8 (neg==1), meaning that if we have - * accumulated a value > 922337203685477580, or equal but the - * next digit is > 7 (or 8), the number is too big, and we will - * return a range error. - * - * Set any if any `digits' consumed; make it negative to indicate - * overflow. - */ - qbase = unsigned(base); - cutoff = neg ? qulonglong(0-(LLONG_MIN + LLONG_MAX)) + LLONG_MAX : LLONG_MAX; - cutlim = cutoff % qbase; - cutoff /= qbase; - for (acc = 0, any = 0;; c = *s++) { - if (!isascii(c)) - break; - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) - any = -1; - else { - any = 1; - acc *= qbase; - acc += c; - } - } - if (any < 0) { - acc = neg ? LLONG_MIN : LLONG_MAX; - if (ok != 0) - *ok = false; - } else if (neg) { - acc = (~acc) + 1; - } - if (endptr != 0) - *endptr = (any >= 0 ? s - 1 : nptr); - - if (ok != 0) - *ok = any > 0; - - return acc; -} - -#ifndef QT_QLOCALE_USES_FCVT - /* From: NetBSD: strtod.c,v 1.26 1998/02/03 18:44:21 perry Exp */ /* $FreeBSD: src/lib/libc/stdlib/netbsd_strtod.c,v 1.2.2.2 2001/03/02 17:14:15 tegge Exp $ */ @@ -2780,54 +2613,5 @@ static char *_qdtoa( NEEDS_VOLATILE double d, int mode, int ndigits, int *decpt, *rve = s; return s0; } -#else -// NOT thread safe! - -#include <errno.h> - -Q_CORE_EXPORT char *qdtoa( double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **resultp) -{ - if(rve) - *rve = 0; - - char *res; - if (mode == 0) - ndigits = 80; - - if (mode == 3) - res = fcvt(d, ndigits, decpt, sign); - else - res = ecvt(d, ndigits, decpt, sign); - - int n = qstrlen(res); - if (mode == 0) { // remove trailing 0's - const int stop = qMax(1, *decpt); - int i; - for (i = n-1; i >= stop; --i) { - if (res[i] != '0') - break; - } - n = i + 1; - } - *resultp = static_cast<char*>(malloc(n + 1)); - Q_CHECK_PTR(resultp); - qstrncpy(*resultp, res, n + 1); - return *resultp; -} - -Q_CORE_EXPORT double qstrtod(const char *s00, const char **se, bool *ok) -{ - double ret = strtod((char*)s00, (char**)se); - if (ok) { - if((ret == 0.0l && errno == ERANGE) - || ret == HUGE_VAL || ret == -HUGE_VAL) - *ok = false; - else - *ok = true; // the result will be that we don't report underflow in this case - } - return ret; -} - -#endif // QT_QLOCALE_USES_FCVT QT_END_NAMESPACE |