Skip to content

Commit e2879f5

Browse files
author
Hana Dusíková
committed
Merge commit '4dba519e4534416dfff885b61720e43572cc7260'
2 parents 344cc48 + 4dba519 commit e2879f5

File tree

2 files changed

+21
-56
lines changed

2 files changed

+21
-56
lines changed

include/ctre/atoms_characters.hpp

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -39,77 +39,38 @@ template <typename... Content> struct set {
3939
}
4040
};
4141

42+
template <auto... Cs> struct enumeration : set<character<Cs>...> { };
43+
4244
template <typename... Content> struct negate {
4345
template <typename CharT> inline static constexpr bool match_char(CharT value) noexcept {
4446
return !(Content::match_char(value) || ... || false);
4547
}
4648
};
4749

48-
// I know word_chars and others can be expressed with set<> and range<>, but this makes the regex bad readable when debugging, please keep this in mind when touching this code in future :)
49-
50-
struct word_chars {
50+
template <auto A, auto B> struct char_range {
5151
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
52-
return (value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z') || (value >= '0' && value <= '9') || (value == '_');
52+
return (value >= A) && (value <= B);
5353
}
5454
};
5555

56-
struct space_chars {
57-
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
58-
return value == ' ' || value == '\t' || value == '\n' || value == '\v' || value == '\f' || value == '\r';
59-
}
60-
};
56+
struct word_chars : set<char_range<'A','Z'>, char_range<'a','z'>, char_range<'0','9'>, character<'_'> > { };
6157

62-
struct alphanum_chars {
63-
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
64-
return (value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z') || (value >= '0' && value <= '9');
65-
}
66-
};
58+
struct space_chars : enumeration<' ', '\t', '\n', '\v', '\f', '\r'> {};
6759

68-
struct alpha_chars {
69-
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
70-
return (value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z');
71-
}
72-
};
60+
struct alphanum_chars : set<char_range<'A','Z'>, char_range<'a','z'>, char_range<'0','9'> > { };
7361

74-
struct xdigit_chars {
75-
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
76-
return (value >= 'A' && value <= 'F') || (value >= 'a' && value <= 'f') || (value >= '0' && value <= '9');
77-
}
78-
};
62+
struct alpha_chars : set<char_range<'A','Z'>, char_range<'a','z'> > { };
7963

80-
struct punct_chars {
81-
template <typename CharT> inline static constexpr bool match_char(CharT value) noexcept {
82-
return value == '!' || value == '"' || value == '#' || value == '$' || value == '%'
83-
|| value == '&' || value == '\''|| value == '(' || value == ')' || value == '*' || value == ','
84-
|| value == '-' || value == '.' || value == '/' || value == ':' || value == ';'
85-
|| value == '<' || value == '=' || value == '>' || value == '?' || value == '@' || value == '['
86-
|| value == '\\'|| value == ']' || value == '^' || value == '_' || value == '`'
87-
|| value == '{' || value == '|' || value == '}' || value == '~';
88-
}
89-
};
64+
struct xdigit_chars : set<char_range<'A','F'>, char_range<'a','f'>, char_range<'0','9'> > { };
9065

91-
struct digit_chars {
92-
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
93-
return (value >= '0' && value <= '9');
94-
}
95-
};
66+
struct punct_chars
67+
: enumeration<'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', ',', '-',
68+
'.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']',
69+
'^', '_', '`', '{', '|', '}', '~'> {};
9670

97-
struct ascii_chars {
98-
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
99-
return (value >= '\x00' && value <= '\x7F');
100-
}
101-
};
71+
struct digit_chars : char_range<'0','9'> { };
10272

103-
template <auto A, auto B> struct char_range {
104-
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
105-
return (value >= A) && (value <= B);
106-
}
107-
};
108-
template <auto... Cs> struct enumeration {
109-
template <typename CharT> inline static constexpr bool match_char(CharT value) noexcept {
110-
return ((value == Cs) || ... || false);
111-
}
112-
};
73+
struct ascii_chars : char_range<'\x00','\x7F'> { };
11374

11475
}
11576

include/ctre/return_type.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ template <size_t Id, typename Name = void> struct captured_content {
5353
constexpr CTRE_FORCE_INLINE operator bool() const noexcept {
5454
return _matched;
5555
}
56-
56+
57+
constexpr CTRE_FORCE_INLINE auto size() const noexcept {
58+
return static_cast<size_t>(std::distance(_begin, _end));
59+
}
60+
5761
constexpr CTRE_FORCE_INLINE auto to_view() const noexcept {
5862
return std::basic_string_view<char_type>(&*_begin, static_cast<size_t>(std::distance(_begin, _end)));
5963
}
@@ -215,4 +219,4 @@ namespace std {
215219
};
216220
}
217221

218-
#endif
222+
#endif

0 commit comments

Comments
 (0)