// std/string.find (and string.contains, which shares its kernel) against // a naive byte-level scan, on generated strings (issue gabla108/louise#380). import check import gen import std/bytes as raw import std/random.{type random} import std/string import std/test.{assert, assert_equal} /// The naive find: the first byte offset at which `needle`'s bytes occur /// in `s`'s, or None (the empty needle at 0). fn naive_find :: s: String needle: String -> Option { let h = raw.from_string s let n = raw.from_string needle if raw.length n == 0 { Some(0) } else { scan_from h n 0 (raw.length h - raw.length n) } } fn scan_from :: h: Bytes n: Bytes at: Int last: Int -> Option { if at > last { None } else { if matches_at h n at 0 { Some(at) } else { scan_from h n (at + 1) last } } } fn matches_at :: h: Bytes n: Bytes at: Int i: Int -> Bool { if i == raw.length n { True } else { raw.read_u8 h (at + i) == raw.read_u8 n i && matches_at h n at (i + 1) } } /// The property: find is the naive scan's answer, and contains agrees. fn agrees :: pair: (String, String) -> Bool { let (s, n) = pair let want = naive_find s n string.find s n == want && string.contains s n == (want != None) } /// A slice of `s` (a sure hit when it is not empty; empty when the bounds /// meet). fn needle_in :: s: String -> String / { let n = string.length s let from = gen.int ~low: 0 ~high: n let to = gen.int ~low: from ~high: n string.slice s from to } /// Haystack and needle from a small alphabet: matches are dense, and the /// needle crosses the kernel's dispatch boundaries (1/4/16/64 bytes). fn short_pair :: -> (String, String) / { ( gen.string ~alphabet: "aabc" ~min: 0 ~max: 200 , gen.string ~alphabet: "aabc" ~min: 0 ~max: 70 ) } /// A haystack, and a needle cut from it (a sure hit when not empty). fn cut_pair :: -> (String, String) / { let s = gen.string ~alphabet: "aabc" ~min: 0 ~max: 200 (s, needle_in s) } /// Multibyte characters: byte matches stay on character boundaries. fn multibyte_pair :: -> (String, String) / { ( gen.string ~alphabet: "é🦀ab" ~min: 0 ~max: 120 , gen.string ~alphabet: "é🦀ab" ~min: 0 ~max: 30 ) } /// A long haystack, a short needle: the wide loop, mostly missing. fn long_pair :: -> (String, String) / { ( gen.string ~alphabet: "aabc de" ~min: 1000 ~max: 3000 , gen.string ~alphabet: "aabc de" ~min: 1 ~max: 6 ) } @test fn directed_cases :: { // The empty needle is found at 0, a longer needle nowhere. assert_equal (string.find "abc" "") (Some(0)) assert_equal (string.find "" "") (Some(0)) assert_equal (string.find "ab" "abc") None // Overlaps: the first occurrence wins. assert_equal (string.find "aaaa" "aa") (Some(0)) assert_equal (string.find "abaaa" "aaa") (Some(2)) // A byte index, on a character boundary. assert_equal (string.find "héllo" "llo") (Some(3)) // The dispatch boundaries, found at the end and missed. assert_equal (string.find "abbbbc" "bbbc") (Some(2)) assert_equal (string.find "abbbbc" "bbbbc") (Some(1)) assert_equal (string.find "abbbbc" "bbx") None assert ( string.find (string.repeat "ab" 40 <> "z") (string.repeat "ab" 8) == Some(0) ) "needle of 16 at 0" assert ( string.find (string.repeat "ab" 40 <> "z") (string.repeat "ab" 8 <> "z") == Some(64) ) "needle of 17 at the end" let long = string.repeat "x" 200 assert_equal (string.find (long <> "mark") "mark") (Some(200)) assert_equal (string.find long (string.repeat "y" 64)) None } @test fn find_is_the_naive_scan_on_short_strings :: { check.seeded ~seed: 3801 ~runs: 300 short_pair agrees } @test fn find_is_the_naive_scan_when_the_needle_is_cut_from_the_haystack :: { check.seeded ~seed: 3802 ~runs: 300 cut_pair agrees } @test fn find_is_the_naive_scan_on_multibyte_strings :: { check.seeded ~seed: 3803 ~runs: 200 multibyte_pair agrees } @test fn find_is_the_naive_scan_on_long_haystacks :: { check.seeded ~seed: 3804 ~runs: 50 long_pair agrees }