* strop.c (scm_string_split): New procedure.
authorMartin Grabmüller <mgrabmue@cs.tu-berlin.de>
Fri, 4 May 2001 04:59:05 +0000 (04:59 +0000)
committerMartin Grabmüller <mgrabmue@cs.tu-berlin.de>
Fri, 4 May 2001 04:59:05 +0000 (04:59 +0000)
* strop.h (scm_string_split): Added prototype.

libguile/ChangeLog
libguile/strop.c
libguile/strop.h

index a21b256..cc9b7ad 100644 (file)
@@ -1,3 +1,9 @@
+2001-05-04  Martin Grabmueller  <mgrabmue@cs.tu-berlin.de>
+
+       * strop.c (scm_string_split): New procedure.
+
+       * strop.h (scm_string_split): Added prototype.
+
 2001-05-04  Gary Houston  <ghouston@arglist.com>
 
        * socket.c: define uint32_t if netdb.h doesn't.  thanks to
index 48f3c33..037b2bd 100644 (file)
@@ -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"
index acd1928..45a3ecb 100644 (file)
@@ -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