From 138f64b8f02f05e6073f61b1db2c3d5e805b75c0 Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Tue, 11 Jan 2011 14:03:54 -0500 Subject: String processing optimizations --- src/c/urweb.c | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) (limited to 'src/c') diff --git a/src/c/urweb.c b/src/c/urweb.c index 17e4bed2..0b4054e9 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -2133,23 +2133,48 @@ uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) { } 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); } -- cgit v1.2.3