changeset 1388:3913cbfd72e9

String processing optimizations
author Adam Chlipala <adam@chlipala.net>
date Tue, 11 Jan 2011 14:03:54 -0500
parents dd99e1702432
children 9a721f0722d3
files include/urweb.h lib/ur/basis.urs lib/ur/string.ur lib/ur/string.urs src/c/urweb.c
diffstat 5 files changed, 37 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Thu Jan 06 19:26:31 2011 -0500
+++ b/include/urweb.h	Tue Jan 11 14:03:54 2011 -0500
@@ -134,6 +134,7 @@
 uw_Basis_time uw_Basis_unurlifyTime(uw_context, char **);
 
 uw_Basis_int uw_Basis_strlen(uw_context, const char *);
+uw_Basis_bool uw_Basis_strlenGe(uw_context, uw_Basis_string, uw_Basis_int);
 uw_Basis_char uw_Basis_strsub(uw_context, const char *, uw_Basis_int);
 uw_Basis_string uw_Basis_strsuffix(uw_context, const char *, uw_Basis_int);
 uw_Basis_string uw_Basis_strcat(uw_context, const char *, const char *);
--- a/lib/ur/basis.urs	Thu Jan 06 19:26:31 2011 -0500
+++ b/lib/ur/basis.urs	Tue Jan 11 14:03:54 2011 -0500
@@ -81,6 +81,7 @@
 (** String operations *)
 
 val strlen : string -> int
+val strlenGe : string -> int -> bool
 val strcat : string -> string -> string
 val strsub : string -> int -> char
 val strsuffix : string -> int -> string
--- a/lib/ur/string.ur	Thu Jan 06 19:26:31 2011 -0500
+++ b/lib/ur/string.ur	Tue Jan 11 14:03:54 2011 -0500
@@ -3,6 +3,7 @@
 val str = Basis.str1
 
 val length = Basis.strlen
+val lengthGe = Basis.strlenGe
 val append = Basis.strcat
 
 val sub = Basis.strsub
--- a/lib/ur/string.urs	Thu Jan 06 19:26:31 2011 -0500
+++ b/lib/ur/string.urs	Tue Jan 11 14:03:54 2011 -0500
@@ -3,6 +3,7 @@
 val str : char -> t
 
 val length : t -> int
+val lengthGe : t -> int -> bool
 
 val append : t -> t -> t
 
--- a/src/c/urweb.c	Thu Jan 06 19:26:31 2011 -0500
+++ b/src/c/urweb.c	Tue Jan 11 14:03:54 2011 -0500
@@ -2133,23 +2133,48 @@
 }
 
 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
-  if (n >= 0 && n < strlen(s))
-    return s[n];
-  else
-    uw_error(ctx, FATAL, "Out-of-bounds strsub");
+  while (n >= 0) {
+    if (*s == 0)
+      uw_error(ctx, FATAL, "Out-of-bounds strsub");
+
+    if (n == 0)
+      return *s;
+
+    --n;
+    ++s;
+  }
+
+  uw_error(ctx, FATAL, "Negative strsub bound");
 }
 
 uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
-  if (n >= 0 && n < strlen(s))
-    return &s[n];
-  else
-    uw_error(ctx, FATAL, "Out-of-bounds strsuffix");
+  while (n >= 0) {
+    if (*s == 0 || n == 0)
+      return s;
+
+    --n;
+    ++s;
+  }
+
+  uw_error(ctx, FATAL, "Negative strsuffix bound");
 }
 
 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) {
   return strlen(s);
 }
 
+uw_Basis_bool uw_Basis_strlenGe(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
+  while (n > 0) {
+    if (*s == 0)
+      return uw_Basis_False;
+
+    --n;
+    ++s;
+  }
+
+  return uw_Basis_True;
+}
+
 uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
   return strchr(s, ch);
 }