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