slovo/examples/projects/std-layout-local-json/src/string.slo

56 lines
1.4 KiB
Plaintext

(module string (export trim_ascii))
(fn len ((value string)) -> i32
(std.string.len value))
(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 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)))