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, uint *basep)
19 /* Look for a 0x prefix */
21 int ch = tolower(s[1]);
27 /* Only select octal if we don't have a base */
32 /* Use decimal by default */
40 * decode_digit() - Decode a single character into its numeric digit value
44 * @ch: Character to convert (expects '0'..'9', 'a'..'f' or 'A'..'F')
45 * Return: value of digit (0..0xf) or 255 if the character is invalid
47 static uint decode_digit(int ch)
54 return ch <= '9' ? ch - '0' : ch - 'a' + 0xa;
57 ulong simple_strtoul(const char *cp, char **endp, uint base)
62 cp = _parse_integer_fixup_radix(cp, &base);
64 while (value = decode_digit(*cp), value < base) {
65 result = result * base + value;
75 ulong hextoul(const char *cp, char **endp)
77 return simple_strtoul(cp, endp, 16);
80 ulong dectoul(const char *cp, char **endp)
82 return simple_strtoul(cp, endp, 10);
85 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
96 val = simple_strtoul(cp, &tail, base);
100 if ((*tail == '\0') ||
101 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
109 long simple_strtol(const char *cp, char **endp, unsigned int base)
112 return -simple_strtoul(cp + 1, endp, base);
114 return simple_strtoul(cp, endp, base);
117 unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
119 unsigned long result = simple_strtoul(cp, endp, base);
120 switch (tolower(**endp)) {
138 unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
140 unsigned long long result = simple_strtoull(cp, endp, base);
141 switch (tolower(**endp)) {
159 unsigned long long simple_strtoull(const char *cp, char **endp,
162 unsigned long long result = 0;
165 cp = _parse_integer_fixup_radix(cp, &base);
167 while (value = decode_digit(*cp), value < base) {
168 result = result * base + value;
178 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
181 return -simple_strtoull(cp + 1, endp, base);
183 return simple_strtoull(cp, endp, base);
186 long trailing_strtoln_end(const char *str, const char *end, char const **endp)
191 end = str + strlen(str);
193 if (p > str && isdigit(*p)) {
195 if (!isdigit(p[-1])) {
198 return dectoul(p, NULL);
208 long trailing_strtoln(const char *str, const char *end)
210 return trailing_strtoln_end(str, end, NULL);
213 long trailing_strtol(const char *str)
215 return trailing_strtoln(str, NULL);
218 void str_to_upper(const char *in, char *out, size_t len)
220 for (; len > 0 && *in; len--)
221 *out++ = toupper(*in++);