Trait in text coil-stdlib

Searchable

trait Searchable<T: Show>
Import
use text::{Searchable};
Source: src/text.hy

Pattern search methods (contains, find, split, replace_all, …) on any [Show] type, which search its display form. string has them built in, so "foobar".contains("oba") works with a string or any other [Pattern] (such as a compiled Regex).

Give your own type the methods with an empty instance; they search value.show():

use text::{Searchable};
use tests::assert;

class Version {
    pub major: int,
    pub minor: int,
}

impl Show for Version {
    pub fn show(Version v) -> string {
        return string::format("%i.%i", v.major, v.minor);
    }
}

impl Searchable for Version {}

fn main() {
    assert!(new Version(1, 12).starts_with("1."));
}

Empty matches step over one character, so an empty pattern matches between every pair of characters ("ab".split("") is ["", "a", "b", ""]).

Required methods#

NameDescription
containsReturns true when pat matches anywhere in hay.
starts_withReturns true when a match of pat starts at the beginning of hay.
ends_withReturns true when a match of pat ends at the end of hay.
findReturns the byte span of the first match of pat, or None.
find_allReturns the byte spans of every non-overlapping match, left to right.
splitReturns the pieces of hay between the matches of pat.
split_onceSplits hay at the first match of pat into the parts before and after it.
replace_firstReturns hay with its first match of pat replaced by with.
replace_allReturns hay with every non-overlapping match of pat replaced by with.

Examples#

use text::{Searchable};
use tests::assert;
assert!("foobar".contains("oba"));
assert!("a,b,,c".split(",").len() == 4);
assert!("a-b-c".replace_all("-", "+") == "a+b+c");

See also#

↑ ↓ navigate ↵ open