changeset 1390:65fbb250b875

More string processing
author Adam Chlipala <adam@chlipala.net>
date Thu, 13 Jan 2011 10:02:50 -0500
parents 9a721f0722d3
children 59c8a19bfb22
files include/urweb.h lib/ur/basis.urs lib/ur/string.ur lib/ur/string.urs src/c/urweb.c
diffstat 5 files changed, 23 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Tue Jan 11 18:04:52 2011 -0500
+++ b/include/urweb.h	Thu Jan 13 10:02:50 2011 -0500
@@ -140,6 +140,7 @@
 uw_Basis_string uw_Basis_strcat(uw_context, const char *, const char *);
 uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...);
 uw_Basis_int *uw_Basis_strindex(uw_context, const char *, uw_Basis_char);
+uw_Basis_int *uw_Basis_strsindex(uw_context, const char *, const char *needle);
 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);
--- a/lib/ur/basis.urs	Tue Jan 11 18:04:52 2011 -0500
+++ b/lib/ur/basis.urs	Thu Jan 13 10:02:50 2011 -0500
@@ -87,6 +87,7 @@
 val strsuffix : string -> int -> string
 val strchr : string -> char -> option string
 val strindex : string -> char -> option int
+val strsindex : string -> string -> option int
 val strcspn : string -> string -> int
 val substring : string -> int -> int -> string
 val str1 : char -> string
--- a/lib/ur/string.ur	Tue Jan 11 18:04:52 2011 -0500
+++ b/lib/ur/string.ur	Thu Jan 13 10:02:50 2011 -0500
@@ -10,6 +10,7 @@
 val suffix = Basis.strsuffix
 
 val index = Basis.strindex
+fun sindex r = Basis.strsindex r.Haystack r.Needle
 val atFirst = Basis.strchr
 
 fun mindex {Haystack = s, Needle = chs} =
@@ -50,6 +51,12 @@
                         sub s i,
                         suffix s (i + 1))
 
+fun ssplit r =
+    case sindex r of
+        None => None
+      | Some i => Some (substring r.Haystack {Start = 0, Len = i},
+                        suffix r.Haystack (i + length r.Needle))
+
 fun all f s =
     let
         val len = length s
--- a/lib/ur/string.urs	Tue Jan 11 18:04:52 2011 -0500
+++ b/lib/ur/string.urs	Thu Jan 13 10:02:50 2011 -0500
@@ -11,6 +11,7 @@
 val suffix : t -> int -> string
 
 val index : t -> char -> option int
+val sindex : {Haystack : t, Needle : t} -> option int
 val atFirst : t -> char -> option string
 
 val mindex : {Haystack : t, Needle : t} -> option int
@@ -24,6 +25,8 @@
 val split' : t -> char -> option (string * string) (* The matched character is kept at the beginning of the suffix. *)
 val msplit : {Haystack : t, Needle : t} -> option (string * char * string)
 
+val ssplit : {Haystack : t, Needle : t} -> option (string * string)
+
 val all : (char -> bool) -> string -> bool
 val mp : (char -> char) -> string -> string
 
--- a/src/c/urweb.c	Tue Jan 11 18:04:52 2011 -0500
+++ b/src/c/urweb.c	Thu Jan 13 10:02:50 2011 -0500
@@ -2194,6 +2194,17 @@
   }
 }
 
+uw_Basis_int *uw_Basis_strsindex(uw_context ctx, const char *haystack, const char *needle) {
+  uw_Basis_string r = strstr(haystack, needle);
+  if (r == NULL)
+    return NULL;
+  else {
+    uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
+    *nr = r - haystack;
+    return nr;
+  }
+}
+
 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
   int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1;
   char *s;