Home

Awesome

language.badge standard.badge license.badge travis.badge appveyor.badge release.badge godbolt.badge wandbox.badge

Wildcards

Wildcards is a simple C++ header-only template library which implements a general purpose algorithm for matching using wildcards. It supports both runtime and compile time execution.

Basic usage

The following examples of the basic usage are functionaly equivalent.

#include <wildcards.hpp>

int main()
{
  {
    using wildcards::match;

    static_assert(match("Hello, World!", "H*World?"), "");
  }

  {
    using wildcards::make_matcher;

    static_assert(make_matcher("H*World?").matches("Hello, World!"), "");
  }

  {
    using namespace wildcards::literals;

    static_assert("H*World?"_wc.matches("Hello, World!"), "");
  }

  return 0;
}

Advanced usage

The following examples of the advanced usage are functionaly equivalent.

#include <wildcards.hpp>

int main()
{
  {
    using wildcards::match;

    static_assert(match("Hello, World!", "H%World_", {'%', '_', '\\'}), "");
  }

  {
    using wildcards::make_matcher;

    static_assert(make_matcher("H%World_", {'%', '_', '\\'}).matches("Hello, World!"), "");
  }

  return 0;
}

See more useful and complex examples and try them online! See also the tests to learn more.

Demonstration on Compiler Explorer

Check compilers output of the following example on Compiler Explorer.

#include <wildcards.hpp>

using namespace wildcards::literals;

constexpr auto pattern = "*.[hc](pp|)"_wc;

// returns true
bool test1()
{
  constexpr auto res = pattern.matches("source.c");

  static_assert(res, "must be true");

  return res;
}

// returns false
bool test2()
{
  constexpr auto res = pattern.matches("source.cc");

  static_assert(!res, "must be false");

  return res;
}

Integration

  1. Single-header approach

    • Copy wildcards.hpp from single_include directory to your project's header search path.
    • Add #include <wildcards.hpp> to your source file.
    • Use wildcards::match() or wildcards::make_matcher(). You can also use operator ""_wc from wildcards::literals namespace.
  2. Multi-header approach

    • Add include directory to your project's header search path.
    • Add #include <wildcards.hpp> to your source file.
    • Use wildcards::match() or wildcards::make_matcher(). You can also use operator ""_wc from wildcards::literals namespace.

Portability

The library requires at least a C++11 compiler to build. It has no external dependencies.

The following compilers are continuously tested at Travis CI and Appveyor CI.

CompilerVersionOperating SystemNotes
Xcode9.0OS X 10.12C++11/14/17
Clang (with libcxx)3.9Ubuntu 14.04 LTSC++14/17
Clang (with libcxx)4.0Ubuntu 14.04 LTSC++11/14/17
Clang (with libcxx)5.0Ubuntu 14.04 LTSC++11/14/17
Clang (with libcxx)6.0Ubuntu 14.04 LTSC++11/14/17
GCC5.5Ubuntu 14.04 LTSC++11/14/17
GCC6.4Ubuntu 14.04 LTSC++11/14/17
GCC7.3Ubuntu 14.04 LTSC++11/14/17
GCC8.1Ubuntu 14.04 LTSC++11/14/17
DJGPP7.2Ubuntu 14.04 LTSC++11/14/17, build only
Visual Studio14 2015Windows Server 2016C++11/14/17, limited
Visual Studio15 2017Windows Server 2016C++11/14/17
MinGW6.3Windows Server 2016C++11/14/17
MinGW7.2Windows Server 2016C++11/14/17
MinGW7.3Windows Server 2016C++11/14/17

License

This project is licensed under the Boost 1.0.

Details

Syntax

PatternMeaning
*Matches everything.
?Matches any single character.
\Escape character.
[abc]Matches any character in Set.
[!abc]Matches any character not in Set.
(ab|c)Matches one of the sequences in Alternative.

Technical Notes