mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-28 16:00:22 +02:00
(SCM_NUM2{SIZE,PTRDIFF,SHORT,USHORT,BITS,UBITS,INT,UINT}[_DEF]): new macros. * unif.h: type renaming: scm_array -> scm_array_t scm_array_dim -> scm_array_dim_t the old names are deprecated, all in-Guile uses changed. * tags.h (scm_ubits_t): new typedef, representing unsigned scm_bits_t. * stacks.h: type renaming: scm_info_frame -> scm_info_frame_t scm_stack -> scm_stack_t the old names are deprecated, all in-Guile uses changed. * srcprop.h: type renaming: scm_srcprops -> scm_srcprops_t scm_srcprops_chunk -> scm_srcprops_chunk_t the old names are deprecated, all in-Guile uses changed. * gsubr.c, procs.c, print.c, ports.c, read.c, rdelim.c, ramap.c, rw.c, smob.c, sort.c, srcprop.c, stacks.c, strings.c, strop.c, strorder.c, strports.c, struct.c, symbols.c, unif.c, values.c, vectors.c, vports.c, weaks.c: various int/size_t -> size_t/scm_bits_t changes. * random.h: type renaming: scm_rstate -> scm_rstate_t scm_rng -> scm_rng_t scm_i_rstate -> scm_i_rstate_t the old names are deprecated, all in-Guile uses changed. * procs.h: type renaming: scm_subr_entry -> scm_subr_entry_t the old name is deprecated, all in-Guile uses changed. * options.h (scm_option_t.val): unsigned long -> scm_bits_t. type renaming: scm_option -> scm_option_t the old name is deprecated, all in-Guile uses changed. * objects.c: various long -> scm_bits_t changes. (scm_i_make_class_object): flags: unsigned long -> scm_ubits_t * numbers.h (SCM_FIXNUM_BIT): deprecated, renamed to SCM_I_FIXNUM_BIT. * num2integral.i.c: new file, multiply included by numbers.c, used to "templatize" the various integral <-> num conversion routines. * numbers.c (scm_mkbig, scm_big2num, scm_adjbig, scm_normbig, scm_copybig, scm_2ulong2big, scm_dbl2big, scm_big2dbl): deprecated. (scm_i_mkbig, scm_i_big2inum, scm_i_adjbig, scm_i_normbig, scm_i_copybig, scm_i_short2big, scm_i_ushort2big, scm_i_int2big, scm_i_uint2big, scm_i_long2big, scm_i_ulong2big, scm_i_bits2big, scm_i_ubits2big, scm_i_size2big, scm_i_ptrdiff2big, scm_i_long_long2big, scm_i_ulong_long2big, scm_i_dbl2big, scm_i_big2dbl, scm_short2num, scm_ushort2num, scm_int2num, scm_uint2num, scm_bits2num, scm_ubits2num, scm_size2num, scm_ptrdiff2num, scm_num2short, scm_num2ushort, scm_num2int, scm_num2uint, scm_num2bits, scm_num2ubits, scm_num2ptrdiff, scm_num2size): new functions. * modules.c (scm_module_reverse_lookup): i, n: int -> scm_bits_t.x * load.c: change int -> size_t in various places (where the variable is used to store a string length). (search-path): call scm_done_free, not scm_done_malloc. * list.c (scm_ilength): return a scm_bits_t, not long. some other {int,long} -> scm_bits_t changes. * hashtab.c: various [u]int -> scm_bits_t changes. scm_ihashx_closure -> scm_ihashx_closure_t (and made a typedef). (scm_ihashx): n: uint -> scm_bits_t use scm_bits2num instead of scm_ulong2num. * gsubr.c: various int -> scm_bits_t changes. * gh_data.c (gh_scm2double): no loss of precision any more. * gh.h (gh_str2scm): len: int -> size_t (gh_{get,set}_substr): start: int -> scm_bits_t, len: int -> size_t (gh_<num>2scm): n: int -> scm_bits_t (gh_*vector_length): return scm_[u]size_t, not unsigned long. (gh_length): return scm_bits_t, not unsigned long. * fports.h: type renaming: scm_fport -> scm_fport_t the old name is deprecated, all in-Guile uses changed. * fports.c (fport_fill_input): count: int -> scm_bits_t (fport_flush): init_size, remaining, count: int -> scm_bits_t * debug.h (scm_lookup_cstr, scm_lookup_soft, scm_evstr): removed those prototypes, as the functions they prototype don't exist. * fports.c (default_buffer_size): int -> size_t (scm_fport_buffer_add): read_size, write_size: int -> scm_bits_t default_size: int -> size_t (scm_setvbuf): csize: int -> scm_bits_t * fluids.c (n_fluids): int -> scm_bits_t (grow_fluids): old_length, i: int -> scm_bits_t (next_fluid_num, scm_fluid_ref, scm_fluid_set_x): n: int -> scm_bits_t (scm_c_with_fluids): flen, vlen: int -> scm_bits_t * filesys.c (s_scm_open_fdes): changed calls to SCM_NUM2LONG to the new and shiny SCM_NUM2INT. * extensions.c: extension -> extension_t (and made a typedef). * eval.h (SCM_IFRAME): cast to scm_bits_t, not int. just so there are no nasty surprises if/when the various deeply magic tag bits move somewhere else. * eval.c: changed the locals used to store results of SCM_IFRAME, scm_ilength and such to be of type scm_bits_t (and not int/long). (iqq): depth, edepth: int -> scm_bits_t (scm_eval_stack): int -> scm_bits_t (SCM_CEVAL): various vars are not scm_bits_t instead of int. (check_map_args, scm_map, scm_for_each): len: long -> scm_bits_t i: int -> scm_bits_t * environments.c: changed the many calls to scm_ulong2num to scm_ubits2num. (import_environment_fold): proc_as_ul: ulong -> scm_ubits_t * dynwind.c (scm_dowinds): delta: long -> scm_bits_t * debug.h: type renaming: scm_debug_info -> scm_debug_info_t scm_debug_frame -> scm_debug_frame_t the old names are deprecated, all in-Guile uses changed. (scm_debug_eframe_size): int -> scm_bits_t * debug.c (scm_init_debug): use scm_c_define instead of the deprecated scm_define. * continuations.h: type renaming: scm_contregs -> scm_contregs_t the old name is deprecated, all in-Guile uses changed. (scm_contregs_t.num_stack_items): size_t -> scm_bits_t (scm_contregs_t.num_stack_items): ulong -> scm_ubits_t * continuations.c (scm_make_continuation): change the type of stack_size form long to scm_bits_t. * ports.h: type renaming: scm_port_rw_active -> scm_port_rw_active_t (and made a typedef) scm_port -> scm_port_t scm_ptob_descriptor -> scm_ptob_descriptor_t the old names are deprecated, all in-Guile uses changed. (scm_port_t.entry): int -> scm_bits_t. (scm_port_t.line_number): int -> long. (scm_port_t.putback_buf_size): int -> size_t. * __scm.h (long_long, ulong_long): deprecated (they pollute the global namespace and have little value besides that). (SCM_BITS_LENGTH): new, is the bit size of scm_bits_t (i.e. of an SCM handle). (ifdef spaghetti): include sys/types.h and sys/stdtypes.h, if they exist (for size_t & ptrdiff_t) (scm_sizet): deprecated. * Makefile.am (noinst_HEADERS): add num2integral.i.c
587 lines
16 KiB
C
587 lines
16 KiB
C
/* classes: src_files */
|
||
|
||
/* Copyright (C) 1994, 1996, 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
|
||
|
||
This program is free software; you can redistribute it and/or modify
|
||
it under the terms of the GNU General Public License as published by
|
||
the Free Software Foundation; either version 2, or (at your option)
|
||
any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU General Public License for more details.
|
||
|
||
You should have received a copy of the GNU General Public License
|
||
along with this software; see the file COPYING. If not, write to the
|
||
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
||
02111-1307 USA */
|
||
|
||
/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
|
||
gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
|
||
|
||
|
||
|
||
|
||
#include <errno.h>
|
||
|
||
#include "libguile/_scm.h"
|
||
#include "libguile/chars.h"
|
||
#include "libguile/strings.h"
|
||
|
||
#include "libguile/validate.h"
|
||
#include "libguile/strop.h"
|
||
#include "libguile/read.h" /*For SCM_CASE_INSENSITIVE_P*/
|
||
|
||
#ifdef HAVE_STRING_H
|
||
#include <string.h>
|
||
#endif
|
||
|
||
|
||
|
||
/*
|
||
xSCM_DEFINE (scm_i_index, "i-index", 2, 2, 0,
|
||
(SCM str, SCM chr, SCM frm, SCM to),
|
||
"@deftypefn {Internal C Function} {static int} scm_i_index (SCM *@var{str}, \n"
|
||
"SCM @var{chr}, int @var{direction}, SCM @var{sub_start}, SCM @var{sub_end}, char *@var{why})
|
||
"This is a workhorse function that performs either an @code{index} or\n"
|
||
"@code{rindex} function, depending on the value of @var{direction}."
|
||
*/
|
||
/* implements index if direction > 0 otherwise rindex. */
|
||
static scm_bits_t
|
||
scm_i_index (SCM *str, SCM chr, int direction, SCM sub_start,
|
||
SCM sub_end, const char *why)
|
||
{
|
||
unsigned char * p;
|
||
scm_bits_t x;
|
||
scm_bits_t lower;
|
||
scm_bits_t upper;
|
||
int ch;
|
||
|
||
SCM_ASSERT (SCM_STRINGP (*str), *str, SCM_ARG1, why);
|
||
SCM_ASSERT (SCM_CHARP (chr), chr, SCM_ARG2, why);
|
||
|
||
if (SCM_FALSEP (sub_start))
|
||
sub_start = SCM_MAKINUM (0);
|
||
|
||
SCM_ASSERT (SCM_INUMP (sub_start), sub_start, SCM_ARG3, why);
|
||
lower = SCM_INUM (sub_start);
|
||
if (lower < 0 || lower > SCM_STRING_LENGTH (*str))
|
||
scm_out_of_range (why, sub_start);
|
||
|
||
if (SCM_FALSEP (sub_end))
|
||
sub_end = SCM_MAKINUM (SCM_STRING_LENGTH (*str));
|
||
|
||
SCM_ASSERT (SCM_INUMP (sub_end), sub_end, SCM_ARG4, why);
|
||
upper = SCM_INUM (sub_end);
|
||
if (upper < SCM_INUM (sub_start) || upper > SCM_STRING_LENGTH (*str))
|
||
scm_out_of_range (why, sub_end);
|
||
|
||
if (direction > 0)
|
||
{
|
||
p = SCM_STRING_UCHARS (*str) + lower;
|
||
ch = SCM_CHAR (chr);
|
||
|
||
for (x = SCM_INUM (sub_start); x < upper; ++x, ++p)
|
||
if (*p == ch)
|
||
return x;
|
||
}
|
||
else
|
||
{
|
||
p = upper - 1 + SCM_STRING_UCHARS (*str);
|
||
ch = SCM_CHAR (chr);
|
||
for (x = upper - 1; x >= lower; --x, --p)
|
||
if (*p == ch)
|
||
return x;
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
|
||
SCM_DEFINE (scm_string_index, "string-index", 2, 2, 0,
|
||
(SCM str, SCM chr, SCM frm, SCM to),
|
||
"Return the index of the first occurrence of @var{chr} in\n"
|
||
"@var{str}. The optional integer arguments @var{frm} and\n"
|
||
"@var{to} limit the search to a portion of the string. This\n"
|
||
"procedure essentially implements the @code{index} or\n"
|
||
"@code{strchr} functions from the C library.\n"
|
||
"\n"
|
||
"@lisp\n"
|
||
"(string-index \"weiner\" #\\e)\n"
|
||
"@result{} 1\n\n"
|
||
"(string-index \"weiner\" #\\e 2)\n"
|
||
"@result{} 4\n\n"
|
||
"(string-index \"weiner\" #\\e 2 4)\n"
|
||
"@result{} #f\n"
|
||
"@end lisp")
|
||
#define FUNC_NAME s_scm_string_index
|
||
{
|
||
scm_bits_t pos;
|
||
|
||
if (SCM_UNBNDP (frm))
|
||
frm = SCM_BOOL_F;
|
||
if (SCM_UNBNDP (to))
|
||
to = SCM_BOOL_F;
|
||
pos = scm_i_index (&str, chr, 1, frm, to, FUNC_NAME);
|
||
return (pos < 0
|
||
? SCM_BOOL_F
|
||
: SCM_MAKINUM (pos));
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
SCM_DEFINE (scm_string_rindex, "string-rindex", 2, 2, 0,
|
||
(SCM str, SCM chr, SCM frm, SCM to),
|
||
"Like @code{string-index}, but search from the right of the\n"
|
||
"string rather than from the left. This procedure essentially\n"
|
||
"implements the @code{rindex} or @code{strrchr} functions from\n"
|
||
"the C library.\n"
|
||
"\n"
|
||
"@lisp\n"
|
||
"(string-rindex \"weiner\" #\\e)\n"
|
||
"@result{} 4\n\n"
|
||
"(string-rindex \"weiner\" #\\e 2 4)\n"
|
||
"@result{} #f\n\n"
|
||
"(string-rindex \"weiner\" #\\e 2 5)\n"
|
||
"@result{} 4\n"
|
||
"@end lisp")
|
||
#define FUNC_NAME s_scm_string_rindex
|
||
{
|
||
scm_bits_t pos;
|
||
|
||
if (SCM_UNBNDP (frm))
|
||
frm = SCM_BOOL_F;
|
||
if (SCM_UNBNDP (to))
|
||
to = SCM_BOOL_F;
|
||
pos = scm_i_index (&str, chr, -1, frm, to, FUNC_NAME);
|
||
return (pos < 0
|
||
? SCM_BOOL_F
|
||
: SCM_MAKINUM (pos));
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
SCM_REGISTER_PROC(s_substring_move_left_x, "substring-move-left!", 5, 0, 0, scm_substring_move_x);
|
||
SCM_REGISTER_PROC(s_substring_move_right_x, "substring-move-right!", 5, 0, 0, scm_substring_move_x);
|
||
|
||
/*
|
||
@defun substring-move-left! str1 start1 end1 str2 start2
|
||
@end defun
|
||
@deftypefn {C Function} SCM scm_substring_move_left_x (SCM @var{str1}, SCM @var{start1}, SCM @var{end1}, SCM @var{str2}, SCM @var{start2})
|
||
[@strong{Note:} this is only valid if you've applied the strop patch].
|
||
|
||
Moves a substring of @var{str1}, from @var{start1} to @var{end1}
|
||
(@var{end1} is exclusive), into @var{str2}, starting at
|
||
@var{start2}. Allows overlapping strings.
|
||
|
||
@lisp
|
||
(define x (make-string 10 #\a))
|
||
(define y "bcd")
|
||
(substring-move-left! x 2 5 y 0)
|
||
y
|
||
@result{} "aaa"
|
||
|
||
x
|
||
@result{} "aaaaaaaaaa"
|
||
|
||
(define y "bcdefg")
|
||
(substring-move-left! x 2 5 y 0)
|
||
y
|
||
@result{} "aaaefg"
|
||
|
||
(define y "abcdefg")
|
||
(substring-move-left! y 2 5 y 3)
|
||
y
|
||
@result{} "abccccg"
|
||
@end lisp
|
||
*/
|
||
|
||
/*
|
||
@defun substring-move-right! str1 start1 end1 str2 start2
|
||
@end defun
|
||
@deftypefn {C Function} SCM scm_substring_move_right_x (SCM @var{str1}, SCM @var{start1}, SCM @var{end1}, SCM @var{str2}, SCM @var{start2})
|
||
[@strong{Note:} this is only valid if you've applied the strop patch, if
|
||
it hasn't made it into the guile tree].
|
||
|
||
Does much the same thing as @code{substring-move-left!}, except it
|
||
starts moving at the end of the sequence, rather than the beginning.
|
||
@lisp
|
||
(define y "abcdefg")
|
||
(substring-move-right! y 2 5 y 0)
|
||
y
|
||
@result{} "ededefg"
|
||
|
||
(define y "abcdefg")
|
||
(substring-move-right! y 2 5 y 3)
|
||
y
|
||
@result{} "abccdeg"
|
||
@end lisp
|
||
*/
|
||
|
||
SCM_DEFINE (scm_substring_move_x, "substring-move!", 5, 0, 0,
|
||
(SCM str1, SCM start1, SCM end1, SCM str2, SCM start2),
|
||
"@deffnx primitive substring-move-left! str1 start1 end1 str2 start2\n"
|
||
"@deffnx primitive substring-move-right! str1 start1 end1 str2 start2\n"
|
||
"Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}\n"
|
||
"into @var{str2} beginning at position @var{start2}.\n"
|
||
"@code{substring-move-right!} begins copying from the rightmost character\n"
|
||
"and moves left, and @code{substring-move-left!} copies from the leftmost\n"
|
||
"character moving right.\n\n"
|
||
"It is useful to have two functions that copy in different directions so\n"
|
||
"that substrings can be copied back and forth within a single string. If\n"
|
||
"you wish to copy text from the left-hand side of a string to the\n"
|
||
"right-hand side of the same string, and the source and destination\n"
|
||
"overlap, you must be careful to copy the rightmost characters of the\n"
|
||
"text first, to avoid clobbering your data. Hence, when @var{str1} and\n"
|
||
"@var{str2} are the same string, you should use\n"
|
||
"@code{substring-move-right!} when moving text from left to right, and\n"
|
||
"@code{substring-move-left!} otherwise. If @code{str1} and @samp{str2}\n"
|
||
"are different strings, it does not matter which function you use.")
|
||
#define FUNC_NAME s_scm_substring_move_x
|
||
{
|
||
scm_bits_t s1, s2, e, len;
|
||
|
||
SCM_VALIDATE_STRING (1,str1);
|
||
SCM_VALIDATE_INUM_COPY (2,start1,s1);
|
||
SCM_VALIDATE_INUM_COPY (3,end1,e);
|
||
SCM_VALIDATE_STRING (4,str2);
|
||
SCM_VALIDATE_INUM_COPY (5,start2,s2);
|
||
len = e - s1;
|
||
SCM_ASSERT_RANGE (3,end1,len >= 0);
|
||
SCM_ASSERT_RANGE (2,start1,s1 <= SCM_STRING_LENGTH (str1) && s1 >= 0);
|
||
SCM_ASSERT_RANGE (5,start2,s2 <= SCM_STRING_LENGTH (str2) && s2 >= 0);
|
||
SCM_ASSERT_RANGE (3,end1,e <= SCM_STRING_LENGTH (str1) && e >= 0);
|
||
SCM_ASSERT_RANGE (5,start2,len+s2 <= SCM_STRING_LENGTH (str2));
|
||
|
||
SCM_SYSCALL(memmove((void *)(&(SCM_STRING_CHARS(str2)[s2])),
|
||
(void *)(&(SCM_STRING_CHARS(str1)[s1])),
|
||
len));
|
||
|
||
return scm_return_first(SCM_UNSPECIFIED, str1, str2);
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
SCM_DEFINE (scm_substring_fill_x, "substring-fill!", 4, 0, 0,
|
||
(SCM str, SCM start, SCM end, SCM fill),
|
||
"Change every character in @var{str} between @var{start} and\n"
|
||
"@var{end} to @var{fill}.\n"
|
||
"\n"
|
||
"@lisp\n"
|
||
"(define y \"abcdefg\")\n"
|
||
"(substring-fill! y 1 3 #\\r)\n"
|
||
"y\n"
|
||
"@result{} \"arrdefg\"\n"
|
||
"@end lisp")
|
||
#define FUNC_NAME s_scm_substring_fill_x
|
||
{
|
||
scm_bits_t i, e;
|
||
char c;
|
||
SCM_VALIDATE_STRING (1,str);
|
||
SCM_VALIDATE_INUM_COPY (2,start,i);
|
||
SCM_VALIDATE_INUM_COPY (3,end,e);
|
||
SCM_VALIDATE_CHAR_COPY (4,fill,c);
|
||
SCM_ASSERT_RANGE (2,start,i <= SCM_STRING_LENGTH (str) && i >= 0);
|
||
SCM_ASSERT_RANGE (3,end,e <= SCM_STRING_LENGTH (str) && e >= 0);
|
||
while (i<e) SCM_STRING_CHARS (str)[i++] = c;
|
||
return SCM_UNSPECIFIED;
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
SCM_DEFINE (scm_string_null_p, "string-null?", 1, 0, 0,
|
||
(SCM str),
|
||
"Return @code{#t} if @var{str}'s length is nonzero, and\n"
|
||
"@code{#f} otherwise.\n"
|
||
"@lisp\n"
|
||
"(string-null? \"\") @result{} #t\n"
|
||
"y @result{} \"foo\"\n"
|
||
"(string-null? y) @result{} #f\n"
|
||
"@end lisp")
|
||
#define FUNC_NAME s_scm_string_null_p
|
||
{
|
||
SCM_VALIDATE_STRING (1,str);
|
||
return SCM_NEGATE_BOOL (SCM_STRING_LENGTH (str));
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
SCM_DEFINE (scm_string_to_list, "string->list", 1, 0, 0,
|
||
(SCM str),
|
||
"Return a newly allocated list of the characters that make up\n"
|
||
"the given string @var{str}. @code{string->list} and\n"
|
||
"@code{list->string} are inverses as far as @samp{equal?} is\n"
|
||
"concerned.")
|
||
#define FUNC_NAME s_scm_string_to_list
|
||
{
|
||
scm_bits_t i;
|
||
SCM res = SCM_EOL;
|
||
unsigned char *src;
|
||
SCM_VALIDATE_STRING (1,str);
|
||
src = SCM_STRING_UCHARS (str);
|
||
for (i = SCM_STRING_LENGTH (str)-1;i >= 0;i--) res = scm_cons (SCM_MAKE_CHAR (src[i]), res);
|
||
return res;
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
/* Helper function for the string copy and string conversion functions.
|
||
* No argument checking is performed. */
|
||
static SCM
|
||
string_copy (SCM str)
|
||
{
|
||
return scm_makfromstr (SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str), 0);
|
||
}
|
||
|
||
|
||
SCM_DEFINE (scm_string_copy, "string-copy", 1, 0, 0,
|
||
(SCM str),
|
||
"Return a newly allocated copy of the given @var{string}.")
|
||
#define FUNC_NAME s_scm_string_copy
|
||
{
|
||
SCM_VALIDATE_STRING (1, str);
|
||
|
||
return string_copy (str);
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
SCM_DEFINE (scm_string_fill_x, "string-fill!", 2, 0, 0,
|
||
(SCM str, SCM chr),
|
||
"Store @var{char} in every element of the given @var{string} and\n"
|
||
"return an unspecified value.")
|
||
#define FUNC_NAME s_scm_string_fill_x
|
||
{
|
||
register char *dst, c;
|
||
register scm_bits_t k;
|
||
SCM_VALIDATE_STRING_COPY (1,str,dst);
|
||
SCM_VALIDATE_CHAR_COPY (2,chr,c);
|
||
for (k = SCM_STRING_LENGTH (str)-1;k >= 0;k--) dst[k] = c;
|
||
return SCM_UNSPECIFIED;
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
/* Helper function for the string uppercase conversion functions.
|
||
* No argument checking is performed. */
|
||
static SCM
|
||
string_upcase_x (SCM v)
|
||
{
|
||
scm_bits_t k;
|
||
|
||
for (k = 0; k < SCM_STRING_LENGTH (v); ++k)
|
||
SCM_STRING_UCHARS (v) [k] = scm_upcase (SCM_STRING_UCHARS (v) [k]);
|
||
|
||
return v;
|
||
}
|
||
|
||
|
||
SCM_DEFINE (scm_string_upcase_x, "string-upcase!", 1, 0, 0,
|
||
(SCM str),
|
||
"Destructively upcase every character in @var{str} and return\n"
|
||
"@var{str}.\n"
|
||
"@lisp\n"
|
||
"y @result{} \"arrdefg\"\n"
|
||
"(string-upcase! y) @result{} \"ARRDEFG\"\n"
|
||
"y @result{} \"ARRDEFG\"\n"
|
||
"@end lisp")
|
||
#define FUNC_NAME s_scm_string_upcase_x
|
||
{
|
||
SCM_VALIDATE_STRING (1, str);
|
||
|
||
return string_upcase_x (str);
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
SCM_DEFINE (scm_string_upcase, "string-upcase", 1, 0, 0,
|
||
(SCM str),
|
||
"Return a freshly allocated string containing the characters of\n"
|
||
"@var{str} in upper case.")
|
||
#define FUNC_NAME s_scm_string_upcase
|
||
{
|
||
SCM_VALIDATE_STRING (1, str);
|
||
|
||
return string_upcase_x (string_copy (str));
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
/* Helper function for the string lowercase conversion functions.
|
||
* No argument checking is performed. */
|
||
static SCM
|
||
string_downcase_x (SCM v)
|
||
{
|
||
scm_bits_t k;
|
||
|
||
for (k = 0; k < SCM_STRING_LENGTH (v); ++k)
|
||
SCM_STRING_UCHARS (v) [k] = scm_downcase (SCM_STRING_UCHARS (v) [k]);
|
||
|
||
return v;
|
||
}
|
||
|
||
|
||
SCM_DEFINE (scm_string_downcase_x, "string-downcase!", 1, 0, 0,
|
||
(SCM str),
|
||
"Destructively downcase every character in @var{str} and return\n"
|
||
"@var{str}.\n"
|
||
"@lisp\n"
|
||
"y @result{} \"ARRDEFG\"\n"
|
||
"(string-downcase! y) @result{} \"arrdefg\"\n"
|
||
"y @result{} \"arrdefg\"\n"
|
||
"@end lisp")
|
||
#define FUNC_NAME s_scm_string_downcase_x
|
||
{
|
||
SCM_VALIDATE_STRING (1, str);
|
||
|
||
return string_downcase_x (str);
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
SCM_DEFINE (scm_string_downcase, "string-downcase", 1, 0, 0,
|
||
(SCM str),
|
||
"Return a freshly allocation string containing the characters in\n"
|
||
"@var{str} in lower case.")
|
||
#define FUNC_NAME s_scm_string_downcase
|
||
{
|
||
SCM_VALIDATE_STRING (1, str);
|
||
|
||
return string_downcase_x (string_copy (str));
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
/* Helper function for the string capitalization functions.
|
||
* No argument checking is performed. */
|
||
static SCM
|
||
string_capitalize_x (SCM str)
|
||
{
|
||
char *sz;
|
||
scm_bits_t i, len;
|
||
int in_word=0;
|
||
|
||
len = SCM_STRING_LENGTH(str);
|
||
sz = SCM_STRING_CHARS (str);
|
||
for(i=0; i<len; i++) {
|
||
if(SCM_NFALSEP(scm_char_alphabetic_p(SCM_MAKE_CHAR(sz[i])))) {
|
||
if(!in_word) {
|
||
sz[i] = scm_upcase(sz[i]);
|
||
in_word = 1;
|
||
} else {
|
||
sz[i] = scm_downcase(sz[i]);
|
||
}
|
||
}
|
||
else in_word = 0;
|
||
}
|
||
return str;
|
||
}
|
||
|
||
|
||
SCM_DEFINE (scm_string_capitalize_x, "string-capitalize!", 1, 0, 0,
|
||
(SCM str),
|
||
"Upcase the first character of every word in @var{str}\n"
|
||
"destructively and return @var{str}.\n"
|
||
"\n"
|
||
"@lisp\n"
|
||
"y @result{} "hello world"\n"
|
||
"(string-capitalize! y) @result{} "Hello World"\n"
|
||
"y @result{} "Hello World"\n"
|
||
"@end lisp")
|
||
#define FUNC_NAME s_scm_string_capitalize_x
|
||
{
|
||
SCM_VALIDATE_STRING (1, str);
|
||
|
||
return string_capitalize_x (str);
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
SCM_DEFINE (scm_string_capitalize, "string-capitalize", 1, 0, 0,
|
||
(SCM str),
|
||
"Return a freshly allocated string with the characters in\n"
|
||
"@var{str}, where the first character of every word is\n"
|
||
"capitalized.")
|
||
#define FUNC_NAME s_scm_string_capitalize
|
||
{
|
||
SCM_VALIDATE_STRING (1, str);
|
||
|
||
return string_capitalize_x (string_copy (str));
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
SCM_DEFINE (scm_string_split, "string-split", 2, 0, 0,
|
||
(SCM str, SCM chr),
|
||
"Split the string @var{str} into the a list of the substrings delimited\n"
|
||
"by appearances of the character @var{chr}. Note that an empty substring\n"
|
||
"between separator characters will result in an empty string in the\n"
|
||
"result list.\n"
|
||
"\n"
|
||
"@lisp\n"
|
||
"(string-split \"root:x:0:0:root:/root:/bin/bash\" #\:)\n"
|
||
"@result{}\n"
|
||
"(\"root\" \"x\" \"0\" \"0\" \"root\" \"/root\" \"/bin/bash\")\n"
|
||
"\n"
|
||
"(string-split \"::\" #\:)\n"
|
||
"@result{}\n"
|
||
"(\"\" \"\" \"\")\n"
|
||
"\n"
|
||
"(string-split \"\" #\:)\n"
|
||
"@result{}\n"
|
||
"(\"\")\n"
|
||
"@end lisp")
|
||
#define FUNC_NAME s_scm_string_split
|
||
{
|
||
scm_bits_t idx, last_idx;
|
||
char * p;
|
||
int ch;
|
||
SCM res = SCM_EOL;
|
||
|
||
SCM_VALIDATE_STRING (1, str);
|
||
SCM_VALIDATE_CHAR (2, chr);
|
||
|
||
idx = SCM_STRING_LENGTH (str);
|
||
p = SCM_STRING_CHARS (str);
|
||
ch = SCM_CHAR (chr);
|
||
while (idx >= 0)
|
||
{
|
||
last_idx = idx;
|
||
while (idx > 0 && p[idx - 1] != ch)
|
||
idx--;
|
||
if (idx >= 0)
|
||
{
|
||
res = scm_cons (scm_makfromstr (p + idx, last_idx - idx, 0), res);
|
||
idx--;
|
||
}
|
||
}
|
||
return res;
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
|
||
SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
|
||
(SCM str),
|
||
"Return the symbol whose name is @var{str}. @var{str} is\n"
|
||
"converted to lowercase before the conversion is done, if Guile\n"
|
||
"is currently reading symbols case--insensitively.")
|
||
#define FUNC_NAME s_scm_string_ci_to_symbol
|
||
{
|
||
return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
|
||
? scm_string_downcase(str)
|
||
: str);
|
||
}
|
||
#undef FUNC_NAME
|
||
|
||
void
|
||
scm_init_strop ()
|
||
{
|
||
#ifndef SCM_MAGIC_SNARFER
|
||
#include "libguile/strop.x"
|
||
#endif
|
||
}
|
||
|
||
/*
|
||
Local Variables:
|
||
c-file-style: "gnu"
|
||
End:
|
||
*/
|