diff src/c/urweb.c @ 1388:3913cbfd72e9

String processing optimizations
author Adam Chlipala <adam@chlipala.net>
date Tue, 11 Jan 2011 14:03:54 -0500
parents 64724ef86149
children 9a721f0722d3
line wrap: on
line diff
--- 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);
 }