maintainer changed: was lord, now jimb; first import
[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
24 \f
25
26 #ifdef __STDC__
27 int
28 scm_i_index (SCM * str, SCM chr, SCM sub_start, SCM sub_end, int pos, int pos2, int pos3, int pos4, char * why)
29 #else
30 int
31 scm_i_index (str, chr, sub_start, sub_end, pos, pos2, pos3, pos4, why)
32 SCM * str;
33 SCM chr;
34 SCM sub_start;
35 SCM sub_end;
36 int pos;
37 int pos2;
38 int pos3;
39 int pos4;
40 char * why;
41 #endif
42 {
43 unsigned char * p;
44 int x;
45 int bound;
46 int ch;
47
48 SCM_ASSERT (SCM_NIMP (*str) && SCM_ROSTRINGP (*str), *str, pos, why);
49 SCM_ASSERT (SCM_ICHRP (chr), chr, pos2, why);
50
51 if (sub_start == SCM_BOOL_F)
52 sub_start = SCM_MAKINUM (0);
53 else
54 SCM_ASSERT ( SCM_INUMP (sub_start)
55 && (0 <= SCM_INUM (sub_start))
56 && (SCM_INUM (sub_start) <= SCM_ROLENGTH (*str)),
57 sub_start, pos3, why);
58
59 if (sub_end == SCM_BOOL_F)
60 sub_end = SCM_MAKINUM (SCM_ROLENGTH (*str));
61 else
62 SCM_ASSERT ( SCM_INUMP (sub_end)
63 && (SCM_INUM (sub_start) <= SCM_INUM (sub_end))
64 && (SCM_INUM (sub_end) <= SCM_ROLENGTH (*str)),
65 sub_end, pos4, why);
66
67 p = (unsigned char *)SCM_ROCHARS (*str) + SCM_INUM (sub_start);
68 bound = SCM_INUM (sub_end);
69 ch = SCM_ICHR (chr);
70
71 for (x = SCM_INUM (sub_start); x < bound; ++x, ++p)
72 if (*p == ch)
73 return x;
74
75 return -1;
76 }
77
78 #ifdef __STDC__
79 int
80 scm_i_rindex (SCM * str, SCM chr, SCM sub_start, SCM sub_end, int pos, int pos2, int pos3, int pos4, char * why)
81 #else
82 int
83 scm_i_rindex (str, chr, sub_start, sub_end, pos, pos2, pos3, pos4, why)
84 SCM * str;
85 SCM chr;
86 SCM sub_start;
87 SCM sub_end;
88 int pos;
89 int pos2;
90 int pos3;
91 int pos4;
92 char * why;
93 #endif
94 {
95 unsigned char * p;
96 int x;
97 int upper_bound;
98 int lower_bound;
99 int ch;
100
101 SCM_ASSERT (SCM_NIMP (*str) && SCM_ROSTRINGP (*str), *str, pos, why);
102 SCM_ASSERT (SCM_ICHRP (chr), chr, pos2, why);
103
104 if (sub_start == SCM_BOOL_F)
105 sub_start = SCM_MAKINUM (0);
106 else
107 SCM_ASSERT ( SCM_INUMP (sub_start)
108 && (0 <= SCM_INUM (sub_start))
109 && (SCM_INUM (sub_start) <= SCM_ROLENGTH (*str)),
110 sub_start, pos3, why);
111
112 if (sub_end == SCM_BOOL_F)
113 sub_end = SCM_MAKINUM (SCM_ROLENGTH (*str));
114 else
115 SCM_ASSERT ( SCM_INUMP (sub_end)
116 && (SCM_INUM (sub_start) <= SCM_INUM (sub_end))
117 && (SCM_INUM (sub_end) <= SCM_ROLENGTH (*str)),
118 sub_end, pos4, why);
119
120 upper_bound = SCM_INUM (sub_end);
121 lower_bound = SCM_INUM (sub_start);
122 p = upper_bound - 1 + (unsigned char *)SCM_ROCHARS (*str);
123 ch = SCM_ICHR (chr);
124 for (x = upper_bound - 1; x >= lower_bound; --x, --p)
125 if (*p == ch)
126 return x;
127
128 return -1;
129 }
130
131
132 SCM_PROC(s_string_index, "string-index", 2, 2, 0, scm_string_index);
133 #ifdef __STDC__
134 SCM
135 scm_string_index (SCM str, SCM chr, SCM frm, SCM to)
136 #else
137 SCM
138 scm_string_index (str, chr, frm, to)
139 SCM str;
140 SCM chr;
141 SCM frm;
142 SCM to;
143 #endif
144 {
145 int pos;
146
147 if (frm == SCM_UNDEFINED)
148 frm = SCM_BOOL_F;
149 if (to == SCM_UNDEFINED)
150 to = SCM_BOOL_F;
151 pos = scm_i_index (&str, chr, frm, to, SCM_ARG1, SCM_ARG2, SCM_ARG3, SCM_ARG4, s_string_index);
152 return (pos < 0
153 ? SCM_BOOL_F
154 : SCM_MAKINUM (pos));
155 }
156
157 SCM_PROC(s_string_rindex, "string-rindex", 2, 2, 0, scm_string_rindex);
158 #ifdef __STDC__
159 SCM
160 scm_string_rindex (SCM str, SCM chr, SCM frm, SCM to)
161 #else
162 SCM
163 scm_string_rindex (str, chr, frm, to)
164 SCM str;
165 SCM chr;
166 SCM frm;
167 SCM to;
168 #endif
169 {
170 int pos;
171
172 if (frm == SCM_UNDEFINED)
173 frm = SCM_BOOL_F;
174 if (to == SCM_UNDEFINED)
175 to = SCM_BOOL_F;
176 pos = scm_i_rindex (&str, chr, frm, to, SCM_ARG1, SCM_ARG2, SCM_ARG3, SCM_ARG4, s_string_index);
177 return (pos < 0
178 ? SCM_BOOL_F
179 : SCM_MAKINUM (pos));
180 }
181
182
183
184
185
186
187 SCM_PROC(s_substring_move_left_x, "substring-move-left!", 2, 0, 1, scm_substring_move_left_x);
188 #ifdef __STDC__
189 SCM
190 scm_substring_move_left_x (SCM str1, SCM start1, SCM args)
191 #else
192 SCM
193 scm_substring_move_left_x (str1, start1, args)
194 SCM str1;
195 SCM start1;
196 SCM args;
197 #endif
198 {
199 SCM end1, str2, start2;
200 long i, j, e;
201 SCM_ASSERT (3==scm_ilength (args), args, SCM_WNA, s_substring_move_left_x);
202 end1 = SCM_CAR (args); args = SCM_CDR (args);
203 str2 = SCM_CAR (args); args = SCM_CDR (args);
204 start2 = SCM_CAR (args);
205 SCM_ASSERT (SCM_NIMP (str1) && SCM_STRINGP (str1), str1, SCM_ARG1, s_substring_move_left_x);
206 SCM_ASSERT (SCM_INUMP (start1), start1, SCM_ARG2, s_substring_move_left_x);
207 SCM_ASSERT (SCM_INUMP (end1), end1, SCM_ARG3, s_substring_move_left_x);
208 SCM_ASSERT (SCM_NIMP (str2) && SCM_STRINGP (str2), str2, SCM_ARG4, s_substring_move_left_x);
209 SCM_ASSERT (SCM_INUMP (start2), start2, SCM_ARG5, s_substring_move_left_x);
210 i = SCM_INUM (start1), j = SCM_INUM (start2), e = SCM_INUM (end1);
211 SCM_ASSERT (i <= SCM_LENGTH (str1) && i >= 0, start1, SCM_OUTOFRANGE, s_substring_move_left_x);
212 SCM_ASSERT (j <= SCM_LENGTH (str2) && j >= 0, start2, SCM_OUTOFRANGE, s_substring_move_left_x);
213 SCM_ASSERT (e <= SCM_LENGTH (str1) && e >= 0, end1, SCM_OUTOFRANGE, s_substring_move_left_x);
214 SCM_ASSERT (e-i+j <= SCM_LENGTH (str2), start2, SCM_OUTOFRANGE, s_substring_move_left_x);
215 while (i<e) SCM_CHARS (str2)[j++] = SCM_CHARS (str1)[i++];
216 return SCM_UNSPECIFIED;
217 }
218
219
220 SCM_PROC(s_substring_move_right_x, "substring-move-right!", 2, 0, 1, scm_substring_move_right_x);
221 #ifdef __STDC__
222 SCM
223 scm_substring_move_right_x (SCM str1, SCM start1, SCM args)
224 #else
225 SCM
226 scm_substring_move_right_x (str1, start1, args)
227 SCM str1;
228 SCM start1;
229 SCM args;
230 #endif
231 {
232 SCM end1, str2, start2;
233 long i, j, e;
234 SCM_ASSERT (3==scm_ilength (args), args, SCM_WNA, s_substring_move_right_x);
235 end1 = SCM_CAR (args); args = SCM_CDR (args);
236 str2 = SCM_CAR (args); args = SCM_CDR (args);
237 start2 = SCM_CAR (args);
238 SCM_ASSERT (SCM_NIMP (str1) && SCM_STRINGP (str1), str1, SCM_ARG1, s_substring_move_right_x);
239 SCM_ASSERT (SCM_INUMP (start1), start1, SCM_ARG2, s_substring_move_right_x);
240 SCM_ASSERT (SCM_INUMP (end1), end1, SCM_ARG3, s_substring_move_right_x);
241 SCM_ASSERT (SCM_NIMP (str2) && SCM_STRINGP (str2), str2, SCM_ARG4, s_substring_move_right_x);
242 SCM_ASSERT (SCM_INUMP (start2), start2, SCM_ARG5, s_substring_move_right_x);
243 i = SCM_INUM (start1), j = SCM_INUM (start2), e = SCM_INUM (end1);
244 SCM_ASSERT (i <= SCM_LENGTH (str1) && i >= 0, start1, SCM_OUTOFRANGE, s_substring_move_right_x);
245 SCM_ASSERT (j <= SCM_LENGTH (str2) && j >= 0, start2, SCM_OUTOFRANGE, s_substring_move_right_x);
246 SCM_ASSERT (e <= SCM_LENGTH (str1) && e >= 0, end1, SCM_OUTOFRANGE, s_substring_move_right_x);
247 SCM_ASSERT ((j = e-i+j) <= SCM_LENGTH (str2), start2, SCM_OUTOFRANGE, s_substring_move_right_x);
248 while (i<e) SCM_CHARS (str2)[--j] = SCM_CHARS (str1)[--e];
249 return SCM_UNSPECIFIED;
250 }
251
252
253 SCM_PROC(s_substring_fill_x, "substring-fill!", 2, 0, 1, scm_substring_fill_x);
254 #ifdef __STDC__
255 SCM
256 scm_substring_fill_x (SCM str, SCM start, SCM args)
257 #else
258 SCM
259 scm_substring_fill_x (str, start, args)
260 SCM str;
261 SCM start;
262 SCM args;
263 #endif
264 {
265 SCM end, fill;
266 long i, e;
267 char c;
268 SCM_ASSERT (2==scm_ilength (args), args, SCM_WNA, s_substring_fill_x);
269 end = SCM_CAR (args); args = SCM_CDR (args);
270 fill = SCM_CAR (args);
271 SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str), str, SCM_ARG1, s_substring_fill_x);
272 SCM_ASSERT (SCM_INUMP (start), start, SCM_ARG2, s_substring_fill_x);
273 SCM_ASSERT (SCM_INUMP (end), end, SCM_ARG3, s_substring_fill_x);
274 SCM_ASSERT (SCM_ICHRP (fill), fill, SCM_ARG4, s_substring_fill_x);
275 i = SCM_INUM (start), e = SCM_INUM (end);c = SCM_ICHR (fill);
276 SCM_ASSERT (i <= SCM_LENGTH (str) && i >= 0, start, SCM_OUTOFRANGE, s_substring_fill_x);
277 SCM_ASSERT (e <= SCM_LENGTH (str) && e >= 0, end, SCM_OUTOFRANGE, s_substring_fill_x);
278 while (i<e) SCM_CHARS (str)[i++] = c;
279 return SCM_UNSPECIFIED;
280 }
281
282
283 SCM_PROC(s_string_null_p, "string-null?", 1, 0, 0, scm_string_null_p);
284 #ifdef __STDC__
285 SCM
286 scm_string_null_p (SCM str)
287 #else
288 SCM
289 scm_string_null_p (str)
290 SCM str;
291 #endif
292 {
293 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_string_null_p);
294 return (SCM_ROLENGTH (str)
295 ? SCM_BOOL_F
296 : SCM_BOOL_T);
297 }
298
299
300 SCM_PROC(s_string_to_list, "string->list", 1, 0, 0, scm_string_to_list);
301 #ifdef __STDC__
302 SCM
303 scm_string_to_list (SCM str)
304 #else
305 SCM
306 scm_string_to_list (str)
307 SCM str;
308 #endif
309 {
310 long i;
311 SCM res = SCM_EOL;
312 unsigned char *src;
313 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_string_to_list);
314 src = SCM_ROUCHARS (str);
315 for (i = SCM_ROLENGTH (str)-1;i >= 0;i--) res = scm_cons ((SCM)SCM_MAKICHR (src[i]), res);
316 return res;
317 }
318
319
320
321 SCM_PROC(s_string_copy, "string-copy", 1, 0, 0, scm_string_copy);
322 #ifdef __STDC__
323 SCM
324 scm_string_copy (SCM str)
325 #else
326 SCM
327 scm_string_copy (str)
328 SCM str;
329 #endif
330 {
331 SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str), str, SCM_ARG1, s_string_copy);
332 return scm_makfromstr (SCM_CHARS (str), (scm_sizet)SCM_LENGTH (str), 0);
333 }
334
335
336 SCM_PROC(s_string_fill_x, "string-fill!", 2, 0, 0, scm_string_fill_x);
337 #ifdef __STDC__
338 SCM
339 scm_string_fill_x (SCM str, SCM chr)
340 #else
341 SCM
342 scm_string_fill_x (str, chr)
343 SCM str;
344 SCM chr;
345 #endif
346 {
347 register char *dst, c;
348 register long k;
349 SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str), str, SCM_ARG1, s_string_fill_x);
350 SCM_ASSERT (SCM_ICHRP (chr), chr, SCM_ARG2, s_string_fill_x);
351 c = SCM_ICHR (chr);
352 dst = SCM_CHARS (str);
353 for (k = SCM_LENGTH (str)-1;k >= 0;k--) dst[k] = c;
354 return SCM_UNSPECIFIED;
355 }
356
357
358 #ifdef __STDC__
359 void
360 scm_init_strop (void)
361 #else
362 void
363 scm_init_strop ()
364 #endif
365 {
366 #include "strop.x"
367 }
368