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