Skip to content

std::matcher

The Matcher interface: what String.split, String.replace, String.starts_with, String.ends_with, String.find and String.find_all accept as the thing to look for. A String matches its own text, a Regex matches its pattern, and a Codepoint matches itself. String.contains takes the same three, as String’s Contains impls.

Import with import std::matcher.

Interfaces

interface Matcher
    fn is_contained_in(m: self, text: String) -> Bool
    fn is_prefix_of(m: self, text: String) -> Bool
    fn is_suffix_of(m: self, text: String) -> Bool
    fn split_in(m: self, text: String) -> List<String>
    fn replace_in(m: self, text: String, replacement: String) -> String
    fn find_all_in(m: self, text: String) -> List<String>
    fn find_in(m: self, text: String) -> Maybe<String>
end

Something to look for in a string. String, Regex and Codepoint implement it, so the String search functions take any of them:

String.split("a,b", ",") // ["a", "b"]

String.split("a1b22c", Regex`\d+`) // ["a", "b", "c"]

Each function returns one String operation for text, with the matcher first. Call the String functions rather than these; they exist so a type can say how it is found, and so a function of your own can take any matcher as a Matcher parameter.

import std::regex.Regex

digits = Regex`\d+`
assert String.contains("order 66", digits)
assert String.contains("order 66", "66")
assert Matcher.split_in(",", "a,b") == ["a", "b"]
fn is_contained_in(m: self, text: String) -> Bool

interface Matcher

True if the matcher matches anywhere in text.

fn is_prefix_of(m: self, text: String) -> Bool

interface Matcher

True if a match starts at the beginning of text.

fn is_suffix_of(m: self, text: String) -> Bool

interface Matcher

True if a match ends at the end of text.

fn split_in(m: self, text: String) -> List<String>

interface Matcher

The pieces of text between non-overlapping matches.

fn replace_in(m: self, text: String, replacement: String) -> String

interface Matcher

text with every non-overlapping match replaced by replacement, taken literally.

fn find_all_in(m: self, text: String) -> List<String>

interface Matcher

Every non-overlapping match in text, left to right.

fn find_in(m: self, text: String) -> Maybe<String>

interface Matcher

The first match in text, the leftmost: the first element find_all_in returns, or None when there is no match.