* genio.c, genio.h: move contents into ports.c, ports.h. The
[bpt/guile.git] / libguile / strings.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995,1996,1998 Free Software Foundation, Inc.
0f2d19dd
JB
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
0f2d19dd
JB
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));
cdbadcac 119 c = SCM_ROCHARS (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 284{
82637e2b
JB
285 SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str),
286 str, SCM_ARG1, s_string_set_x);
0f2d19dd
JB
287 SCM_ASSERT (SCM_INUMP (k), k, SCM_ARG2, s_string_set_x);
288 SCM_ASSERT (SCM_ICHRP (chr), chr, SCM_ARG3, s_string_set_x);
82637e2b
JB
289 if (! SCM_RWSTRINGP (str))
290 scm_misc_error (s_string_set_x, "argument is a read-only string", str);
cc5ca028
JB
291 SCM_ASSERT ((SCM_INUM (k) >= 0
292 && ((unsigned) SCM_INUM (k)) < SCM_LENGTH (str)),
82637e2b 293 k, SCM_OUTOFRANGE, s_string_set_x);
a65b9c80 294 SCM_UCHARS (str)[SCM_INUM (k)] = SCM_ICHR (chr);
0f2d19dd
JB
295 return SCM_UNSPECIFIED;
296}
297
298
299
300SCM_PROC(s_substring, "substring", 2, 1, 0, scm_substring);
1cc91f1b 301
0f2d19dd
JB
302SCM
303scm_substring (str, start, end)
304 SCM str;
305 SCM start;
306 SCM end;
0f2d19dd
JB
307{
308 long l;
309 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str),
310 str, SCM_ARG1, s_substring);
311 SCM_ASSERT (SCM_INUMP (start), start, SCM_ARG2, s_substring);
312 if (end == SCM_UNDEFINED)
313 end = SCM_MAKINUM (SCM_ROLENGTH (str));
314 SCM_ASSERT (SCM_INUMP (end), end, SCM_ARG3, s_substring);
315 SCM_ASSERT (SCM_INUM (start) <= SCM_ROLENGTH (str), start, SCM_OUTOFRANGE, s_substring);
316 SCM_ASSERT (SCM_INUM (end) <= SCM_ROLENGTH (str), end, SCM_OUTOFRANGE, s_substring);
317 l = SCM_INUM (end)-SCM_INUM (start);
318 SCM_ASSERT (l >= 0, SCM_MAKINUM (l), SCM_OUTOFRANGE, s_substring);
319 return scm_makfromstr (&SCM_ROCHARS (str)[SCM_INUM (start)], (scm_sizet)l, 0);
320}
321
322SCM_PROC(s_string_append, "string-append", 0, 0, 1, scm_string_append);
1cc91f1b 323
0f2d19dd
JB
324SCM
325scm_string_append (args)
326 SCM args;
0f2d19dd
JB
327{
328 SCM res;
329 register long i = 0;
330 register SCM l, s;
a65b9c80 331 register unsigned char *data;
0f2d19dd
JB
332 for (l = args;SCM_NIMP (l);) {
333 SCM_ASSERT (SCM_CONSP (l), l, SCM_ARGn, s_string_append);
334 s = SCM_CAR (l);
335 SCM_ASSERT (SCM_NIMP (s) && SCM_ROSTRINGP (s),
336 s, SCM_ARGn, s_string_append);
337 i += SCM_ROLENGTH (s);
338 l = SCM_CDR (l);
339 }
340 SCM_ASSERT (SCM_NULLP (l), args, SCM_ARGn, s_string_append);
341 res = scm_makstr (i, 0);
a65b9c80 342 data = SCM_UCHARS (res);
0f2d19dd
JB
343 for (l = args;SCM_NIMP (l);l = SCM_CDR (l)) {
344 s = SCM_CAR (l);
a65b9c80 345 for (i = 0;i<SCM_ROLENGTH (s);i++) *data++ = SCM_ROUCHARS (s)[i];
0f2d19dd
JB
346 }
347 return res;
348}
349
350SCM_PROC(s_make_shared_substring, "make-shared-substring", 1, 2, 0, scm_make_shared_substring);
1cc91f1b 351
0f2d19dd
JB
352SCM
353scm_make_shared_substring (str, frm, to)
354 SCM str;
355 SCM frm;
356 SCM to;
0f2d19dd
JB
357{
358 long f;
359 long t;
360 SCM answer;
361 SCM len_str;
362
363 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_make_shared_substring);
364
365 if (frm == SCM_UNDEFINED)
366 frm = SCM_MAKINUM (0);
367 else
368 SCM_ASSERT (SCM_INUMP (frm), frm, SCM_ARG2, s_make_shared_substring);
369
370 if (to == SCM_UNDEFINED)
371 to = SCM_MAKINUM (SCM_ROLENGTH (str));
372 else
373 SCM_ASSERT (SCM_INUMP (to), to, SCM_ARG3, s_make_shared_substring);
374
375 f = SCM_INUM (frm);
376 t = SCM_INUM (to);
377 SCM_ASSERT ((f >= 0), frm, SCM_OUTOFRANGE, s_make_shared_substring);
c2cb2500
JB
378 SCM_ASSERT ((f <= t) && (t <= SCM_ROLENGTH (str)), to, SCM_OUTOFRANGE,
379 s_make_shared_substring);
0f2d19dd
JB
380
381 SCM_NEWCELL (answer);
382 SCM_NEWCELL (len_str);
383
384 SCM_DEFER_INTS;
385 if (SCM_SUBSTRP (str))
386 {
387 long offset;
388 offset = SCM_INUM (SCM_SUBSTR_OFFSET (str));
389 f += offset;
390 t += offset;
391 SCM_SETCAR (len_str, SCM_MAKINUM (f));
392 SCM_SETCDR (len_str, SCM_SUBSTR_STR (str));
393 SCM_SETCDR (answer, len_str);
394 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
395 }
396 else
397 {
398 SCM_SETCAR (len_str, SCM_MAKINUM (f));
399 SCM_SETCDR (len_str, str);
400 SCM_SETCDR (answer, len_str);
401 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
402 }
403 SCM_ALLOW_INTS;
404 return answer;
405}
406
1cc91f1b 407
0f2d19dd
JB
408void
409scm_init_strings ()
0f2d19dd
JB
410{
411#include "strings.x"
412}
413