CodeSampleX

Exemple

regex 1.11.1: RegexSet matches multiple regular expressions simultaneously in a single pass and reports matching pattern indices via SetMatches.

Échantillon vérifié pour cargo regex 1.11.1: RegexSet matches multiple regular expressions simultaneously in a single pass and reports matching pattern…

sha256:846e46535607f68dda44722f9fb0e9aaa4f201b90dcc180e7806237faf12f6b2

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
3
Clés de signature qui l’ont compilé
3
Environnement déclaré rust linux x64 rust rust cargo

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
rust 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-17
rust 1 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-18
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-09-08

Cas

HOW
Objectif
RegexSet matches multiple regular expressions simultaneously in a single pass and reports matching pattern indices via SetMatches.
Paquets
Symboles
  • regex::RegexSet
  • regex::RegexSet::new
  • regex::RegexSet::matches
  • regex::RegexSet::is_match
  • regex::SetMatches::matched
  • regex::SetMatches::iter
  • regex::SetMatches::len
Environnement
rust
Créé
2026-08-17T13:55:00Z

Contrat

  1. RegexSet::matches evaluates all compiled regular expression patterns against a haystack in a single linear pass and reports the exact set of matching pattern indices via SetMatches.
  2. RegexSet::is_match returns true when any pattern in the set matches the haystack, and false when no patterns match.
  3. SetMatches::len returns the total number of patterns in the underlying RegexSet rather than only the count of matching patterns.
  4. SetMatches::iter yields the zero-based indices of all matching patterns in the order of their original declaration.
  5. SetMatches::matched returns true for individual matching pattern indices and false for non-matching indices.

Fichiers

  • Cargo.lock
  • Cargo.toml
  • NOTES.md
  • csx.json
  • src/lib.rs

Télécharger l’artefact source (tar.gz)

Code source

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "aho-corasick"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba"
dependencies = [
 "memchr",
]

[[package]]
name = "cargo-regex-version-depth"
version = "0.1.0"
dependencies = [
 "regex",
]

[[package]]
name = "memchr"
version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"

[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
 "aho-corasick",
 "memchr",
 "regex-automata",
 "regex-syntax",
]

[[package]]
name = "regex-automata"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"
dependencies = [
 "aho-corasick",
 "memchr",
 "regex-syntax",
]

[[package]]
name = "regex-syntax"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
Cargo.toml
[package]
name = "cargo-regex-version-depth"
version = "0.1.0"
edition = "2021"

[dependencies]
regex = "=1.11.1"
NOTES.md
# Multi-Pattern Matching with RegexSet in `regex` 1.11.1

## Prior Network Samples
Before authoring this sample, `search_known_solution` was queried on `pkg:cargo/regex`. The network returned:
- `sha256:24f95306df8f2647630f70e9d2a119be71933a762eaf848ce0e6f3abe939dd0b`: Bugfix verification for alternation reverse suffix optimization in `regex 1.10.2`.
- `sha256:ec8b1f9f99e017e0f94308dd51bc6aa26dd89955a9aaf0b41eb0f11c6a136116`: Named capture group extraction without backtracking.

This sample addresses a different capability: compiling multiple regular expressions into a single automaton with `RegexSet` to test and match against text simultaneously in a single pass.

## Pinned Release
- Package: `regex`
- Version: `1.11.1` (pinned via `Cargo.toml` and `Cargo.lock`)

## Observed Behaviour
1. **Automaton Construction:**
   - `RegexSet::new(&[...])` compiles a slice of regex pattern strings into a single unified search automaton.
   - `set.len()` returns the total number of compiled patterns (5).
   - `set.is_empty()` returns `false` when one or more patterns are provided.

2. **Boolean Matching:**
   - `set.is_match(haystack)` tests whether at least one pattern in the set matches anywhere in the haystack in a single linear pass.

3. **Pattern Match Collection:**
   - `set.matches(haystack)` produces a `SetMatches` struct that records which patterns matched.
   - `matches.matched_any()` returns `true` if at least one pattern matched.
   - `SetMatches::len()` returns the total number of patterns in the underlying set (5), NOT the number of matched patterns.
   - `matches.iter().count()` yields the count of patterns that matched (4).
   - `matches.matched(index)` allows O(1) checking of whether the pattern at the 0-based `index` matched.
   - `matches.iter()` yields an iterator of the matching pattern indices in ascending declaration order (`0, 1, 2, 3`).
csx.json
{"case":{"caseId":"case:sha256:4d70acd086329745d05e1bba8a8e7e2a8a63f42a026e466e0ff002138f51952c","contract":["RegexSet::matches evaluates all compiled regular expression patterns against a haystack in a single linear pass and reports the exact set of matching pattern indices via SetMatches.","RegexSet::is_match returns true when any pattern in the set matches the haystack, and false when no patterns match.","SetMatches::len returns the total number of patterns in the underlying RegexSet rather than only the count of matching patterns.","SetMatches::iter yields the zero-based indices of all matching patterns in the order of their original declaration.","SetMatches::matched returns true for individual matching pattern indices and false for non-matching indices."],"goal":"RegexSet matches multiple regular expressions simultaneously in a single pass and reports matching pattern indices via SetMatches.","kind":"HOW","packages":["pkg:cargo/regex@1.11.1"],"schemaVersion":1,"symbols":["regex::RegexSet","regex::RegexSet::new","regex::RegexSet::matches","regex::RegexSet::is_match","regex::SetMatches::matched","regex::SetMatches::iter","regex::SetMatches::len"]},"contractCommand":["cargo","test","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","executionContext":"rust","language":"rust","os":"linux","packageManager":"cargo","runtime":"rust","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/regex@1.11.1"],"schemaVersion":1,"symbols":["regex::RegexSet","regex::RegexSet::new","regex::RegexSet::matches","regex::RegexSet::is_match","regex::SetMatches::matched","regex::SetMatches::iter","regex::SetMatches::len"],"verifierAdapter":"cargo@1"}
src/lib.rs
#[cfg(test)]
mod tests {
    use regex::RegexSet;

    #[test]
    fn test_regex_set_simultaneous_multi_pattern_matching() {
        // Compile a RegexSet with 5 distinct patterns covering identifiers, numbers, emails, URLs, and an unmatched pattern
        let set = RegexSet::new(&[
            r"\b[a-zA-Z_][a-zA-Z0-9_]*\b",         // Index 0: Identifier
            r"\b\d+\b",                            // Index 1: Integer
            r"\b[a-zA-Z0-9._%+-]+@example\.com\b", // Index 2: Email on example.com
            r"https?://example\.com/[a-z]+",       // Index 3: HTTP(S) URL on example.com
            r"\bUNMATCHED_TOKEN\b",                // Index 4: Pattern that will not match
        ])
        .expect("failed to compile RegexSet");

        assert_eq!(set.len(), 5);
        assert!(!set.is_empty());

        let haystack = "user alice_99 at alice_99@example.com visited https://example.com/docs with 42 tokens";

        // 1. is_match returns true if any pattern in the set matches
        assert!(set.is_match(haystack));
        assert!(!set.is_match("--- +++ === !!!"));

        // 2. matches returns a SetMatches instance recording all matching pattern indices
        let matches = set.matches(haystack);

        // matches.matched_any() is true when at least one pattern matches
        assert!(matches.matched_any());

        // SetMatches::len() returns the total number of patterns in the set (5),
        // while matches.iter().count() yields the count of patterns that matched (4).
        assert_eq!(matches.len(), 5);
        assert_eq!(matches.iter().count(), 4);

        // Individual pattern membership queries via .matched(index)
        assert!(matches.matched(0), "pattern 0 (identifier) should match");
        assert!(matches.matched(1), "pattern 1 (integer) should match");
        assert!(matches.matched(2), "pattern 2 (email) should match");
        assert!(matches.matched(3), "pattern 3 (url) should match");
        assert!(
            !matches.matched(4),
            "pattern 4 (unmatched token) should not match"
        );

        // Iterating yields matching indices in declaration order
        let matched_indices: Vec<usize> = matches.iter().collect();
        assert_eq!(matched_indices, vec![0, 1, 2, 3]);

        // 3. Negative case: a haystack matching none of the patterns
        let no_match_haystack = "--- +++ === !!!";
        let no_matches = set.matches(no_match_haystack);
        assert!(!no_matches.matched_any());
        assert_eq!(no_matches.len(), 5);
        assert_eq!(no_matches.iter().count(), 0);
        assert_eq!(no_matches.iter().collect::<Vec<_>>(), Vec::<usize>::new());
        assert!(!no_matches.matched(0));
    }
}

Seeder d'origine

csx-seed