"Completing" strip_prefix, trim_start_matches, matches, find, etc

Uh this is a bit of a ramble but we promise there's a point to this. We were going through str's methods and we built the following table from them:

Method family Returns matched Returns unmatched Matches many
strip_* No Yes No
trim_*_matches No Yes Yes
matches Yes No Disjoint
find Yes No No
contains No No N/A

There could be some more useful variants to these. For example, there's currently no trim_*_matches equivalent that returns matched instead of unmatched. Having a *_matches family (i.e. start_matches, end_matches), between trim_*_matches and matches, would fill in that gap.

Thoughts? (Disagreements about how to build the table are welcome ofc :p)

To add a function to stdlib, you need a use case. The stdlib doesn't aim to be complete.

2 Likes

Well, unfortunately the use-case cleaned up too good:

        let stdout = String::from_utf8(stdout);
        match stdout.as_ref().ok().map(|x| x.trim()).filter(|x| {
            x.trim_start_matches(|x| {
                char::is_ascii_digit(&x)
            }).trim_end_matches(|x| {
                char::is_ascii_digit(&x)
            }) == "\t"
        }).and_then(|x| {
            let (y, z) = x.split_once("\t")?;
            Some((y.parse::<u64>().ok()?, z.parse::<u64>().ok()?))
        }) {
            Some(v) => return Ok(v),
            None => (),
        }

We can no longer figure out how to fit start_matches/end_matches into this.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.