1 /* Copyright (C) 1995,1996,1998 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. */
53 SCM_PROC(s_string_p
, "string?", 1, 0, 0, scm_string_p
);
61 return SCM_STRINGP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
64 SCM_PROC(s_read_only_string_p
, "read-only-string?", 1, 0, 0, scm_read_only_string_p
);
67 scm_read_only_string_p (x
)
72 return SCM_ROSTRINGP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
75 SCM_PROC(s_list_to_string
, "list->string", 1, 0, 0, scm_string
);
76 SCM_PROC(s_string
, "string", 0, 0, 1, scm_string
);
83 register unsigned char *data
;
87 i
= scm_ilength (chrs
);
91 SCM_ASSERT (0, chrs
, SCM_ARG1
, s_string
);
97 for (len
= 0, s
= chrs
; s
!= SCM_EOL
; s
= SCM_CDR (s
))
98 if (SCM_ICHRP (SCM_CAR (s
)))
100 else if (SCM_NIMP (SCM_CAR (s
)) && SCM_ROSTRINGP (SCM_CAR (s
)))
101 len
+= SCM_ROLENGTH (SCM_CAR (s
));
105 SCM_ASSERT (0, s
, SCM_ARG1
, s_string
);
108 res
= scm_makstr (len
, 0);
109 data
= SCM_UCHARS (res
);
110 for (;SCM_NNULLP (chrs
);chrs
= SCM_CDR (chrs
))
112 if (SCM_ICHRP (SCM_CAR (chrs
)))
113 *data
++ = SCM_ICHR (SCM_CAR (chrs
));
118 l
= SCM_ROLENGTH (SCM_CAR (chrs
));
119 c
= SCM_ROCHARS (SCM_CAR (chrs
));
133 scm_makstr (len
, slots
)
142 mem
= (SCM
*)scm_must_malloc (sizeof (SCM
) * (slots
+ 1) + len
+ 1,
147 mem
[slots
] = (SCM
)mem
;
148 for (x
= 0; x
< slots
; ++x
)
151 SCM_SETCHARS (s
, (char *) (mem
+ slots
+ 1));
152 SCM_SETLENGTH (s
, len
, scm_tc7_string
);
154 SCM_CHARS (s
)[len
] = 0;
158 /* converts C scm_array of strings to SCM scm_list of strings. */
159 /* If argc < 0, a null terminated scm_array is assumed. */
162 scm_makfromstrs (argc
, argv
)
169 for (i
= 0; argv
[i
]; i
++);
171 lst
= scm_cons (scm_makfromstr (argv
[i
], (scm_sizet
) strlen (argv
[i
]), 0), lst
);
176 /* This function must only be applied to memory obtained via malloc,
177 since the GC is going to apply `free' to it when the string is
180 Also, s[len] must be `\0', since we promise that strings are
181 null-terminated. Perhaps we could handle non-null-terminated
182 strings by claiming they're shared substrings of a string we just
185 scm_take_str (char *s
, int len
)
188 SCM_NEWCELL (answer
);
190 SCM_SETLENGTH (answer
, len
, scm_tc7_string
);
191 scm_done_malloc (len
+ 1);
192 SCM_SETCHARS (answer
, s
);
197 /* `s' must be a malloc'd string. See scm_take_str. */
199 scm_take0str (char *s
)
201 return scm_take_str (s
, strlen (s
));
206 scm_makfromstr (src
, len
, slots
)
213 s
= scm_makstr ((long) len
, slots
);
223 scm_makfrom0str (src
)
226 if (!src
) return SCM_BOOL_F
;
227 return scm_makfromstr (src
, (scm_sizet
) strlen (src
), 0);
232 scm_makfrom0str_opt (src
)
235 return scm_makfrom0str (src
);
241 SCM_PROC(s_make_string
, "make-string", 1, 1, 0, scm_make_string
);
244 scm_make_string (k
, chr
)
249 register unsigned char *dst
;
251 SCM_ASSERT (SCM_INUMP (k
) && (k
>= 0), k
, SCM_ARG1
, s_make_string
);
253 res
= scm_makstr (i
, 0);
254 dst
= SCM_UCHARS (res
);
257 char c
= SCM_ICHR (chr
);
266 SCM_PROC(s_string_length
, "string-length", 1, 0, 0, scm_string_length
);
269 scm_string_length (str
)
272 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
, s_string_length
);
273 return SCM_MAKINUM (SCM_ROLENGTH (str
));
276 SCM_PROC(s_string_ref
, "string-ref", 1, 1, 0, scm_string_ref
);
279 scm_string_ref (str
, k
)
283 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
, s_string_ref
);
284 if (k
== SCM_UNDEFINED
)
286 SCM_ASSERT (SCM_INUMP (k
), k
, SCM_ARG2
, s_string_ref
);
287 SCM_ASSERT (SCM_INUM (k
) < SCM_ROLENGTH (str
) && SCM_INUM (k
) >= 0, k
, SCM_OUTOFRANGE
, s_string_ref
);
288 return SCM_MAKICHR (SCM_ROUCHARS (str
)[SCM_INUM (k
)]);
291 SCM_PROC(s_string_set_x
, "string-set!", 3, 0, 0, scm_string_set_x
);
294 scm_string_set_x (str
, k
, chr
)
299 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
),
300 str
, SCM_ARG1
, s_string_set_x
);
301 SCM_ASSERT (SCM_INUMP (k
), k
, SCM_ARG2
, s_string_set_x
);
302 SCM_ASSERT (SCM_ICHRP (chr
), chr
, SCM_ARG3
, s_string_set_x
);
303 if (! SCM_RWSTRINGP (str
))
304 scm_misc_error (s_string_set_x
, "argument is a read-only string", str
);
305 SCM_ASSERT ((SCM_INUM (k
) >= 0
306 && ((unsigned) SCM_INUM (k
)) < SCM_LENGTH (str
)),
307 k
, SCM_OUTOFRANGE
, s_string_set_x
);
308 SCM_UCHARS (str
)[SCM_INUM (k
)] = SCM_ICHR (chr
);
309 return SCM_UNSPECIFIED
;
314 SCM_PROC(s_substring
, "substring", 2, 1, 0, scm_substring
);
317 scm_substring (str
, start
, end
)
323 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
),
324 str
, SCM_ARG1
, s_substring
);
325 SCM_ASSERT (SCM_INUMP (start
), start
, SCM_ARG2
, s_substring
);
326 if (end
== SCM_UNDEFINED
)
327 end
= SCM_MAKINUM (SCM_ROLENGTH (str
));
328 SCM_ASSERT (SCM_INUMP (end
), end
, SCM_ARG3
, s_substring
);
329 SCM_ASSERT (SCM_INUM (start
) <= SCM_ROLENGTH (str
), start
, SCM_OUTOFRANGE
, s_substring
);
330 SCM_ASSERT (SCM_INUM (end
) <= SCM_ROLENGTH (str
), end
, SCM_OUTOFRANGE
, s_substring
);
331 l
= SCM_INUM (end
)-SCM_INUM (start
);
332 SCM_ASSERT (l
>= 0, SCM_MAKINUM (l
), SCM_OUTOFRANGE
, s_substring
);
333 return scm_makfromstr (&SCM_ROCHARS (str
)[SCM_INUM (start
)], (scm_sizet
)l
, 0);
336 SCM_PROC(s_string_append
, "string-append", 0, 0, 1, scm_string_append
);
339 scm_string_append (args
)
345 register unsigned char *data
;
346 for (l
= args
;SCM_NIMP (l
);) {
347 SCM_ASSERT (SCM_CONSP (l
), l
, SCM_ARGn
, s_string_append
);
349 SCM_ASSERT (SCM_NIMP (s
) && SCM_ROSTRINGP (s
),
350 s
, SCM_ARGn
, s_string_append
);
351 i
+= SCM_ROLENGTH (s
);
354 SCM_ASSERT (SCM_NULLP (l
), args
, SCM_ARGn
, s_string_append
);
355 res
= scm_makstr (i
, 0);
356 data
= SCM_UCHARS (res
);
357 for (l
= args
;SCM_NIMP (l
);l
= SCM_CDR (l
)) {
359 for (i
= 0;i
<SCM_ROLENGTH (s
);i
++) *data
++ = SCM_ROUCHARS (s
)[i
];
364 SCM_PROC(s_make_shared_substring
, "make-shared-substring", 1, 2, 0, scm_make_shared_substring
);
367 scm_make_shared_substring (str
, frm
, to
)
377 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
, s_make_shared_substring
);
379 if (frm
== SCM_UNDEFINED
)
380 frm
= SCM_MAKINUM (0);
382 SCM_ASSERT (SCM_INUMP (frm
), frm
, SCM_ARG2
, s_make_shared_substring
);
384 if (to
== SCM_UNDEFINED
)
385 to
= SCM_MAKINUM (SCM_ROLENGTH (str
));
387 SCM_ASSERT (SCM_INUMP (to
), to
, SCM_ARG3
, s_make_shared_substring
);
391 SCM_ASSERT ((f
>= 0), frm
, SCM_OUTOFRANGE
, s_make_shared_substring
);
392 SCM_ASSERT ((f
<= t
) && (t
<= SCM_ROLENGTH (str
)), to
, SCM_OUTOFRANGE
,
393 s_make_shared_substring
);
395 SCM_NEWCELL (answer
);
396 SCM_NEWCELL (len_str
);
399 if (SCM_SUBSTRP (str
))
402 offset
= SCM_INUM (SCM_SUBSTR_OFFSET (str
));
405 SCM_SETCAR (len_str
, SCM_MAKINUM (f
));
406 SCM_SETCDR (len_str
, SCM_SUBSTR_STR (str
));
407 SCM_SETCDR (answer
, len_str
);
408 SCM_SETLENGTH (answer
, t
- f
, scm_tc7_substring
);
412 SCM_SETCAR (len_str
, SCM_MAKINUM (f
));
413 SCM_SETCDR (len_str
, str
);
414 SCM_SETCDR (answer
, len_str
);
415 SCM_SETLENGTH (answer
, t
- f
, scm_tc7_substring
);