@@ -56,9 +56,10 @@ let .is-digit(base: CString): U64 = (
5656);
5757
5858let $"[]"(base: CString, idx: U64): U8 = (
59- if idx < base.length
59+ if (base as USize) == 0 then fail(c"Index Out Of Bounds in String[]");
60+ else if idx < base.length
6061 then (base as U8[])[idx]
61- else fail("Index Out Of Bounds in String[]");
62+ else fail(c "Index Out Of Bounds in String[]");
6263);
6364
6465let .into(s: CString, tgt: Type<String>): String = intern(s);
@@ -104,10 +105,10 @@ let .get-suffix(s: String, pre: String): String = (
104105 s[s.length - pre.length:]
105106);
106107
107- let head-string(x: CString): U8 = (x as U8[])[0_u64];
108- let tail-string(x: CString): CString = (( x as U8[]) + 1_u64) as CString;
109- let head(x: CString): U8 = (x as U8[])[0_u64] ;
110- let tail(x: CString): CString = ((x as U8[]) + 1_u64) as CString ;
108+ let head-string(x: CString): U8 = if (x as USize)==0 then 0_u8 else (x as U8[])[0_u64];
109+ let tail-string(x: CString): CString = if (x as USize)==0 then c"" else ((( x as U8[]) + 1_u64) as CString) ;
110+ let head(x: CString): U8 = head-string(x) ;
111+ let tail(x: CString): CString = tail-string(x) ;
111112
112113let deep-hash(key: CString): U64 = (
113114 let i = 0_u64;
@@ -125,11 +126,13 @@ let deep-hash(key: CString): U64 = (
125126);
126127
127128let .length(s: CString): U64 = (
128- let si = 0_u64;
129- while (s as U8[])[si] != 0 {
130- si = si + 1;
131- };
132- si
129+ if (s as USize) == 0 then 0 else {
130+ let si = 0_u64;
131+ while (s as U8[])[si] != 0 {
132+ si = si + 1;
133+ };
134+ si
135+ }
133136);
134137
135138let .has-prefix(base: CString, pfx: CString): U64 = (
@@ -204,10 +207,13 @@ let .contains(base: CString, pat: CString): U64 = (
204207);
205208
206209let $"+"(l: CString, r: CString): CString = (
210+ let l_length = l.length;
211+ let r_length = r.length;
207212 let buf = malloc(l.length + r.length + 1) as C<"char">[];
208213 memset(buf, 0, l.length+r.length+1);
209214 strcat(buf, l as C<"char">[]);
210215 strcat(buf, r as C<"char">[]);
216+ buf[l_length + r_length] = 0 as C<"char">;
211217 buf as CString
212218);
213219
0 commit comments