Simple string matching with questionmark- and star-wildcard operator

Overview

wildmatch

build status docs downloads crate license codecov

Match strings against a simple wildcard pattern. Tests a wildcard pattern p against an input string s. Returns true only when p matches the entirety of s.

See also the example described on wikipedia for matching wildcards.

  • ? matches exactly one occurrence of any character.
  • * matches arbitrary many (including zero) occurrences of any character.
  • No escape characters are defined.

For example the pattern ca? will match cat or car. The pattern https://* will match all https urls, such as https://google.de or https://github.com/becheran/wildmatch.

Compared to the rust regex library, wildmatch pattern compile much faster and match with about the same speed. Compared to glob pattern wildmtach is faster in both compile and match time:

Benchmark wildmatch regex glob
compiling/text 990 ns 476,980 ns 4,517 ns
compiling/complex 122 ns 177,510 ns 562 ns
matching/text 568 ns 655 ns 1,896 ns
matching/complex 664 ns 575 ns 4,208 ns

The library only depends on the rust stdlib.

See the documentation for usage and more examples.

You might also like...
Simple and performant hot-reloading for Rust

reloady Simple, performant hot-reloading for Rust. Requires Rust nightly and only works on Linux for now. installing CLI To install the CLI helper car

Mote is a systems-programming language designed to be practical, performant, and simple.
Mote is a systems-programming language designed to be practical, performant, and simple.

Mote NOTE: this following lists the goals for what Mote is supposed to be. It does not promise that any of the features here will be accomplished or a

A simple and convenient way to bundle owned data with a borrowing type.

A simple and convenient way to bundle owned data with a borrowing type. The Problem One of the main selling points of Rust is its borrow checker, whic

A simple heads or tails (cross and pile) on Solana

cross-pile (Coin flip) A heads or tails (cross and pile) on Solana. It's meant to serve as an example of how to use solrand, a randomness Oracle: http

A Simple Audio Control and Notifications Daemon

SACAND This is sacand, a Simple Audio Control and Notifications Daemon As it name oaths to imply, this is intended to be a simple audio control daemon

A simple programming language for something between C and Rust.

inuc inuc is a systems programming language that is something between C and Rust. Features : [] Strong , static typing (type inference not a priority

Rust language from simple to deep, and then to strengthen learning in multiple module

Rust Library This project is used to learn rust language from simple to deep, and then to strengthen learning in multiple module. It is used to help n

A simple thread schedule and priority library for rust

thread-priority A simple library to control thread schedule policies and thread priority. If your operating system isn't yet supported, please, create

TypeRust - simple Rust playground where you can build or run your Rust code and share it with others

Rust playground Welcome to TypeRust! This is a simple Rust playground where you can build or run your Rust code and share it with others. There are a

Comments
  • Add benchmark, some minor improvements

    Add benchmark, some minor improvements

    This adds a benchmark to compare wildmatch with regex. It also implements minor optimizations:

    • Use Vec::with_capacity to reduce the number of allocations
    • Use Vec::is_empty, instead of counting the chars, which is inefficient

    It also implements some useful traits:

    • impl Default for WildMatch allows creating a default WildMatch, which matches the empty string
    • impl Display for WildMatch allows formatting the type with format!("{}", wild_match). Types implementing Display automatically implement ToString, so it's recommended to implement Display manually, but not ToString
    • impl PartialEq<&str> for WildMatch allows comparing a WildMatch and a &str with the == operator. This is more convenient than .is_match().

    Benchmark results

    The benchmarks measure both the time it takes to compile a pattern/regex and the time the matching takes.

    | Benchmark | Time| | ---- | ----:| | compiling/text pattern | 1,060 ns | compiling/text regex | 384,300 ns | compiling/complex pattern | 137 ns | compiling/complex regex | 254,610 ns | matching/text pattern | 828 ns | matching/text regex | 1,782 ns | matching/complex pattern | 1,394 ns | matching/complex regex | 597 ns

    This shows that, while Regex can be faster at matching strings, it is much, much slower at compiling a pattern than WildMatch. The performance is really impressive.

    opened by Aloso 4
  • #[test_case(

    #[test_case("*o?a*r", "foobar")] fails

    Describe the bug *o?a*r should match foobar. However, the result of WildMatch::new("*o?a*r").matches("foobar") is false.

    To Reproduce Add the following test case:

    #[test_case("*o?a*r", "foobar")]
    

    Run cargo test.

    Expected behavior Tests should pass.

    Desktop (please complete the following information):

    • OS: Windows 10
    • Version: 21H2

    Additional context rustc version

    rustc 1.61.0 (fe5b13d68 2022-05-18)
    
    bug 
    opened by Lweb 2
  • "xyz*" pattern produce a match even if the string does not start with xyz

    Describe the bug "cat*" is a match for "wildcats", I think it only should match to "*cat*"

    To Reproduce Steps to reproduce the behavior: println!("{}",WildMatch::new("cat*").is_match("wildcats"));

    Expected behavior WildMatch::new("cat*").is_match("wildcats")) --> false

    Desktop (please complete the following information):

    • OS: win 10
    bug 
    opened by 20iahazban 2
  • #[test_case(

    #[test_case("*32", "332")] fails

    Describe the bug *foo should match any string which ends in foo. *32 detects a match in 432 but doesn't detect a match in 332

    To Reproduce Add the following test cases:

    #[test_case("*32", "432")]
    #[test_case("*32", "332")]
    

    Run cargo test

    Expected behavior Tests should pass

    Desktop (please complete the following information):

    • OS: Mac OSX 10.15.4
    • Browser NA
    • Version NA

    rustc -v reports

    rustc 1.42.0 (b8cedc004 2020-03-09)
    
    opened by davehylands 1
Owner
Armin Becher
Armin Becher
An example of a Kubernetes operator implemented in Rust

Rust Kubernetes operator example A Kubernetes operator built on top of kube-rs project. There is an explanatory article available. Steps to run on Lin

Pavel Pscheidl 91 Jan 1, 2023
Compact, clone-on-write vector and string.

ecow Compact, clone-on-write vector and string. Types An EcoVec is a reference-counted clone-on-write vector. It takes up two words of space (= 2 usiz

Typst 78 Apr 18, 2023
Parses a relative time string and returns a `Duration`

humantime_to_duration A Rust crate for parsing human-readable relative time strings and converting them to a Duration. Features Parses a variety of hu

null 5 Apr 25, 2023
Stack heap flexible string designed to improve performance for Rust

flexible-string A stack heap flexible string designed to improve performance. FlexibleString was first implemented in spdlog-rs crate, which improved

Sprite 6 Feb 9, 2022
A crate for converting an ASCII text string or file to a single unicode character

A crate for converting an ASCII text string or file to a single unicode character. Also provides a macro to embed encoded source code into a Rust source file. Can also do the same to Python code while still letting the code run as before by wrapping it in a decoder.

Johanna Sörngård 17 Dec 31, 2022
Rust crate for obfuscating string literals.

Obfustring This crate provides a obfuscation macro for string literals. This makes it easy to protect them from common reverse engineering attacks lik

null 7 Mar 1, 2023
A special rope, designed to work with any data type that is not String

AnyRope AnyRope is an arbitrary data type rope for Rust, designed for similar operations that a rope would do, but targeted at data types that are not

ahoyiski 27 Mar 22, 2023
Idiomatic Rust implementations for various Windows string types (like UNICODE_STRING)

nt-string by Colin Finck <[email protected]> Provides idiomatic Rust implementations for various Windows string types: NtUnicodeString (with NtUnicode

Colin Finck 5 Jun 4, 2023
Rust based magic-string with source map chains support

enhanced-magic-string Rust implementation of https://www.npmjs.com/package/magic-string with original sourcemap chain support. license. This project i

Farm 3 Nov 5, 2023
Fast and simple datetime, date, time and duration parsing for rust.

speedate Fast and simple datetime, date, time and duration parsing for rust. speedate is a lax† RFC 3339 date and time parser, in other words, it pars

Samuel Colvin 43 Nov 25, 2022