slovo/examples/projects/std-layout-local-vec_string/src/vec_string.slo
2026-05-22 08:38:43 +02:00

229 lines
8.2 KiB
Plaintext

(module vec_string (export empty append len at singleton append2 append3 pair triple is_empty index_or first_or last_or index_option first_option last_option index_of_option last_index_of_option contains count_of concat take starts_with without_prefix ends_with without_suffix drop reverse subvec insert_at insert_range replace_at replace_range remove_at remove_range))
(fn empty () -> (vec string)
(std.vec.string.empty))
(fn append ((values (vec string)) (value string)) -> (vec string)
(std.vec.string.append values value))
(fn len ((values (vec string))) -> i32
(std.vec.string.len values))
(fn at ((values (vec string)) (position i32)) -> string
(std.vec.string.index values position))
(fn singleton ((value string)) -> (vec string)
(append (empty) value))
(fn append2 ((values (vec string)) (first string) (second string)) -> (vec string)
(append (append values first) second))
(fn append3 ((values (vec string)) (first string) (second string) (third string)) -> (vec string)
(append (append2 values first second) third))
(fn pair ((first string) (second string)) -> (vec string)
(append2 (empty) first second))
(fn triple ((first string) (second string) (third string)) -> (vec string)
(append3 (empty) first second third))
(fn is_empty ((values (vec string))) -> bool
(= (len values) 0))
(fn index_or ((values (vec string)) (position i32) (fallback string)) -> string
(if (< position 0)
fallback
(if (< position (len values))
(at values position)
fallback)))
(fn first_or ((values (vec string)) (fallback string)) -> string
(index_or values 0 fallback))
(fn last_or ((values (vec string)) (fallback string)) -> string
(if (is_empty values)
fallback
(at values (- (len values) 1))))
(fn index_option ((values (vec string)) (position i32)) -> (option string)
(if (< position 0)
(none string)
(if (< position (len values))
(some string (at values position))
(none string))))
(fn first_option ((values (vec string))) -> (option string)
(index_option values 0))
(fn last_option ((values (vec string))) -> (option string)
(if (is_empty values)
(none string)
(some string (at values (- (len values) 1)))))
(fn index_of_option_loop ((values (vec string)) (target string) (position i32) (values_len i32)) -> (option i32)
(if (< position values_len)
(if (= (at values position) target)
(some i32 position)
(index_of_option_loop values target (+ position 1) values_len))
(none i32)))
(fn index_of_option ((values (vec string)) (target string)) -> (option i32)
(index_of_option_loop values target 0 (len values)))
(fn last_index_of_option_loop ((values (vec string)) (target string) (position i32) (values_len i32) (found (option i32))) -> (option i32)
(if (< position values_len)
(if (= (at values position) target)
(last_index_of_option_loop values target (+ position 1) values_len (some i32 position))
(last_index_of_option_loop values target (+ position 1) values_len found))
found))
(fn last_index_of_option ((values (vec string)) (target string)) -> (option i32)
(last_index_of_option_loop values target 0 (len values) (none i32)))
(fn contains_loop ((values (vec string)) (target string) (position i32) (values_len i32)) -> bool
(if (< position values_len)
(if (= (at values position) target)
true
(contains_loop values target (+ position 1) values_len))
false))
(fn contains ((values (vec string)) (target string)) -> bool
(contains_loop values target 0 (len values)))
(fn count_of_loop ((values (vec string)) (target string) (position i32) (values_len i32) (hits i32)) -> i32
(if (< position values_len)
(count_of_loop values target (+ position 1) values_len (+ hits (if (= (at values position) target)
1
0)))
hits))
(fn count_of ((values (vec string)) (target string)) -> i32
(count_of_loop values target 0 (len values) 0))
(fn concat_loop ((built (vec string)) (right (vec string)) (position i32) (right_len i32)) -> (vec string)
(if (< position right_len)
(concat_loop (append built (at right position)) right (+ position 1) right_len)
built))
(fn concat ((left (vec string)) (right (vec string))) -> (vec string)
(let right_len i32 (len right))
(concat_loop left right 0 right_len))
(fn take_loop ((values (vec string)) (position i32) (limit i32) (built (vec string))) -> (vec string)
(if (< position limit)
(take_loop values (+ position 1) limit (append built (at values position)))
built))
(fn take ((values (vec string)) (count i32)) -> (vec string)
(let values_len i32 (len values))
(if (< count 0)
(empty)
(if (< count values_len)
(take_loop values 0 count (empty))
values)))
(fn starts_with ((values (vec string)) (prefix (vec string))) -> bool
(let prefix_len i32 (len prefix))
(if (< (len values) prefix_len)
false
(= (take values prefix_len) prefix)))
(fn without_prefix ((values (vec string)) (prefix (vec string))) -> (vec string)
(if (starts_with values prefix)
(drop values (len prefix))
values))
(fn ends_with ((values (vec string)) (suffix (vec string))) -> bool
(let values_len i32 (len values))
(let suffix_len i32 (len suffix))
(if (< values_len suffix_len)
false
(= (drop values (- values_len suffix_len)) suffix)))
(fn without_suffix ((values (vec string)) (suffix (vec string))) -> (vec string)
(if (ends_with values suffix)
(take values (- (len values) (len suffix)))
values))
(fn drop_loop ((values (vec string)) (position i32) (values_len i32) (built (vec string))) -> (vec string)
(if (< position values_len)
(drop_loop values (+ position 1) values_len (append built (at values position)))
built))
(fn drop ((values (vec string)) (count i32)) -> (vec string)
(let values_len i32 (len values))
(if (< count 0)
values
(if (< count values_len)
(drop_loop values count values_len (empty))
(empty))))
(fn reverse_loop ((values (vec string)) (position i32) (built (vec string))) -> (vec string)
(if (< position 0)
built
(reverse_loop values (- position 1) (append built (at values position)))))
(fn reverse ((values (vec string))) -> (vec string)
(reverse_loop values (- (len values) 1) (empty)))
(fn subvec ((values (vec string)) (start i32) (end_exclusive i32)) -> (vec string)
(if (< start 0)
(empty)
(if (< start end_exclusive)
(take (drop values start) (- end_exclusive start))
(empty))))
(fn insert_at ((values (vec string)) (position i32) (value string)) -> (vec string)
(let values_len i32 (len values))
(if (< position 0)
values
(if (< position values_len)
(concat (append (take values position) value) (drop values position))
(if (= position values_len)
(append values value)
values))))
(fn insert_range ((values (vec string)) (position i32) (inserted (vec string))) -> (vec string)
(let values_len i32 (len values))
(if (< position 0)
values
(if (< position values_len)
(concat (concat (take values position) inserted) (drop values position))
(if (= position values_len)
(concat values inserted)
values))))
(fn replace_at ((values (vec string)) (position i32) (replacement string)) -> (vec string)
(if (< position 0)
values
(if (< position (len values))
(concat (append (take values position) replacement) (drop values (+ position 1)))
values)))
(fn replace_range ((values (vec string)) (start i32) (end_exclusive i32) (replacement (vec string))) -> (vec string)
(let values_len i32 (len values))
(if (< start 0)
values
(if (< start values_len)
(if (< start end_exclusive)
(concat (concat (take values start) replacement) (drop values end_exclusive))
values)
values)))
(fn remove_at ((values (vec string)) (position i32)) -> (vec string)
(if (< position 0)
values
(if (< position (len values))
(concat (take values position) (drop values (+ position 1)))
values)))
(fn remove_range ((values (vec string)) (start i32) (end_exclusive i32)) -> (vec string)
(let values_len i32 (len values))
(if (< start 0)
values
(if (< start values_len)
(if (< start end_exclusive)
(concat (take values start) (drop values end_exclusive))
values)
values)))