Skip to content

Commit 7de344d

Browse files
authored
Merge pull request #25 from leni536/refactor_atom_characters2
Refactor atoms_characters.hpp
2 parents 715364e + 6e7922c commit 7de344d

File tree

1 file changed

+15
-52
lines changed

1 file changed

+15
-52
lines changed

include/ctre/atoms_characters.hpp

+15-52
Original file line numberDiff line numberDiff line change
@@ -39,75 +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-
struct word_chars {
50+
template <auto A, auto B> struct char_range {
4951
template <typename CharT> CTRE_FORCE_INLINE static constexpr bool match_char(CharT value) noexcept {
50-
return (value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z') || (value >= '0' && value <= '9') || (value == '_');
52+
return (value >= A) && (value <= B);
5153
}
5254
};
5355

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

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

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

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

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

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

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

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

11275
}
11376

0 commit comments

Comments
 (0)