/// Fast string search, hashing and sorting: StringZilla's SIMD kernels /// (github.com/ashvardanian/StringZilla, Apache-2.0, vendored release /// v4.6.3) behind a Louise API (ADR 0005). The library picks its kernels /// at runtime (`capabilities` says which), so one build runs everywhere. /// /// import stringzilla /// /// stringzilla.find("hello world", "world") // Some(6), a byte offset /// stringzilla.count("banana", "an") // 2 /// stringzilla.sort(["b", "a", "c"]) // Ok(["a", "b", "c"]) /// /// Everything here performs ``; offsets and counts are in **bytes** /// (UTF-8), not characters — `string.length` counts scalar values, these /// count what `string.byte_length` counts. The interpreter cannot call C: /// this package's functions and tests need a native build (`louise test /// --native`), as with the argon2 package. /// /// Not bound (see the README): edit distance and the streaming hashes /// (StringZilla 4 moved them to the C++ StringZillas library, which a C /// native dependency cannot build), the UTF-8 segmenters, and SHA-256. import bindings import std/array.{type Array} import std/ffi.{type Resource} import std/result import std/string import std/test.{assert, assert_equal} /// The byte offset of `needle`'s first occurrence in `haystack`, or /// `None`. An empty needle matches at 0. pub fn find :: haystack: String needle: String -> Option / { offset (bindings.szl_find haystack needle) } /// The byte offset of `needle`'s last occurrence in `haystack`, or /// `None`. An empty needle matches at `byte_length(haystack)`. pub fn find_last :: haystack: String needle: String -> Option / { offset (bindings.szl_rfind haystack needle) } /// The non-overlapping occurrences of `needle` in `haystack`; an empty /// needle matches at every gap: `byte_length(haystack) + 1`. pub fn count :: haystack: String needle: String -> Int / { bindings.szl_count haystack needle } /// The byte offset of the first byte of `s` equal to `byte` (0 to 255), /// or `None`. pub fn find_byte :: s: String byte: Int -> Option / { offset (bindings.szl_find_byte s byte) } /// The byte offset of the last byte of `s` equal to `byte` (0 to 255), /// or `None`. pub fn find_byte_last :: s: String byte: Int -> Option / { offset (bindings.szl_rfind_byte s byte) } /// `a` against `b`, byte-wise (the order `Ord` uses). pub fn compare :: a: String b: String -> Order / { match bindings.szl_order a b { -1 -> Lt, 0 -> Eq, _ -> Gt } } /// The low 63 bits of StringZilla's 64-bit hash of `s` with `seed`: a /// fast fingerprint, not a cryptographic hash. pub fn hash :: s: String seed: Int -> Int / { bindings.szl_hash s seed } /// The low 63 bits of StringZilla's byte-sum sketch of `s`. pub fn bytesum :: s: String -> Int / { bindings.szl_bytesum s } /// `xs` sorted by bytes (stable for distinct strings; equal strings are /// interchangeable). An `Error` if the C library runs out of memory. pub fn sort :: xs: List -> Result, String> / { let s = bindings.szl_sort_new () use _ <- result.try (push_all s xs) if bindings.szl_sort_argsort s { Ok(permute s (array.from_list xs) (bindings.szl_sort_count s - 1) []) } else { Error(out_of_memory ()) } } /// The vendored StringZilla's version: "major.minor.patch". pub fn version :: -> String / { bindings.szl_version () } /// The SIMD tiers StringZilla uses on this machine, e.g. /// "serial,westmere,haswell". pub fn capabilities :: -> String / { bindings.szl_capabilities () } /// `Some(at)` for an offset `at` of 0 or more, `None` for -1. fn offset :: at: Int -> Option { if at < 0 { None } else { Some(at) } } fn out_of_memory :: -> String { "stringzilla: out of memory" } fn push_all :: s: Resource xs: List -> Result<(), String> / { match xs { [] -> Ok(()) , [ x , ..rest ] -> if bindings.szl_sort_push s x { push_all s rest } else { Error(out_of_memory ()) } } } /// The strings of `xs` in the order `s` sorted them: the pushed index of /// the i-th smallest, read back from `i = count - 1` down, onto `acc`. fn permute :: s: Resource xs: Array i: Int acc: List -> List / { if i < 0 { acc } else { // The index is in range: `s` holds exactly the strings of `xs`. match array.get xs (bindings.szl_sort_order s i) { Some(x) -> permute s xs (i - 1) [x, ..acc] , None -> panic "stringzilla: the sort returned an index out of range" } } } @test fn offsets_and_counts_on_fixed_strings :: -> () / { assert_equal (find "hello world" "world") (Some(6)) assert_equal (find "hello world" "o") (Some(4)) assert_equal (find_last "hello world" "o") (Some(7)) assert_equal (find "hello" "z") None assert_equal (find "" "") (Some(0)) assert_equal (find "abc" "") (Some(0)) assert_equal (find_last "abc" "") (Some(3)) assert_equal (count "banana" "an") 2 assert_equal (count "aaaa" "aa") 2 assert_equal (count "abc" "") 4 assert_equal (count "" "") 1 // "é" is two UTF-8 bytes: offsets are byte offsets. assert_equal (find "héllo" "llo") (Some(3)) assert_equal (string.byte_length "héllo") 6 } @test fn compare_is_byte_wise :: -> () / { assert_equal (order_int (compare "abc" "abd")) (-1) assert_equal (order_int (compare "abc" "abc")) 0 assert_equal (order_int (compare "b" "abc")) 1 assert_equal (order_int (compare "" "")) 0 assert_equal (order_int (compare "abc" "ab")) 1 } /// `Lt`, `Eq`, `Gt` as -1, 0, 1 (`Order` has no `Eq`). fn order_int :: o: Order -> Int { match o { Lt -> -1, Eq -> 0, Gt -> 1 } }