-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
151 lines (126 loc) · 3.58 KB
/
string.c
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
/* This file is part of 34S.
*
* 34S is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 34S is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 34S. If not, see <http://www.gnu.org/licenses/>.
*/
#include "xeq.h"
/* Some utilities to replace the various string and mem functions.
* This one does something akin to memmove and memcpy.
*/
void *xcopy(void *d, const void *s, int n) {
char *dp = d;
const char *sp = s;
if (sp > dp)
while (n--)
*dp++ = *sp++;
else if (sp < dp)
while (n--)
dp[n] = sp[n];
return d;
}
/* And a little something to set memory to a value.
*/
void *xset(void *d, const char c, int n) {
char *dp = d;
while (n--)
*dp++ = c;
return d;
}
#if defined(REALBUILD) && !defined(HOSTBUILD)
/* Needed by the C runtime */
#undef memcpy
#undef memset
void *memcpy() __attribute__((alias("xcopy")));
void *memset() __attribute__((alias("xset")));
#endif
/* Return the length of a string */
int slen(const char *s) {
const char *p;
for (p=s; *p != '\0'; p++);
return p-s;
}
/* Find a character in a string -- strchr */
char *find_char(const char *s, const char c) {
do
if (*s == c)
return (char *)s;
while (*s++);
return NULL;
}
#if defined INCLUDE_YREG_CODE && defined INCLUDE_YREG_HMS
void replace_char(char *str, const char from, const char to) {
while (*str) {
if (*str == from) {
*str = to;
}
++str;
}
}
#endif
/* Copy a string across and return a pointer to the terminating null
*/
char *scopy(char *d, const char *s) {
while (*s != '\0')
*d++ = *s++;
*d = '\0';
return d;
}
/* Copy a string to a given size limit and null terminate
*/
const char *sncopy(char *d, const char *s, int n) {
const char *const d0 = d;
while (n-- && *s != '\0')
*d++ = *s++;
*d = '\0';
return d0;
}
/* Copy a string to the buffer and append a character.
*/
char *scopy_char(char *d, const char *s, const char c) {
d = scopy(d, s);
*d++ = c;
return d;
}
char *scopy_spc(char *d, const char *s) {
return scopy_char(d, s, ' ');
}
char *sncopy_char(char *d, const char *s, int n, const char c) {
while (n-- && *s != '\0')
*d++ = *s++;
*d++ = c;
return d;
}
char *sncopy_spc(char *d, const char *s, int n) {
return sncopy_char(d, s, n, ' ');
}
/* Convert an n digit number to a string with leading zeros
*/
char *num_arg_0(char *d, unsigned int arg, int n) {
int i;
for (i=0; i<n; i++) {
d[n-i-1] = '0' + arg % 10;
arg /= 10;
}
return d + n;
}
char *num_arg(char *d, unsigned int arg) {
char buf[24];
char *p = buf;
do {
*p++ = '0' + arg % 10;
arg /= 10;
} while (arg != 0);
while (--p >= buf)
*d++ = *p;
return d;
}