More efficient implementations in String.aes

This commit is contained in:
Hans Svensson 2020-02-24 15:34:23 +01:00
parent b9acf24dca
commit 2bad76314f

View File

@ -15,30 +15,41 @@ namespace String =
(StringInternal.from_list(List.take(i, cs)), StringInternal.from_list(List.drop(i, cs))) (StringInternal.from_list(List.take(i, cs)), StringInternal.from_list(List.drop(i, cs)))
function at(ix : int, s : string) = function at(ix : int, s : string) =
switch(List.drop(ix - 1, StringInternal.to_list(s))) switch(List.drop(ix, StringInternal.to_list(s)))
[] => None [] => None
x :: _ => Some(x) x :: _ => Some(x)
function tokens(s : string, pat : string) =
let pat_len = StringInternal.length(pat)
tokens_(StringInternal.to_list(pat), StringInternal.to_list(s), [])
function to_upper(s : string) = function to_upper(s : string) =
StringInternal.from_list([ Char.to_upper(c) | c <- StringInternal.to_list(s) ]) StringInternal.from_list(List.map(Char.to_upper, StringInternal.to_list(s)))
function to_lower(s : string) = function to_lower(s : string) =
StringInternal.from_list([ Char.to_lower(c) | c <- StringInternal.to_list(s) ]) StringInternal.from_list(List.map(Char.to_lower, StringInternal.to_list(s)))
function contains(str : string, substr : string) : option(int) = function contains(str : string, substr : string) : option(int) =
let last_ix = StringInternal.length(str) - (StringInternal.length(substr) - 1) if(substr == "") Some(0)
contains_(1, last_ix, StringInternal.to_list(str), StringInternal.to_list(substr)) else
contains_(0, StringInternal.to_list(str), StringInternal.to_list(substr))
function function tokens(s : string, pat : string) =
to_int : (string, int) => option(int) require(pat != "", "String.tokens: empty pattern")
to_int(s, 10) = to_int_(List.reverse(StringInternal.to_list(s)), ch_to_int_10, 0, 1, 10) tokens_(StringInternal.to_list(pat), StringInternal.to_list(s), [])
to_int(s, 16) = to_int_(List.reverse(StringInternal.to_list(s)), ch_to_int_16, 0, 1, 16)
function function to_int(s : string) : option(int) =
let s = StringInternal.to_list(s)
switch(is_prefix(['-'], s))
None => to_int_pos(s)
Some(s) => switch(to_int_pos(s))
None => None
Some(x) => Some(-x)
private function to_int_pos(s : list(char)) =
switch(is_prefix(['0', 'x'], s))
None =>
to_int_(s, ch_to_int_10, 0, 10)
Some(s) =>
to_int_(s, ch_to_int_16, 0, 16)
private function
tokens_(_, [], acc) = [StringInternal.from_list(List.reverse(acc))] tokens_(_, [], acc) = [StringInternal.from_list(List.reverse(acc))]
tokens_(pat, str, acc) = tokens_(pat, str, acc) =
switch(is_prefix(pat, str)) switch(is_prefix(pat, str))
@ -48,36 +59,36 @@ namespace String =
let c :: cs = str let c :: cs = str
tokens_(pat, cs, c :: acc) tokens_(pat, cs, c :: acc)
function contains_(ix, lix, str, substr) = private function
if(ix > lix) None contains_(_, [], _) = None
else contains_(ix, str, substr) =
switch(is_prefix(substr, str)) switch(is_prefix(substr, str))
None => None =>
let _ :: str = str let _ :: str = str
contains_(ix + 1, lix, str, substr) contains_(ix + 1, str, substr)
Some(_) => Some(_) =>
Some(ix) Some(ix)
function private function
is_prefix([], ys) = Some(ys) is_prefix([], ys) = Some(ys)
is_prefix(_, []) = None is_prefix(_, []) = None
is_prefix(x :: xs, y :: ys) = is_prefix(x :: xs, y :: ys) =
if(x == y) is_prefix(xs, ys) if(x == y) is_prefix(xs, ys)
else None else None
function private function
to_int_([], _, x, _, _) = Some(x) to_int_([], _, x, _) = Some(x)
to_int_(i :: is, c, x, t, f) = to_int_(i :: is, value, x, b) =
switch(c(i)) switch(value(i))
None => None None => None
Some(i) => to_int_(is, c, x + t * i, t * f, f) Some(i) => to_int_(is, value, x * b + i, b)
function ch_to_int_10(c) = private function ch_to_int_10(c) =
let c = Char.to_int(c) let c = Char.to_int(c)
if(c >= 48 && c =< 57) Some(c - 48) if(c >= 48 && c =< 57) Some(c - 48)
else None else None
function ch_to_int_16(c) = private function ch_to_int_16(c) =
let c = Char.to_int(c) let c = Char.to_int(c)
if(c >= 48 && c =< 57) Some(c - 48) if(c >= 48 && c =< 57) Some(c - 48)
elif(c >= 65 && c =< 70) Some(c - 55) elif(c >= 65 && c =< 70) Some(c - 55)