f4bba65e2b8d421cfb464eecf3f5af79f06925a9
[bpt/guile.git] / libguile / strings.c
1 /* Copyright (C) 1995,1996,1998,2000 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
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 #include <stdio.h>
48 #include "_scm.h"
49 #include "chars.h"
50
51 #include "strings.h"
52 #include "validate.h"
53 \f
54
55 /* {Strings}
56 */
57
58 SCM_DEFINE (scm_string_p, "string?", 1, 0, 0,
59 (SCM obj),
60 "Returns #t iff OBJ is a string, else returns #f.")
61 #define FUNC_NAME s_scm_string_p
62 {
63 return SCM_BOOL(SCM_STRINGP (obj));
64 }
65 #undef FUNC_NAME
66
67 SCM_DEFINE (scm_read_only_string_p, "read-only-string?", 1, 0, 0,
68 (SCM x),
69 "Return true if OBJ can be read as a string,\n\n"
70 "This illustrates the difference between @code{string?} and\n"
71 "@code{read-only-string?}:\n\n"
72 "@example\n"
73 "(string? \"a string\") @result{} #t\n"
74 "(string? 'a-symbol) @result{} #f\n\n"
75 "(read-only-string? \"a string\") @result{} #t\n"
76 "(read-only-string? 'a-symbol) @result{} #t\n"
77 "@end example")
78 #define FUNC_NAME s_scm_read_only_string_p
79 {
80 return SCM_BOOL(SCM_ROSTRINGP (x));
81 }
82 #undef FUNC_NAME
83
84 SCM_REGISTER_PROC (s_scm_list_to_string, "list->string", 1, 0, 0, scm_string);
85
86 SCM_DEFINE (scm_string, "string", 0, 0, 1,
87 (SCM chrs),
88 "Returns a newly allocated string composed of the arguments, CHRS.")
89 #define FUNC_NAME s_scm_string
90 {
91 SCM result;
92
93 {
94 long i = scm_ilength (chrs);
95
96 SCM_ASSERT (i >= 0, chrs, SCM_ARGn, FUNC_NAME);
97 result = scm_makstr (i, 0);
98 }
99
100 {
101 unsigned char *data = SCM_UCHARS (result);
102
103 while (SCM_NNULLP (chrs))
104 {
105 SCM elt = SCM_CAR (chrs);
106
107 SCM_VALIDATE_CHAR (SCM_ARGn, elt);
108 *data++ = SCM_CHAR (elt);
109 chrs = SCM_CDR (chrs);
110 }
111 }
112 return result;
113 }
114 #undef FUNC_NAME
115
116 SCM
117 scm_makstr (long len, int slots)
118 {
119 SCM s;
120 SCM * mem;
121 SCM_NEWCELL (s);
122 --slots;
123 SCM_REDEFER_INTS;
124 mem = (SCM *)scm_must_malloc (sizeof (SCM) * (slots + 1) + len + 1,
125 "scm_makstr");
126 if (slots >= 0)
127 {
128 int x;
129 mem[slots] = (SCM)mem;
130 for (x = 0; x < slots; ++x)
131 mem[x] = SCM_BOOL_F;
132 }
133 SCM_SETCHARS (s, (char *) (mem + slots + 1));
134 SCM_SETLENGTH (s, len, scm_tc7_string);
135 SCM_REALLOW_INTS;
136 SCM_CHARS (s)[len] = 0;
137 return s;
138 }
139
140 /* converts C scm_array of strings to SCM scm_list of strings. */
141 /* If argc < 0, a null terminated scm_array is assumed. */
142
143 SCM
144 scm_makfromstrs (int argc, char **argv)
145 {
146 int i = argc;
147 SCM lst = SCM_EOL;
148 if (0 > i)
149 for (i = 0; argv[i]; i++);
150 while (i--)
151 lst = scm_cons (scm_makfromstr (argv[i], (scm_sizet) strlen (argv[i]), 0), lst);
152 return lst;
153 }
154
155
156 /* This function must only be applied to memory obtained via malloc,
157 since the GC is going to apply `free' to it when the string is
158 dropped.
159
160 Also, s[len] must be `\0', since we promise that strings are
161 null-terminated. Perhaps we could handle non-null-terminated
162 strings by claiming they're shared substrings of a string we just
163 made up. */
164 SCM
165 scm_take_str (char *s, int len)
166 {
167 SCM answer;
168 SCM_NEWCELL (answer);
169 SCM_DEFER_INTS;
170 SCM_SETLENGTH (answer, len, scm_tc7_string);
171 scm_done_malloc (len + 1);
172 SCM_SETCHARS (answer, s);
173 SCM_ALLOW_INTS;
174 return answer;
175 }
176
177 /* `s' must be a malloc'd string. See scm_take_str. */
178 SCM
179 scm_take0str (char *s)
180 {
181 return scm_take_str (s, strlen (s));
182 }
183
184 SCM
185 scm_makfromstr (const char *src, scm_sizet len, int slots)
186 {
187 SCM s = scm_makstr (len, slots);
188 char *dst = SCM_CHARS (s);
189
190 while (len--)
191 *dst++ = *src++;
192 return s;
193 }
194
195 SCM
196 scm_makfrom0str (const char *src)
197 {
198 if (!src) return SCM_BOOL_F;
199 return scm_makfromstr (src, (scm_sizet) strlen (src), 0);
200 }
201
202
203 SCM
204 scm_makfrom0str_opt (const char *src)
205 {
206 return scm_makfrom0str (src);
207 }
208
209
210
211
212 SCM_DEFINE (scm_make_string, "make-string", 1, 1, 0,
213 (SCM k, SCM chr),
214 "Returns a newly allocated string of\n"
215 "length K. If CHR is given, then all elements of the string\n"
216 "are initialized to CHR, otherwise the contents of the\n"
217 "STRING are unspecified.\n")
218 #define FUNC_NAME s_scm_make_string
219 {
220 SCM res;
221 register long i;
222 SCM_VALIDATE_INUM_MIN_COPY (1,k,0,i);
223 res = scm_makstr (i, 0);
224 if (!SCM_UNBNDP (chr))
225 {
226 SCM_VALIDATE_CHAR (2,chr);
227 {
228 unsigned char *dst = SCM_UCHARS (res);
229 char c = SCM_CHAR (chr);
230
231 memset (dst, c, i);
232 }
233 }
234 return res;
235 }
236 #undef FUNC_NAME
237
238 SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0,
239 (SCM string),
240 "Returns the number of characters in STRING")
241 #define FUNC_NAME s_scm_string_length
242 {
243 SCM_VALIDATE_ROSTRING (1,string);
244 return SCM_MAKINUM (SCM_ROLENGTH (string));
245 }
246 #undef FUNC_NAME
247
248 SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0,
249 (SCM str, SCM k),
250 "Returns character K of STR using zero-origin indexing.\n"
251 "K must be a valid index of STR.")
252 #define FUNC_NAME s_scm_string_ref
253 {
254 int idx;
255
256 SCM_VALIDATE_ROSTRING (1, str);
257 SCM_VALIDATE_INUM_COPY (2, k, idx);
258 SCM_ASSERT_RANGE (2, k, idx >= 0 && idx < SCM_ROLENGTH (str));
259 return SCM_MAKE_CHAR (SCM_ROUCHARS (str)[idx]);
260 }
261 #undef FUNC_NAME
262
263 SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0,
264 (SCM str, SCM k, SCM chr),
265 "Stores CHR in element K of STRING and returns an unspecified value.\n"
266 "K must be a valid index of STR.")
267 #define FUNC_NAME s_scm_string_set_x
268 {
269 SCM_VALIDATE_RWSTRING (1,str);
270 SCM_VALIDATE_INUM_RANGE (2,k,0,SCM_LENGTH(str));
271 SCM_VALIDATE_CHAR (3,chr);
272 SCM_UCHARS (str)[SCM_INUM (k)] = SCM_CHAR (chr);
273 return SCM_UNSPECIFIED;
274 }
275 #undef FUNC_NAME
276
277
278
279 SCM_DEFINE (scm_substring, "substring", 2, 1, 0,
280 (SCM str, SCM start, SCM end),
281 "Returns a newly allocated string formed from the characters\n"
282 "of STR beginning with index START (inclusive) and ending with\n"
283 "index END (exclusive).\n"
284 "STR must be a string, START and END must be exact integers satisfying:\n\n"
285 "0 <= START <= END <= (string-length STR).")
286 #define FUNC_NAME s_scm_substring
287 {
288 long l;
289 SCM_VALIDATE_ROSTRING (1,str);
290 SCM_VALIDATE_INUM (2,start);
291 SCM_VALIDATE_INUM_DEF (3,end,SCM_ROLENGTH(str));
292 SCM_ASSERT_RANGE (2,start,SCM_INUM (start) <= SCM_ROLENGTH (str));
293 SCM_ASSERT_RANGE (2,end,SCM_INUM (end) <= SCM_ROLENGTH (str));
294 l = SCM_INUM (end)-SCM_INUM (start);
295 SCM_ASSERT (l >= 0, SCM_MAKINUM (l), SCM_OUTOFRANGE, FUNC_NAME);
296 return scm_makfromstr (&SCM_ROCHARS (str)[SCM_INUM (start)], (scm_sizet)l, 0);
297 }
298 #undef FUNC_NAME
299
300 SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1,
301 (SCM args),
302 "Returns a newly allocated string whose characters form the\n"
303 "concatenation of the given strings, ARGS.")
304 #define FUNC_NAME s_scm_string_append
305 {
306 SCM res;
307 register long i = 0;
308 register SCM l, s;
309 register unsigned char *data;
310 for (l = args;SCM_CONSP (l);) {
311 s = SCM_CAR (l);
312 SCM_VALIDATE_ROSTRING (SCM_ARGn,s);
313 i += SCM_ROLENGTH (s);
314 l = SCM_CDR (l);
315 }
316 SCM_ASSERT (SCM_NULLP (l), args, SCM_ARGn, FUNC_NAME);
317 res = scm_makstr (i, 0);
318 data = SCM_UCHARS (res);
319 for (l = args;SCM_NIMP (l);l = SCM_CDR (l)) {
320 s = SCM_CAR (l);
321 for (i = 0;i<SCM_ROLENGTH (s);i++) *data++ = SCM_ROUCHARS (s)[i];
322 }
323 return res;
324 }
325 #undef FUNC_NAME
326
327 SCM_DEFINE (scm_make_shared_substring, "make-shared-substring", 1, 2, 0,
328 (SCM str, SCM frm, SCM to),
329 "Return a shared substring of @var{str}. The semantics are the same as\n"
330 "for the @code{substring} function: the shared substring returned\n"
331 "includes all of the text from @var{str} between indexes @var{start}\n"
332 "(inclusive) and @var{end} (exclusive). If @var{end} is omitted, it\n"
333 "defaults to the end of @var{str}. The shared substring returned by\n"
334 "@code{make-shared-substring} occupies the same storage space as\n"
335 "@var{str}.")
336 #define FUNC_NAME s_scm_make_shared_substring
337 {
338 long f;
339 long t;
340 SCM answer;
341 SCM len_str;
342
343 SCM_VALIDATE_ROSTRING (1,str);
344 SCM_VALIDATE_INUM_DEF_COPY (2,frm,0,f);
345 SCM_VALIDATE_INUM_DEF_COPY (3,to,SCM_ROLENGTH(str),t);
346
347 SCM_ASSERT_RANGE (2,frm,(f >= 0));
348 SCM_ASSERT_RANGE (3,to, (f <= t) && (t <= SCM_ROLENGTH (str)));
349
350 SCM_NEWCELL (answer);
351 SCM_NEWCELL (len_str);
352
353 SCM_DEFER_INTS;
354 if (SCM_SUBSTRP (str))
355 {
356 long offset;
357 offset = SCM_INUM (SCM_SUBSTR_OFFSET (str));
358 f += offset;
359 t += offset;
360 SCM_SETCAR (len_str, SCM_MAKINUM (f));
361 SCM_SETCDR (len_str, SCM_SUBSTR_STR (str));
362 SCM_SETCDR (answer, len_str);
363 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
364 }
365 else
366 {
367 SCM_SETCAR (len_str, SCM_MAKINUM (f));
368 SCM_SETCDR (len_str, str);
369 SCM_SETCDR (answer, len_str);
370 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
371 }
372 SCM_ALLOW_INTS;
373 return answer;
374 }
375 #undef FUNC_NAME
376
377 void
378 scm_init_strings ()
379 {
380 #include "strings.x"
381 }
382