Utilities: Replace ctype.h usage with AK/CharacterTypes.h

This commit is contained in:
Sam Atkins 2023-03-09 17:37:16 +00:00 committed by Andreas Kling
parent ee4e9b807a
commit 0f95ff64ed
Notes: sideshowbarker 2024-07-17 08:43:11 +09:00
14 changed files with 51 additions and 41 deletions

View file

@ -4,11 +4,11 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/DeprecatedString.h>
#include <AK/Optional.h>
#include <AK/Vector.h>
#include <LibMain/Main.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
@ -72,7 +72,7 @@ static int handle_size_arguments(size_t& numeric_value, StringView argument)
unsigned suffix_multiplier = 1;
auto suffix = value[value.length() - 1];
switch (tolower(suffix)) {
switch (to_ascii_lowercase(suffix)) {
case 'k':
suffix_multiplier = KiB;
value = value.substring_view(0, value.length() - 1);

View file

@ -6,6 +6,7 @@
*/
#include <AK/Array.h>
#include <AK/CharacterTypes.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
@ -56,7 +57,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
out(" |");
for (auto const& byte : line) {
if (isprint(byte))
if (is_ascii_printable(byte))
putchar(byte);
else
putchar('.');

View file

@ -4,10 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/Optional.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@ -48,7 +48,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Optional<unsigned> number;
if (isalpha(strings[1][1])) {
if (is_ascii_alpha(strings[1][1])) {
int value = getsignalbyname(&strings[1][1]);
if (value >= 0 && value < NSIG)
number = value;

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/DeprecatedString.h>
#include <LibCore/ProcessStatisticsReader.h>
#include <LibCore/System.h>
@ -48,7 +49,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Optional<unsigned> number;
if (isalpha(arguments.argv[1][1])) {
if (is_ascii_alpha(arguments.argv[1][1])) {
int value = getsignalbyname(&arguments.argv[1][1]);
if (value >= 0 && value < NSIG)
number = value;

View file

@ -211,7 +211,7 @@ static int print_escaped(StringView name)
}
for (auto c : name) {
if (isprint(c)) {
if (is_ascii_printable(c)) {
putchar(c);
printed++;
} else {

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/DeprecatedString.h>
#include <AK/GenericLexer.h>
#include <AK/JsonArray.h>
@ -38,8 +39,8 @@ static bool parse_name(StringView name, OpenFile& file)
return true;
} else {
file.type = component1;
auto component2 = lexer.consume_while([](char c) { return isprint(c) && c != '('; });
lexer.ignore_while(isspace);
auto component2 = lexer.consume_while([](char c) { return is_ascii_printable(c) && c != '('; });
lexer.ignore_while(is_ascii_space);
file.name = component2;
if (lexer.tell_remaining() == 0) {

View file

@ -6,6 +6,7 @@
*/
#include <AK/Base64.h>
#include <AK/CharacterTypes.h>
#include <AK/GenericLexer.h>
#include <AK/LexicalPath.h>
#include <AK/MaybeOwned.h>
@ -30,7 +31,7 @@ public:
{
GenericLexer lexer(value);
lexer.ignore_while(isspace);
lexer.ignore_while(is_ascii_space);
if (lexer.consume_specific("inline")) {
m_kind = Kind::Inline;
@ -42,7 +43,7 @@ public:
if (lexer.consume_specific("attachment")) {
m_kind = Kind::Attachment;
if (lexer.consume_specific(";")) {
lexer.ignore_while(isspace);
lexer.ignore_while(is_ascii_space);
if (lexer.consume_specific("filename=")) {
// RFC 2183: "A short (length <= 78 characters)
// parameter value containing only non-`tspecials' characters SHOULD be
@ -64,7 +65,7 @@ public:
if (lexer.consume_specific("form-data")) {
m_kind = Kind::FormData;
while (lexer.consume_specific(";")) {
lexer.ignore_while(isspace);
lexer.ignore_while(is_ascii_space);
if (lexer.consume_specific("name=")) {
m_name = lexer.consume_quoted_string();
} else if (lexer.consume_specific("filename=")) {

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/DeprecatedString.h>
#include <AK/LexicalPath.h>
#include <AK/StringBuilder.h>
@ -338,7 +339,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
out(" Magic: ");
for (char i : StringView { header.e_ident, sizeof(header.e_ident) }) {
if (isprint(i)) {
if (is_ascii_printable(i)) {
out("{:c} ", i);
} else {
out("{:02x} ", i);

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/DeprecatedString.h>
#include <AK/HashMap.h>
#include <AK/QuickSort.h>
@ -13,7 +14,6 @@
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <ctype.h>
struct Line {
StringView key;
@ -74,7 +74,7 @@ static ErrorOr<void> load_file(Options options, StringView filename, Vector<Line
if (options.key_field != 0) {
auto split = (options.separator[0])
? line.split_view(options.separator[0])
: line.split_view(isspace);
: line.split_view(is_ascii_space);
if (options.key_field - 1 >= split.size()) {
key = ""sv;
} else {

View file

@ -7,6 +7,7 @@
#define __USE_MISC
#define TTYDEFCHARS
#include <AK/CharacterTypes.h>
#include <AK/DeprecatedString.h>
#include <AK/Optional.h>
#include <AK/Result.h>
@ -265,9 +266,9 @@ Result<void, int> apply_stty_readable_modes(StringView mode_string, termios& t)
auto parse_hex = [&](StringView v) {
tcflag_t ret = 0;
for (auto c : v) {
c = tolower(c);
c = to_ascii_lowercase(c);
ret *= 16;
if (isdigit(c)) {
if (is_ascii_digit(c)) {
ret += c - '0';
} else {
VERIFY(c >= 'a' && c <= 'f');
@ -310,8 +311,8 @@ Result<void, int> apply_modes(size_t parameter_count, char** raw_parameters, ter
auto looks_like_stty_readable = [&](size_t idx) {
bool contains_colon = false;
for (auto c : parameters[idx]) {
c = tolower(c);
if (!isdigit(c) && !(c >= 'a' && c <= 'f') && c != ':')
c = to_ascii_lowercase(c);
if (!is_ascii_digit(c) && !(c >= 'a' && c <= 'f') && c != ':')
return false;
if (c == ':')
contains_colon = true;
@ -326,7 +327,7 @@ Result<void, int> apply_modes(size_t parameter_count, char** raw_parameters, ter
// We should add the _POSIX_VDISABLE macro.
return 0;
} else if (parameters[idx][0] == '^' && parameters[idx].length() == 2) {
return toupper(parameters[idx][1]) - 0x40;
return to_ascii_uppercase(parameters[idx][1]) - 0x40;
} else if (parameters[idx].starts_with("0x"sv)) {
cc_t value = 0;
if (parameters[idx].length() == 2) {
@ -334,12 +335,12 @@ Result<void, int> apply_modes(size_t parameter_count, char** raw_parameters, ter
return {};
}
for (size_t i = 2; i < parameters[idx].length(); ++i) {
char ch = tolower(parameters[idx][i]);
if (!isdigit(ch) && !(ch >= 'a' && ch <= 'f')) {
char ch = to_ascii_lowercase(parameters[idx][i]);
if (!is_ascii_digit(ch) && !(ch >= 'a' && ch <= 'f')) {
warnln("Invalid hexadecimal character code {}", parameters[idx]);
return {};
}
value = 16 * value + (isdigit(ch)) ? (ch - '0') : (ch - 'a');
value = 16 * value + (is_ascii_digit(ch) ? (ch - '0') : (ch - 'a'));
}
return value;
} else if (parameters[idx].starts_with("0"sv)) {
@ -353,7 +354,7 @@ Result<void, int> apply_modes(size_t parameter_count, char** raw_parameters, ter
value = 8 * value + (ch - '0');
}
return value;
} else if (isdigit(parameters[idx][0])) {
} else if (is_ascii_digit(parameters[idx][0])) {
auto maybe_value = parameters[idx].to_uint<cc_t>();
if (!maybe_value.has_value()) {
warnln("Invalid decimal character code {}", parameters[idx]);
@ -449,7 +450,7 @@ Result<void, int> apply_modes(size_t parameter_count, char** raw_parameters, ter
auto maybe_error = apply_stty_readable_modes(parameters[parameter_idx], t);
if (maybe_error.is_error())
return maybe_error.error();
} else if (isdigit(parameters[parameter_idx][0])) {
} else if (is_ascii_digit(parameters[parameter_idx][0])) {
auto new_baud = parse_baud(parameter_idx);
if (!new_baud.has_value()) {
warnln("Invalid baud rate {}", parameters[parameter_idx]);

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/CheckedFormatString.h>
#include <AK/GenericLexer.h>
#include <AK/Time.h>
@ -60,7 +61,7 @@ static void parse_time(StringView input_time, timespec& atime, timespec& mtime)
err("invalid time format '{}'", input_time);
};
while (!lexer.is_eof() && lexer.next_is(isdigit))
while (!lexer.is_eof() && lexer.next_is(is_ascii_digit))
lex_number();
if (parameters.size() > 6)
err("invalid time format '{}' -- too many parameters", input_time);
@ -131,7 +132,7 @@ static void parse_datetime(StringView input_datetime, timespec& atime, timespec&
millisecond = 0;
if (!lexer.is_eof()) {
if (lexer.consume_specific(',') || lexer.consume_specific('.')) {
auto fractional_second = lexer.consume_while(isdigit);
auto fractional_second = lexer.consume_while(is_ascii_digit);
if (fractional_second.is_empty())
err("invalid datetime format '{}' -- expected floating seconds", input_datetime);
for (u8 i = 0; i < 3 && i < fractional_second.length(); ++i) {

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/DeprecatedString.h>
#include <AK/GenericLexer.h>
#include <AK/Optional.h>
@ -34,29 +35,29 @@ static ErrorOr<DeprecatedString> build_set(StringView specification)
while (!lexer.is_eof()) {
if (lexer.consume_specific("[:alnum:]"sv))
TRY(generate_character_class(isalnum, out));
TRY(generate_character_class(is_ascii_alphanumeric, out));
else if (lexer.consume_specific("[:blank:]"sv))
TRY(generate_character_class(isblank, out));
TRY(generate_character_class(is_ascii_blank, out));
else if (lexer.consume_specific("[:digit:]"sv))
TRY(generate_character_class(isdigit, out));
TRY(generate_character_class(is_ascii_digit, out));
else if (lexer.consume_specific("[:lower:]"sv))
TRY(generate_character_class(islower, out));
TRY(generate_character_class(is_ascii_lower_alpha, out));
else if (lexer.consume_specific("[:punct:]"sv))
TRY(generate_character_class(ispunct, out));
TRY(generate_character_class(is_ascii_punctuation, out));
else if (lexer.consume_specific("[:upper:]"sv))
TRY(generate_character_class(isupper, out));
TRY(generate_character_class(is_ascii_upper_alpha, out));
else if (lexer.consume_specific("[:alpha:]"sv))
TRY(generate_character_class(isalpha, out));
TRY(generate_character_class(is_ascii_alpha, out));
else if (lexer.consume_specific("[:cntrl:]"sv))
TRY(generate_character_class(iscntrl, out));
TRY(generate_character_class(is_ascii_control, out));
else if (lexer.consume_specific("[:graph:]"sv))
TRY(generate_character_class(isgraph, out));
TRY(generate_character_class(is_ascii_graphical, out));
else if (lexer.consume_specific("[:print:]"sv))
TRY(generate_character_class(isprint, out));
TRY(generate_character_class(is_ascii_printable, out));
else if (lexer.consume_specific("[:space:]"sv))
TRY(generate_character_class(isspace, out));
TRY(generate_character_class(is_ascii_space, out));
else if (lexer.consume_specific("[:xdigit:]"sv))
TRY(generate_character_class(isxdigit, out));
TRY(generate_character_class(is_ascii_hex_digit, out));
else if (lexer.consume_specific("\\\\"sv))
TRY(out.try_append('\\'));
else if (lexer.consume_specific("\\a"sv))

View file

@ -7,6 +7,7 @@
*/
#include <AK/Base64.h>
#include <AK/CharacterTypes.h>
#include <AK/DeprecatedString.h>
#include <AK/Random.h>
#include <LibCore/ArgsParser.h>
@ -59,7 +60,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}
// Disallow names starting with _ and -
if (username[0] == '_' || username[0] == '-' || !isalpha(username[0])) {
if (username[0] == '_' || username[0] == '-' || !is_ascii_alpha(username[0])) {
warnln("invalid username, {}", username);
return 1;
}

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/DeprecatedString.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
@ -58,7 +59,7 @@ static Count get_count(DeprecatedString const& file_specifier)
bool start_a_new_word = true;
for (int ch = fgetc(file_pointer); ch != EOF; ch = fgetc(file_pointer)) {
count.bytes++;
if (isspace(ch)) {
if (is_ascii_space(ch)) {
start_a_new_word = true;
if (ch == '\n')
count.lines++;