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