annotate libiberty/strtoull.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /*
kono
parents:
diff changeset
2 * Copyright (c) 2014 Regents of the University of California.
kono
parents:
diff changeset
3 * All rights reserved.
kono
parents:
diff changeset
4 *
kono
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
kono
parents:
diff changeset
7 * are met:
kono
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
kono
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
kono
parents:
diff changeset
13 * 3. [rescinded 22 July 1999]
kono
parents:
diff changeset
14 * 4. Neither the name of the University nor the names of its contributors
kono
parents:
diff changeset
15 * may be used to endorse or promote products derived from this software
kono
parents:
diff changeset
16 * without specific prior written permission.
kono
parents:
diff changeset
17 *
kono
parents:
diff changeset
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
kono
parents:
diff changeset
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
kono
parents:
diff changeset
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
kono
parents:
diff changeset
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
kono
parents:
diff changeset
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
kono
parents:
diff changeset
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
kono
parents:
diff changeset
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
kono
parents:
diff changeset
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
kono
parents:
diff changeset
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
kono
parents:
diff changeset
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
kono
parents:
diff changeset
28 * SUCH DAMAGE.
kono
parents:
diff changeset
29 */
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 #ifdef HAVE_CONFIG_H
kono
parents:
diff changeset
32 #include "config.h"
kono
parents:
diff changeset
33 #endif
kono
parents:
diff changeset
34 #ifdef HAVE_LIMITS_H
kono
parents:
diff changeset
35 #include <limits.h>
kono
parents:
diff changeset
36 #endif
kono
parents:
diff changeset
37 #ifdef HAVE_SYS_PARAM_H
kono
parents:
diff changeset
38 #include <sys/param.h>
kono
parents:
diff changeset
39 #endif
kono
parents:
diff changeset
40 #include <errno.h>
kono
parents:
diff changeset
41 #ifdef NEED_DECLARATION_ERRNO
kono
parents:
diff changeset
42 extern int errno;
kono
parents:
diff changeset
43 #endif
kono
parents:
diff changeset
44 #if 0
kono
parents:
diff changeset
45 #include <stdlib.h>
kono
parents:
diff changeset
46 #endif
kono
parents:
diff changeset
47 #include "ansidecl.h"
kono
parents:
diff changeset
48 #include "safe-ctype.h"
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 #ifdef HAVE_LONG_LONG
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 __extension__
kono
parents:
diff changeset
53 typedef unsigned long long ullong_type;
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 #ifndef ULLONG_MAX
kono
parents:
diff changeset
56 #define ULLONG_MAX (~(ullong_type)0) /* 0xFFFFFFFFFFFFFFFF */
kono
parents:
diff changeset
57 #endif
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 /*
kono
parents:
diff changeset
60 * Convert a string to an unsigned long long integer.
kono
parents:
diff changeset
61 *
kono
parents:
diff changeset
62 * Ignores `locale' stuff. Assumes that the upper and lower case
kono
parents:
diff changeset
63 * alphabets and digits are each contiguous.
kono
parents:
diff changeset
64 */
kono
parents:
diff changeset
65 ullong_type
kono
parents:
diff changeset
66 strtoull(const char *nptr, char **endptr, register int base)
kono
parents:
diff changeset
67 {
kono
parents:
diff changeset
68 register const char *s = nptr;
kono
parents:
diff changeset
69 register ullong_type acc;
kono
parents:
diff changeset
70 register int c;
kono
parents:
diff changeset
71 register ullong_type cutoff;
kono
parents:
diff changeset
72 register int neg = 0, any, cutlim;
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 /*
kono
parents:
diff changeset
75 * See strtol for comments as to the logic used.
kono
parents:
diff changeset
76 */
kono
parents:
diff changeset
77 do {
kono
parents:
diff changeset
78 c = *s++;
kono
parents:
diff changeset
79 } while (ISSPACE(c));
kono
parents:
diff changeset
80 if (c == '-') {
kono
parents:
diff changeset
81 neg = 1;
kono
parents:
diff changeset
82 c = *s++;
kono
parents:
diff changeset
83 } else if (c == '+')
kono
parents:
diff changeset
84 c = *s++;
kono
parents:
diff changeset
85 if ((base == 0 || base == 16) &&
kono
parents:
diff changeset
86 c == '0' && (*s == 'x' || *s == 'X')) {
kono
parents:
diff changeset
87 c = s[1];
kono
parents:
diff changeset
88 s += 2;
kono
parents:
diff changeset
89 base = 16;
kono
parents:
diff changeset
90 }
kono
parents:
diff changeset
91 if (base == 0)
kono
parents:
diff changeset
92 base = c == '0' ? 8 : 10;
kono
parents:
diff changeset
93 cutoff = (ullong_type)ULLONG_MAX / (ullong_type)base;
kono
parents:
diff changeset
94 cutlim = (ullong_type)ULLONG_MAX % (ullong_type)base;
kono
parents:
diff changeset
95 for (acc = 0, any = 0;; c = *s++) {
kono
parents:
diff changeset
96 if (ISDIGIT(c))
kono
parents:
diff changeset
97 c -= '0';
kono
parents:
diff changeset
98 else if (ISALPHA(c))
kono
parents:
diff changeset
99 c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
kono
parents:
diff changeset
100 else
kono
parents:
diff changeset
101 break;
kono
parents:
diff changeset
102 if (c >= base)
kono
parents:
diff changeset
103 break;
kono
parents:
diff changeset
104 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
kono
parents:
diff changeset
105 any = -1;
kono
parents:
diff changeset
106 else {
kono
parents:
diff changeset
107 any = 1;
kono
parents:
diff changeset
108 acc *= base;
kono
parents:
diff changeset
109 acc += c;
kono
parents:
diff changeset
110 }
kono
parents:
diff changeset
111 }
kono
parents:
diff changeset
112 if (any < 0) {
kono
parents:
diff changeset
113 acc = ULLONG_MAX;
kono
parents:
diff changeset
114 errno = ERANGE;
kono
parents:
diff changeset
115 } else if (neg)
kono
parents:
diff changeset
116 acc = -acc;
kono
parents:
diff changeset
117 if (endptr != 0)
kono
parents:
diff changeset
118 *endptr = (char *) (any ? s - 1 : nptr);
kono
parents:
diff changeset
119 return (acc);
kono
parents:
diff changeset
120 }
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 #endif /* ifdef HAVE_LONG_LONG */