moonbitstack/moonstl/text does not have a README file
test {
let ac = @text.AhoCorasick::new(["he", "she", "his", "hers"])
@test.assert_eq(ac.search_all("ushers").to_array(), [(1, 1), (0, 2), (3, 2)])
assert_true(ac.is_match("ushers"))
assert_false(ac.is_match("us"))
}test {
let bm = @text.BoyerMoore::new("needle")
assert_eq(bm.search("a needle in a haystack"), Some(2))
assert_eq(bm.search("haystack"), None)
}test {
let kmp = @text.Kmp::new("ab")
assert_eq(kmp.search("cababc"), Some(1))
assert_eq(kmp.search_all("abab").to_array(), [0, 2])
}test {
let rope : @text.Rope = @text.Rope::from_string("hello world")
rope.insert(5, ",")
assert_true(rope.to_string() == "hello, world")
rope.edit(start=0, end=5, "goodbye")
assert_true(rope.to_string() == "goodbye, world")
assert_true(rope.length() == 14)
}test {
let builder : @text.RopeBuilder = @text.RopeBuilder::new()
builder.append("one ")
builder.append("two")
let rope = builder.finish()
assert_true(rope.to_string() == "one two")
}test {
let rope : @text.Rope = @text.Rope::from_string("abcdef")
let window = rope.slice(start=2, end=4)
assert_true(window.to_string() == "cd")
assert_true(window.length() == 2)
}test {
let left : Array[Char] = "abcd".iter().to_array()
let right : Array[Char] = "bcde".iter().to_array()
let matcher : @text.SequenceMatcher[Char] = @text.SequenceMatcher::new(
left[:],
right[:],
)
@test.assert_eq(matcher.ratio() > 0.7, true)
let blocks = matcher.get_matching_blocks()
@test.assert_eq(blocks.length(), 2)
}fn[T : Hash + Eq] SequenceMatcher::find_longest_match(self : SequenceMatcher[T], alo : Int, ahi : Int, blo : Int, bhi : Int) -> Matchfn[T : Hash + Eq] SequenceMatcher::new(a : ArrayView[T], b : ArrayView[T], options? : SequenceMatcherOptions[T]) -> SequenceMatcher[T]pub(all) struct SequenceMatcherOptions[T] {
autojunk : Bool
junk : (T) -> Bool
}test {
let array : @text.SuffixArray = @text.SuffixArray::new("banana")
@test.assert_eq(array.search("ana"), [1, 3])
@test.assert_eq(array.length(), 6)
}test {
let automaton : @text.SuffixAutomaton = @text.SuffixAutomaton::new("banana")
@test.assert_eq(automaton.contains("ana"), true)
@test.assert_eq(automaton.count_occurrences("ana"), 2)
@test.assert_eq(automaton.contains("band"), false)
}fn SuffixAutomaton::longest_common_substring(self : SuffixAutomaton, other : StringView) -> (Int, Int)fn context_diff(a : StringView, b : StringView, fromfile? : StringView, tofile? : StringView, n? : Int) -> Stringfn is_character_junk(ch : Char) -> Boolfn is_line_junk(line : StringView) -> Boolfn longest_palindrome(text : StringView) -> (Int, Int)test {
@test.assert_eq(@text.longest_palindrome("forgeeksskeegfor"), (3, 13))
@test.assert_eq(@text.longest_palindrome("abc"), (0, 1))
}fn ndiff(a : StringView, b : StringView) -> Stringfn restore(diff : StringView) -> Stringfn unified_diff(a : StringView, b : StringView, fromfile? : StringView, tofile? : StringView, n? : Int) -> Stringtest {
@test.assert_eq(@text.z_function("aabxaab"), [7, 1, 0, 0, 3, 1, 0])
}Install
Download zipData structures and algorithms the MoonBit core library leaves out.