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