diff options
author | Gary Houston <ghouston@arglist.com> | 1997-02-04 05:27:57 +0000 |
---|---|---|
committer | Gary Houston <ghouston@arglist.com> | 1997-02-04 05:27:57 +0000 |
commit | 03bc4386431ce688ae9e59db6bdbcee421738843 (patch) | |
tree | fa4eb851704673295577f5ebdb606570545357f5 /libguile/strop.c | |
parent | 538c267ac6aba74ac3bf78cfea5a2c4c2d1b2d88 (diff) | |
download | guile-03bc4386431ce688ae9e59db6bdbcee421738843.tar.gz |
* * net_db.c (scm_lnaof): change scheme name from lnaof to inet-lnaof.
* read.c (scm_lreadr): use scm_misc_error to improve one of the
"unknown # object" error messages.
* strop.c (scm_i_index, scm_i_rindex): combine into one procedure
(scm_i_index) and declare it static. Add a 'direction' argument
to indicate what way the search should go.
(scm_i_index): throw out-of-range error instead of wrong-type-arg
if indices are bad.
(scm_string_index, scm_string_rindex): adjust usage of scm_i_index.
strop.h: remove scm_i_index, scm_i_rindex prototypes.
Diffstat (limited to 'libguile/strop.c')
-rw-r--r-- | libguile/strop.c | 110 |
1 files changed, 39 insertions, 71 deletions
diff --git a/libguile/strop.c b/libguile/strop.c index c0e95561c..cb658124c 100644 --- a/libguile/strop.c +++ b/libguile/strop.c @@ -26,58 +26,15 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ -int -scm_i_index (str, chr, sub_start, sub_end, pos, pos2, pos3, pos4, why) - SCM * str; - SCM chr; - SCM sub_start; - SCM sub_end; - int pos; - int pos2; - int pos3; - int pos4; - char * why; -{ - unsigned char * p; - int x; - int bound; - int ch; - - SCM_ASSERT (SCM_NIMP (*str) && SCM_ROSTRINGP (*str), *str, pos, why); - SCM_ASSERT (SCM_ICHRP (chr), chr, pos2, why); - - if (sub_start == SCM_BOOL_F) - sub_start = SCM_MAKINUM (0); - else - SCM_ASSERT ( SCM_INUMP (sub_start) - && (0 <= SCM_INUM (sub_start)) - && (SCM_INUM (sub_start) <= SCM_ROLENGTH (*str)), - sub_start, pos3, why); - - if (sub_end == SCM_BOOL_F) - sub_end = SCM_MAKINUM (SCM_ROLENGTH (*str)); - else - SCM_ASSERT ( SCM_INUMP (sub_end) - && (SCM_INUM (sub_start) <= SCM_INUM (sub_end)) - && (SCM_INUM (sub_end) <= SCM_ROLENGTH (*str)), - sub_end, pos4, why); - - p = (unsigned char *)SCM_ROCHARS (*str) + SCM_INUM (sub_start); - bound = SCM_INUM (sub_end); - ch = SCM_ICHR (chr); - - for (x = SCM_INUM (sub_start); x < bound; ++x, ++p) - if (*p == ch) - return x; +static int scm_i_index SCM_P ((SCM * str, SCM chr, int direction, SCM sub_start, SCM sub_end, int pos, int pos2, int pos3, int pos4, char * why)); - return -1; -} - - -int -scm_i_rindex (str, chr, sub_start, sub_end, pos, pos2, pos3, pos4, why) +/* implements index if direction > 0 otherwise rindex. */ +static int +scm_i_index (str, chr, direction, sub_start, sub_end, pos, pos2, pos3, pos4, + why) SCM * str; SCM chr; + int direction; SCM sub_start; SCM sub_end; int pos; @@ -88,8 +45,8 @@ scm_i_rindex (str, chr, sub_start, sub_end, pos, pos2, pos3, pos4, why) { unsigned char * p; int x; - int upper_bound; - int lower_bound; + int lower; + int upper; int ch; SCM_ASSERT (SCM_NIMP (*str) && SCM_ROSTRINGP (*str), *str, pos, why); @@ -97,32 +54,43 @@ scm_i_rindex (str, chr, sub_start, sub_end, pos, pos2, pos3, pos4, why) if (sub_start == SCM_BOOL_F) sub_start = SCM_MAKINUM (0); - else - SCM_ASSERT ( SCM_INUMP (sub_start) - && (0 <= SCM_INUM (sub_start)) - && (SCM_INUM (sub_start) <= SCM_ROLENGTH (*str)), - sub_start, pos3, why); + + SCM_ASSERT (SCM_INUMP (sub_start), sub_start, pos3, why); + lower = SCM_INUM (sub_start); + if (lower < 0 + || lower >= SCM_ROLENGTH (*str)) + scm_out_of_range (why, sub_start); if (sub_end == SCM_BOOL_F) sub_end = SCM_MAKINUM (SCM_ROLENGTH (*str)); + + SCM_ASSERT (SCM_INUMP (sub_end), sub_end, pos4, why); + upper = SCM_INUM (sub_end); + if (upper < SCM_INUM (sub_start) + || upper > SCM_ROLENGTH (*str)) + scm_out_of_range (why, sub_end); + + if (direction > 0) + { + p = (unsigned char *)SCM_ROCHARS (*str) + lower; + ch = SCM_ICHR (chr); + + for (x = SCM_INUM (sub_start); x < upper; ++x, ++p) + if (*p == ch) + return x; + } else - SCM_ASSERT ( SCM_INUMP (sub_end) - && (SCM_INUM (sub_start) <= SCM_INUM (sub_end)) - && (SCM_INUM (sub_end) <= SCM_ROLENGTH (*str)), - sub_end, pos4, why); - - upper_bound = SCM_INUM (sub_end); - lower_bound = SCM_INUM (sub_start); - p = upper_bound - 1 + (unsigned char *)SCM_ROCHARS (*str); - ch = SCM_ICHR (chr); - for (x = upper_bound - 1; x >= lower_bound; --x, --p) - if (*p == ch) - return x; + { + p = upper - 1 + (unsigned char *)SCM_ROCHARS (*str); + ch = SCM_ICHR (chr); + for (x = upper - 1; x >= lower; --x, --p) + if (*p == ch) + return x; + } return -1; } - SCM_PROC(s_string_index, "string-index", 2, 2, 0, scm_string_index); SCM @@ -138,7 +106,7 @@ scm_string_index (str, chr, frm, to) frm = SCM_BOOL_F; if (to == SCM_UNDEFINED) to = SCM_BOOL_F; - pos = scm_i_index (&str, chr, frm, to, SCM_ARG1, SCM_ARG2, SCM_ARG3, SCM_ARG4, s_string_index); + pos = scm_i_index (&str, chr, 1, frm, to, SCM_ARG1, SCM_ARG2, SCM_ARG3, SCM_ARG4, s_string_index); return (pos < 0 ? SCM_BOOL_F : SCM_MAKINUM (pos)); @@ -159,7 +127,7 @@ scm_string_rindex (str, chr, frm, to) frm = SCM_BOOL_F; if (to == SCM_UNDEFINED) to = SCM_BOOL_F; - pos = scm_i_rindex (&str, chr, frm, to, SCM_ARG1, SCM_ARG2, SCM_ARG3, SCM_ARG4, s_string_index); + pos = scm_i_index (&str, chr, -1, frm, to, SCM_ARG1, SCM_ARG2, SCM_ARG3, SCM_ARG4, s_string_index); return (pos < 0 ? SCM_BOOL_F : SCM_MAKINUM (pos)); |