Skip to content

Commit b1141c2

Browse files
authored
Merge pull request #67 from TartanLlama/master
Add documentation
2 parents 5bbc089 + 6d354c5 commit b1141c2

File tree

5 files changed

+318
-0
lines changed

5 files changed

+318
-0
lines changed

docs/api.rst

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
API
2+
===
3+
4+
.. class:: ctll::fixed_string
5+
6+
A compile-time fixed string.
7+
8+
Example: ::
9+
10+
static constexpr auto pattern = ctll::fixed_string{ "h.*" };
11+
12+
constexpr auto match(std::string_view sv) noexcept {
13+
return ctre::match<pattern>(sv);
14+
}
15+
16+
.. class:: template<class Iterator, class... Captures> ctre::regex_results
17+
18+
.. type:: char_type = typename std::iterator_traits<Iterator>::value_type
19+
20+
The character type used by the ``Iterator``.
21+
22+
.. function:: template<size_t Id> constexpr captured_content<Id, void>::storage<Iterator> get()
23+
template<class Name> constexpr captured_content<deduced, Name>::storage<Iterator> get()
24+
template<ctll::fixed_string Name> constexpr captured_content<deduced, Name>::storage<Iterator> get()
25+
26+
Returns the capture specified by ``Id`` or ``Name``. ID ``0`` is the full match, ID ``1`` is the first capture group, ID ``2`` is the second, etc.
27+
Named groups are specified using ``(?<name>)``.
28+
29+
Example: ::
30+
31+
if (auto m = ctre::match<"(?<chars>[a-z]+)([0-9]+)">("abc123")) {
32+
m.get<"chars">(); //abc
33+
m.get<2>(); //123
34+
}
35+
36+
.. function:: constexpr size_t size()
37+
38+
Returns the number of captures in this result object.
39+
40+
.. function:: constexpr operator bool() const noexcept
41+
42+
Returns whether the match was successful.
43+
44+
.. function:: constexpr operator std::basic_string_view<char_type>() const noexcept
45+
constexpr std::basic_string_view<char_type> to_view() const noexcept
46+
constexpr std::basic_string_view<char_type> view() const noexcept
47+
48+
Converts the match to a string view.
49+
50+
.. function:: constexpr explicit operator std::basic_string<char_type>() const noexcept
51+
constexpr std::basic_string<char_type> to_string() const noexcept
52+
constexpr std::basic_string<char_type> str() const noexcept
53+
54+
Converts the match to a string view.
55+
56+
.. class:: template<size_t Id, typename Name = void> captured_content
57+
58+
.. class:: template <typename Iterator> storage
59+
60+
.. function:: constexpr auto begin() const noexcept
61+
constexpr auto end() const noexcept
62+
63+
Returns the begin or end iterator for the captured content.
64+
65+
.. function:: constexpr operator bool() const noexcept
66+
67+
Returns whether the match was successful.
68+
69+
.. function:: constexpr auto size() const noexcept
70+
71+
Returns the number of characters in the capture.
72+
73+
.. function:: constexpr operator std::basic_string_view<char_type>() const noexcept
74+
constexpr std::basic_string_view<char_type> to_view() const noexcept
75+
constexpr std::basic_string_view<char_type> view() const noexcept
76+
77+
Converts the capture to a string view.
78+
79+
.. function:: constexpr explicit operator std::basic_string<char_type>() const noexcept
80+
constexpr std::basic_string<char_type> to_string() const noexcept
81+
constexpr std::basic_string<char_type> str() const noexcept
82+
83+
Converts the capture to a string view.
84+
85+
.. function:: constexpr static size_t get_id() noexcept
86+
87+
Returns ``Id``
88+
89+
.. function:: template<auto & RE, class... Args> constexpr ctre::regex_results<deduced> match(Args&&... args)
90+
template<ctll::fixed_string RE, class... Args> constexpr ctre::regex_results<deduced> match(Args&&... args)
91+
92+
Matches ``RE`` against the whole input.
93+
``Args...`` must be either a string-like object with ``begin`` and ``end`` member functions, or a pair of forward iterators.
94+
95+
.. function:: template<auto & RE, class... Args> constexpr ctre::regex_results<deduced> search(Args&&... args)
96+
template<ctll::fixed_string RE, class... Args> constexpr ctre::regex_results<deduced> search(Args&&... args)
97+
98+
Searches for a match somewhere within the input.
99+
``Args...`` must be either a string-like object with ``begin`` and ``end`` member functions, or a pair of forward iterators.
100+

docs/conf.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# This file only contains a selection of the most common options. For a full
4+
# list see the documentation:
5+
# http://www.sphinx-doc.org/en/master/config
6+
7+
# -- Path setup --------------------------------------------------------------
8+
9+
# If extensions (or modules to document with autodoc) are in another directory,
10+
# add these directories to sys.path here. If the directory is relative to the
11+
# documentation root, use os.path.abspath to make it absolute, like shown here.
12+
#
13+
# import os
14+
# import sys
15+
# sys.path.insert(0, os.path.abspath('.'))
16+
17+
18+
# -- Project information -----------------------------------------------------
19+
20+
project = 'ctre'
21+
copyright = '2019, Hana Dusikova'
22+
author = 'Hana Dusikova'
23+
master_doc = 'index'
24+
primary_domain = 'cpp'
25+
highlight_language = 'cpp'
26+
27+
# -- General configuration ---------------------------------------------------
28+
29+
# Add any Sphinx extension module names here, as strings. They can be
30+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
31+
# ones.
32+
extensions = [
33+
]
34+
35+
# Add any paths that contain templates here, relative to this directory.
36+
templates_path = ['_templates']
37+
38+
# List of patterns, relative to source directory, that match files and
39+
# directories to ignore when looking for source files.
40+
# This pattern also affects html_static_path and html_extra_path.
41+
exclude_patterns = []
42+
43+
44+
# -- Options for HTML output -------------------------------------------------
45+
46+
# The theme to use for HTML and HTML Help pages. See the documentation for
47+
# a list of builtin themes.
48+
#
49+
html_theme = 'sphinx_rtd_theme'
50+
51+
# Add any paths that contain custom static files (such as style sheets) here,
52+
# relative to this directory. They are copied after the builtin static files,
53+
# so a file named "default.css" will overwrite the builtin "default.css".
54+
html_static_path = ['_static']

docs/examples.rst

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Examples
2+
========
3+
4+
Extracting a number from input
5+
------------------------------
6+
::
7+
8+
std::optional<std::string_view> extract_number(std::string_view s) noexcept {
9+
if (auto m = ctre::match<"[a-z]+([0-9]+)">(s)) {
10+
return m.get<1>().to_view();
11+
} else {
12+
return std::nullopt;
13+
}
14+
}
15+
16+
`link to compiler explorer <https://gcc.godbolt.org/z/5U67_e>`_
17+
18+
Extracting values from date
19+
---------------------------
20+
::
21+
22+
23+
struct date { std::string_view year; std::string_view month; std::string_view day; };
24+
std::optional<date> extract_date(std::string_view s) noexcept {
25+
using namespace ctre::literals;
26+
if (auto [whole, year, month, day] = ctre::match<"(\\d{4})/(\\d{1,2})/(\\d{1,2})">(s); whole) {
27+
return date{year, month, day};
28+
} else {
29+
return std::nullopt;
30+
}
31+
}
32+
33+
//static_assert(extract_date("2018/08/27"sv).has_value());
34+
//static_assert((*extract_date("2018/08/27"sv)).year == "2018"sv);
35+
//static_assert((*extract_date("2018/08/27"sv)).month == "08"sv);
36+
//static_assert((*extract_date("2018/08/27"sv)).day == "27"sv);
37+
38+
`link to compiler explorer <https://gcc.godbolt.org/z/x64CVp>`_
39+
40+
Lexer
41+
-----
42+
::
43+
44+
enum class type {
45+
unknown, identifier, number
46+
};
47+
48+
struct lex_item {
49+
type t;
50+
std::string_view c;
51+
};
52+
53+
std::optional<lex_item> lexer(std::string_view v) noexcept {
54+
if (auto [m,id,num] = ctre::match<"([a-z]+)|([0-9]+)">(v); m) {
55+
if (id) {
56+
return lex_item{type::identifier, id};
57+
} else if (num) {
58+
return lex_item{type::number, num};
59+
}
60+
}
61+
return std::nullopt;
62+
}
63+
64+
`link to compiler explorer <https://gcc.godbolt.org/z/PKTiCC>`_
65+
66+
Range over input
67+
----------------
68+
69+
This support is preliminary and probably the API will be changed.
70+
71+
::
72+
73+
auto input = "123,456,768"sv;
74+
75+
for (auto match: ctre::range<"([0-9]+),?">(input)) {
76+
std::cout << std::string_view{match.get<0>()} << "\n";
77+
}

docs/index.rst

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
ctre
2+
====
3+
4+
A compile-time (almost) PCRE-compatible regular expression matcher for C++.
5+
6+
Overview
7+
========
8+
9+
Fast compile-time regular expressions with support for matching/searching/capturing at compile-time or runtime. ::
10+
11+
ctre::match<"REGEX">(subject); // C++20
12+
"REGEX"_ctre.match(subject); // C++17 + N3599 extension
13+
14+
.. toctree::
15+
:maxdepth: 2
16+
17+
api
18+
examples
19+
regex_syntax
20+
21+
Supported compilers
22+
===================
23+
24+
- clang 6.0+ (template UDL, C++17 syntax)
25+
- xcode clang 10.0+ (template UDL, C++17 syntax)
26+
- gcc 7.4+ (template UDL, C++17 syntax)
27+
- gcc 9.0+ (C++17 & C++20 cNTTP syntax)
28+
- MSVC 15.8.8+ (C++17 syntax only)
29+
30+
Basic Usage
31+
===========
32+
33+
Template UDL syntax
34+
-------------------
35+
36+
Compiler must support N3599 extension, as GNU extension in gcc (not in GCC 9.1+) and clang. ::
37+
38+
constexpr auto match(std::string_view sv) noexcept {
39+
using namespace ctre::literals;
40+
return "h.*"_ctre.match(sv);
41+
}
42+
43+
If you need N3599 extension in GCC 9.1+ you can't use -pedantic mode and define the macro ``CTRE_ENABLE_LITERALS``.
44+
45+
C++17 syntax
46+
------------
47+
48+
You can provide pattern as a constexpr ``ctll::fixed_string variable``. ::
49+
50+
static constexpr auto pattern = ctll::fixed_string{ "h.*" };
51+
52+
constexpr auto match(std::string_view sv) noexcept {
53+
return ctre::match<pattern>(sv);
54+
}
55+
56+
(this is tested in MSVC 15.8.8)
57+
58+
C++20 syntax
59+
------------
60+
61+
Currently only compiler which supports cNTTP syntax ``ctre::match<PATTERN>(subject)`` is GCC 9+. ::
62+
63+
constexpr auto match(std::string_view sv) noexcept {
64+
return ctre::match<"h.*">(sv);
65+
}
66+

docs/regex_syntax.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Regex Syntax
2+
============
3+
4+
The library supports most of the `PCRE <pcre.org>`_ syntax with a few exceptions:
5+
6+
- atomic groups
7+
- boundaries other than ^$
8+
- callouts
9+
- character properties
10+
- comments
11+
- conditional patterns
12+
- control characters (\\cX)
13+
- horizontal / vertical character classes (\\h\\H\\v\\V)
14+
- match point reset (\\K)
15+
- named characters
16+
- octal numbers
17+
- options / modes
18+
- subroutines
19+
- unicode grapheme cluster (\\X)
20+
21+
TODO more detailed regex information

0 commit comments

Comments
 (0)