Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
regex.hpp
Go to the documentation of this file.
1/*
2 REGEX.hpp - helpers for working with regular expressions
3
4 Copyright (C)
5 2022, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
14
20#ifndef LIB_REGEX_H
21#define LIB_REGEX_H
22
23
24#include "lib/iter-adapter.hpp"
25
26#include <regex>
27#include <string>
28#include <optional>
29
30namespace util {
31
32 using std::regex;
33 using std::smatch;
34 using std::string;
35 using std::string_view;
36
37
38
41 : std::sregex_iterator
42 {
43 RegexSearchIter() = default;
44
45 RegexSearchIter (string const& toParse, regex const& regex)
46 : std::sregex_iterator{toParse.begin(), toParse.end(), regex}
47 { }
48 RegexSearchIter (string_view toParse, regex const& regex)
49 : std::sregex_iterator{string::const_iterator{toParse.begin()}
50 ,string::const_iterator{toParse.end()}, regex}
51 { }
52
53 operator bool() const { return isValid(); }
54
55 bool isValid () const { return (*this)->ready() and not (*this)->empty(); }
56 bool empty () const { return not isValid(); }
57
58 LIFT_PARENT_INCREMENT_OPERATOR (std::sregex_iterator);
60 };
61
62
69 template<typename STR>
70 std::optional<smatch>
71 matchAtStart (STR&& toParse, regex const& regex)
72 {
73 auto search = RegexSearchIter{std::forward<STR> (toParse), regex};
74 if (search and 0 == search->position(0))
75 return *search;
76 else
77 return std::nullopt;
78 }
79
81 template<typename STR>
82 size_t
83 leadingWhitespace (STR&& toParse)
84 {
85 static const regex LEADING_WHITESPACE{"^\\s*", regex::optimize};
86 auto search = RegexSearchIter{std::forward<STR> (toParse), LEADING_WHITESPACE};
87 return search? search->length() : 0;
88 }
89
90}// namespace util
91
92namespace lib {
93 using std::regex;
94 using std::smatch;
95 using std::string;
96}// namespace lib
97#endif/*LIB_REGEX_H*/
Helper template(s) for creating Lumiera Forward Iterators.
Implementation namespace for support and library code.
STL namespace.
size_t leadingWhitespace(STR &&toParse)
Definition regex.hpp:83
std::optional< smatch > matchAtStart(STR &&toParse, regex const &regex)
Helper algorithm to perform a search but require the match to start at the beginning of the string or...
Definition regex.hpp:71
wrapped regex iterator to allow usage in foreach loops
Definition regex.hpp:42
RegexSearchIter(string_view toParse, regex const &regex)
Definition regex.hpp:48
RegexSearchIter(string const &toParse, regex const &regex)
Definition regex.hpp:45
bool isValid() const
Definition regex.hpp:55
bool empty() const
Definition regex.hpp:56
LIFT_PARENT_INCREMENT_OPERATOR(std::sregex_iterator)
ENABLE_USE_IN_STD_RANGE_FOR_LOOPS(RegexSearchIter)