changeset 828:14a6c0971d89

String.length
author Adam Chlipala <adamc@hcoop.net>
date Sat, 30 May 2009 09:59:10 -0400
parents 497c7dbcc695
children 20fe00fd81da
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, 20 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Thu May 28 13:47:05 2009 -0400
+++ b/include/urweb.h	Sat May 30 09:59:10 2009 -0400
@@ -111,6 +111,7 @@
 uw_Basis_bool uw_Basis_unurlifyBool(uw_context, char **);
 uw_Basis_time uw_Basis_unurlifyTime(uw_context, char **);
 
+uw_Basis_int uw_Basis_strlen(uw_context, const char *);
 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/js/urweb.js	Thu May 28 13:47:05 2009 -0400
+++ b/lib/js/urweb.js	Sat May 30 09:59:10 2009 -0400
@@ -237,7 +237,6 @@
   x.innerHTML = html;
   addNode(x);
   runScripts(x);
-  alert("HTML:\n" + html);
 }  
 
 var thisScript = null;
@@ -351,8 +350,9 @@
 function ts(x) { return x.toString() }
 function bs(b) { return (b ? "True" : "False") }
 
-function sub(x, i) { return x[i]; }
-function suf(x, i) { return x.substring(i); }
+function sub(s, i) { return s[i]; }
+function suf(s, i) { return s.substring(i); }
+function slen(s) { return s.length; }
 
 function pi(s) {
   var r = parseInt(s);
--- a/lib/ur/basis.urs	Thu May 28 13:47:05 2009 -0400
+++ b/lib/ur/basis.urs	Sat May 30 09:59:10 2009 -0400
@@ -53,6 +53,7 @@
 
 (** String operations *)
 
+val strlen : string -> int
 val strcat : string -> string -> string
 val strsub : string -> int -> char
 val strsuffix : string -> int -> string
--- a/lib/ur/string.ur	Thu May 28 13:47:05 2009 -0400
+++ b/lib/ur/string.ur	Sat May 30 09:59:10 2009 -0400
@@ -1,4 +1,7 @@
 type t = Basis.string
 
+val length = Basis.strlen
+val append = Basis.strcat
+
 val sub = Basis.strsub
 val suffix = Basis.strsuffix
--- a/lib/ur/string.urs	Thu May 28 13:47:05 2009 -0400
+++ b/lib/ur/string.urs	Sat May 30 09:59:10 2009 -0400
@@ -1,4 +1,8 @@
 type t = string
 
+val length : t -> int
+
+val append : t -> t -> t
+
 val sub : t -> int -> char
 val suffix : t -> int -> string
--- a/src/c/urweb.c	Thu May 28 13:47:05 2009 -0400
+++ b/src/c/urweb.c	Sat May 30 09:59:10 2009 -0400
@@ -1807,8 +1807,12 @@
     uw_error(ctx, FATAL, "Out-of-bounds strsuffix");
 }
 
+uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) {
+  return strlen(s);
+}
+
 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
-  int len = strlen(s1) + strlen(s2) + 1;
+  int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1;
   char *s;
 
   uw_check_heap(ctx, len);
@@ -1823,7 +1827,7 @@
 }
 
 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
-  int len = strlen(s1) + 1;
+  int len = uw_Basis_strlen(ctx, s1) + 1;
   char *s;
 
   uw_check_heap(ctx, len);
--- a/src/settings.sml	Thu May 28 13:47:05 2009 -0400
+++ b/src/settings.sml	Sat May 30 09:59:10 2009 -0400
@@ -152,7 +152,8 @@
                           ("attrifyBool", "bs"),
                           ("boolToString", "ts"),
                           ("strsub", "sub"),
-                          ("strsuffix", "suf")]
+                          ("strsuffix", "suf"),
+                          ("strlen", "slen")]
 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)