This repository was archived by the owner on Jan 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringn.c
More file actions
185 lines (167 loc) · 3.89 KB
/
stringn.c
File metadata and controls
185 lines (167 loc) · 3.89 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdlib.h>
#include <string.h>
char * strnstr(
const char * haystack, /* [I] String where to search */
const char * needle, /* [I] String to search for */
size_t len) /* [I] Maximum string length */
{
unsigned int hi = 0;
unsigned int ni = 0;
do
{
if (*(haystack + hi) == *(needle + ni))
{
hi++;
ni++;
}
else
if (ni)
ni = 0;
else
hi++;
if (!*(needle + ni))
return (char *)(haystack + hi - ni);
}
while (!*(haystack + hi) && hi < len);
return (char *)NULL;
}
wchar_t * wcsnstr(
const wchar_t * haystack, /* [I] String where to search */
const wchar_t * needle, /* [I] String to search for */
size_t len) /* [I] Maximum string length */
{
unsigned int hi = 0;
unsigned int ni = 0;
do
{
if (*(haystack + hi) == *(needle + ni))
{
hi++;
ni++;
}
else
if (ni)
ni = 0;
else
hi++;
if (!*(needle + ni))
return (wchar_t *)(haystack + hi - ni);
}
while (!*(haystack + hi) && hi < len);
return (wchar_t *)NULL;
}
char * ltoa(
long value, /* [I] Value to be converted */
char * str, /* [O] Destination for the converted value */
int radix) /* [I] Number base for conversion */
{
unsigned long val;
int negative;
char buffer[33];
char *pos;
int digit;
if (value < 0 && radix == 10) {
negative = 1;
val = -value;
} else {
negative = 0;
val = value;
} /* if */
pos = &buffer[32];
*pos = '\0';
do {
digit = val % radix;
val = val / radix;
if (digit < 10) {
*--pos = '0' + digit;
} else {
*--pos = 'a' + digit - 10;
} /* if */
} while (val != 0L);
if (negative) {
*--pos = '-';
} /* if */
memcpy(str, pos, &buffer[32] - pos + 1);
return str;
}
char * itoa(
int value, /* [I] Value to be converted */
char * str, /* [O] Destination for the converted value */
int radix) /* [I] Number base for conversion */
{
return ltoa(value, str, radix);
}
wchar_t * ltow(
long value, /* [I] Value to be converted */
wchar_t * str, /* [O] Destination for the converted value */
int radix) /* [I] Number base for conversion */
{
unsigned long val;
int negative;
wchar_t buffer[33];
wchar_t *pos;
int digit;
if (value < 0 && radix == 10) {
negative = 1;
val = -value;
} else {
negative = 0;
val = value;
} /* if */
pos = &buffer[32];
*pos = '\0';
do {
digit = val % radix;
val = val / radix;
if (digit < 10) {
*--pos = '0' + digit;
} else {
*--pos = 'a' + digit - 10;
} /* if */
} while (val != 0L);
if (negative) {
*--pos = '-';
} /* if */
memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(wchar_t));
return str;
}
wchar_t * itow(
int value, /* [I] Value to be converted */
wchar_t * str, /* [O] Destination for the converted value */
int radix) /* [I] Number base for conversion */
{
return ltow(value, str, radix);
}
size_t chrlen(
const char *c) /* [I] Pointer to multibyte char */
{
size_t l;
for (l = 0; (*c & (0x80 >> l)) && l < 8; l++);
l &= 0x07;
switch (l)
{
case 0:
return 1;
default:
return l;
}
}
int asciisanitize(
char *str) /* [IO] String to cut multi-byte chars from */
{
char *s, *d;
s = d = str;
const char *b = s + strlen(s);
while (*s)
{
size_t l = chrlen(s);
if (l > 1)
s += l;
else
*(d++) = *(s++);
if (d > b || s > b)
return -1;
}
*d = 0;
return 0;
}