changeset 831:5e1a4b12c83a

strcspn and friends
author Adam Chlipala <adamc@hcoop.net>
date Sun, 31 May 2009 12:41:34 -0400
parents d07980bf1444
children 249740301a0a
files include/urweb.h lib/js/urweb.js lib/ur/basis.urs lib/ur/string.ur lib/ur/string.urs src/c/urweb.c src/settings.sml
diffstat 7 files changed, 26 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Sat May 30 14:44:29 2009 -0400
+++ b/include/urweb.h	Sun May 31 12:41:34 2009 -0400
@@ -117,6 +117,7 @@
 uw_Basis_string uw_Basis_strcat(uw_context, const char *, const char *);
 uw_Basis_int *uw_Basis_strindex(uw_context, const char *, uw_Basis_char);
 uw_Basis_string uw_Basis_strchr(uw_context, const char *, uw_Basis_char);
+uw_Basis_int uw_Basis_strcspn(uw_context, const char *, const char *);
 uw_Basis_string uw_Basis_substring(uw_context, const char *, uw_Basis_int, uw_Basis_int);
 
 uw_Basis_string uw_strdup(uw_context, const char *);
--- a/lib/js/urweb.js	Sat May 30 14:44:29 2009 -0400
+++ b/lib/js/urweb.js	Sun May 31 12:41:34 2009 -0400
@@ -360,6 +360,13 @@
   else
     return r;
 }
+function sspn(s, chs) {
+  for (var i = 0; i < s.length; ++i)
+    if (chs.indexOf(s[i]) != -1)
+      return i;
+
+  return null;
+}
 function schr(s, ch) {
   var r = s.indexOf(ch);
   if (r == -1)
--- a/lib/ur/basis.urs	Sat May 30 14:44:29 2009 -0400
+++ b/lib/ur/basis.urs	Sun May 31 12:41:34 2009 -0400
@@ -59,6 +59,7 @@
 val strsuffix : string -> int -> string
 val strchr : string -> char -> option string
 val strindex : string -> char -> option int
+val strcspn : string -> string -> option int
 val substring : string -> int -> int -> string
 
 class show
--- a/lib/ur/string.ur	Sat May 30 14:44:29 2009 -0400
+++ b/lib/ur/string.ur	Sun May 31 12:41:34 2009 -0400
@@ -9,6 +9,8 @@
 val index = Basis.strindex
 val atFirst = Basis.strchr
 
+fun mindex {Haystack = s, Needle = chs} = Basis.strcspn s chs
+
 fun substring s {Start = start, Len = len} = Basis.substring s start len
 
 fun split s ch =
@@ -16,3 +18,9 @@
         None => None
       | Some i => Some (substring s {Start = 0, Len = i},
                         substring s {Start = i + 1, Len = length s - i - 1})
+fun msplit {Haystack = s, Needle = chs} =
+    case mindex {Haystack = s, Needle = chs} of
+        None => None
+      | Some i => Some (substring s {Start = 0, Len = i},
+                        sub s i,
+                        substring s {Start = i + 1, Len = length s - i - 1})
--- a/lib/ur/string.urs	Sat May 30 14:44:29 2009 -0400
+++ b/lib/ur/string.urs	Sun May 31 12:41:34 2009 -0400
@@ -10,6 +10,9 @@
 val index : t -> char -> option int
 val atFirst : t -> char -> option string
 
+val mindex : {Haystack : t, Needle : t} -> option int
+
 val substring : t -> {Start : int, Len : int} -> string
 
 val split : t -> char -> option (string * string)
+val msplit : {Haystack : t, Needle : t} -> option (string * char * string)
--- a/src/c/urweb.c	Sat May 30 14:44:29 2009 -0400
+++ b/src/c/urweb.c	Sun May 31 12:41:34 2009 -0400
@@ -1815,6 +1815,10 @@
   return strchr(s, ch);
 }
 
+uw_Basis_int uw_Basis_strcspn(uw_context ctx, uw_Basis_string s, uw_Basis_string chs) {
+  return strcspn(s, chs);
+}
+
 uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
   uw_Basis_string r = strchr(s, ch);
   if (r == NULL)
--- a/src/settings.sml	Sat May 30 14:44:29 2009 -0400
+++ b/src/settings.sml	Sun May 31 12:41:34 2009 -0400
@@ -156,7 +156,8 @@
                           ("strlen", "slen"),
                           ("strindex", "sidx"),
                           ("strchr", "schr"),
-                          ("substring", "ssub")]
+                          ("substring", "ssub"),
+                          ("strcspn", "sspn")]
 val jsFuncs = ref jsFuncsBase
 fun setJsFuncs ls = jsFuncs := foldl (fn ((k, v), m) => M.insert (m, k, v)) jsFuncsBase ls
 fun jsFunc x = M.find (!jsFuncs, x)