* COPYING, boot-9.scm, debug.scm, emacs.scm, expect.scm, gtcl.scm,
[bpt/guile.git] / libguile / strings.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 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, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41\f
42
43#include <stdio.h>
44#include "_scm.h"
20e6290e 45#include "chars.h"
0f2d19dd 46
20e6290e 47#include "strings.h"
0f2d19dd
JB
48\f
49
50/* {Strings}
51 */
52
53SCM_PROC(s_string_p, "string?", 1, 0, 0, scm_string_p);
1cc91f1b 54
0f2d19dd
JB
55SCM
56scm_string_p (x)
57 SCM x;
0f2d19dd
JB
58{
59 if (SCM_IMP (x))
60 return SCM_BOOL_F;
61 return SCM_STRINGP (x) ? SCM_BOOL_T : SCM_BOOL_F;
62}
63
64SCM_PROC(s_read_only_string_p, "read-only-string?", 1, 0, 0, scm_read_only_string_p);
1cc91f1b 65
0f2d19dd
JB
66SCM
67scm_read_only_string_p (x)
68 SCM x;
0f2d19dd
JB
69{
70 if (SCM_IMP (x))
71 return SCM_BOOL_F;
72 return SCM_ROSTRINGP (x) ? SCM_BOOL_T : SCM_BOOL_F;
73}
74
75SCM_PROC(s_list_to_string, "list->string", 1, 0, 0, scm_string);
76SCM_PROC(s_string, "string", 0, 0, 1, scm_string);
1cc91f1b 77
0f2d19dd
JB
78SCM
79scm_string (chrs)
80 SCM chrs;
0f2d19dd
JB
81{
82 SCM res;
a65b9c80 83 register unsigned char *data;
0f2d19dd
JB
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);
a65b9c80 109 data = SCM_UCHARS (res);
0f2d19dd
JB
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));
a65b9c80 119 c = SCM_ROUCHARS (SCM_CAR (chrs));
0f2d19dd
JB
120 while (l)
121 {
122 --l;
123 *data++ = *c++;
124 }
125 }
126 }
127 SCM_ALLOW_INTS;
128 return res;
129}
130
1cc91f1b 131
0f2d19dd
JB
132SCM
133scm_makstr (len, slots)
134 long len;
135 int slots;
0f2d19dd
JB
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. */
1cc91f1b 160
0f2d19dd
JB
161SCM
162scm_makfromstrs (argc, argv)
163 int argc;
164 char **argv;
0f2d19dd
JB
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
1cc91f1b 176
0f2d19dd
JB
177SCM
178scm_take0str (it)
179 char * it;
0f2d19dd
JB
180{
181 SCM answer;
182 SCM_NEWCELL (answer);
183 SCM_DEFER_INTS;
184 SCM_SETLENGTH (answer, strlen (it), scm_tc7_string);
185 SCM_SETCHARS (answer, it);
186 SCM_ALLOW_INTS;
187 return answer;
188}
189
1cc91f1b 190
0f2d19dd
JB
191SCM
192scm_makfromstr (src, len, slots)
193 const char *src;
194 scm_sizet len;
195 int slots;
0f2d19dd
JB
196{
197 SCM s;
198 register char *dst;
199 s = scm_makstr ((long) len, slots);
200 dst = SCM_CHARS (s);
201 while (len--)
202 *dst++ = *src++;
203 return s;
204}
205
206
1cc91f1b 207
0f2d19dd
JB
208SCM
209scm_makfrom0str (src)
dbece3a2 210 const char *src;
0f2d19dd
JB
211{
212 if (!src) return SCM_BOOL_F;
213 return scm_makfromstr (src, (scm_sizet) strlen (src), 0);
214}
215
1cc91f1b 216
0f2d19dd
JB
217SCM
218scm_makfrom0str_opt (src)
dbece3a2 219 const char *src;
0f2d19dd
JB
220{
221 return scm_makfrom0str (src);
222}
223
224
225
226
227SCM_PROC(s_make_string, "make-string", 1, 1, 0, scm_make_string);
1cc91f1b 228
0f2d19dd
JB
229SCM
230scm_make_string (k, chr)
231 SCM k;
232 SCM chr;
0f2d19dd
JB
233{
234 SCM res;
a65b9c80 235 register unsigned char *dst;
0f2d19dd
JB
236 register long i;
237 SCM_ASSERT (SCM_INUMP (k) && (k >= 0), k, SCM_ARG1, s_make_string);
238 i = SCM_INUM (k);
239 res = scm_makstr (i, 0);
a65b9c80 240 dst = SCM_UCHARS (res);
0f2d19dd
JB
241 if SCM_ICHRP (chr)
242 {
243 char c = SCM_ICHR (chr);
244 for (i--;i >= 0;i--)
245 {
246 dst[i] = c;
247 }
248 }
249 return res;
250}
251
252SCM_PROC(s_string_length, "string-length", 1, 0, 0, scm_string_length);
1cc91f1b 253
0f2d19dd
JB
254SCM
255scm_string_length (str)
256 SCM str;
0f2d19dd
JB
257{
258 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_string_length);
259 return SCM_MAKINUM (SCM_ROLENGTH (str));
260}
261
262SCM_PROC(s_string_ref, "string-ref", 1, 1, 0, scm_string_ref);
1cc91f1b 263
0f2d19dd
JB
264SCM
265scm_string_ref (str, k)
266 SCM str;
267 SCM k;
0f2d19dd
JB
268{
269 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_string_ref);
270 if (k == SCM_UNDEFINED)
271 k = SCM_MAKINUM (0);
272 SCM_ASSERT (SCM_INUMP (k), k, SCM_ARG2, s_string_ref);
273 SCM_ASSERT (SCM_INUM (k) < SCM_ROLENGTH (str) && SCM_INUM (k) >= 0, k, SCM_OUTOFRANGE, s_string_ref);
a65b9c80 274 return SCM_MAKICHR (SCM_ROUCHARS (str)[SCM_INUM (k)]);
0f2d19dd
JB
275}
276
277SCM_PROC(s_string_set_x, "string-set!", 3, 0, 0, scm_string_set_x);
1cc91f1b 278
0f2d19dd
JB
279SCM
280scm_string_set_x (str, k, chr)
281 SCM str;
282 SCM k;
283 SCM chr;
0f2d19dd
JB
284{
285 SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str), str, SCM_ARG1, s_string_set_x);
286 SCM_ASSERT (SCM_INUMP (k), k, SCM_ARG2, s_string_set_x);
287 SCM_ASSERT (SCM_ICHRP (chr), chr, SCM_ARG3, s_string_set_x);
288 SCM_ASSERT (SCM_INUM (k) < SCM_LENGTH (str) && SCM_INUM (k) >= 0, k, SCM_OUTOFRANGE, s_string_set_x);
a65b9c80 289 SCM_UCHARS (str)[SCM_INUM (k)] = SCM_ICHR (chr);
0f2d19dd
JB
290 return SCM_UNSPECIFIED;
291}
292
293
294
295SCM_PROC(s_substring, "substring", 2, 1, 0, scm_substring);
1cc91f1b 296
0f2d19dd
JB
297SCM
298scm_substring (str, start, end)
299 SCM str;
300 SCM start;
301 SCM end;
0f2d19dd
JB
302{
303 long l;
304 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str),
305 str, SCM_ARG1, s_substring);
306 SCM_ASSERT (SCM_INUMP (start), start, SCM_ARG2, s_substring);
307 if (end == SCM_UNDEFINED)
308 end = SCM_MAKINUM (SCM_ROLENGTH (str));
309 SCM_ASSERT (SCM_INUMP (end), end, SCM_ARG3, s_substring);
310 SCM_ASSERT (SCM_INUM (start) <= SCM_ROLENGTH (str), start, SCM_OUTOFRANGE, s_substring);
311 SCM_ASSERT (SCM_INUM (end) <= SCM_ROLENGTH (str), end, SCM_OUTOFRANGE, s_substring);
312 l = SCM_INUM (end)-SCM_INUM (start);
313 SCM_ASSERT (l >= 0, SCM_MAKINUM (l), SCM_OUTOFRANGE, s_substring);
314 return scm_makfromstr (&SCM_ROCHARS (str)[SCM_INUM (start)], (scm_sizet)l, 0);
315}
316
317SCM_PROC(s_string_append, "string-append", 0, 0, 1, scm_string_append);
1cc91f1b 318
0f2d19dd
JB
319SCM
320scm_string_append (args)
321 SCM args;
0f2d19dd
JB
322{
323 SCM res;
324 register long i = 0;
325 register SCM l, s;
a65b9c80 326 register unsigned char *data;
0f2d19dd
JB
327 for (l = args;SCM_NIMP (l);) {
328 SCM_ASSERT (SCM_CONSP (l), l, SCM_ARGn, s_string_append);
329 s = SCM_CAR (l);
330 SCM_ASSERT (SCM_NIMP (s) && SCM_ROSTRINGP (s),
331 s, SCM_ARGn, s_string_append);
332 i += SCM_ROLENGTH (s);
333 l = SCM_CDR (l);
334 }
335 SCM_ASSERT (SCM_NULLP (l), args, SCM_ARGn, s_string_append);
336 res = scm_makstr (i, 0);
a65b9c80 337 data = SCM_UCHARS (res);
0f2d19dd
JB
338 for (l = args;SCM_NIMP (l);l = SCM_CDR (l)) {
339 s = SCM_CAR (l);
a65b9c80 340 for (i = 0;i<SCM_ROLENGTH (s);i++) *data++ = SCM_ROUCHARS (s)[i];
0f2d19dd
JB
341 }
342 return res;
343}
344
345SCM_PROC(s_make_shared_substring, "make-shared-substring", 1, 2, 0, scm_make_shared_substring);
1cc91f1b 346
0f2d19dd
JB
347SCM
348scm_make_shared_substring (str, frm, to)
349 SCM str;
350 SCM frm;
351 SCM to;
0f2d19dd
JB
352{
353 long f;
354 long t;
355 SCM answer;
356 SCM len_str;
357
358 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_make_shared_substring);
359
360 if (frm == SCM_UNDEFINED)
361 frm = SCM_MAKINUM (0);
362 else
363 SCM_ASSERT (SCM_INUMP (frm), frm, SCM_ARG2, s_make_shared_substring);
364
365 if (to == SCM_UNDEFINED)
366 to = SCM_MAKINUM (SCM_ROLENGTH (str));
367 else
368 SCM_ASSERT (SCM_INUMP (to), to, SCM_ARG3, s_make_shared_substring);
369
370 f = SCM_INUM (frm);
371 t = SCM_INUM (to);
372 SCM_ASSERT ((f >= 0), frm, SCM_OUTOFRANGE, s_make_shared_substring);
373 SCM_ASSERT ((f <= t) && (t <= SCM_ROLENGTH (str)), to, SCM_OUTOFRANGE, s_make_shared_substring);
374
375 SCM_NEWCELL (answer);
376 SCM_NEWCELL (len_str);
377
378 SCM_DEFER_INTS;
379 if (SCM_SUBSTRP (str))
380 {
381 long offset;
382 offset = SCM_INUM (SCM_SUBSTR_OFFSET (str));
383 f += offset;
384 t += offset;
385 SCM_SETCAR (len_str, SCM_MAKINUM (f));
386 SCM_SETCDR (len_str, SCM_SUBSTR_STR (str));
387 SCM_SETCDR (answer, len_str);
388 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
389 }
390 else
391 {
392 SCM_SETCAR (len_str, SCM_MAKINUM (f));
393 SCM_SETCDR (len_str, str);
394 SCM_SETCDR (answer, len_str);
395 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
396 }
397 SCM_ALLOW_INTS;
398 return answer;
399}
400
1cc91f1b 401
0f2d19dd
JB
402void
403scm_init_strings ()
0f2d19dd
JB
404{
405#include "strings.x"
406}
407