Repository: typst/typst
File: crates/typst-library/src/foundations/str.rs (Rust)

Code before:
        }
    }

    /// Searches for the specified pattern in the string and returns a
    /// dictionary with details about the first match or `{none}` if there is no
    /// match.
    ///
    /// The returned dictionary has the following keys:
    /// - `start`: The start offset of the match
    /// - `end`: The end offset of the match
    /// - `text`: The text that matched.
    /// - `captures`: An array containing a string for each matched capturing
    ///   group. The first item of the array contains the first matched
    ///   capturing, not the whole match! This is empty unless the `pattern` was
    ///   a regex with capturing groups.
    ///
    /// ```example
    /// #(
    ///   "Is there a".match("for this?"),
    ///   "The time of my life.".match(regex("[mit]+e")),
    /// )
    /// ```
    ///
    /// ```example
    /// #let pattern = regex(
    ///   `not (an?) ([\p{Letter}\s]+)\p{Punctuation}`.text,
    /// )
    /// #"I'm a doctor, not an escalator.".match(pattern) \
    /// #"I am not a merry man!".match(pattern)
    /// ```
    #[func]
    pub fn match_(
        &self,
        /// The pattern to search for.
        pattern: StrPattern,
    ) -> Option<Dict> {
        match pattern {
            StrPattern::Str(pat) => {
                self.0.match_indices(pat.as_str()).next().map(match_to_dict)
            }
            StrPattern::Regex(re) => re.captures(self).map(captures_to_dict),
        }
    }

    /// Searches for the specified pattern in the string and returns an array of
    /// dictionaries with details about all matches. For details about the
    /// returned dictionaries, see [above]($str.match).
    ///
    /// ```example
    /// #"Day by Day.".matches("Day")
    /// ```

Review comment (suggestion):
<img width="300" alt="Image" src="https://github.com/user-attachments/assets/8b864c80-65d5-4809-94e6-ca071d919e71" />

Code after:
        }
    }

    /// Searches for the specified pattern in the string and returns a
    /// dictionary with details about the first match or `{none}` if there is no
    /// match.
    ///
    /// The returned dictionary has the following keys:
    /// - `start`: The start offset of the match
    /// - `end`: The end offset of the match
    /// - `text`: The text that matched.
    /// - `captures`: An array containing a string for each matched capturing
    ///   group. The first item of the array contains the first matched
    ///   capturing, not the whole match! This is empty unless the `pattern` was
    ///   a regex with capturing groups.
    ///
    /// ```example
    /// #assert.eq("Is there a".match("for this?"), none)
    /// #"The time of my life.".match(regex("[mit]+e"))
    /// ```
    ///
    /// ```example
    /// #let pat = regex("not (a|an) (apple|cat)")
    /// #"I'm a doctor, not an apple.".match(pat) \
    /// #"I am not a cat!".match(pat)
    /// ```
    #[func]
    pub fn match_(
        &self,
        /// The pattern to search for.
        pattern: StrPattern,
    ) -> Option<Dict> {
        match pattern {
            StrPattern::Str(pat) => {
                self.0.match_indices(pat.as_str()).next().map(match_to_dict)
            }
            StrPattern::Regex(re) => re.captures(self).map(captures_to_dict),
        }
    }

    /// Searches for the specified pattern in the string and returns an array of
    /// dictionaries with details about all matches. For details about the
    /// returned dictionaries, see [above]($str.match).
    ///
    /// ```example
    /// #"Day by Day.".matches("Day")
    /// ```
    #[func]
    pub fn matches(
        &self,
        /// The pattern to search for.