]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/strto.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
14 #include <linux/ctype.h>
16 /* from lib/kstrtox.c */
17 static const char *_parse_integer_fixup_radix(const char *s
, unsigned int *base
)
21 if (tolower(s
[1]) == 'x' && isxdigit(s
[2]))
32 var
= tolower(s
[i
++]);
33 if (var
>= 'a' && var
<= 'f') {
41 if (*base
== 16 && s
[0] == '0' && tolower(s
[1]) == 'x')
46 unsigned long simple_strtoul(const char *cp
, char **endp
,
49 unsigned long result
= 0;
52 cp
= _parse_integer_fixup_radix(cp
, &base
);
54 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
55 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
56 result
= result
*base
+ value
;
66 int strict_strtoul(const char *cp
, unsigned int base
, unsigned long *res
)
77 val
= simple_strtoul(cp
, &tail
, base
);
81 if ((*tail
== '\0') ||
82 ((len
== (size_t)(tail
- cp
) + 1) && (*tail
== '\n'))) {
90 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
93 return -simple_strtoul(cp
+ 1, endp
, base
);
95 return simple_strtoul(cp
, endp
, base
);
98 unsigned long ustrtoul(const char *cp
, char **endp
, unsigned int base
)
100 unsigned long result
= simple_strtoul(cp
, endp
, base
);
101 switch (tolower(**endp
)) {
119 unsigned long long ustrtoull(const char *cp
, char **endp
, unsigned int base
)
121 unsigned long long result
= simple_strtoull(cp
, endp
, base
);
122 switch (tolower(**endp
)) {
140 unsigned long long simple_strtoull(const char *cp
, char **endp
,
143 unsigned long long result
= 0, value
;
145 cp
= _parse_integer_fixup_radix(cp
, &base
);
147 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
- '0'
148 : (islower(*cp
) ? toupper(*cp
) : *cp
) - 'A' + 10) < base
) {
149 result
= result
* base
+ value
;
159 long trailing_strtoln(const char *str
, const char *end
)
164 end
= str
+ strlen(str
);
165 if (isdigit(end
[-1])) {
166 for (p
= end
- 1; p
> str
; p
--) {
168 return simple_strtoul(p
+ 1, NULL
, 10);
175 long trailing_strtol(const char *str
)
177 return trailing_strtoln(str
, NULL
);