diff --git a/libguile/ChangeLog b/libguile/ChangeLog index a21b2562f..cc9b7ad64 100644 --- a/libguile/ChangeLog +++ b/libguile/ChangeLog @@ -1,3 +1,9 @@ +2001-05-04 Martin Grabmueller + + * strop.c (scm_string_split): New procedure. + + * strop.h (scm_string_split): Added prototype. + 2001-05-04 Gary Houston * socket.c: define uint32_t if netdb.h doesn't. thanks to diff --git a/libguile/strop.c b/libguile/strop.c index 48f3c33b6..037b2bd60 100644 --- a/libguile/strop.c +++ b/libguile/strop.c @@ -509,6 +509,55 @@ SCM_DEFINE (scm_string_capitalize, "string-capitalize", 1, 0, 0, #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 +{ + int 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" diff --git a/libguile/strop.h b/libguile/strop.h index acd1928ed..45a3ecb84 100644 --- a/libguile/strop.h +++ b/libguile/strop.h @@ -64,6 +64,7 @@ extern SCM scm_string_downcase_x (SCM v); extern SCM scm_string_downcase (SCM v); extern SCM scm_string_capitalize_x (SCM v); extern SCM scm_string_capitalize (SCM v); +extern SCM scm_string_split (SCM str, SCM chr); extern SCM scm_string_ci_to_symbol (SCM v); #define scm_substring_move_left_x scm_substring_move_x