1 /* Copyright (C) 1995,1996,1998,2000,2001 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
49 #include "libguile/_scm.h"
50 #include "libguile/chars.h"
51 #include "libguile/root.h"
52 #include "libguile/strings.h"
53 #include "libguile/validate.h"
59 SCM_DEFINE (scm_string_p
, "string?", 1, 0, 0,
61 "Return @code{#t} iff @var{obj} is a string, else returns\n"
63 #define FUNC_NAME s_scm_string_p
65 return SCM_BOOL (SCM_STRINGP (obj
));
69 #if SCM_DEBUG_DEPRECATED == 0
71 /* The concept of read-only strings will disappear in next release
75 SCM_DEFINE (scm_read_only_string_p
, "read-only-string?", 1, 0, 0,
77 "Return @code{#t} if @var{obj} is either a string or a symbol,\n"
78 "otherwise return @code{#f}.")
79 #define FUNC_NAME s_scm_read_only_string_p
81 return SCM_BOOL(SCM_ROSTRINGP (obj
));
85 #endif /* DEPRECATED */
87 SCM_REGISTER_PROC (s_scm_list_to_string
, "list->string", 1, 0, 0, scm_string
);
89 SCM_DEFINE (scm_string
, "string", 0, 0, 1,
91 "@deffnx primitive list->string chrs\n"
92 "Return a newly allocated string composed of the arguments,\n"
94 #define FUNC_NAME s_scm_string
99 long i
= scm_ilength (chrs
);
101 SCM_ASSERT (i
>= 0, chrs
, SCM_ARGn
, FUNC_NAME
);
102 result
= scm_allocate_string (i
);
106 unsigned char *data
= SCM_STRING_UCHARS (result
);
108 while (!SCM_NULLP (chrs
))
110 SCM elt
= SCM_CAR (chrs
);
112 SCM_VALIDATE_CHAR (SCM_ARGn
, elt
);
113 *data
++ = SCM_CHAR (elt
);
114 chrs
= SCM_CDR (chrs
);
121 #if (SCM_DEBUG_DEPRECATED == 0)
124 scm_makstr (size_t len
, int dummy
)
125 #define FUNC_NAME "scm_makstr"
130 SCM_ASSERT_RANGE (1, scm_long2num (len
), len
<= SCM_STRING_MAX_LENGTH
);
132 mem
= (char *) scm_must_malloc (len
+ 1, FUNC_NAME
);
136 SCM_SET_STRING_CHARS (s
, mem
);
137 SCM_SET_STRING_LENGTH (s
, len
);
143 #endif /* SCM_DEBUG_DEPRECATED == 0 */
145 /* converts C scm_array of strings to SCM scm_list of strings. */
146 /* If argc < 0, a null terminated scm_array is assumed. */
149 scm_makfromstrs (int argc
, char **argv
)
154 for (i
= 0; argv
[i
]; i
++);
156 lst
= scm_cons (scm_mem2string (argv
[i
], strlen (argv
[i
])), lst
);
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
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
170 scm_take_str (char *s
, size_t len
)
171 #define FUNC_NAME "scm_take_str"
175 SCM_ASSERT_RANGE (2, scm_ulong2num (len
), len
<= SCM_STRING_MAX_LENGTH
);
177 SCM_NEWCELL (answer
);
178 SCM_SET_STRING_CHARS (answer
, s
);
179 SCM_SET_STRING_LENGTH (answer
, len
);
180 scm_done_malloc (len
+ 1);
187 /* `s' must be a malloc'd string. See scm_take_str. */
189 scm_take0str (char *s
)
191 return scm_take_str (s
, strlen (s
));
194 #if (SCM_DEBUG_DEPRECATED == 0)
197 scm_makfromstr (const char *src
, size_t len
, int dummy SCM_UNUSED
)
199 scm_c_issue_deprecation_warning ("`scm_makfromstr' is deprecated. "
200 "Use `scm_mem2string' instead.");
202 return scm_mem2string (src
, len
);
208 scm_mem2string (const char *src
, size_t len
)
210 SCM s
= scm_allocate_string (len
);
211 char *dst
= SCM_STRING_CHARS (s
);
219 scm_makfrom0str (const char *src
)
221 if (!src
) return SCM_BOOL_F
;
222 return scm_mem2string (src
, strlen (src
));
227 scm_makfrom0str_opt (const char *src
)
229 return scm_makfrom0str (src
);
234 scm_allocate_string (size_t len
)
235 #define FUNC_NAME "scm_allocate_string"
240 SCM_ASSERT_RANGE (1, scm_long2num (len
), len
<= SCM_STRING_MAX_LENGTH
);
242 mem
= (char *) scm_must_malloc (len
+ 1, FUNC_NAME
);
246 SCM_SET_STRING_CHARS (s
, mem
);
247 SCM_SET_STRING_LENGTH (s
, len
);
254 SCM_DEFINE (scm_make_string
, "make-string", 1, 1, 0,
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")
260 #define FUNC_NAME s_scm_make_string
264 long int i
= SCM_INUM (k
);
267 SCM_ASSERT_RANGE (1, k
, i
>= 0);
269 res
= scm_allocate_string (i
);
270 if (!SCM_UNBNDP (chr
))
274 SCM_VALIDATE_CHAR (2, chr
);
276 dst
= SCM_STRING_UCHARS (res
);
277 memset (dst
, SCM_CHAR (chr
), i
);
282 else if (SCM_BIGP (k
))
283 SCM_OUT_OF_RANGE (1, k
);
285 SCM_WRONG_TYPE_ARG (1, k
);
290 SCM_DEFINE (scm_string_length
, "string-length", 1, 0, 0,
292 "Return the number of characters in @var{string}.")
293 #define FUNC_NAME s_scm_string_length
295 SCM_VALIDATE_STRING (1, string
);
296 return SCM_MAKINUM (SCM_STRING_LENGTH (string
));
300 SCM_DEFINE (scm_string_ref
, "string-ref", 2, 0, 0,
302 "Return character @var{k} of @var{str} using zero-origin\n"
303 "indexing. @var{k} must be a valid index of @var{str}.")
304 #define FUNC_NAME s_scm_string_ref
308 SCM_VALIDATE_STRING (1, str
);
309 SCM_VALIDATE_INUM_COPY (2, k
, idx
);
310 SCM_ASSERT_RANGE (2, k
, idx
>= 0 && idx
< SCM_STRING_LENGTH (str
));
311 return SCM_MAKE_CHAR (SCM_STRING_UCHARS (str
)[idx
]);
316 SCM_DEFINE (scm_string_set_x
, "string-set!", 3, 0, 0,
317 (SCM str
, SCM k
, SCM chr
),
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"
321 #define FUNC_NAME s_scm_string_set_x
323 #if (SCM_DEBUG_DEPRECATED == 0)
324 SCM_VALIDATE_RWSTRING (1, str
);
326 SCM_VALIDATE_STRING (1, str
);
328 SCM_VALIDATE_INUM_RANGE (2,k
,0,SCM_STRING_LENGTH(str
));
329 SCM_VALIDATE_CHAR (3,chr
);
330 SCM_STRING_UCHARS (str
)[SCM_INUM (k
)] = SCM_CHAR (chr
);
331 return SCM_UNSPECIFIED
;
336 SCM_DEFINE (scm_substring
, "substring", 2, 1, 0,
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}).")
344 #define FUNC_NAME s_scm_substring
350 SCM_VALIDATE_STRING (1, str
);
351 SCM_VALIDATE_INUM (2, start
);
352 SCM_VALIDATE_INUM_DEF (3, end
, SCM_STRING_LENGTH (str
));
354 from
= SCM_INUM (start
);
355 SCM_ASSERT_RANGE (2, start
, 0 <= from
&& from
<= SCM_STRING_LENGTH (str
));
357 SCM_ASSERT_RANGE (3, end
, from
<= to
&& to
<= SCM_STRING_LENGTH (str
));
359 substr
= scm_mem2string (&SCM_STRING_CHARS (str
)[from
], to
- from
);
360 scm_remember_upto_here_1 (str
);
366 SCM_DEFINE (scm_string_append
, "string-append", 0, 0, 1,
368 "Return a newly allocated string whose characters form the\n"
369 "concatenation of the given strings, @var{args}.")
370 #define FUNC_NAME s_scm_string_append
375 register unsigned char *data
;
377 SCM_VALIDATE_REST_ARGUMENT (args
);
378 for (l
= args
; !SCM_NULLP (l
); l
= SCM_CDR (l
)) {
380 SCM_VALIDATE_STRING (SCM_ARGn
,s
);
381 i
+= SCM_STRING_LENGTH (s
);
383 res
= scm_allocate_string (i
);
384 data
= SCM_STRING_UCHARS (res
);
385 for (l
= args
; !SCM_NULLP (l
);l
= SCM_CDR (l
)) {
387 for (i
= 0;i
<SCM_STRING_LENGTH (s
);i
++) *data
++ = SCM_STRING_UCHARS (s
)[i
];
393 #if SCM_DEBUG_DEPRECATED == 0
395 /* Explicit shared substrings will disappear from Guile.
397 * Instead, "normal" strings will be implemented using sharing
398 * internally, combined with a copy-on-write strategy.
401 SCM_DEFINE (scm_make_shared_substring
, "make-shared-substring", 1, 2, 0,
402 (SCM str
, SCM start
, SCM end
),
403 "Return a shared substring of @var{str}. The arguments are the\n"
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}.")
410 #define FUNC_NAME s_scm_make_shared_substring
417 SCM_VALIDATE_ROSTRING (1,str
);
418 SCM_VALIDATE_INUM_DEF_COPY (2,start
,0,f
);
419 SCM_VALIDATE_INUM_DEF_COPY (3,end
,SCM_ROLENGTH(str
),t
);
421 SCM_ASSERT_RANGE (2,start
,(f
>= 0));
422 SCM_ASSERT_RANGE (3,end
, (f
<= t
) && (t
<= SCM_ROLENGTH (str
)));
424 SCM_NEWCELL (answer
);
425 SCM_NEWCELL (len_str
);
428 if (SCM_SUBSTRP (str
))
431 offset
= SCM_INUM (SCM_SUBSTR_OFFSET (str
));
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
);
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
);
451 #endif /* DEPRECATED */
456 scm_nullstr
= scm_allocate_string (0);
458 #ifndef SCM_MAGIC_SNARFER
459 #include "libguile/strings.x"