diff options
author | Max Wipfli <mail@maxwipfli.ch> | 2021-06-01 21:18:08 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-03 13:31:46 +0200 |
commit | bc8d16ad28afb7436bfde1fd0a21faf73d652230 (patch) | |
tree | 7209f27957cc40f9cc1ce27a54f4a670a69a4683 /Userland/Libraries/LibRegex | |
parent | 1c9d87c4558304cf2e955df7e4c49f9f60cd55f2 (diff) | |
download | serenity-bc8d16ad28afb7436bfde1fd0a21faf73d652230.zip |
Everywhere: Replace ctype.h to avoid narrowing conversions
This replaces ctype.h with CharacterType.h everywhere I could find
issues with narrowing conversions. While using it will probably make
sense almost everywhere in the future, the most critical places should
have been addressed.
Diffstat (limited to 'Userland/Libraries/LibRegex')
-rw-r--r-- | Userland/Libraries/LibRegex/RegexByteCode.cpp | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/Userland/Libraries/LibRegex/RegexByteCode.cpp b/Userland/Libraries/LibRegex/RegexByteCode.cpp index 68009575e0..d508851ab0 100644 --- a/Userland/Libraries/LibRegex/RegexByteCode.cpp +++ b/Userland/Libraries/LibRegex/RegexByteCode.cpp @@ -7,10 +7,9 @@ #include "RegexByteCode.h" #include "AK/StringBuilder.h" #include "RegexDebug.h" +#include <AK/CharacterTypes.h> #include <AK/Debug.h> -#include <ctype.h> - namespace regex { const char* OpCode::name(OpCodeId opcode_id) @@ -241,7 +240,7 @@ ALWAYS_INLINE ExecutionResult OpCode_CheckBegin::execute(const MatchInput& input ALWAYS_INLINE ExecutionResult OpCode_CheckBoundary::execute(const MatchInput& input, MatchState& state, MatchOutput&) const { - auto isword = [](auto ch) { return isalnum(ch) || ch == '_'; }; + auto isword = [](auto ch) { return is_ascii_alphanumeric(ch) || ch == '_'; }; auto is_word_boundary = [&] { if (state.string_position == input.view.length()) { if (state.string_position > 0 && isword(input.view[state.string_position - 1])) @@ -510,8 +509,8 @@ ALWAYS_INLINE void OpCode_Compare::compare_char(const MatchInput& input, MatchSt u32 ch2 = input.view[state.string_position]; if (input.regex_options & AllFlags::Insensitive) { - ch1 = tolower(ch1); - ch2 = tolower(ch2); + ch1 = to_ascii_lowercase(ch1); + ch2 = to_ascii_uppercase(ch2); } if (ch1 == ch2) { @@ -551,7 +550,7 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp { switch (character_class) { case CharClass::Alnum: - if (isalnum(ch)) { + if (is_ascii_alphanumeric(ch)) { if (inverse) inverse_matched = true; else @@ -559,11 +558,11 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp } break; case CharClass::Alpha: - if (isalpha(ch)) + if (is_ascii_alpha(ch)) ++state.string_position; break; case CharClass::Blank: - if (ch == ' ' || ch == '\t') { + if (is_ascii_blank(ch)) { if (inverse) inverse_matched = true; else @@ -571,7 +570,7 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp } break; case CharClass::Cntrl: - if (iscntrl(ch)) { + if (is_ascii_control(ch)) { if (inverse) inverse_matched = true; else @@ -579,7 +578,7 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp } break; case CharClass::Digit: - if (isdigit(ch)) { + if (is_ascii_digit(ch)) { if (inverse) inverse_matched = true; else @@ -587,7 +586,7 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp } break; case CharClass::Graph: - if (isgraph(ch)) { + if (is_ascii_graphical(ch)) { if (inverse) inverse_matched = true; else @@ -595,7 +594,7 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp } break; case CharClass::Lower: - if (islower(ch) || ((input.regex_options & AllFlags::Insensitive) && isupper(ch))) { + if (is_ascii_lower_alpha(ch) || ((input.regex_options & AllFlags::Insensitive) && is_ascii_upper_alpha(ch))) { if (inverse) inverse_matched = true; else @@ -603,7 +602,7 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp } break; case CharClass::Print: - if (isprint(ch)) { + if (is_ascii_printable(ch)) { if (inverse) inverse_matched = true; else @@ -611,7 +610,7 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp } break; case CharClass::Punct: - if (ispunct(ch)) { + if (is_ascii_punctuation(ch)) { if (inverse) inverse_matched = true; else @@ -619,7 +618,7 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp } break; case CharClass::Space: - if (isspace(ch)) { + if (is_ascii_space(ch)) { if (inverse) inverse_matched = true; else @@ -627,7 +626,7 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp } break; case CharClass::Upper: - if (isupper(ch) || ((input.regex_options & AllFlags::Insensitive) && islower(ch))) { + if (is_ascii_upper_alpha(ch) || ((input.regex_options & AllFlags::Insensitive) && is_ascii_lower_alpha(ch))) { if (inverse) inverse_matched = true; else @@ -635,7 +634,7 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp } break; case CharClass::Word: - if (isalnum(ch) || ch == '_') { + if (is_ascii_alphanumeric(ch) || ch == '_') { if (inverse) inverse_matched = true; else @@ -643,7 +642,7 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp } break; case CharClass::Xdigit: - if (isxdigit(ch)) { + if (is_ascii_hex_digit(ch)) { if (inverse) inverse_matched = true; else @@ -656,9 +655,9 @@ ALWAYS_INLINE void OpCode_Compare::compare_character_class(const MatchInput& inp ALWAYS_INLINE void OpCode_Compare::compare_character_range(const MatchInput& input, MatchState& state, u32 from, u32 to, u32 ch, bool inverse, bool& inverse_matched) { if (input.regex_options & AllFlags::Insensitive) { - from = tolower(from); - to = tolower(to); - ch = tolower(ch); + from = to_ascii_lowercase(from); + to = to_ascii_lowercase(to); + ch = to_ascii_lowercase(ch); } if (ch >= from && ch <= to) { @@ -689,7 +688,7 @@ const Vector<String> OpCode_Compare::variable_arguments_to_string(Optional<Match if (compare_type == CharacterCompareType::Char) { auto ch = m_bytecode->at(offset++); - auto is_ascii = isascii(ch) && isprint(ch); + auto is_ascii = is_ascii_printable(ch); if (is_ascii) result.empend(String::formatted("value='{:c}'", static_cast<char>(ch))); else |