Skip to content

Commit cb7d415

Browse files
[fix] #514
1 parent 19eb973 commit cb7d415

File tree

8 files changed

+327
-376
lines changed

8 files changed

+327
-376
lines changed

src/libc/strtol.c

Lines changed: 6 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,8 @@
1-
/************************************************************************/
2-
/* */
3-
/* Copyright (C)1987-2008 by */
4-
/* Zilog, Inc. */
5-
/* */
6-
/* San Jose, California */
7-
/* */
8-
/************************************************************************/
9-
#include <stdio.h>
10-
#include <stdlib.h>
11-
#include <limits.h>
12-
#include <stddef.h>
13-
#include <ctype.h>
14-
#include <errno.h>
1+
#define STRTOX_TYPE long
2+
#define STRTOX_MAX LONG_MAX
3+
#define STRTOX_MIN LONG_MIN
154

16-
/*************************************************
17-
*
18-
* strtol - string to long conversion
19-
*
20-
* Inputs:
21-
* cp - pointer to the character string
22-
* endptr - place to put ptr to first invalid character
23-
* base - radix
24-
*
25-
* Returns:
26-
* the value of the number
27-
*
28-
*************************************************/
29-
long strtol(const char *__restrict nptr,
30-
char **__restrict endptr, int base)
31-
{
32-
long sum,psum;
33-
char sign;
34-
int radix = base;
35-
char *cp = (char*)nptr;
36-
char digit;
5+
#define STRTOX_SIGNED 1
6+
#define STRTOX_NAME strtol
377

38-
while (isspace(*cp))
39-
++cp;
40-
41-
sign = 0;
42-
if ( *cp == (char)'-' ) {
43-
sign = 1;
44-
++cp;
45-
}
46-
else if ( *cp == (char)'+' )
47-
++cp;
48-
49-
if (base == 0)
50-
{
51-
radix = 10;
52-
if (*cp == (char)'0')
53-
{
54-
if (cp[1] == (char)'x' || cp[1] == (char)'X')
55-
{
56-
radix = 16;
57-
}
58-
else
59-
{
60-
radix = 8;
61-
}
62-
}
63-
}
64-
65-
if (base == 16 && *cp == (char)'0' && (cp[1] == (char)'x' || cp[1] == (char)'X'))
66-
cp += 2;
67-
68-
sum = 0;
69-
for (;;) {
70-
digit = toupper(*(cp++));
71-
if (digit >= (char)'A')
72-
digit = (digit - (char)'A') + (char)10;
73-
else
74-
digit -= (char)'0';
75-
if (digit < (char)0 || digit >= (char)radix)
76-
break;
77-
psum = sum;
78-
sum *= radix;
79-
sum += digit;
80-
if (sum < psum) { /* overflow */
81-
sum = sign ? LONG_MIN : LONG_MAX;
82-
errno=ERANGE;
83-
break;
84-
}
85-
}
86-
87-
if (endptr) {
88-
--cp;
89-
if (base == 0) {
90-
while (*cp == (char)'h' || *cp == (char)'u' ||
91-
*cp == (char)'l' || *cp == (char)'L')
92-
++cp;
93-
}
94-
*endptr = (char*)cp;
95-
}
96-
return(sign ? -sum : sum);
97-
}
8+
#include "strtox.h"

src/libc/strtoll.c

Lines changed: 6 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,8 @@
1-
/************************************************************************/
2-
/* */
3-
/* Copyright (C)1987-2008 by */
4-
/* Zilog, Inc. */
5-
/* */
6-
/* San Jose, California */
7-
/* */
8-
/************************************************************************/
9-
#include <stdio.h>
10-
#include <stdlib.h>
11-
#include <limits.h>
12-
#include <stddef.h>
13-
#include <ctype.h>
14-
#include <errno.h>
1+
#define STRTOX_TYPE long long
2+
#define STRTOX_MAX LLONG_MAX
3+
#define STRTOX_MIN LLONG_MIN
154

16-
/*************************************************
17-
*
18-
* strtoll - string to long long conversion
19-
*
20-
* Inputs:
21-
* cp - pointer to the character string
22-
* endptr - place to put ptr to first invalid character
23-
* base - radix
24-
*
25-
* Returns:
26-
* the value of the number
27-
*
28-
*************************************************/
29-
long long strtoll(const char *__restrict nptr,
30-
char **__restrict endptr, int base)
31-
{
32-
long long sum,psum;
33-
char sign;
34-
int radix = base;
35-
char *cp = (char*)nptr;
36-
char digit;
5+
#define STRTOX_SIGNED 1
6+
#define STRTOX_NAME strtoll
377

38-
while (isspace(*cp))
39-
++cp;
40-
41-
sign = 0;
42-
if ( *cp == (char)'-' ) {
43-
sign = 1;
44-
++cp;
45-
}
46-
else if ( *cp == (char)'+' )
47-
++cp;
48-
49-
if (base == 0)
50-
{
51-
radix = 10;
52-
if (*cp == (char)'0')
53-
{
54-
if (cp[1] == (char)'x' || cp[1] == (char)'X')
55-
{
56-
radix = 16;
57-
}
58-
else
59-
{
60-
radix = 8;
61-
}
62-
}
63-
}
64-
65-
if (base == 16 && *cp == (char)'0' && (cp[1] == (char)'x' || cp[1] == (char)'X'))
66-
cp += 2;
67-
68-
sum = 0;
69-
for (;;) {
70-
digit = toupper(*(cp++));
71-
if (digit >= (char)'A')
72-
digit = (digit - (char)'A') + (char)10;
73-
else
74-
digit -= (char)'0';
75-
if (digit < (char)0 || digit >= (char)radix)
76-
break;
77-
psum = sum;
78-
sum *= radix;
79-
sum += digit;
80-
if (sum < psum) { /* overflow */
81-
sum = sign ? LLONG_MIN : LLONG_MAX;
82-
errno=ERANGE;
83-
break;
84-
}
85-
}
86-
87-
if (endptr) {
88-
--cp;
89-
if (base == 0) {
90-
while (*cp == (char)'h' || *cp == (char)'u' ||
91-
*cp == (char)'l' || *cp == (char)'L')
92-
++cp;
93-
}
94-
*endptr = (char*)cp;
95-
}
96-
return(sign ? -sum : sum);
97-
}
8+
#include "strtox.h"

src/libc/strtoul.c

Lines changed: 6 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,8 @@
1-
/*************************************************
2-
* Copyright (C) 2006-2008 by Zilog, Inc.
3-
* All Rights Reserved
4-
*************************************************/
1+
#define STRTOX_TYPE long
2+
#define STRTOX_MAX ULONG_MAX
3+
#define STRTOX_MIN 0
54

6-
#include <stdio.h>
7-
#include <stdlib.h>
8-
#include <limits.h>
9-
#include <stddef.h>
10-
#include <ctype.h>
11-
#include <errno.h>
5+
#define STRTOX_SIGNED 0
6+
#define STRTOX_NAME strtoul
127

13-
/*************************************************
14-
*
15-
* strtoul - string to unsigned long conversion
16-
*
17-
* Inputs:
18-
* nptr - pointer to the character string
19-
* endptr - place to put ptr to first invalid character
20-
* base - radix
21-
*
22-
* Returns:
23-
* the value of the number
24-
*
25-
*************************************************/
26-
unsigned long strtoul(const char *__restrict nptr,
27-
char **__restrict endptr, int base)
28-
{
29-
unsigned long sum, psum;
30-
unsigned char sign;
31-
unsigned char digit;
32-
unsigned char radix = base;
33-
char *cp = (char*)nptr;
34-
35-
while (isspace(*cp))
36-
++cp;
37-
38-
sign = 0;
39-
40-
if ( *cp == '+' ) {
41-
++cp;
42-
}
43-
else if ( *cp == '-' ) {
44-
++cp;
45-
sign = 1;
46-
}
47-
48-
if (base == 0) {
49-
if (*cp == '0')
50-
if (cp[1] == 'x' || cp[1] == 'X')
51-
radix = 16;
52-
else
53-
radix = 8;
54-
else
55-
radix = 10;
56-
}
57-
58-
/* If the base is declared as 16, the "0x" prefix may be ignored. */
59-
if (base == 16 && *cp == '0' && (cp[1] == 'x' || cp[1] == 'X'))
60-
cp += 2;
61-
62-
sum = 0;
63-
for (;;) {
64-
digit = toupper(*(cp++));
65-
if (digit >= 'A')
66-
digit = digit - ('A' - 10);
67-
else
68-
digit -= '0';
69-
if (digit >= radix)
70-
break;
71-
psum = sum;
72-
sum = sum * radix + digit;
73-
if (sum < psum) { /* overflow */
74-
errno = ERANGE;
75-
sum = ULONG_MAX;
76-
}
77-
}
78-
79-
if (sign) {
80-
// errno = ERANGE;
81-
// sum = 0;
82-
sum = -sum;
83-
}
84-
85-
if (endptr) {
86-
--cp;
87-
if (base == 0) {
88-
while (*cp == 'h' || *cp == 'u' ||
89-
*cp == 'l' || *cp == 'L')
90-
++cp;
91-
}
92-
*endptr = (char*)cp;
93-
}
94-
return(sum);
95-
}
8+
#include "strtox.h"

0 commit comments

Comments
 (0)