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, 675 Mass Ave, Cambridge, MA 02139, USA.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
53 SCM_PROC(s_string_p
, "string?", 1, 0, 0, scm_string_p
);
65 return SCM_STRINGP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
68 SCM_PROC(s_read_only_string_p
, "read-only-string?", 1, 0, 0, scm_read_only_string_p
);
71 scm_read_only_string_p (SCM x
)
74 scm_read_only_string_p (x
)
80 return SCM_ROSTRINGP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
83 SCM_PROC(s_list_to_string
, "list->string", 1, 0, 0, scm_string
);
84 SCM_PROC(s_string
, "string", 0, 0, 1, scm_string
);
99 i
= scm_ilength (chrs
);
103 SCM_ASSERT (0, chrs
, SCM_ARG1
, s_string
);
109 for (len
= 0, s
= chrs
; s
!= SCM_EOL
; s
= SCM_CDR (s
))
110 if (SCM_ICHRP (SCM_CAR (s
)))
112 else if (SCM_NIMP (SCM_CAR (s
)) && SCM_ROSTRINGP (SCM_CAR (s
)))
113 len
+= SCM_ROLENGTH (SCM_CAR (s
));
117 SCM_ASSERT (0, s
, SCM_ARG1
, s_string
);
120 res
= scm_makstr (len
, 0);
121 data
= SCM_CHARS (res
);
122 for (;SCM_NNULLP (chrs
);chrs
= SCM_CDR (chrs
))
124 if (SCM_ICHRP (SCM_CAR (chrs
)))
125 *data
++ = SCM_ICHR (SCM_CAR (chrs
));
130 l
= SCM_ROLENGTH (SCM_CAR (chrs
));
131 c
= SCM_ROCHARS (SCM_CAR (chrs
));
145 scm_makstr (long len
, int slots
)
148 scm_makstr (len
, slots
)
158 mem
= (SCM
*)scm_must_malloc (sizeof (SCM
) * (slots
+ 1) + len
+ 1,
163 mem
[slots
] = (SCM
)mem
;
164 for (x
= 0; x
< slots
; ++x
)
167 SCM_SETCHARS (s
, (char *) (mem
+ slots
+ 1));
168 SCM_SETLENGTH (s
, len
, scm_tc7_string
);
170 SCM_CHARS (s
)[len
] = 0;
174 /* converts C scm_array of strings to SCM scm_list of strings. */
175 /* If argc < 0, a null terminated scm_array is assumed. */
178 scm_makfromstrs (int argc
, char **argv
)
181 scm_makfromstrs (argc
, argv
)
189 for (i
= 0; argv
[i
]; i
++);
191 lst
= scm_cons (scm_makfromstr (argv
[i
], (scm_sizet
) strlen (argv
[i
]), 0), lst
);
198 scm_take0str (char * it
)
206 SCM_NEWCELL (answer
);
208 SCM_SETLENGTH (answer
, strlen (it
), scm_tc7_string
);
209 SCM_SETCHARS (answer
, it
);
216 scm_makfromstr (const char *src
, scm_sizet len
, int slots
)
219 scm_makfromstr (src
, len
, slots
)
227 s
= scm_makstr ((long) len
, slots
);
237 scm_makfrom0str (char *src
)
240 scm_makfrom0str (src
)
244 if (!src
) return SCM_BOOL_F
;
245 return scm_makfromstr (src
, (scm_sizet
) strlen (src
), 0);
250 scm_makfrom0str_opt (char *src
)
253 scm_makfrom0str_opt (src
)
257 return scm_makfrom0str (src
);
263 SCM_PROC(s_make_string
, "make-string", 1, 1, 0, scm_make_string
);
266 scm_make_string (SCM k
, SCM chr
)
269 scm_make_string (k
, chr
)
277 SCM_ASSERT (SCM_INUMP (k
) && (k
>= 0), k
, SCM_ARG1
, s_make_string
);
279 res
= scm_makstr (i
, 0);
280 dst
= SCM_CHARS (res
);
283 char c
= SCM_ICHR (chr
);
292 SCM_PROC(s_string_length
, "string-length", 1, 0, 0, scm_string_length
);
295 scm_string_length (SCM str
)
298 scm_string_length (str
)
302 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
, s_string_length
);
303 return SCM_MAKINUM (SCM_ROLENGTH (str
));
306 SCM_PROC(s_string_ref
, "string-ref", 1, 1, 0, scm_string_ref
);
309 scm_string_ref (SCM str
, SCM k
)
312 scm_string_ref (str
, k
)
317 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
, s_string_ref
);
318 if (k
== SCM_UNDEFINED
)
320 SCM_ASSERT (SCM_INUMP (k
), k
, SCM_ARG2
, s_string_ref
);
321 SCM_ASSERT (SCM_INUM (k
) < SCM_ROLENGTH (str
) && SCM_INUM (k
) >= 0, k
, SCM_OUTOFRANGE
, s_string_ref
);
322 return SCM_MAKICHR (SCM_ROCHARS (str
)[SCM_INUM (k
)]);
325 SCM_PROC(s_string_set_x
, "string-set!", 3, 0, 0, scm_string_set_x
);
328 scm_string_set_x (SCM str
, SCM k
, SCM chr
)
331 scm_string_set_x (str
, k
, chr
)
337 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_string_set_x
);
338 SCM_ASSERT (SCM_INUMP (k
), k
, SCM_ARG2
, s_string_set_x
);
339 SCM_ASSERT (SCM_ICHRP (chr
), chr
, SCM_ARG3
, s_string_set_x
);
340 SCM_ASSERT (SCM_INUM (k
) < SCM_LENGTH (str
) && SCM_INUM (k
) >= 0, k
, SCM_OUTOFRANGE
, s_string_set_x
);
341 SCM_CHARS (str
)[SCM_INUM (k
)] = SCM_ICHR (chr
);
342 return SCM_UNSPECIFIED
;
347 SCM_PROC(s_substring
, "substring", 2, 1, 0, scm_substring
);
350 scm_substring (SCM str
, SCM start
, SCM end
)
353 scm_substring (str
, start
, end
)
360 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
),
361 str
, SCM_ARG1
, s_substring
);
362 SCM_ASSERT (SCM_INUMP (start
), start
, SCM_ARG2
, s_substring
);
363 if (end
== SCM_UNDEFINED
)
364 end
= SCM_MAKINUM (SCM_ROLENGTH (str
));
365 SCM_ASSERT (SCM_INUMP (end
), end
, SCM_ARG3
, s_substring
);
366 SCM_ASSERT (SCM_INUM (start
) <= SCM_ROLENGTH (str
), start
, SCM_OUTOFRANGE
, s_substring
);
367 SCM_ASSERT (SCM_INUM (end
) <= SCM_ROLENGTH (str
), end
, SCM_OUTOFRANGE
, s_substring
);
368 l
= SCM_INUM (end
)-SCM_INUM (start
);
369 SCM_ASSERT (l
>= 0, SCM_MAKINUM (l
), SCM_OUTOFRANGE
, s_substring
);
370 return scm_makfromstr (&SCM_ROCHARS (str
)[SCM_INUM (start
)], (scm_sizet
)l
, 0);
373 SCM_PROC(s_string_append
, "string-append", 0, 0, 1, scm_string_append
);
376 scm_string_append (SCM args
)
379 scm_string_append (args
)
387 for (l
= args
;SCM_NIMP (l
);) {
388 SCM_ASSERT (SCM_CONSP (l
), l
, SCM_ARGn
, s_string_append
);
390 SCM_ASSERT (SCM_NIMP (s
) && SCM_ROSTRINGP (s
),
391 s
, SCM_ARGn
, s_string_append
);
392 i
+= SCM_ROLENGTH (s
);
395 SCM_ASSERT (SCM_NULLP (l
), args
, SCM_ARGn
, s_string_append
);
396 res
= scm_makstr (i
, 0);
397 data
= SCM_CHARS (res
);
398 for (l
= args
;SCM_NIMP (l
);l
= SCM_CDR (l
)) {
400 for (i
= 0;i
<SCM_ROLENGTH (s
);i
++) *data
++ = SCM_ROCHARS (s
)[i
];
405 SCM_PROC(s_make_shared_substring
, "make-shared-substring", 1, 2, 0, scm_make_shared_substring
);
408 scm_make_shared_substring (SCM str
, SCM frm
, SCM to
)
411 scm_make_shared_substring (str
, frm
, to
)
422 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
, s_make_shared_substring
);
424 if (frm
== SCM_UNDEFINED
)
425 frm
= SCM_MAKINUM (0);
427 SCM_ASSERT (SCM_INUMP (frm
), frm
, SCM_ARG2
, s_make_shared_substring
);
429 if (to
== SCM_UNDEFINED
)
430 to
= SCM_MAKINUM (SCM_ROLENGTH (str
));
432 SCM_ASSERT (SCM_INUMP (to
), to
, SCM_ARG3
, s_make_shared_substring
);
436 SCM_ASSERT ((f
>= 0), frm
, SCM_OUTOFRANGE
, s_make_shared_substring
);
437 SCM_ASSERT ((f
<= t
) && (t
<= SCM_ROLENGTH (str
)), to
, SCM_OUTOFRANGE
, s_make_shared_substring
);
439 SCM_NEWCELL (answer
);
440 SCM_NEWCELL (len_str
);
443 if (SCM_SUBSTRP (str
))
446 offset
= SCM_INUM (SCM_SUBSTR_OFFSET (str
));
449 SCM_SETCAR (len_str
, SCM_MAKINUM (f
));
450 SCM_SETCDR (len_str
, SCM_SUBSTR_STR (str
));
451 SCM_SETCDR (answer
, len_str
);
452 SCM_SETLENGTH (answer
, t
- f
, scm_tc7_substring
);
456 SCM_SETCAR (len_str
, SCM_MAKINUM (f
));
457 SCM_SETCDR (len_str
, str
);
458 SCM_SETCDR (answer
, len_str
);
459 SCM_SETLENGTH (answer
, t
- f
, scm_tc7_substring
);
467 scm_init_strings (void)