Skip to content

Latest commit

 

History

History

example

Examples

Example 1a wandbox.badge

Example 1b wandbox.badge

Example 1c wandbox.badge

Compile time matching only.

Does nothing at runtime.

Example 2 wandbox.badge

Compile time and runtime check of string ID's validity using a pattern.

Defines valid_id() function which can be used when defining a string ID which has to match a given pattern.

// compile time
constexpr auto id = valid_id("test_something", "[Tt]est_*");

When used at runtime and the string ID does not match,

// runtime
auto id = valid_id("tst_something_different", "[Tt]est_*");

std::logic_error("Invalid ID") is thrown.

Usage:

$ ./build/example/example02
Valid ID: test_something
Valid ID: Test_something_else
Valid ID: test_something
Valid ID: Test_something_else
Invalid ID

Example 3 wandbox.badge

Example 2 extended with a default pattern and with a user-defined string literal.

The valid_id() can also be used with a default pattern

// compile time
constexpr auto id = valid_id("test_something");

and via _valid_id string literal.

// compile time
constexpr auto id = "test_something"_valid_id;

Usage:

$ ./build/example/example03
Valid ID: test_something
Valid ID: Test_something_else
Valid ID: test_something
Valid ID: Test_something_else
Invalid ID

Example 4 wandbox.badge

Matching of command line arguments.

Syntax:

example04 [-v] sequence pattern

-v stands for verbose and can be used to visualize where exactly the matching failed.

Usage:

$ ./build/example/example04 "You are so nice!" "[Y|y]ou are ((so |)nice|(too |)ugly)([.\!]|)"
$ echo $?
0
$
$ ./build/example/example04 "you're so nice" "[Y|y]ou are ((so |)nice|(too |)ugly)([.\!]|)"
$ echo $?
1
$
$ ./build/example/example04 -v "you're so nice" "[Y|y]ou are ((so |)nice|(too |)ugly)([.\!]|)"
you're so nice
   ^
[Y|y]ou are ((so |)nice|(too |)ugly)([.\!]|)
       ^
$ echo $?
1

Example 5 wandbox.badge

List files on a given path which match a given pattern.

This example makes use of filesystem library, therefore is not built for all configurations.

Syntax:

example05 path pattern

Usage:

$ ./build/example/example05 test "*.[hc]pp"
test/src/catch.cpp
test/src/wildcards/match_test.cpp
test/src/cx/array_test.cpp
test/src/cx/tuple_test.cpp
test/src/cx/utility_test.cpp
test/src/cx/string_view_test.cpp
test/include/catch.hpp

Example 6 wandbox.badge

Matching of generated DNA sequences.

A DNA sequence consists of letters A, G, T, C and is up to 10 letters long (but can also be empty).

This example demonstrates the general purposeness of the library since the DNA sequences are internally represented as shown below - i.e. not as strings.

enum class nucleobase
{
  adenine,
  cytosine,
  guanine,
  thymine
};

using dna_sequence_type = std::vector<nucleobase>;

Syntax:

example06 pattern

Usage:

$ ./build/example/example06 "?AG(TT|CC)*(A|G)"
...
...
...
TAGCCTAAG
TAGCCAG
GAGTTCG
CAGCCGG
TAGTTTCAA
GAGTTGG
CAGCCTG
GAGTTAA
TAGCCG
AAGTTGGTG
Generated : 1000000
Found     : 1528