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