* ioext.c (scm_do_read_line): Rewritten to use memchr to find the
[bpt/guile.git] / libguile / strings.c
1 /* Copyright (C) 1995,1996,1998 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41 \f
42
43 #include <stdio.h>
44 #include "_scm.h"
45 #include "chars.h"
46
47 #include "strings.h"
48 \f
49
50 /* {Strings}
51 */
52
53 SCM_PROC(s_string_p, "string?", 1, 0, 0, scm_string_p);
54
55 SCM
56 scm_string_p (x)
57 SCM x;
58 {
59 if (SCM_IMP (x))
60 return SCM_BOOL_F;
61 return SCM_STRINGP (x) ? SCM_BOOL_T : SCM_BOOL_F;
62 }
63
64 SCM_PROC(s_read_only_string_p, "read-only-string?", 1, 0, 0, scm_read_only_string_p);
65
66 SCM
67 scm_read_only_string_p (x)
68 SCM x;
69 {
70 if (SCM_IMP (x))
71 return SCM_BOOL_F;
72 return SCM_ROSTRINGP (x) ? SCM_BOOL_T : SCM_BOOL_F;
73 }
74
75 SCM_PROC(s_list_to_string, "list->string", 1, 0, 0, scm_string);
76 SCM_PROC(s_string, "string", 0, 0, 1, scm_string);
77
78 SCM
79 scm_string (chrs)
80 SCM chrs;
81 {
82 SCM res;
83 register unsigned char *data;
84 long i;
85 long len;
86 SCM_DEFER_INTS;
87 i = scm_ilength (chrs);
88 if (i < 0)
89 {
90 SCM_ALLOW_INTS;
91 SCM_ASSERT (0, chrs, SCM_ARG1, s_string);
92 }
93 len = 0;
94 {
95 SCM s;
96
97 for (len = 0, s = chrs; s != SCM_EOL; s = SCM_CDR (s))
98 if (SCM_ICHRP (SCM_CAR (s)))
99 len += 1;
100 else if (SCM_NIMP (SCM_CAR (s)) && SCM_ROSTRINGP (SCM_CAR (s)))
101 len += SCM_ROLENGTH (SCM_CAR (s));
102 else
103 {
104 SCM_ALLOW_INTS;
105 SCM_ASSERT (0, s, SCM_ARG1, s_string);
106 }
107 }
108 res = scm_makstr (len, 0);
109 data = SCM_UCHARS (res);
110 for (;SCM_NNULLP (chrs);chrs = SCM_CDR (chrs))
111 {
112 if (SCM_ICHRP (SCM_CAR (chrs)))
113 *data++ = SCM_ICHR (SCM_CAR (chrs));
114 else
115 {
116 int l;
117 char * c;
118 l = SCM_ROLENGTH (SCM_CAR (chrs));
119 c = SCM_ROCHARS (SCM_CAR (chrs));
120 while (l)
121 {
122 --l;
123 *data++ = *c++;
124 }
125 }
126 }
127 SCM_ALLOW_INTS;
128 return res;
129 }
130
131
132 SCM
133 scm_makstr (len, slots)
134 long len;
135 int slots;
136 {
137 SCM s;
138 SCM * mem;
139 SCM_NEWCELL (s);
140 --slots;
141 SCM_REDEFER_INTS;
142 mem = (SCM *)scm_must_malloc (sizeof (SCM) * (slots + 1) + len + 1,
143 s_string);
144 if (slots >= 0)
145 {
146 int x;
147 mem[slots] = (SCM)mem;
148 for (x = 0; x < slots; ++x)
149 mem[x] = SCM_BOOL_F;
150 }
151 SCM_SETCHARS (s, (char *) (mem + slots + 1));
152 SCM_SETLENGTH (s, len, scm_tc7_string);
153 SCM_REALLOW_INTS;
154 SCM_CHARS (s)[len] = 0;
155 return s;
156 }
157
158 /* converts C scm_array of strings to SCM scm_list of strings. */
159 /* If argc < 0, a null terminated scm_array is assumed. */
160
161 SCM
162 scm_makfromstrs (argc, argv)
163 int argc;
164 char **argv;
165 {
166 int i = argc;
167 SCM lst = SCM_EOL;
168 if (0 > i)
169 for (i = 0; argv[i]; i++);
170 while (i--)
171 lst = scm_cons (scm_makfromstr (argv[i], (scm_sizet) strlen (argv[i]), 0), lst);
172 return lst;
173 }
174
175
176 /* This function must only be applied to memory obtained via malloc,
177 since the GC is going to apply `free' to it when the string is
178 dropped.
179
180 Also, s[len] must be `\0', since we promise that strings are
181 null-terminated. Perhaps we could handle non-null-terminated
182 strings by claiming they're shared substrings of a string we just
183 made up. */
184 SCM
185 scm_take_str (char *s, int len)
186 {
187 SCM answer;
188 SCM_NEWCELL (answer);
189 SCM_DEFER_INTS;
190 SCM_SETLENGTH (answer, len, scm_tc7_string);
191 scm_done_malloc (len + 1);
192 SCM_SETCHARS (answer, s);
193 SCM_ALLOW_INTS;
194 return answer;
195 }
196
197 /* `s' must be a malloc'd string. See scm_take_str. */
198 SCM
199 scm_take0str (char *s)
200 {
201 return scm_take_str (s, strlen (s));
202 }
203
204
205 SCM
206 scm_makfromstr (src, len, slots)
207 const char *src;
208 scm_sizet len;
209 int slots;
210 {
211 SCM s;
212 register char *dst;
213 s = scm_makstr ((long) len, slots);
214 dst = SCM_CHARS (s);
215 while (len--)
216 *dst++ = *src++;
217 return s;
218 }
219
220
221
222 SCM
223 scm_makfrom0str (src)
224 const char *src;
225 {
226 if (!src) return SCM_BOOL_F;
227 return scm_makfromstr (src, (scm_sizet) strlen (src), 0);
228 }
229
230
231 SCM
232 scm_makfrom0str_opt (src)
233 const char *src;
234 {
235 return scm_makfrom0str (src);
236 }
237
238
239
240
241 SCM_PROC(s_make_string, "make-string", 1, 1, 0, scm_make_string);
242
243 SCM
244 scm_make_string (k, chr)
245 SCM k;
246 SCM chr;
247 {
248 SCM res;
249 register unsigned char *dst;
250 register long i;
251 SCM_ASSERT (SCM_INUMP (k) && (k >= 0), k, SCM_ARG1, s_make_string);
252 i = SCM_INUM (k);
253 res = scm_makstr (i, 0);
254 dst = SCM_UCHARS (res);
255 if SCM_ICHRP (chr)
256 {
257 char c = SCM_ICHR (chr);
258 for (i--;i >= 0;i--)
259 {
260 dst[i] = c;
261 }
262 }
263 return res;
264 }
265
266 SCM_PROC(s_string_length, "string-length", 1, 0, 0, scm_string_length);
267
268 SCM
269 scm_string_length (str)
270 SCM str;
271 {
272 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_string_length);
273 return SCM_MAKINUM (SCM_ROLENGTH (str));
274 }
275
276 SCM_PROC(s_string_ref, "string-ref", 1, 1, 0, scm_string_ref);
277
278 SCM
279 scm_string_ref (str, k)
280 SCM str;
281 SCM k;
282 {
283 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_string_ref);
284 if (k == SCM_UNDEFINED)
285 k = SCM_MAKINUM (0);
286 SCM_ASSERT (SCM_INUMP (k), k, SCM_ARG2, s_string_ref);
287 SCM_ASSERT (SCM_INUM (k) < SCM_ROLENGTH (str) && SCM_INUM (k) >= 0, k, SCM_OUTOFRANGE, s_string_ref);
288 return SCM_MAKICHR (SCM_ROUCHARS (str)[SCM_INUM (k)]);
289 }
290
291 SCM_PROC(s_string_set_x, "string-set!", 3, 0, 0, scm_string_set_x);
292
293 SCM
294 scm_string_set_x (str, k, chr)
295 SCM str;
296 SCM k;
297 SCM chr;
298 {
299 SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str),
300 str, SCM_ARG1, s_string_set_x);
301 SCM_ASSERT (SCM_INUMP (k), k, SCM_ARG2, s_string_set_x);
302 SCM_ASSERT (SCM_ICHRP (chr), chr, SCM_ARG3, s_string_set_x);
303 if (! SCM_RWSTRINGP (str))
304 scm_misc_error (s_string_set_x, "argument is a read-only string", str);
305 SCM_ASSERT ((SCM_INUM (k) >= 0
306 && ((unsigned) SCM_INUM (k)) < SCM_LENGTH (str)),
307 k, SCM_OUTOFRANGE, s_string_set_x);
308 SCM_UCHARS (str)[SCM_INUM (k)] = SCM_ICHR (chr);
309 return SCM_UNSPECIFIED;
310 }
311
312
313
314 SCM_PROC(s_substring, "substring", 2, 1, 0, scm_substring);
315
316 SCM
317 scm_substring (str, start, end)
318 SCM str;
319 SCM start;
320 SCM end;
321 {
322 long l;
323 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str),
324 str, SCM_ARG1, s_substring);
325 SCM_ASSERT (SCM_INUMP (start), start, SCM_ARG2, s_substring);
326 if (end == SCM_UNDEFINED)
327 end = SCM_MAKINUM (SCM_ROLENGTH (str));
328 SCM_ASSERT (SCM_INUMP (end), end, SCM_ARG3, s_substring);
329 SCM_ASSERT (SCM_INUM (start) <= SCM_ROLENGTH (str), start, SCM_OUTOFRANGE, s_substring);
330 SCM_ASSERT (SCM_INUM (end) <= SCM_ROLENGTH (str), end, SCM_OUTOFRANGE, s_substring);
331 l = SCM_INUM (end)-SCM_INUM (start);
332 SCM_ASSERT (l >= 0, SCM_MAKINUM (l), SCM_OUTOFRANGE, s_substring);
333 return scm_makfromstr (&SCM_ROCHARS (str)[SCM_INUM (start)], (scm_sizet)l, 0);
334 }
335
336 SCM_PROC(s_string_append, "string-append", 0, 0, 1, scm_string_append);
337
338 SCM
339 scm_string_append (args)
340 SCM args;
341 {
342 SCM res;
343 register long i = 0;
344 register SCM l, s;
345 register unsigned char *data;
346 for (l = args;SCM_NIMP (l);) {
347 SCM_ASSERT (SCM_CONSP (l), l, SCM_ARGn, s_string_append);
348 s = SCM_CAR (l);
349 SCM_ASSERT (SCM_NIMP (s) && SCM_ROSTRINGP (s),
350 s, SCM_ARGn, s_string_append);
351 i += SCM_ROLENGTH (s);
352 l = SCM_CDR (l);
353 }
354 SCM_ASSERT (SCM_NULLP (l), args, SCM_ARGn, s_string_append);
355 res = scm_makstr (i, 0);
356 data = SCM_UCHARS (res);
357 for (l = args;SCM_NIMP (l);l = SCM_CDR (l)) {
358 s = SCM_CAR (l);
359 for (i = 0;i<SCM_ROLENGTH (s);i++) *data++ = SCM_ROUCHARS (s)[i];
360 }
361 return res;
362 }
363
364 SCM_PROC(s_make_shared_substring, "make-shared-substring", 1, 2, 0, scm_make_shared_substring);
365
366 SCM
367 scm_make_shared_substring (str, frm, to)
368 SCM str;
369 SCM frm;
370 SCM to;
371 {
372 long f;
373 long t;
374 SCM answer;
375 SCM len_str;
376
377 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_make_shared_substring);
378
379 if (frm == SCM_UNDEFINED)
380 frm = SCM_MAKINUM (0);
381 else
382 SCM_ASSERT (SCM_INUMP (frm), frm, SCM_ARG2, s_make_shared_substring);
383
384 if (to == SCM_UNDEFINED)
385 to = SCM_MAKINUM (SCM_ROLENGTH (str));
386 else
387 SCM_ASSERT (SCM_INUMP (to), to, SCM_ARG3, s_make_shared_substring);
388
389 f = SCM_INUM (frm);
390 t = SCM_INUM (to);
391 SCM_ASSERT ((f >= 0), frm, SCM_OUTOFRANGE, s_make_shared_substring);
392 SCM_ASSERT ((f <= t) && (t <= SCM_ROLENGTH (str)), to, SCM_OUTOFRANGE,
393 s_make_shared_substring);
394
395 SCM_NEWCELL (answer);
396 SCM_NEWCELL (len_str);
397
398 SCM_DEFER_INTS;
399 if (SCM_SUBSTRP (str))
400 {
401 long offset;
402 offset = SCM_INUM (SCM_SUBSTR_OFFSET (str));
403 f += offset;
404 t += offset;
405 SCM_SETCAR (len_str, SCM_MAKINUM (f));
406 SCM_SETCDR (len_str, SCM_SUBSTR_STR (str));
407 SCM_SETCDR (answer, len_str);
408 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
409 }
410 else
411 {
412 SCM_SETCAR (len_str, SCM_MAKINUM (f));
413 SCM_SETCDR (len_str, str);
414 SCM_SETCDR (answer, len_str);
415 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
416 }
417 SCM_ALLOW_INTS;
418 return answer;
419 }
420
421
422 void
423 scm_init_strings ()
424 {
425 #include "strings.x"
426 }
427