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