C files should #include only the header files they need, not
[bpt/guile.git] / libguile / strop.c
1 /* classes: src_files */
2
3 /* Copyright (C) 1994 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this software; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18
19 \f
20
21 #include <stdio.h>
22 #include "_scm.h"
23 #include "chars.h"
24
25 #include "strop.h"
26 \f
27
28 #ifdef __STDC__
29 int
30 scm_i_index (SCM * str, SCM chr, SCM sub_start, SCM sub_end, int pos, int pos2, int pos3, int pos4, char * why)
31 #else
32 int
33 scm_i_index (str, chr, sub_start, sub_end, pos, pos2, pos3, pos4, why)
34 SCM * str;
35 SCM chr;
36 SCM sub_start;
37 SCM sub_end;
38 int pos;
39 int pos2;
40 int pos3;
41 int pos4;
42 char * why;
43 #endif
44 {
45 unsigned char * p;
46 int x;
47 int bound;
48 int ch;
49
50 SCM_ASSERT (SCM_NIMP (*str) && SCM_ROSTRINGP (*str), *str, pos, why);
51 SCM_ASSERT (SCM_ICHRP (chr), chr, pos2, why);
52
53 if (sub_start == SCM_BOOL_F)
54 sub_start = SCM_MAKINUM (0);
55 else
56 SCM_ASSERT ( SCM_INUMP (sub_start)
57 && (0 <= SCM_INUM (sub_start))
58 && (SCM_INUM (sub_start) <= SCM_ROLENGTH (*str)),
59 sub_start, pos3, why);
60
61 if (sub_end == SCM_BOOL_F)
62 sub_end = SCM_MAKINUM (SCM_ROLENGTH (*str));
63 else
64 SCM_ASSERT ( SCM_INUMP (sub_end)
65 && (SCM_INUM (sub_start) <= SCM_INUM (sub_end))
66 && (SCM_INUM (sub_end) <= SCM_ROLENGTH (*str)),
67 sub_end, pos4, why);
68
69 p = (unsigned char *)SCM_ROCHARS (*str) + SCM_INUM (sub_start);
70 bound = SCM_INUM (sub_end);
71 ch = SCM_ICHR (chr);
72
73 for (x = SCM_INUM (sub_start); x < bound; ++x, ++p)
74 if (*p == ch)
75 return x;
76
77 return -1;
78 }
79
80 #ifdef __STDC__
81 int
82 scm_i_rindex (SCM * str, SCM chr, SCM sub_start, SCM sub_end, int pos, int pos2, int pos3, int pos4, char * why)
83 #else
84 int
85 scm_i_rindex (str, chr, sub_start, sub_end, pos, pos2, pos3, pos4, why)
86 SCM * str;
87 SCM chr;
88 SCM sub_start;
89 SCM sub_end;
90 int pos;
91 int pos2;
92 int pos3;
93 int pos4;
94 char * why;
95 #endif
96 {
97 unsigned char * p;
98 int x;
99 int upper_bound;
100 int lower_bound;
101 int ch;
102
103 SCM_ASSERT (SCM_NIMP (*str) && SCM_ROSTRINGP (*str), *str, pos, why);
104 SCM_ASSERT (SCM_ICHRP (chr), chr, pos2, why);
105
106 if (sub_start == SCM_BOOL_F)
107 sub_start = SCM_MAKINUM (0);
108 else
109 SCM_ASSERT ( SCM_INUMP (sub_start)
110 && (0 <= SCM_INUM (sub_start))
111 && (SCM_INUM (sub_start) <= SCM_ROLENGTH (*str)),
112 sub_start, pos3, why);
113
114 if (sub_end == SCM_BOOL_F)
115 sub_end = SCM_MAKINUM (SCM_ROLENGTH (*str));
116 else
117 SCM_ASSERT ( SCM_INUMP (sub_end)
118 && (SCM_INUM (sub_start) <= SCM_INUM (sub_end))
119 && (SCM_INUM (sub_end) <= SCM_ROLENGTH (*str)),
120 sub_end, pos4, why);
121
122 upper_bound = SCM_INUM (sub_end);
123 lower_bound = SCM_INUM (sub_start);
124 p = upper_bound - 1 + (unsigned char *)SCM_ROCHARS (*str);
125 ch = SCM_ICHR (chr);
126 for (x = upper_bound - 1; x >= lower_bound; --x, --p)
127 if (*p == ch)
128 return x;
129
130 return -1;
131 }
132
133
134 SCM_PROC(s_string_index, "string-index", 2, 2, 0, scm_string_index);
135 #ifdef __STDC__
136 SCM
137 scm_string_index (SCM str, SCM chr, SCM frm, SCM to)
138 #else
139 SCM
140 scm_string_index (str, chr, frm, to)
141 SCM str;
142 SCM chr;
143 SCM frm;
144 SCM to;
145 #endif
146 {
147 int pos;
148
149 if (frm == SCM_UNDEFINED)
150 frm = SCM_BOOL_F;
151 if (to == SCM_UNDEFINED)
152 to = SCM_BOOL_F;
153 pos = scm_i_index (&str, chr, frm, to, SCM_ARG1, SCM_ARG2, SCM_ARG3, SCM_ARG4, s_string_index);
154 return (pos < 0
155 ? SCM_BOOL_F
156 : SCM_MAKINUM (pos));
157 }
158
159 SCM_PROC(s_string_rindex, "string-rindex", 2, 2, 0, scm_string_rindex);
160 #ifdef __STDC__
161 SCM
162 scm_string_rindex (SCM str, SCM chr, SCM frm, SCM to)
163 #else
164 SCM
165 scm_string_rindex (str, chr, frm, to)
166 SCM str;
167 SCM chr;
168 SCM frm;
169 SCM to;
170 #endif
171 {
172 int pos;
173
174 if (frm == SCM_UNDEFINED)
175 frm = SCM_BOOL_F;
176 if (to == SCM_UNDEFINED)
177 to = SCM_BOOL_F;
178 pos = scm_i_rindex (&str, chr, frm, to, SCM_ARG1, SCM_ARG2, SCM_ARG3, SCM_ARG4, s_string_index);
179 return (pos < 0
180 ? SCM_BOOL_F
181 : SCM_MAKINUM (pos));
182 }
183
184
185
186
187
188
189 SCM_PROC(s_substring_move_left_x, "substring-move-left!", 2, 0, 1, scm_substring_move_left_x);
190 #ifdef __STDC__
191 SCM
192 scm_substring_move_left_x (SCM str1, SCM start1, SCM args)
193 #else
194 SCM
195 scm_substring_move_left_x (str1, start1, args)
196 SCM str1;
197 SCM start1;
198 SCM args;
199 #endif
200 {
201 SCM end1, str2, start2;
202 long i, j, e;
203 SCM_ASSERT (3==scm_ilength (args), args, SCM_WNA, s_substring_move_left_x);
204 end1 = SCM_CAR (args); args = SCM_CDR (args);
205 str2 = SCM_CAR (args); args = SCM_CDR (args);
206 start2 = SCM_CAR (args);
207 SCM_ASSERT (SCM_NIMP (str1) && SCM_STRINGP (str1), str1, SCM_ARG1, s_substring_move_left_x);
208 SCM_ASSERT (SCM_INUMP (start1), start1, SCM_ARG2, s_substring_move_left_x);
209 SCM_ASSERT (SCM_INUMP (end1), end1, SCM_ARG3, s_substring_move_left_x);
210 SCM_ASSERT (SCM_NIMP (str2) && SCM_STRINGP (str2), str2, SCM_ARG4, s_substring_move_left_x);
211 SCM_ASSERT (SCM_INUMP (start2), start2, SCM_ARG5, s_substring_move_left_x);
212 i = SCM_INUM (start1), j = SCM_INUM (start2), e = SCM_INUM (end1);
213 SCM_ASSERT (i <= SCM_LENGTH (str1) && i >= 0, start1, SCM_OUTOFRANGE, s_substring_move_left_x);
214 SCM_ASSERT (j <= SCM_LENGTH (str2) && j >= 0, start2, SCM_OUTOFRANGE, s_substring_move_left_x);
215 SCM_ASSERT (e <= SCM_LENGTH (str1) && e >= 0, end1, SCM_OUTOFRANGE, s_substring_move_left_x);
216 SCM_ASSERT (e-i+j <= SCM_LENGTH (str2), start2, SCM_OUTOFRANGE, s_substring_move_left_x);
217 while (i<e) SCM_CHARS (str2)[j++] = SCM_CHARS (str1)[i++];
218 return SCM_UNSPECIFIED;
219 }
220
221
222 SCM_PROC(s_substring_move_right_x, "substring-move-right!", 2, 0, 1, scm_substring_move_right_x);
223 #ifdef __STDC__
224 SCM
225 scm_substring_move_right_x (SCM str1, SCM start1, SCM args)
226 #else
227 SCM
228 scm_substring_move_right_x (str1, start1, args)
229 SCM str1;
230 SCM start1;
231 SCM args;
232 #endif
233 {
234 SCM end1, str2, start2;
235 long i, j, e;
236 SCM_ASSERT (3==scm_ilength (args), args, SCM_WNA, s_substring_move_right_x);
237 end1 = SCM_CAR (args); args = SCM_CDR (args);
238 str2 = SCM_CAR (args); args = SCM_CDR (args);
239 start2 = SCM_CAR (args);
240 SCM_ASSERT (SCM_NIMP (str1) && SCM_STRINGP (str1), str1, SCM_ARG1, s_substring_move_right_x);
241 SCM_ASSERT (SCM_INUMP (start1), start1, SCM_ARG2, s_substring_move_right_x);
242 SCM_ASSERT (SCM_INUMP (end1), end1, SCM_ARG3, s_substring_move_right_x);
243 SCM_ASSERT (SCM_NIMP (str2) && SCM_STRINGP (str2), str2, SCM_ARG4, s_substring_move_right_x);
244 SCM_ASSERT (SCM_INUMP (start2), start2, SCM_ARG5, s_substring_move_right_x);
245 i = SCM_INUM (start1), j = SCM_INUM (start2), e = SCM_INUM (end1);
246 SCM_ASSERT (i <= SCM_LENGTH (str1) && i >= 0, start1, SCM_OUTOFRANGE, s_substring_move_right_x);
247 SCM_ASSERT (j <= SCM_LENGTH (str2) && j >= 0, start2, SCM_OUTOFRANGE, s_substring_move_right_x);
248 SCM_ASSERT (e <= SCM_LENGTH (str1) && e >= 0, end1, SCM_OUTOFRANGE, s_substring_move_right_x);
249 SCM_ASSERT ((j = e-i+j) <= SCM_LENGTH (str2), start2, SCM_OUTOFRANGE, s_substring_move_right_x);
250 while (i<e) SCM_CHARS (str2)[--j] = SCM_CHARS (str1)[--e];
251 return SCM_UNSPECIFIED;
252 }
253
254
255 SCM_PROC(s_substring_fill_x, "substring-fill!", 2, 0, 1, scm_substring_fill_x);
256 #ifdef __STDC__
257 SCM
258 scm_substring_fill_x (SCM str, SCM start, SCM args)
259 #else
260 SCM
261 scm_substring_fill_x (str, start, args)
262 SCM str;
263 SCM start;
264 SCM args;
265 #endif
266 {
267 SCM end, fill;
268 long i, e;
269 char c;
270 SCM_ASSERT (2==scm_ilength (args), args, SCM_WNA, s_substring_fill_x);
271 end = SCM_CAR (args); args = SCM_CDR (args);
272 fill = SCM_CAR (args);
273 SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str), str, SCM_ARG1, s_substring_fill_x);
274 SCM_ASSERT (SCM_INUMP (start), start, SCM_ARG2, s_substring_fill_x);
275 SCM_ASSERT (SCM_INUMP (end), end, SCM_ARG3, s_substring_fill_x);
276 SCM_ASSERT (SCM_ICHRP (fill), fill, SCM_ARG4, s_substring_fill_x);
277 i = SCM_INUM (start), e = SCM_INUM (end);c = SCM_ICHR (fill);
278 SCM_ASSERT (i <= SCM_LENGTH (str) && i >= 0, start, SCM_OUTOFRANGE, s_substring_fill_x);
279 SCM_ASSERT (e <= SCM_LENGTH (str) && e >= 0, end, SCM_OUTOFRANGE, s_substring_fill_x);
280 while (i<e) SCM_CHARS (str)[i++] = c;
281 return SCM_UNSPECIFIED;
282 }
283
284
285 SCM_PROC(s_string_null_p, "string-null?", 1, 0, 0, scm_string_null_p);
286 #ifdef __STDC__
287 SCM
288 scm_string_null_p (SCM str)
289 #else
290 SCM
291 scm_string_null_p (str)
292 SCM str;
293 #endif
294 {
295 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_string_null_p);
296 return (SCM_ROLENGTH (str)
297 ? SCM_BOOL_F
298 : SCM_BOOL_T);
299 }
300
301
302 SCM_PROC(s_string_to_list, "string->list", 1, 0, 0, scm_string_to_list);
303 #ifdef __STDC__
304 SCM
305 scm_string_to_list (SCM str)
306 #else
307 SCM
308 scm_string_to_list (str)
309 SCM str;
310 #endif
311 {
312 long i;
313 SCM res = SCM_EOL;
314 unsigned char *src;
315 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_string_to_list);
316 src = SCM_ROUCHARS (str);
317 for (i = SCM_ROLENGTH (str)-1;i >= 0;i--) res = scm_cons ((SCM)SCM_MAKICHR (src[i]), res);
318 return res;
319 }
320
321
322
323 SCM_PROC(s_string_copy, "string-copy", 1, 0, 0, scm_string_copy);
324 #ifdef __STDC__
325 SCM
326 scm_string_copy (SCM str)
327 #else
328 SCM
329 scm_string_copy (str)
330 SCM str;
331 #endif
332 {
333 SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str), str, SCM_ARG1, s_string_copy);
334 return scm_makfromstr (SCM_CHARS (str), (scm_sizet)SCM_LENGTH (str), 0);
335 }
336
337
338 SCM_PROC(s_string_fill_x, "string-fill!", 2, 0, 0, scm_string_fill_x);
339 #ifdef __STDC__
340 SCM
341 scm_string_fill_x (SCM str, SCM chr)
342 #else
343 SCM
344 scm_string_fill_x (str, chr)
345 SCM str;
346 SCM chr;
347 #endif
348 {
349 register char *dst, c;
350 register long k;
351 SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str), str, SCM_ARG1, s_string_fill_x);
352 SCM_ASSERT (SCM_ICHRP (chr), chr, SCM_ARG2, s_string_fill_x);
353 c = SCM_ICHR (chr);
354 dst = SCM_CHARS (str);
355 for (k = SCM_LENGTH (str)-1;k >= 0;k--) dst[k] = c;
356 return SCM_UNSPECIFIED;
357 }
358
359
360 #ifdef __STDC__
361 void
362 scm_init_strop (void)
363 #else
364 void
365 scm_init_strop ()
366 #endif
367 {
368 #include "strop.x"
369 }
370