diff src/c/urweb.c @ 821:395a5d450cc0

Chars and more string operations
author Adam Chlipala <adamc@hcoop.net>
date Tue, 26 May 2009 12:25:06 -0400
parents 5f49a6b759cb
children 14a6c0971d89
line wrap: on
line diff
--- a/src/c/urweb.c	Sat May 23 10:14:51 2009 -0400
+++ b/src/c/urweb.c	Tue May 26 12:25:06 2009 -0400
@@ -1793,6 +1793,20 @@
   return uw_unit_v;
 }
 
+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");
+}
+
+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");
+}
+
 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
   int len = strlen(s1) + strlen(s2) + 1;
   char *s;
@@ -2081,6 +2095,13 @@
   return r;
 }
 
+uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) {
+  char *r = uw_malloc(ctx, 2);
+  r[0] = ch;
+  r[1] = 0;
+  return r;
+}
+
 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
   if (b == uw_Basis_False)
     return "False";
@@ -2127,6 +2148,20 @@
     return NULL;
 }
 
+uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) {
+  if (s[0] == 0) {
+    uw_Basis_char *r = uw_malloc(ctx, 1);
+    r[0] = 0;
+    return r;
+  } else if (s[1] != 0)
+    return NULL;
+  else {
+    uw_Basis_char *r = uw_malloc(ctx, 1);
+    r[0] = s[0];
+    return r;
+  }
+}
+
 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
   static uw_Basis_bool true = uw_Basis_True;
   static uw_Basis_bool false = uw_Basis_False;
@@ -2215,6 +2250,15 @@
     uw_error(ctx, FATAL, "Can't parse float: %s", s);
 }
 
+uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
+  if (s[0] == 0)
+    return 0;
+  else if (s[1] != 0)
+    uw_error(ctx, FATAL, "Can't parse char: %s", s);
+  else
+    return s[0];
+}
+
 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
   if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
     return uw_Basis_True;