Commit | Line | Data |
---|---|---|
be54b15d | 1 | /* Copyright (C) 1995,1996,1998,2000,2001 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. */ |
1bbd0b84 GB |
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 | ||
0f2d19dd JB |
45 | \f |
46 | ||
faf2c9d7 MD |
47 | #include <string.h> |
48 | ||
a0599745 MD |
49 | #include "libguile/_scm.h" |
50 | #include "libguile/chars.h" | |
0f2d19dd | 51 | |
a0599745 MD |
52 | #include "libguile/strings.h" |
53 | #include "libguile/validate.h" | |
0f2d19dd JB |
54 | \f |
55 | ||
56 | /* {Strings} | |
57 | */ | |
58 | ||
3b3b36dd | 59 | SCM_DEFINE (scm_string_p, "string?", 1, 0, 0, |
0d26a824 MG |
60 | (SCM obj), |
61 | "Returns @code{#t} iff @var{obj} is a string, else returns\n" | |
62 | "@code{#f}.") | |
1bbd0b84 | 63 | #define FUNC_NAME s_scm_string_p |
0f2d19dd | 64 | { |
e53cc817 | 65 | return SCM_BOOL (SCM_STRINGP (obj)); |
0f2d19dd | 66 | } |
1bbd0b84 | 67 | #undef FUNC_NAME |
0f2d19dd | 68 | |
e53cc817 MD |
69 | #if SCM_DEBUG_DEPRECATED == 0 |
70 | ||
71 | /* The concept of read-only strings will disappear in next release | |
72 | * of Guile. | |
73 | */ | |
74 | ||
3b3b36dd | 75 | SCM_DEFINE (scm_read_only_string_p, "read-only-string?", 1, 0, 0, |
0d26a824 MG |
76 | (SCM obj), |
77 | "Return true if @var{obj} can be read as a string,\n\n" | |
b380b885 MD |
78 | "This illustrates the difference between @code{string?} and\n" |
79 | "@code{read-only-string?}:\n\n" | |
80 | "@example\n" | |
81 | "(string? \"a string\") @result{} #t\n" | |
82 | "(string? 'a-symbol) @result{} #f\n\n" | |
83 | "(read-only-string? \"a string\") @result{} #t\n" | |
84 | "(read-only-string? 'a-symbol) @result{} #t\n" | |
85 | "@end example") | |
1bbd0b84 | 86 | #define FUNC_NAME s_scm_read_only_string_p |
0f2d19dd | 87 | { |
0d26a824 | 88 | return SCM_BOOL(SCM_ROSTRINGP (obj)); |
0f2d19dd | 89 | } |
1bbd0b84 | 90 | #undef FUNC_NAME |
0f2d19dd | 91 | |
e53cc817 MD |
92 | #endif /* DEPRECATED */ |
93 | ||
bd9e24b3 | 94 | SCM_REGISTER_PROC (s_scm_list_to_string, "list->string", 1, 0, 0, scm_string); |
1bbd0b84 | 95 | |
3b3b36dd | 96 | SCM_DEFINE (scm_string, "string", 0, 0, 1, |
6fa73e72 | 97 | (SCM chrs), |
11768c04 | 98 | "@deffnx primitive list->string chrs\n" |
0d26a824 MG |
99 | "Returns a newly allocated string composed of the arguments,\n" |
100 | "@var{chrs}.") | |
1bbd0b84 | 101 | #define FUNC_NAME s_scm_string |
0f2d19dd | 102 | { |
bd9e24b3 GH |
103 | SCM result; |
104 | ||
0f2d19dd | 105 | { |
bd9e24b3 GH |
106 | long i = scm_ilength (chrs); |
107 | ||
108 | SCM_ASSERT (i >= 0, chrs, SCM_ARGn, FUNC_NAME); | |
be54b15d | 109 | result = scm_allocate_string (i); |
0f2d19dd | 110 | } |
bd9e24b3 GH |
111 | |
112 | { | |
322ac0c5 | 113 | unsigned char *data = SCM_STRING_UCHARS (result); |
bd9e24b3 GH |
114 | |
115 | while (SCM_NNULLP (chrs)) | |
116 | { | |
117 | SCM elt = SCM_CAR (chrs); | |
118 | ||
7866a09b GB |
119 | SCM_VALIDATE_CHAR (SCM_ARGn, elt); |
120 | *data++ = SCM_CHAR (elt); | |
bd9e24b3 GH |
121 | chrs = SCM_CDR (chrs); |
122 | } | |
123 | } | |
124 | return result; | |
0f2d19dd | 125 | } |
1bbd0b84 | 126 | #undef FUNC_NAME |
0f2d19dd | 127 | |
be54b15d | 128 | #if (SCM_DEBUG_DEPRECATED == 0) |
cb0d8be2 | 129 | |
0f2d19dd | 130 | SCM |
28b06554 | 131 | scm_makstr (long len, int dummy) |
cb0d8be2 | 132 | #define FUNC_NAME "scm_makstr" |
0f2d19dd JB |
133 | { |
134 | SCM s; | |
cb0d8be2 DH |
135 | char *mem; |
136 | ||
137 | SCM_ASSERT_RANGE (1, scm_long2num (len), len <= SCM_STRING_MAX_LENGTH); | |
fee7ef83 | 138 | |
cb0d8be2 | 139 | mem = (char *) scm_must_malloc (len + 1, FUNC_NAME); |
28b06554 | 140 | mem[len] = 0; |
cb0d8be2 | 141 | |
0f2d19dd | 142 | SCM_NEWCELL (s); |
6a0476fd | 143 | SCM_SET_STRING_CHARS (s, mem); |
93778877 | 144 | SCM_SET_STRING_LENGTH (s, len); |
28b06554 | 145 | |
0f2d19dd JB |
146 | return s; |
147 | } | |
cb0d8be2 DH |
148 | #undef FUNC_NAME |
149 | ||
be54b15d | 150 | #endif /* SCM_DEBUG_DEPRECATED == 0 */ |
0f2d19dd JB |
151 | |
152 | /* converts C scm_array of strings to SCM scm_list of strings. */ | |
153 | /* If argc < 0, a null terminated scm_array is assumed. */ | |
1cc91f1b | 154 | |
0f2d19dd | 155 | SCM |
1bbd0b84 | 156 | scm_makfromstrs (int argc, char **argv) |
0f2d19dd JB |
157 | { |
158 | int i = argc; | |
159 | SCM lst = SCM_EOL; | |
160 | if (0 > i) | |
161 | for (i = 0; argv[i]; i++); | |
162 | while (i--) | |
163 | lst = scm_cons (scm_makfromstr (argv[i], (scm_sizet) strlen (argv[i]), 0), lst); | |
164 | return lst; | |
165 | } | |
166 | ||
167 | ||
ee149d03 JB |
168 | /* This function must only be applied to memory obtained via malloc, |
169 | since the GC is going to apply `free' to it when the string is | |
170 | dropped. | |
171 | ||
172 | Also, s[len] must be `\0', since we promise that strings are | |
173 | null-terminated. Perhaps we could handle non-null-terminated | |
174 | strings by claiming they're shared substrings of a string we just | |
175 | made up. */ | |
0f2d19dd | 176 | SCM |
ee149d03 | 177 | scm_take_str (char *s, int len) |
cb0d8be2 | 178 | #define FUNC_NAME "scm_take_str" |
0f2d19dd JB |
179 | { |
180 | SCM answer; | |
cb0d8be2 DH |
181 | |
182 | SCM_ASSERT_RANGE (2, scm_ulong2num (len), len <= SCM_STRING_MAX_LENGTH); | |
183 | ||
0f2d19dd | 184 | SCM_NEWCELL (answer); |
cb0d8be2 | 185 | SCM_SET_STRING_CHARS (answer, s); |
93778877 | 186 | SCM_SET_STRING_LENGTH (answer, len); |
ee149d03 | 187 | scm_done_malloc (len + 1); |
cb0d8be2 | 188 | |
0f2d19dd JB |
189 | return answer; |
190 | } | |
cb0d8be2 DH |
191 | #undef FUNC_NAME |
192 | ||
0f2d19dd | 193 | |
ee149d03 JB |
194 | /* `s' must be a malloc'd string. See scm_take_str. */ |
195 | SCM | |
196 | scm_take0str (char *s) | |
197 | { | |
198 | return scm_take_str (s, strlen (s)); | |
199 | } | |
200 | ||
0f2d19dd | 201 | SCM |
28b06554 | 202 | scm_makfromstr (const char *src, scm_sizet len, int dummy) |
0f2d19dd | 203 | { |
be54b15d | 204 | SCM s = scm_allocate_string (len); |
86c991c2 | 205 | char *dst = SCM_STRING_CHARS (s); |
bd9e24b3 | 206 | |
0f2d19dd JB |
207 | while (len--) |
208 | *dst++ = *src++; | |
209 | return s; | |
210 | } | |
211 | ||
0f2d19dd | 212 | SCM |
1bbd0b84 | 213 | scm_makfrom0str (const char *src) |
0f2d19dd JB |
214 | { |
215 | if (!src) return SCM_BOOL_F; | |
216 | return scm_makfromstr (src, (scm_sizet) strlen (src), 0); | |
217 | } | |
218 | ||
1cc91f1b | 219 | |
0f2d19dd | 220 | SCM |
1bbd0b84 | 221 | scm_makfrom0str_opt (const char *src) |
0f2d19dd JB |
222 | { |
223 | return scm_makfrom0str (src); | |
224 | } | |
225 | ||
226 | ||
be54b15d DH |
227 | SCM |
228 | scm_allocate_string (scm_sizet len) | |
229 | #define FUNC_NAME "scm_allocate_string" | |
230 | { | |
231 | char *mem; | |
232 | SCM s; | |
233 | ||
234 | SCM_ASSERT_RANGE (1, scm_long2num (len), len <= SCM_STRING_MAX_LENGTH); | |
235 | ||
236 | mem = (char *) scm_must_malloc (len + 1, FUNC_NAME); | |
237 | mem[len] = 0; | |
238 | ||
239 | SCM_NEWCELL (s); | |
240 | SCM_SET_STRING_CHARS (s, mem); | |
241 | SCM_SET_STRING_LENGTH (s, len); | |
242 | ||
243 | return s; | |
244 | } | |
245 | #undef FUNC_NAME | |
246 | ||
247 | ||
3b3b36dd | 248 | SCM_DEFINE (scm_make_string, "make-string", 1, 1, 0, |
6fa73e72 | 249 | (SCM k, SCM chr), |
0d26a824 MG |
250 | "Return a newly allocated string of\n" |
251 | "length @var{k}. If @var{chr} is given, then all elements of\n" | |
252 | "the string are initialized to @var{chr}, otherwise the contents\n" | |
253 | "of the @var{string} are unspecified.\n") | |
1bbd0b84 | 254 | #define FUNC_NAME s_scm_make_string |
0f2d19dd | 255 | { |
cb0d8be2 | 256 | if (SCM_INUMP (k)) |
0f2d19dd | 257 | { |
cb0d8be2 DH |
258 | long int i = SCM_INUM (k); |
259 | SCM res; | |
260 | ||
261 | SCM_ASSERT_RANGE (1, k, i >= 0); | |
262 | ||
be54b15d | 263 | res = scm_allocate_string (i); |
cb0d8be2 DH |
264 | if (!SCM_UNBNDP (chr)) |
265 | { | |
266 | unsigned char *dst; | |
267 | ||
268 | SCM_VALIDATE_CHAR (2, chr); | |
269 | ||
270 | dst = SCM_STRING_UCHARS (res); | |
271 | memset (dst, SCM_CHAR (chr), i); | |
272 | } | |
273 | ||
274 | return res; | |
0f2d19dd | 275 | } |
cb0d8be2 DH |
276 | else if (SCM_BIGP (k)) |
277 | SCM_OUT_OF_RANGE (1, k); | |
278 | else | |
279 | SCM_WRONG_TYPE_ARG (1, k); | |
0f2d19dd | 280 | } |
1bbd0b84 | 281 | #undef FUNC_NAME |
0f2d19dd | 282 | |
cb0d8be2 | 283 | |
3b3b36dd | 284 | SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0, |
0d26a824 MG |
285 | (SCM string), |
286 | "Return the number of characters in @var{string}.") | |
1bbd0b84 | 287 | #define FUNC_NAME s_scm_string_length |
0f2d19dd | 288 | { |
d1ca2c64 | 289 | SCM_VALIDATE_STRING (1, string); |
bfa974f0 | 290 | return SCM_MAKINUM (SCM_STRING_LENGTH (string)); |
0f2d19dd | 291 | } |
1bbd0b84 | 292 | #undef FUNC_NAME |
0f2d19dd | 293 | |
bd9e24b3 | 294 | SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0, |
6fa73e72 | 295 | (SCM str, SCM k), |
0d26a824 MG |
296 | "Return character @var{k} of @var{str} using zero-origin\n" |
297 | "indexing. @var{k} must be a valid index of @var{str}.") | |
1bbd0b84 | 298 | #define FUNC_NAME s_scm_string_ref |
0f2d19dd | 299 | { |
bd9e24b3 GH |
300 | int idx; |
301 | ||
d1ca2c64 | 302 | SCM_VALIDATE_STRING (1, str); |
bd9e24b3 | 303 | SCM_VALIDATE_INUM_COPY (2, k, idx); |
d1ca2c64 | 304 | SCM_ASSERT_RANGE (2, k, idx >= 0 && idx < SCM_STRING_LENGTH (str)); |
34f0f2b8 | 305 | return SCM_MAKE_CHAR (SCM_STRING_UCHARS (str)[idx]); |
0f2d19dd | 306 | } |
1bbd0b84 | 307 | #undef FUNC_NAME |
0f2d19dd | 308 | |
f0942910 | 309 | |
3b3b36dd | 310 | SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0, |
6fa73e72 | 311 | (SCM str, SCM k, SCM chr), |
0d26a824 MG |
312 | "Store @var{chr} in element @var{k} of @var{str} and return\n" |
313 | "an unspecified value. @var{k} must be a valid index of\n" | |
314 | "@var{str}.") | |
1bbd0b84 | 315 | #define FUNC_NAME s_scm_string_set_x |
0f2d19dd | 316 | { |
f0942910 DH |
317 | #if (SCM_DEBUG_DEPRECATED == 0) |
318 | SCM_VALIDATE_RWSTRING (1, str); | |
319 | #else | |
320 | SCM_VALIDATE_STRING (1, str); | |
321 | #endif | |
bfa974f0 | 322 | SCM_VALIDATE_INUM_RANGE (2,k,0,SCM_STRING_LENGTH(str)); |
7866a09b | 323 | SCM_VALIDATE_CHAR (3,chr); |
322ac0c5 | 324 | SCM_STRING_UCHARS (str)[SCM_INUM (k)] = SCM_CHAR (chr); |
0f2d19dd JB |
325 | return SCM_UNSPECIFIED; |
326 | } | |
1bbd0b84 | 327 | #undef FUNC_NAME |
0f2d19dd JB |
328 | |
329 | ||
3b3b36dd | 330 | SCM_DEFINE (scm_substring, "substring", 2, 1, 0, |
0d26a824 MG |
331 | (SCM str, SCM start, SCM end), |
332 | "Return a newly allocated string formed from the characters\n" | |
333 | "of @var{str} beginning with index @var{start} (inclusive) and\n" | |
334 | "ending with index @var{end} (exclusive).\n" | |
335 | "@var{str} must be a string, @var{start} and @var{end} must be\n" | |
336 | "exact integers satisfying:\n\n" | |
337 | "0 <= @var{start} <= @var{end} <= (string-length @var{str}).") | |
1bbd0b84 | 338 | #define FUNC_NAME s_scm_substring |
0f2d19dd | 339 | { |
685c0d71 DH |
340 | long int from; |
341 | long int to; | |
342 | ||
d1ca2c64 | 343 | SCM_VALIDATE_STRING (1, str); |
685c0d71 | 344 | SCM_VALIDATE_INUM (2, start); |
d1ca2c64 | 345 | SCM_VALIDATE_INUM_DEF (3, end, SCM_STRING_LENGTH (str)); |
685c0d71 DH |
346 | |
347 | from = SCM_INUM (start); | |
d1ca2c64 | 348 | SCM_ASSERT_RANGE (2, start, 0 <= from && from <= SCM_STRING_LENGTH (str)); |
685c0d71 | 349 | to = SCM_INUM (end); |
d1ca2c64 | 350 | SCM_ASSERT_RANGE (3, end, from <= to && to <= SCM_STRING_LENGTH (str)); |
685c0d71 | 351 | |
34f0f2b8 | 352 | return scm_makfromstr (&SCM_STRING_CHARS (str)[from], (scm_sizet) (to - from), 0); |
0f2d19dd | 353 | } |
1bbd0b84 | 354 | #undef FUNC_NAME |
0f2d19dd | 355 | |
685c0d71 | 356 | |
3b3b36dd | 357 | SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1, |
6fa73e72 | 358 | (SCM args), |
0d26a824 MG |
359 | "Return a newly allocated string whose characters form the\n" |
360 | "concatenation of the given strings, @var{args}.") | |
1bbd0b84 | 361 | #define FUNC_NAME s_scm_string_append |
0f2d19dd JB |
362 | { |
363 | SCM res; | |
364 | register long i = 0; | |
365 | register SCM l, s; | |
a65b9c80 | 366 | register unsigned char *data; |
af45e3b0 DH |
367 | |
368 | SCM_VALIDATE_REST_ARGUMENT (args); | |
369 | for (l = args; !SCM_NULLP (l); l = SCM_CDR (l)) { | |
0f2d19dd | 370 | s = SCM_CAR (l); |
d1ca2c64 DH |
371 | SCM_VALIDATE_STRING (SCM_ARGn,s); |
372 | i += SCM_STRING_LENGTH (s); | |
0f2d19dd | 373 | } |
be54b15d | 374 | res = scm_allocate_string (i); |
322ac0c5 | 375 | data = SCM_STRING_UCHARS (res); |
0f2d19dd JB |
376 | for (l = args;SCM_NIMP (l);l = SCM_CDR (l)) { |
377 | s = SCM_CAR (l); | |
34f0f2b8 | 378 | for (i = 0;i<SCM_STRING_LENGTH (s);i++) *data++ = SCM_STRING_UCHARS (s)[i]; |
0f2d19dd JB |
379 | } |
380 | return res; | |
381 | } | |
1bbd0b84 | 382 | #undef FUNC_NAME |
0f2d19dd | 383 | |
e53cc817 MD |
384 | #if SCM_DEBUG_DEPRECATED == 0 |
385 | ||
386 | /* Explicit shared substrings will disappear from Guile. | |
387 | * | |
388 | * Instead, "normal" strings will be implemented using sharing | |
389 | * internally, combined with a copy-on-write strategy. | |
390 | */ | |
391 | ||
3b3b36dd | 392 | SCM_DEFINE (scm_make_shared_substring, "make-shared-substring", 1, 2, 0, |
1bbd0b84 | 393 | (SCM str, SCM frm, SCM to), |
b380b885 MD |
394 | "Return a shared substring of @var{str}. The semantics are the same as\n" |
395 | "for the @code{substring} function: the shared substring returned\n" | |
396 | "includes all of the text from @var{str} between indexes @var{start}\n" | |
397 | "(inclusive) and @var{end} (exclusive). If @var{end} is omitted, it\n" | |
398 | "defaults to the end of @var{str}. The shared substring returned by\n" | |
399 | "@code{make-shared-substring} occupies the same storage space as\n" | |
400 | "@var{str}.") | |
1bbd0b84 | 401 | #define FUNC_NAME s_scm_make_shared_substring |
0f2d19dd JB |
402 | { |
403 | long f; | |
404 | long t; | |
405 | SCM answer; | |
406 | SCM len_str; | |
407 | ||
3b3b36dd GB |
408 | SCM_VALIDATE_ROSTRING (1,str); |
409 | SCM_VALIDATE_INUM_DEF_COPY (2,frm,0,f); | |
410 | SCM_VALIDATE_INUM_DEF_COPY (3,to,SCM_ROLENGTH(str),t); | |
0f2d19dd | 411 | |
5bff3127 GB |
412 | SCM_ASSERT_RANGE (2,frm,(f >= 0)); |
413 | SCM_ASSERT_RANGE (3,to, (f <= t) && (t <= SCM_ROLENGTH (str))); | |
0f2d19dd JB |
414 | |
415 | SCM_NEWCELL (answer); | |
416 | SCM_NEWCELL (len_str); | |
417 | ||
418 | SCM_DEFER_INTS; | |
419 | if (SCM_SUBSTRP (str)) | |
420 | { | |
421 | long offset; | |
422 | offset = SCM_INUM (SCM_SUBSTR_OFFSET (str)); | |
423 | f += offset; | |
424 | t += offset; | |
425 | SCM_SETCAR (len_str, SCM_MAKINUM (f)); | |
426 | SCM_SETCDR (len_str, SCM_SUBSTR_STR (str)); | |
427 | SCM_SETCDR (answer, len_str); | |
428 | SCM_SETLENGTH (answer, t - f, scm_tc7_substring); | |
429 | } | |
430 | else | |
431 | { | |
432 | SCM_SETCAR (len_str, SCM_MAKINUM (f)); | |
433 | SCM_SETCDR (len_str, str); | |
434 | SCM_SETCDR (answer, len_str); | |
435 | SCM_SETLENGTH (answer, t - f, scm_tc7_substring); | |
436 | } | |
437 | SCM_ALLOW_INTS; | |
438 | return answer; | |
439 | } | |
1bbd0b84 | 440 | #undef FUNC_NAME |
1cc91f1b | 441 | |
e53cc817 MD |
442 | #endif /* DEPRECATED */ |
443 | ||
0f2d19dd JB |
444 | void |
445 | scm_init_strings () | |
0f2d19dd | 446 | { |
8dc9439f | 447 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 448 | #include "libguile/strings.x" |
8dc9439f | 449 | #endif |
0f2d19dd JB |
450 | } |
451 | ||
89e00824 ML |
452 | |
453 | /* | |
454 | Local Variables: | |
455 | c-file-style: "gnu" | |
456 | End: | |
457 | */ |