root/memdump/strtoul.c

Revision b5cf24944a66ef4a6d287f746bceebf6f1291af4, 1.1 KB (checked in by H. Peter Anvin <hpa@…>, 3 years ago)

Run Nindent on memdump/strtoul.c

Automatically reformat memdump/strtoul.c using Nindent.

Do this for all files except HDT, gPXE and externally maintained
libraries (zlib, tinyjpeg, libpng).

Signed-off-by: H. Peter Anvin <hpa@…>

  • Property mode set to 100644
Line 
1/*
2 * strtoul.c
3 *
4 */
5
6#include "mystuff.h"
7
8static inline int isspace(int c)
9{
10    return (c <= ' ');          /* Close enough */
11}
12
13static inline int digitval(int ch)
14{
15    if (ch >= '0' && ch <= '9') {
16        return ch - '0';
17    } else if (ch >= 'A' && ch <= 'Z') {
18        return ch - 'A' + 10;
19    } else if (ch >= 'a' && ch <= 'z') {
20        return ch - 'a' + 10;
21    } else {
22        return -1;
23    }
24}
25
26unsigned long strtoul(const char *nptr, char **endptr, int base)
27{
28    int minus = 0;
29    unsigned long v = 0;
30    int d;
31
32    while (isspace((unsigned char)*nptr)) {
33        nptr++;
34    }
35
36    /* Single optional + or - */
37    {
38        char c = *nptr;
39        if (c == '-' || c == '+') {
40            minus = (c == '-');
41            nptr++;
42        }
43    }
44
45    if (base == 0) {
46        if (nptr[0] == '0' && (nptr[1] == 'x' || nptr[1] == 'X')) {
47            nptr += 2;
48            base = 16;
49        } else if (nptr[0] == '0') {
50            nptr++;
51            base = 8;
52        } else {
53            base = 10;
54        }
55    } else if (base == 16) {
56        if (nptr[0] == '0' && (nptr[1] == 'x' || nptr[1] == 'X')) {
57            nptr += 2;
58        }
59    }
60
61    while ((d = digitval(*nptr)) >= 0 && d < base) {
62        v = v * base + d;
63        nptr++;
64    }
65
66    if (endptr)
67        *endptr = (char *)nptr;
68
69    return minus ? -v : v;
70}
Note: See TracBrowser for help on using the browser.