1 /* Copyright (C) 1995,1996 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
);
182 SCM_NEWCELL (answer
);
184 SCM_SETLENGTH (answer
, strlen (it
), scm_tc7_string
);
185 SCM_SETCHARS (answer
, it
);
192 scm_makfromstr (src
, len
, slots
)
199 s
= scm_makstr ((long) len
, slots
);
209 scm_makfrom0str (src
)
212 if (!src
) return SCM_BOOL_F
;
213 return scm_makfromstr (src
, (scm_sizet
) strlen (src
), 0);
218 scm_makfrom0str_opt (src
)
221 return scm_makfrom0str (src
);
227 SCM_PROC(s_make_string
, "make-string", 1, 1, 0, scm_make_string
);
230 scm_make_string (k
, chr
)
235 register unsigned char *dst
;
237 SCM_ASSERT (SCM_INUMP (k
) && (k
>= 0), k
, SCM_ARG1
, s_make_string
);
239 res
= scm_makstr (i
, 0);
240 dst
= SCM_UCHARS (res
);
243 char c
= SCM_ICHR (chr
);
252 SCM_PROC(s_string_length
, "string-length", 1, 0, 0, scm_string_length
);
255 scm_string_length (str
)
258 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
, s_string_length
);
259 return SCM_MAKINUM (SCM_ROLENGTH (str
));
262 SCM_PROC(s_string_ref
, "string-ref", 1, 1, 0, scm_string_ref
);
265 scm_string_ref (str
, k
)
269 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
, s_string_ref
);
270 if (k
== SCM_UNDEFINED
)
272 SCM_ASSERT (SCM_INUMP (k
), k
, SCM_ARG2
, s_string_ref
);
273 SCM_ASSERT (SCM_INUM (k
) < SCM_ROLENGTH (str
) && SCM_INUM (k
) >= 0, k
, SCM_OUTOFRANGE
, s_string_ref
);
274 return SCM_MAKICHR (SCM_ROUCHARS (str
)[SCM_INUM (k
)]);
277 SCM_PROC(s_string_set_x
, "string-set!", 3, 0, 0, scm_string_set_x
);
280 scm_string_set_x (str
, k
, chr
)
285 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
),
286 str
, SCM_ARG1
, s_string_set_x
);
287 SCM_ASSERT (SCM_INUMP (k
), k
, SCM_ARG2
, s_string_set_x
);
288 SCM_ASSERT (SCM_ICHRP (chr
), chr
, SCM_ARG3
, s_string_set_x
);
289 if (! SCM_RWSTRINGP (str
))
290 scm_misc_error (s_string_set_x
, "argument is a read-only string", str
);
291 SCM_ASSERT ((SCM_INUM (k
) >= 0
292 && ((unsigned) SCM_INUM (k
)) < SCM_LENGTH (str
)),
293 k
, SCM_OUTOFRANGE
, s_string_set_x
);
294 SCM_UCHARS (str
)[SCM_INUM (k
)] = SCM_ICHR (chr
);
295 return SCM_UNSPECIFIED
;
300 SCM_PROC(s_substring
, "substring", 2, 1, 0, scm_substring
);
303 scm_substring (str
, start
, end
)
309 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
),
310 str
, SCM_ARG1
, s_substring
);
311 SCM_ASSERT (SCM_INUMP (start
), start
, SCM_ARG2
, s_substring
);
312 if (end
== SCM_UNDEFINED
)
313 end
= SCM_MAKINUM (SCM_ROLENGTH (str
));
314 SCM_ASSERT (SCM_INUMP (end
), end
, SCM_ARG3
, s_substring
);
315 SCM_ASSERT (SCM_INUM (start
) <= SCM_ROLENGTH (str
), start
, SCM_OUTOFRANGE
, s_substring
);
316 SCM_ASSERT (SCM_INUM (end
) <= SCM_ROLENGTH (str
), end
, SCM_OUTOFRANGE
, s_substring
);
317 l
= SCM_INUM (end
)-SCM_INUM (start
);
318 SCM_ASSERT (l
>= 0, SCM_MAKINUM (l
), SCM_OUTOFRANGE
, s_substring
);
319 return scm_makfromstr (&SCM_ROCHARS (str
)[SCM_INUM (start
)], (scm_sizet
)l
, 0);
322 SCM_PROC(s_string_append
, "string-append", 0, 0, 1, scm_string_append
);
325 scm_string_append (args
)
331 register unsigned char *data
;
332 for (l
= args
;SCM_NIMP (l
);) {
333 SCM_ASSERT (SCM_CONSP (l
), l
, SCM_ARGn
, s_string_append
);
335 SCM_ASSERT (SCM_NIMP (s
) && SCM_ROSTRINGP (s
),
336 s
, SCM_ARGn
, s_string_append
);
337 i
+= SCM_ROLENGTH (s
);
340 SCM_ASSERT (SCM_NULLP (l
), args
, SCM_ARGn
, s_string_append
);
341 res
= scm_makstr (i
, 0);
342 data
= SCM_UCHARS (res
);
343 for (l
= args
;SCM_NIMP (l
);l
= SCM_CDR (l
)) {
345 for (i
= 0;i
<SCM_ROLENGTH (s
);i
++) *data
++ = SCM_ROUCHARS (s
)[i
];
350 SCM_PROC(s_make_shared_substring
, "make-shared-substring", 1, 2, 0, scm_make_shared_substring
);
353 scm_make_shared_substring (str
, frm
, to
)
363 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
, s_make_shared_substring
);
365 if (frm
== SCM_UNDEFINED
)
366 frm
= SCM_MAKINUM (0);
368 SCM_ASSERT (SCM_INUMP (frm
), frm
, SCM_ARG2
, s_make_shared_substring
);
370 if (to
== SCM_UNDEFINED
)
371 to
= SCM_MAKINUM (SCM_ROLENGTH (str
));
373 SCM_ASSERT (SCM_INUMP (to
), to
, SCM_ARG3
, s_make_shared_substring
);
377 SCM_ASSERT ((f
>= 0), frm
, SCM_OUTOFRANGE
, s_make_shared_substring
);
378 SCM_ASSERT ((f
<= t
) && (t
<= SCM_ROLENGTH (str
)), to
, SCM_OUTOFRANGE
,
379 s_make_shared_substring
);
381 SCM_NEWCELL (answer
);
382 SCM_NEWCELL (len_str
);
385 if (SCM_SUBSTRP (str
))
388 offset
= SCM_INUM (SCM_SUBSTR_OFFSET (str
));
391 SCM_SETCAR (len_str
, SCM_MAKINUM (f
));
392 SCM_SETCDR (len_str
, SCM_SUBSTR_STR (str
));
393 SCM_SETCDR (answer
, len_str
);
394 SCM_SETLENGTH (answer
, t
- f
, scm_tc7_substring
);
398 SCM_SETCAR (len_str
, SCM_MAKINUM (f
));
399 SCM_SETCDR (len_str
, str
);
400 SCM_SETCDR (answer
, len_str
);
401 SCM_SETLENGTH (answer
, t
- f
, scm_tc7_substring
);