-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.h
More file actions
126 lines (107 loc) · 2.63 KB
/
stdlib.h
File metadata and controls
126 lines (107 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// stdlib.h clone - SimpleC
#ifndef SIMPLEC_STDLIB_H
#define SIMPLEC_STDLIB_H
static inline void exit(int code) {
asm volatile (
"movl $1, %%eax\n" // sys_exit
"movl %[status], %%ebx\n"
"int $0x80\n"
:
: [status] "r" (code)
: "eax", "ebx"
);
}
static inline int atoi(const char *s) {
int result = 0;
int sign = 1;
if (*s == '-') {
sign = -1;
s++;
}
while (*s >= '0' && *s <= '9') {
result = result * 10 + (*s - '0');
s++;
}
return sign * result;
}
static inline char *itoa(int value, char *buf) {
char *p = buf;
int is_negative = 0;
if (value < 0) {
is_negative = 1;
value = -value;
}
char *start = p;
do {
*p++ = '0' + (value % 10);
value /= 10;
} while (value);
if (is_negative) *p++ = '-';
*p = '\0';
// Reverse the string
for (char *a = start, *b = p - 1; a < b; a++, b--) {
char t = *a;
*a = *b;
*b = t;
}
return buf;
}
static inline long strtol(const char *s, char **endptr, int base) {
long result = 0;
int sign = 1;
while (*s == ' ' || *s == '\t') s++; // skip whitespace
if (*s == '-') { sign = -1; s++; }
else if (*s == '+') { s++; }
if (base == 0) {
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
base = 16;
s += 2;
} else if (s[0] == '0') {
base = 8;
s++;
} else {
base = 10;
}
}
while (*s) {
int digit;
if (*s >= '0' && *s <= '9') digit = *s - '0';
else if (*s >= 'a' && *s <= 'f') digit = *s - 'a' + 10;
else if (*s >= 'A' && *s <= 'F') digit = *s - 'A' + 10;
else break;
if (digit >= base) break;
result = result * base + digit;
s++;
}
if (endptr) *endptr = (char *)s;
return sign * result;
}
static inline void ftoa(double value, char *buf, int precision) {
if (value < 0) {
*buf++ = '-';
value = -value;
}
int int_part = (int)value;
double frac_part = value - int_part;
// Convert integer part
char temp[16];
char *p = temp;
if (int_part == 0) *p++ = '0';
else {
while (int_part) {
*p++ = '0' + (int_part % 10);
int_part /= 10;
}
}
while (p != temp) *buf++ = *--p;
*buf++ = '.';
// Convert fractional part
for (int i = 0; i < precision; i++) {
frac_part *= 10;
int digit = (int)frac_part;
*buf++ = '0' + digit;
frac_part -= digit;
}
*buf = '\0';
}
#endif // SIMPLEC_STDLIB_H