235 lines
6.8 KiB
Plaintext
235 lines
6.8 KiB
Plaintext
(module string (export len concat byte_at_result slice_result starts_with ends_with contains index_of_option last_index_of_option trim_ascii_start trim_ascii_end trim_ascii parse_i32_result parse_i32_option parse_u32_result parse_u32_option parse_i64_result parse_i64_option parse_u64_result parse_u64_option parse_f64_result parse_f64_option parse_bool_result parse_bool_option parse_i32_or_zero parse_u32_or_zero parse_i64_or_zero parse_u64_or_zero parse_f64_or_zero parse_bool_or_false parse_i32_or parse_u32_or parse_i64_or parse_u64_or parse_f64_or parse_bool_or))
|
|
|
|
(import std.result (ok_or_none_i32 ok_or_none_u32 ok_or_none_i64 ok_or_none_u64 ok_or_none_f64 ok_or_none_bool))
|
|
|
|
(fn len ((value string)) -> i32
|
|
(std.string.len value))
|
|
|
|
(fn concat ((left string) (right string)) -> string
|
|
(std.string.concat left right))
|
|
|
|
(fn byte_at_result ((value string) (index i32)) -> (result i32 i32)
|
|
(std.string.byte_at_result value index))
|
|
|
|
(fn slice_result ((value string) (start i32) (count i32)) -> (result string i32)
|
|
(std.string.slice_result value start count))
|
|
|
|
(fn starts_with ((value string) (prefix string)) -> bool
|
|
(std.string.starts_with value prefix))
|
|
|
|
(fn ends_with ((value string) (suffix string)) -> bool
|
|
(std.string.ends_with value suffix))
|
|
|
|
(fn suffix_starts_with ((value string) (needle string) (position i32) (value_len i32)) -> bool
|
|
(match (slice_result value position (- value_len position))
|
|
((ok text)
|
|
(starts_with text needle))
|
|
((err code)
|
|
false)))
|
|
|
|
(fn index_of_option ((value string) (needle string)) -> (option i32)
|
|
(let value_len i32 (len value))
|
|
(let needle_len i32 (len needle))
|
|
(let max_start i32 (- value_len needle_len))
|
|
(var position i32 0)
|
|
(var found_position i32 -1)
|
|
(while (and (> needle_len 0) (and (< found_position 0) (<= position max_start)))
|
|
(set found_position (if (suffix_starts_with value needle position value_len)
|
|
position
|
|
found_position))
|
|
(set position (+ position 1)))
|
|
(if (= needle_len 0)
|
|
(some i32 0)
|
|
(if (< found_position 0)
|
|
(none i32)
|
|
(some i32 found_position))))
|
|
|
|
(fn last_index_of_option ((value string) (needle string)) -> (option i32)
|
|
(let value_len i32 (len value))
|
|
(let needle_len i32 (len needle))
|
|
(let max_start i32 (- value_len needle_len))
|
|
(var position i32 0)
|
|
(var found_position i32 -1)
|
|
(while (and (> needle_len 0) (<= position max_start))
|
|
(set found_position (if (suffix_starts_with value needle position value_len)
|
|
position
|
|
found_position))
|
|
(set position (+ position 1)))
|
|
(if (= needle_len 0)
|
|
(some i32 value_len)
|
|
(if (< found_position 0)
|
|
(none i32)
|
|
(some i32 found_position))))
|
|
|
|
(fn contains ((value string) (needle string)) -> bool
|
|
(match (index_of_option value needle)
|
|
((some position)
|
|
true)
|
|
((none)
|
|
false)))
|
|
|
|
(fn is_ascii_trim_byte ((value i32)) -> bool
|
|
(if (= value 9)
|
|
true
|
|
(if (= value 10)
|
|
true
|
|
(if (= value 11)
|
|
true
|
|
(if (= value 12)
|
|
true
|
|
(if (= value 13)
|
|
true
|
|
(= value 32)))))))
|
|
|
|
(fn byte_is_ascii_trim ((value string) (position i32)) -> bool
|
|
(match (byte_at_result value position)
|
|
((ok byte)
|
|
(is_ascii_trim_byte byte))
|
|
((err code)
|
|
false)))
|
|
|
|
(fn trim_ascii_start ((value string)) -> string
|
|
(let value_len i32 (len value))
|
|
(var start i32 0)
|
|
(while (and (< start value_len) (byte_is_ascii_trim value start))
|
|
(set start (+ start 1)))
|
|
(match (slice_result value start (- value_len start))
|
|
((ok text)
|
|
text)
|
|
((err code)
|
|
value)))
|
|
|
|
(fn trim_ascii_end ((value string)) -> string
|
|
(let value_len i32 (len value))
|
|
(var end i32 value_len)
|
|
(while (and (> end 0) (byte_is_ascii_trim value (- end 1)))
|
|
(set end (- end 1)))
|
|
(match (slice_result value 0 end)
|
|
((ok text)
|
|
text)
|
|
((err code)
|
|
value)))
|
|
|
|
(fn trim_ascii ((value string)) -> string
|
|
(trim_ascii_end (trim_ascii_start value)))
|
|
|
|
(fn parse_i32_result ((value string)) -> (result i32 i32)
|
|
(std.string.parse_i32_result value))
|
|
|
|
(fn parse_i32_option ((value string)) -> (option i32)
|
|
(ok_or_none_i32 (parse_i32_result value)))
|
|
|
|
(fn parse_u32_result ((value string)) -> (result u32 i32)
|
|
(std.string.parse_u32_result value))
|
|
|
|
(fn parse_u32_option ((value string)) -> (option u32)
|
|
(ok_or_none_u32 (parse_u32_result value)))
|
|
|
|
(fn parse_i64_result ((value string)) -> (result i64 i32)
|
|
(std.string.parse_i64_result value))
|
|
|
|
(fn parse_i64_option ((value string)) -> (option i64)
|
|
(ok_or_none_i64 (parse_i64_result value)))
|
|
|
|
(fn parse_u64_result ((value string)) -> (result u64 i32)
|
|
(std.string.parse_u64_result value))
|
|
|
|
(fn parse_u64_option ((value string)) -> (option u64)
|
|
(ok_or_none_u64 (parse_u64_result value)))
|
|
|
|
(fn parse_f64_result ((value string)) -> (result f64 i32)
|
|
(std.string.parse_f64_result value))
|
|
|
|
(fn parse_f64_option ((value string)) -> (option f64)
|
|
(ok_or_none_f64 (parse_f64_result value)))
|
|
|
|
(fn parse_bool_result ((value string)) -> (result bool i32)
|
|
(std.string.parse_bool_result value))
|
|
|
|
(fn parse_bool_option ((value string)) -> (option bool)
|
|
(ok_or_none_bool (parse_bool_result value)))
|
|
|
|
(fn parse_i32_or_zero ((value string)) -> i32
|
|
(match (parse_i32_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
0)))
|
|
|
|
(fn parse_u32_or_zero ((value string)) -> u32
|
|
(match (parse_u32_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
0u32)))
|
|
|
|
(fn parse_i64_or_zero ((value string)) -> i64
|
|
(match (parse_i64_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
0i64)))
|
|
|
|
(fn parse_u64_or_zero ((value string)) -> u64
|
|
(match (parse_u64_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
0u64)))
|
|
|
|
(fn parse_f64_or_zero ((value string)) -> f64
|
|
(match (parse_f64_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
0.0)))
|
|
|
|
(fn parse_bool_or_false ((value string)) -> bool
|
|
(match (parse_bool_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
false)))
|
|
|
|
(fn parse_i32_or ((value string) (fallback i32)) -> i32
|
|
(match (parse_i32_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
fallback)))
|
|
|
|
(fn parse_u32_or ((value string) (fallback u32)) -> u32
|
|
(match (parse_u32_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
fallback)))
|
|
|
|
(fn parse_i64_or ((value string) (fallback i64)) -> i64
|
|
(match (parse_i64_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
fallback)))
|
|
|
|
(fn parse_u64_or ((value string) (fallback u64)) -> u64
|
|
(match (parse_u64_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
fallback)))
|
|
|
|
(fn parse_f64_or ((value string) (fallback f64)) -> f64
|
|
(match (parse_f64_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
fallback)))
|
|
|
|
(fn parse_bool_or ((value string) (fallback bool)) -> bool
|
|
(match (parse_bool_result value)
|
|
((ok payload)
|
|
payload)
|
|
((err code)
|
|
fallback)))
|