OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc2/] [newlib/] [libc/] [stdlib/] [wcstoll_r.c] - Diff between revs 207 and 520

Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 520
/*
/*
  This code is based on strtoul.c which has the following copyright.
  This code is based on strtoul.c which has the following copyright.
  It is used to convert a wide string into a signed long long.
  It is used to convert a wide string into a signed long long.
 
 
  long long _wcstoll_r (struct _reent *rptr, const wchar_t *s,
  long long _wcstoll_r (struct _reent *rptr, const wchar_t *s,
                        wchar_t **ptr, int base);
                        wchar_t **ptr, int base);
*/
*/
 
 
/*-
/*-
 * Copyright (c) 1990 The Regents of the University of California.
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 * All rights reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * modification, are permitted provided that the following conditions
 * are met:
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    must display the following acknowledgement:
 *      This product includes software developed by the University of
 *      This product includes software developed by the University of
 *      California, Berkeley and its contributors.
 *      California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of 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
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *    without specific prior written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * 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
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * SUCH DAMAGE.
 */
 */
 
 
#ifdef __GNUC__
#ifdef __GNUC__
 
 
#define _GNU_SOURCE
#define _GNU_SOURCE
#include <_ansi.h>
#include <_ansi.h>
#include <limits.h>
#include <limits.h>
#include <wctype.h>
#include <wctype.h>
#include <errno.h>
#include <errno.h>
#include <wchar.h>
#include <wchar.h>
#include <reent.h>
#include <reent.h>
 
 
/*
/*
 * Convert a wide string to a long long integer.
 * Convert a wide string to a long long integer.
 *
 *
 * Ignores `locale' stuff.  Assumes that the upper and lower case
 * Ignores `locale' stuff.  Assumes that the upper and lower case
 * alphabets and digits are each contiguous.
 * alphabets and digits are each contiguous.
 */
 */
long long
long long
_DEFUN (_wcstoll_r, (rptr, nptr, endptr, base),
_DEFUN (_wcstoll_r, (rptr, nptr, endptr, base),
        struct _reent *rptr _AND
        struct _reent *rptr _AND
        _CONST wchar_t *nptr _AND
        _CONST wchar_t *nptr _AND
        wchar_t **endptr _AND
        wchar_t **endptr _AND
        int base)
        int base)
{
{
        register const wchar_t *s = nptr;
        register const wchar_t *s = nptr;
        register unsigned long long acc;
        register unsigned long long acc;
        register int c;
        register int c;
        register unsigned long long cutoff;
        register unsigned long long cutoff;
        register int neg = 0, any, cutlim;
        register int neg = 0, any, cutlim;
 
 
        /*
        /*
         * Skip white space and pick up leading +/- sign if any.
         * Skip white space and pick up leading +/- sign if any.
         * If base is 0, allow 0x for hex and 0 for octal, else
         * If base is 0, allow 0x for hex and 0 for octal, else
         * assume decimal; if base is already 16, allow 0x.
         * assume decimal; if base is already 16, allow 0x.
         */
         */
        do {
        do {
                c = *s++;
                c = *s++;
        } while (iswspace(c));
        } while (iswspace(c));
        if (c == L'-') {
        if (c == L'-') {
                neg = 1;
                neg = 1;
                c = *s++;
                c = *s++;
        } else if (c == L'+')
        } else if (c == L'+')
                c = *s++;
                c = *s++;
        if ((base == 0 || base == 16) &&
        if ((base == 0 || base == 16) &&
            c == L'0' && (*s == L'x' || *s == L'X')) {
            c == L'0' && (*s == L'x' || *s == L'X')) {
                c = s[1];
                c = s[1];
                s += 2;
                s += 2;
                base = 16;
                base = 16;
        }
        }
        if (base == 0)
        if (base == 0)
                base = c == L'0' ? 8 : 10;
                base = c == L'0' ? 8 : 10;
 
 
        /*
        /*
         * Compute the cutoff value between legal numbers and illegal
         * Compute the cutoff value between legal numbers and illegal
         * numbers.  That is the largest legal value, divided by the
         * numbers.  That is the largest legal value, divided by the
         * base.  An input number that is greater than this value, if
         * base.  An input number that is greater than this value, if
         * followed by a legal input character, is too big.  One that
         * followed by a legal input character, is too big.  One that
         * is equal to this value may be valid or not; the limit
         * is equal to this value may be valid or not; the limit
         * between valid and invalid numbers is then based on the last
         * between valid and invalid numbers is then based on the last
         * digit.  For instance, if the range for longs is
         * digit.  For instance, if the range for longs is
         * [-2147483648..2147483647] and the input base is 10,
         * [-2147483648..2147483647] and the input base is 10,
         * cutoff will be set to 214748364 and cutlim to either
         * cutoff will be set to 214748364 and cutlim to either
         * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
         * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
         * a value > 214748364, or equal but the next digit is > 7 (or 8),
         * a value > 214748364, or equal but the next digit is > 7 (or 8),
         * the number is too big, and we will return a range error.
         * the number is too big, and we will return a range error.
         *
         *
         * Set any if any `digits' consumed; make it negative to indicate
         * Set any if any `digits' consumed; make it negative to indicate
         * overflow.
         * overflow.
         */
         */
        cutoff = neg ? -(unsigned long long)LONG_LONG_MIN : LONG_LONG_MAX;
        cutoff = neg ? -(unsigned long long)LONG_LONG_MIN : LONG_LONG_MAX;
        cutlim = cutoff % (unsigned long long)base;
        cutlim = cutoff % (unsigned long long)base;
        cutoff /= (unsigned long long)base;
        cutoff /= (unsigned long long)base;
        for (acc = 0, any = 0;; c = *s++) {
        for (acc = 0, any = 0;; c = *s++) {
                if (iswdigit(c))
                if (iswdigit(c))
                        c -= L'0';
                        c -= L'0';
                else if (iswalpha(c))
                else if (iswalpha(c))
                        c -= iswupper(c) ? L'A' - 10 : L'a' - 10;
                        c -= iswupper(c) ? L'A' - 10 : L'a' - 10;
                else
                else
                        break;
                        break;
                if (c >= base)
                if (c >= base)
                        break;
                        break;
               if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
               if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
                        any = -1;
                        any = -1;
                else {
                else {
                        any = 1;
                        any = 1;
                        acc *= base;
                        acc *= base;
                        acc += c;
                        acc += c;
                }
                }
        }
        }
        if (any < 0) {
        if (any < 0) {
                acc = neg ? LONG_LONG_MIN : LONG_LONG_MAX;
                acc = neg ? LONG_LONG_MIN : LONG_LONG_MAX;
                rptr->_errno = ERANGE;
                rptr->_errno = ERANGE;
        } else if (neg)
        } else if (neg)
                acc = -acc;
                acc = -acc;
        if (endptr != 0)
        if (endptr != 0)
                *endptr = (wchar_t *) (any ? s - 1 : nptr);
                *endptr = (wchar_t *) (any ? s - 1 : nptr);
        return (acc);
        return (acc);
}
}
 
 
#endif /* __GNUC__ */
#endif /* __GNUC__ */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.