Last active
October 3, 2019 05:26
-
-
Save Andersama/4fd79ba11af37ff22be38e009a570141 to your computer and use it in GitHub Desktop.
compile time calculation of the least and most amount of characters a regex may match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| //requires c++17 | |
| //see: https://github.com/hanickadot/compile-time-regular-expressions/blob/master/single-header/ctre.hpp | |
| //specifically: https://github.com/hanickadot/compile-time-regular-expressions/blob/96de8f7a519b61abd4ef53ec37cd89565880aa50/single-header/ctre.hpp | |
| #include <string_view> | |
| #include "ctre.hpp" | |
| namespace regex::analyzer { | |
| //issues w/o unicode (regex fails to compile), these are the (?: .. etc patterns that change how a group capture functions | |
| static constexpr auto ignore_group_pattern = ctll::fixed_string(R"RAW(^(?:[?](?:[\x{003A}\x{007C}\x{003E}]|\x{003C}?[\x{003D}\x{0021}]|\x{0027}\w+\x{0027})))RAW"); | |
| //do we need a regex to match | ? not really | |
| static constexpr auto pipe_pattern = ctll::fixed_string(R"RAW(^\|)RAW"); | |
| //matches the modifiers * + ? and {} | |
| static constexpr auto repeat_pattern = ctll::fixed_string(R"RAW(^(?:([+*\?])|([{](\d*),(\d*)[}])|([{]\d+[}])))RAW"); | |
| //matches capture groups (just ( for brevity), [] character classes, unicode-ish things w/ \x, anchors (we ignore) and lastly escaped literals...but not | | |
| static constexpr auto literals_pattern = ctll::fixed_string(R"RAW(^(?:([(])|\[(?:\\.|[^\]])*\]|\\x(?:\{\d+\}|\d{1,3})|(\\[AZzbBG]|[$^])|\\.|[^\|]))RAW"); | |
| struct rgx_bound { | |
| size_t least; | |
| size_t most; | |
| }; | |
| struct regex_bound { | |
| const size_t least; | |
| const size_t most; | |
| }; | |
| static constexpr long long pow10[] = { | |
| 1LL, | |
| 10LL, | |
| 100LL, | |
| 1000LL, | |
| 10000LL, | |
| 100000LL, | |
| 1000000LL, | |
| 10000000LL, | |
| 100000000LL, | |
| 1000000000LL, | |
| 10000000000LL, | |
| 100000000000LL, | |
| 1000000000000LL, | |
| 10000000000000LL, | |
| 100000000000000LL, | |
| 1000000000000000LL, | |
| 10000000000000000LL, | |
| 100000000000000000LL, | |
| 1000000000000000000LL | |
| }; | |
| //needs a constexpr string to long long function | |
| static constexpr auto longlong_pattern = ctll::fixed_string(R"RAW(-?(\d+))RAW"); | |
| constexpr long long stoll(const std::string_view &rgx) { | |
| long long ret = 0; | |
| auto match = ctre::match<longlong_pattern>(rgx); | |
| auto longlong = match.get<0>(); | |
| if (longlong) { | |
| //negative number | |
| bool is_neg = match.get<0>().size() != match.get<1>().size(); | |
| long long neg = is_neg ? -1LL : 1LL; | |
| auto num_view = match.get<1>().to_view(); | |
| for (size_t i = 0; i < num_view.size(); i++) { | |
| size_t last = (num_view.size() - 1); | |
| long long t = (num_view[last - i] - '0') * pow10[i] * neg; | |
| long long tmp = ret + t; | |
| //underflow | |
| if (is_neg && (tmp > ret)) { | |
| ret = LLONG_MIN; | |
| break; | |
| //overflow | |
| } | |
| else if (!is_neg && (tmp < ret)) { | |
| ret = LLONG_MAX; | |
| break; | |
| } | |
| else { | |
| ret = tmp; | |
| } | |
| } | |
| } | |
| return ret; | |
| }; | |
| constexpr std::string_view balanced_token(const std::string_view& str, const std::string_view& l, const std::string_view& r) { | |
| size_t first = str.find_first_of(l, 0); | |
| size_t lcount = 0; | |
| size_t rcount = 0; | |
| if (first < str.size()) | |
| lcount = 1; | |
| size_t last = first; | |
| size_t idx = first; | |
| for (; idx < str.size() && lcount > rcount;) { | |
| //find the next right hand token | |
| size_t next = str.find_first_of(r, idx+1); | |
| if (next < str.size()) | |
| rcount++; | |
| //find any left hand tokens inbetween the pair | |
| while (idx < next) { | |
| size_t between = str.find_first_of(l, idx+1); | |
| if (between < next) { | |
| lcount++; | |
| idx = between; | |
| } else { | |
| idx = next; | |
| break; | |
| } | |
| } | |
| //we've found an even # of left and right hand tokens, now return the view | |
| if (lcount == rcount) | |
| last = next+1; | |
| } | |
| return str.substr(first, last-first); | |
| }; | |
| constexpr regex_bound regex_bounds(const std::string_view rgx) { | |
| size_t idx = 0; | |
| //start off w/ best case, we need at least 0 characters and at most 0 characters for a match | |
| rgx_bound ret = { 0, 0 }; | |
| rgx_bound alt = { 0, 0 }; | |
| bool has_alt = false; | |
| for (; idx < rgx.size();) { | |
| auto match = ctre::search<literals_pattern>(rgx.substr(idx)); | |
| if (match.get<0>()) { | |
| //matched an anchor floating around ~skip | |
| auto anchor_expr = match.get<2>(); | |
| if (anchor_expr) { | |
| idx += anchor_expr.size(); | |
| continue; | |
| } | |
| size_t lit_match_width = match.get<0>().size(); | |
| size_t mod_match_width = 0; | |
| //a normal literal character match requires at least 1 character and at most 1 character | |
| //for now we assume nothing | |
| rgx_bound lit_bounds = {0,0}; | |
| rgx_bound mod_lit_bounds = {0,0}; | |
| auto sub_expr = match.get<1>(); | |
| if (sub_expr) { | |
| std::string_view sub_expr_view = balanced_token(rgx.substr(idx),"(",")"); | |
| lit_match_width = sub_expr_view.size(); | |
| //parsing () or unbounded ( | |
| //this matches "nothing", requires at least 0 characters and at most ?, assuming 0 | |
| if (sub_expr_view.size() <= 2) { | |
| lit_bounds = { 0, 0 }; | |
| } else { | |
| //since we know w/ this match we're looking at () we can trim the ends | |
| std::string_view sub_rgx = sub_expr_view.substr(1, sub_expr_view.size() - 2); | |
| auto group_modifier = ctre::search<ignore_group_pattern>(sub_rgx); | |
| //recurse into the subexpression | |
| if (group_modifier.get<0>()) { | |
| size_t s = group_modifier.get<0>().size(); | |
| auto sub_bounds = regex_bounds(sub_rgx.substr(s, sub_rgx.size() - s)); | |
| lit_bounds = { sub_bounds.least, sub_bounds.most }; | |
| } | |
| else { | |
| auto sub_bounds = regex_bounds(sub_rgx); | |
| lit_bounds = { sub_bounds.least, sub_bounds.most }; | |
| } | |
| } | |
| } else { | |
| lit_bounds = { 1, 1 }; | |
| } | |
| idx += lit_match_width; | |
| if (idx < rgx.size()) { | |
| //match a trailing modifier * + ? or {min, max} | |
| auto match2 = ctre::search<repeat_pattern>(rgx.substr(idx)); | |
| if (match2.get<0>()) { | |
| //assume * (matching 0 - infinite) | |
| size_t mn = 0; | |
| size_t mx = -1; | |
| if (match2.get<1>()) { | |
| mod_match_width = match2.get<1>().size(); | |
| if (match2.get<1>().to_view() == "+") { | |
| mn = 1; | |
| } | |
| else if (match2.get<1>().to_view() == "?") { | |
| mx = 1; | |
| } | |
| } | |
| else { | |
| mod_match_width = match2.get<2>().size(); | |
| //{min, max} | |
| //a missing min is defaulted to 0 and a missing max is defaulted to infinite | |
| //nice thing we assume that to start | |
| if (match2.get<3>()) { | |
| mn = (size_t)stoll(match2.get<3>()); | |
| } else if (match2.get<5>()) { | |
| mn = (size_t)stoll(match2.get<5>()); | |
| } | |
| if (match2.get<4>()) { | |
| mx = (size_t)stoll(match2.get<4>()); | |
| } | |
| } | |
| //overflow protection | |
| mod_lit_bounds = { | |
| mn > 0 && lit_bounds.least > (UINT64_MAX / mn) ? -1 : lit_bounds.least * mn, | |
| mx > 0 && lit_bounds.most > (UINT64_MAX / mx) ? -1 : lit_bounds.most * mx }; | |
| } else { | |
| mod_lit_bounds = lit_bounds; | |
| } | |
| } else { | |
| mod_lit_bounds = lit_bounds; | |
| } | |
| //now we've parsed some expression and figured what it at least and most takes | |
| //add it onto the existing requirements | |
| if (mod_lit_bounds.most > 0 && ret.most > (UINT64_MAX - mod_lit_bounds.most)) { | |
| ret.most = -1; | |
| } else { | |
| ret.most += mod_lit_bounds.most; | |
| } | |
| if (mod_lit_bounds.least > 0 && ret.least > (UINT64_MAX - mod_lit_bounds.least)) { | |
| ret.least = -1; | |
| } else { | |
| ret.least += mod_lit_bounds.least; | |
| } | |
| //move on to the next part of the regex | |
| idx += mod_match_width; | |
| continue; | |
| } else { | |
| //we didn't find a literal, check for an alternatation | |
| auto match4 = ctre::search<pipe_pattern>(rgx.substr(idx)); | |
| if (match4.get<0>()) { | |
| if (has_alt) { | |
| ret.least = std::min(ret.least, alt.least); | |
| ret.most = std::max(ret.most, alt.most); | |
| } else { | |
| has_alt = true; | |
| } | |
| //save for the next pop | |
| alt = ret; | |
| //start over | |
| ret = { 0, 0 }; | |
| //pipes take up one character...easy | |
| idx++; | |
| continue; | |
| } else { | |
| //no match at all...impossible! (we're done) | |
| break; | |
| } | |
| } | |
| } | |
| //pop the last alternation if we had one | |
| if (has_alt) { | |
| ret.least = std::min(ret.least, alt.least); | |
| ret.most = std::max(ret.most, alt.most); | |
| } | |
| regex_bound r{ret.least, ret.most}; | |
| return r; | |
| }; | |
| static_assert(4 == regex_bounds("aa(bbb)?cc").least); | |
| static_assert(1 == regex_bounds("aa(bbb)?cc|c").least); | |
| static_assert(4 == regex_bounds("(?:aa(bbb)?cc)").least); | |
| static_assert(1 == regex_bounds("(?:aa(bbb)?cc|c)").least); | |
| static_assert(7 == regex_bounds("aab{0,3}cc").most); | |
| static_assert(4 == regex_bounds("aab{0,3}cc").least); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment