1 /* Copyright (C) 1995,1996,1998,2000 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 */
50 #include "libguile/_scm.h"
51 #include "libguile/chars.h"
53 #include "libguile/strings.h"
54 #include "libguile/validate.h"
60 SCM_DEFINE (scm_string_p
, "string?", 1, 0, 0,
62 "Returns #t iff OBJ is a string, else returns #f.")
63 #define FUNC_NAME s_scm_string_p
65 return SCM_BOOL(SCM_STRINGP (obj
));
69 SCM_DEFINE (scm_read_only_string_p
, "read-only-string?", 1, 0, 0,
71 "Return true if OBJ can be read as a string,\n\n"
72 "This illustrates the difference between @code{string?} and\n"
73 "@code{read-only-string?}:\n\n"
75 "(string? \"a string\") @result{} #t\n"
76 "(string? 'a-symbol) @result{} #f\n\n"
77 "(read-only-string? \"a string\") @result{} #t\n"
78 "(read-only-string? 'a-symbol) @result{} #t\n"
80 #define FUNC_NAME s_scm_read_only_string_p
82 return SCM_BOOL(SCM_ROSTRINGP (x
));
86 SCM_REGISTER_PROC (s_scm_list_to_string
, "list->string", 1, 0, 0, scm_string
);
88 SCM_DEFINE (scm_string
, "string", 0, 0, 1,
90 "Returns a newly allocated string composed of the arguments, CHRS.")
91 #define FUNC_NAME s_scm_string
96 long i
= scm_ilength (chrs
);
98 SCM_ASSERT (i
>= 0, chrs
, SCM_ARGn
, FUNC_NAME
);
99 result
= scm_makstr (i
, 0);
103 unsigned char *data
= SCM_UCHARS (result
);
105 while (SCM_NNULLP (chrs
))
107 SCM elt
= SCM_CAR (chrs
);
109 SCM_VALIDATE_CHAR (SCM_ARGn
, elt
);
110 *data
++ = SCM_CHAR (elt
);
111 chrs
= SCM_CDR (chrs
);
119 scm_makstr (long len
, int slots
)
127 mem
= (scm_bits_t
*) scm_must_malloc (sizeof (scm_bits_t
) * (slots
+ 1)
128 + len
+ 1, "scm_makstr");
132 mem
[slots
] = (scm_bits_t
) mem
;
133 for (x
= 0; x
< slots
; ++x
)
134 mem
[x
] = SCM_UNPACK (SCM_BOOL_F
);
136 SCM_SETCHARS (s
, (char *) (mem
+ slots
+ 1));
137 SCM_SETLENGTH (s
, len
, scm_tc7_string
);
139 SCM_CHARS (s
)[len
] = 0;
143 /* converts C scm_array of strings to SCM scm_list of strings. */
144 /* If argc < 0, a null terminated scm_array is assumed. */
147 scm_makfromstrs (int argc
, char **argv
)
152 for (i
= 0; argv
[i
]; i
++);
154 lst
= scm_cons (scm_makfromstr (argv
[i
], (scm_sizet
) strlen (argv
[i
]), 0), lst
);
159 /* This function must only be applied to memory obtained via malloc,
160 since the GC is going to apply `free' to it when the string is
163 Also, s[len] must be `\0', since we promise that strings are
164 null-terminated. Perhaps we could handle non-null-terminated
165 strings by claiming they're shared substrings of a string we just
168 scm_take_str (char *s
, int len
)
171 SCM_NEWCELL (answer
);
173 SCM_SETLENGTH (answer
, len
, scm_tc7_string
);
174 scm_done_malloc (len
+ 1);
175 SCM_SETCHARS (answer
, s
);
180 /* `s' must be a malloc'd string. See scm_take_str. */
182 scm_take0str (char *s
)
184 return scm_take_str (s
, strlen (s
));
188 scm_makfromstr (const char *src
, scm_sizet len
, int slots
)
190 SCM s
= scm_makstr (len
, slots
);
191 char *dst
= SCM_CHARS (s
);
199 scm_makfrom0str (const char *src
)
201 if (!src
) return SCM_BOOL_F
;
202 return scm_makfromstr (src
, (scm_sizet
) strlen (src
), 0);
207 scm_makfrom0str_opt (const char *src
)
209 return scm_makfrom0str (src
);
215 SCM_DEFINE (scm_make_string
, "make-string", 1, 1, 0,
217 "Returns a newly allocated string of\n"
218 "length K. If CHR is given, then all elements of the string\n"
219 "are initialized to CHR, otherwise the contents of the\n"
220 "STRING are unspecified.\n")
221 #define FUNC_NAME s_scm_make_string
225 SCM_VALIDATE_INUM_MIN_COPY (1,k
,0,i
);
226 res
= scm_makstr (i
, 0);
227 if (!SCM_UNBNDP (chr
))
229 SCM_VALIDATE_CHAR (2,chr
);
231 unsigned char *dst
= SCM_UCHARS (res
);
232 char c
= SCM_CHAR (chr
);
241 SCM_DEFINE (scm_string_length
, "string-length", 1, 0, 0,
243 "Returns the number of characters in STRING")
244 #define FUNC_NAME s_scm_string_length
246 SCM_VALIDATE_ROSTRING (1,string
);
247 return SCM_MAKINUM (SCM_ROLENGTH (string
));
251 SCM_DEFINE (scm_string_ref
, "string-ref", 2, 0, 0,
253 "Returns character K of STR using zero-origin indexing.\n"
254 "K must be a valid index of STR.")
255 #define FUNC_NAME s_scm_string_ref
259 SCM_VALIDATE_ROSTRING (1, str
);
260 SCM_VALIDATE_INUM_COPY (2, k
, idx
);
261 SCM_ASSERT_RANGE (2, k
, idx
>= 0 && idx
< SCM_ROLENGTH (str
));
262 return SCM_MAKE_CHAR (SCM_ROUCHARS (str
)[idx
]);
266 SCM_DEFINE (scm_string_set_x
, "string-set!", 3, 0, 0,
267 (SCM str
, SCM k
, SCM chr
),
268 "Stores CHR in element K of STRING and returns an unspecified value.\n"
269 "K must be a valid index of STR.")
270 #define FUNC_NAME s_scm_string_set_x
272 SCM_VALIDATE_RWSTRING (1,str
);
273 SCM_VALIDATE_INUM_RANGE (2,k
,0,SCM_LENGTH(str
));
274 SCM_VALIDATE_CHAR (3,chr
);
275 SCM_UCHARS (str
)[SCM_INUM (k
)] = SCM_CHAR (chr
);
276 return SCM_UNSPECIFIED
;
282 SCM_DEFINE (scm_substring
, "substring", 2, 1, 0,
283 (SCM str
, SCM start
, SCM end
),
284 "Returns a newly allocated string formed from the characters\n"
285 "of STR beginning with index START (inclusive) and ending with\n"
286 "index END (exclusive).\n"
287 "STR must be a string, START and END must be exact integers satisfying:\n\n"
288 "0 <= START <= END <= (string-length STR).")
289 #define FUNC_NAME s_scm_substring
292 SCM_VALIDATE_ROSTRING (1,str
);
293 SCM_VALIDATE_INUM (2,start
);
294 SCM_VALIDATE_INUM_DEF (3,end
,SCM_ROLENGTH(str
));
295 SCM_ASSERT_RANGE (2,start
,SCM_INUM (start
) <= SCM_ROLENGTH (str
));
296 SCM_ASSERT_RANGE (2,end
,SCM_INUM (end
) <= SCM_ROLENGTH (str
));
297 l
= SCM_INUM (end
)-SCM_INUM (start
);
298 SCM_ASSERT (l
>= 0, SCM_MAKINUM (l
), SCM_OUTOFRANGE
, FUNC_NAME
);
299 return scm_makfromstr (&SCM_ROCHARS (str
)[SCM_INUM (start
)], (scm_sizet
)l
, 0);
303 SCM_DEFINE (scm_string_append
, "string-append", 0, 0, 1,
305 "Returns a newly allocated string whose characters form the\n"
306 "concatenation of the given strings, ARGS.")
307 #define FUNC_NAME s_scm_string_append
312 register unsigned char *data
;
314 SCM_VALIDATE_REST_ARGUMENT (args
);
315 for (l
= args
; !SCM_NULLP (l
); l
= SCM_CDR (l
)) {
317 SCM_VALIDATE_ROSTRING (SCM_ARGn
,s
);
318 i
+= SCM_ROLENGTH (s
);
320 res
= scm_makstr (i
, 0);
321 data
= SCM_UCHARS (res
);
322 for (l
= args
;SCM_NIMP (l
);l
= SCM_CDR (l
)) {
324 for (i
= 0;i
<SCM_ROLENGTH (s
);i
++) *data
++ = SCM_ROUCHARS (s
)[i
];
330 SCM_DEFINE (scm_make_shared_substring
, "make-shared-substring", 1, 2, 0,
331 (SCM str
, SCM frm
, SCM to
),
332 "Return a shared substring of @var{str}. The semantics are the same as\n"
333 "for the @code{substring} function: the shared substring returned\n"
334 "includes all of the text from @var{str} between indexes @var{start}\n"
335 "(inclusive) and @var{end} (exclusive). If @var{end} is omitted, it\n"
336 "defaults to the end of @var{str}. The shared substring returned by\n"
337 "@code{make-shared-substring} occupies the same storage space as\n"
339 #define FUNC_NAME s_scm_make_shared_substring
346 SCM_VALIDATE_ROSTRING (1,str
);
347 SCM_VALIDATE_INUM_DEF_COPY (2,frm
,0,f
);
348 SCM_VALIDATE_INUM_DEF_COPY (3,to
,SCM_ROLENGTH(str
),t
);
350 SCM_ASSERT_RANGE (2,frm
,(f
>= 0));
351 SCM_ASSERT_RANGE (3,to
, (f
<= t
) && (t
<= SCM_ROLENGTH (str
)));
353 SCM_NEWCELL (answer
);
354 SCM_NEWCELL (len_str
);
357 if (SCM_SUBSTRP (str
))
360 offset
= SCM_INUM (SCM_SUBSTR_OFFSET (str
));
363 SCM_SETCAR (len_str
, SCM_MAKINUM (f
));
364 SCM_SETCDR (len_str
, SCM_SUBSTR_STR (str
));
365 SCM_SETCDR (answer
, len_str
);
366 SCM_SETLENGTH (answer
, t
- f
, scm_tc7_substring
);
370 SCM_SETCAR (len_str
, SCM_MAKINUM (f
));
371 SCM_SETCDR (len_str
, str
);
372 SCM_SETCDR (answer
, len_str
);
373 SCM_SETLENGTH (answer
, t
- f
, scm_tc7_substring
);
383 #include "libguile/strings.x"