Mercurial > repo
comparison nasmbuild/nasm-2.13rc9/nasmlib/string.c @ 10554:587a0a262d22
<moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
author | HackBot |
---|---|
date | Thu, 30 Mar 2017 20:58:41 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
10553:93dc2a984de0 | 10554:587a0a262d22 |
---|---|
1 /* ----------------------------------------------------------------------- * | |
2 * | |
3 * Copyright 1996-2016 The NASM Authors - All Rights Reserved | |
4 * See the file AUTHORS included with the NASM distribution for | |
5 * the specific copyright holders. | |
6 * | |
7 * Redistribution and use in source and binary forms, with or without | |
8 * modification, are permitted provided that the following | |
9 * conditions are met: | |
10 * | |
11 * * Redistributions of source code must retain the above copyright | |
12 * notice, this list of conditions and the following disclaimer. | |
13 * * Redistributions in binary form must reproduce the above | |
14 * copyright notice, this list of conditions and the following | |
15 * disclaimer in the documentation and/or other materials provided | |
16 * with the distribution. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | |
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | |
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | |
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 * | |
32 * ----------------------------------------------------------------------- */ | |
33 | |
34 /* | |
35 * nasmlib.c library routines for the Netwide Assembler | |
36 */ | |
37 | |
38 #include "compiler.h" | |
39 | |
40 #include <stdlib.h> | |
41 #include <ctype.h> | |
42 | |
43 #include "nasmlib.h" | |
44 | |
45 /* | |
46 * Prepare a table of tolower() results. This avoids function calls | |
47 * on some platforms. | |
48 */ | |
49 | |
50 unsigned char nasm_tolower_tab[256]; | |
51 | |
52 void tolower_init(void) | |
53 { | |
54 int i; | |
55 | |
56 for (i = 0; i < 256; i++) | |
57 nasm_tolower_tab[i] = tolower(i); | |
58 } | |
59 | |
60 #ifndef nasm_stricmp | |
61 int nasm_stricmp(const char *s1, const char *s2) | |
62 { | |
63 unsigned char c1, c2; | |
64 int d; | |
65 | |
66 while (1) { | |
67 c1 = nasm_tolower(*s1++); | |
68 c2 = nasm_tolower(*s2++); | |
69 d = c1-c2; | |
70 | |
71 if (d) | |
72 return d; | |
73 if (!c1) | |
74 break; | |
75 } | |
76 return 0; | |
77 } | |
78 #endif | |
79 | |
80 #ifndef nasm_strnicmp | |
81 int nasm_strnicmp(const char *s1, const char *s2, size_t n) | |
82 { | |
83 unsigned char c1, c2; | |
84 int d; | |
85 | |
86 while (n--) { | |
87 c1 = nasm_tolower(*s1++); | |
88 c2 = nasm_tolower(*s2++); | |
89 d = c1-c2; | |
90 | |
91 if (d) | |
92 return d; | |
93 if (!c1) | |
94 break; | |
95 } | |
96 return 0; | |
97 } | |
98 #endif | |
99 | |
100 int nasm_memicmp(const char *s1, const char *s2, size_t n) | |
101 { | |
102 unsigned char c1, c2; | |
103 int d; | |
104 | |
105 while (n--) { | |
106 c1 = nasm_tolower(*s1++); | |
107 c2 = nasm_tolower(*s2++); | |
108 d = c1-c2; | |
109 if (d) | |
110 return d; | |
111 } | |
112 return 0; | |
113 } | |
114 | |
115 #ifndef nasm_strsep | |
116 char *nasm_strsep(char **stringp, const char *delim) | |
117 { | |
118 char *s = *stringp; | |
119 char *e; | |
120 | |
121 if (!s) | |
122 return NULL; | |
123 | |
124 e = strpbrk(s, delim); | |
125 if (e) | |
126 *e++ = '\0'; | |
127 | |
128 *stringp = e; | |
129 return s; | |
130 } | |
131 #endif | |
132 | |
133 /* skip leading spaces */ | |
134 char *nasm_skip_spaces(const char *p) | |
135 { | |
136 if (p) | |
137 while (*p && nasm_isspace(*p)) | |
138 p++; | |
139 return (char *)p; | |
140 } | |
141 | |
142 /* skip leading non-spaces */ | |
143 char *nasm_skip_word(const char *p) | |
144 { | |
145 if (p) | |
146 while (*p && !nasm_isspace(*p)) | |
147 p++; | |
148 return (char *)p; | |
149 } | |
150 | |
151 /* zap leading spaces with zero */ | |
152 char *nasm_zap_spaces_fwd(char *p) | |
153 { | |
154 if (p) | |
155 while (*p && nasm_isspace(*p)) | |
156 *p++ = 0x0; | |
157 return p; | |
158 } | |
159 | |
160 /* zap spaces with zero in reverse order */ | |
161 char *nasm_zap_spaces_rev(char *p) | |
162 { | |
163 if (p) | |
164 while (*p && nasm_isspace(*p)) | |
165 *p-- = 0x0; | |
166 return p; | |
167 } | |
168 | |
169 /* zap leading and trailing spaces */ | |
170 char *nasm_trim_spaces(char *p) | |
171 { | |
172 p = nasm_zap_spaces_fwd(p); | |
173 nasm_zap_spaces_fwd(nasm_skip_word(p)); | |
174 | |
175 return p; | |
176 } | |
177 | |
178 /* | |
179 * return the word extracted from a stream | |
180 * or NULL if nothing left | |
181 */ | |
182 char *nasm_get_word(char *p, char **tail) | |
183 { | |
184 char *word = nasm_skip_spaces(p); | |
185 char *next = nasm_skip_word(word); | |
186 | |
187 if (word && *word) { | |
188 if (*next) | |
189 *next++ = '\0'; | |
190 } else | |
191 word = next = NULL; | |
192 | |
193 /* NOTE: the tail may start with spaces */ | |
194 *tail = next; | |
195 | |
196 return word; | |
197 } | |
198 | |
199 /* | |
200 * Extract "opt=val" values from the stream and | |
201 * returns "opt" | |
202 * | |
203 * Exceptions: | |
204 * 1) If "=val" passed the NULL returned though | |
205 * you may continue handling the tail via "next" | |
206 * 2) If "=" passed the NULL is returned and "val" | |
207 * is set to NULL as well | |
208 */ | |
209 char *nasm_opt_val(char *p, char **val, char **next) | |
210 { | |
211 char *q, *nxt; | |
212 | |
213 *val = *next = NULL; | |
214 | |
215 p = nasm_get_word(p, &nxt); | |
216 if (!p) | |
217 return NULL; | |
218 | |
219 q = strchr(p, '='); | |
220 if (q) { | |
221 if (q == p) | |
222 p = NULL; | |
223 *q++='\0'; | |
224 if (*q) { | |
225 *val = q; | |
226 } else { | |
227 q = nasm_get_word(q + 1, &nxt); | |
228 if (q) | |
229 *val = q; | |
230 } | |
231 } else { | |
232 q = nasm_skip_spaces(nxt); | |
233 if (q && *q == '=') { | |
234 q = nasm_get_word(q + 1, &nxt); | |
235 if (q) | |
236 *val = q; | |
237 } | |
238 } | |
239 | |
240 *next = nxt; | |
241 return p; | |
242 } |