mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:17:35 +00:00
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.
This commit is contained in:
parent
1c9d87c455
commit
bc8d16ad28
16 changed files with 153 additions and 266 deletions
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
#include "Editor.h"
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/Debug.h>
|
||||
#include <AK/GenericLexer.h>
|
||||
#include <AK/JsonObject.h>
|
||||
|
@ -19,7 +20,6 @@
|
|||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/Notifier.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
|
@ -1338,7 +1338,7 @@ void Editor::refresh_display()
|
|||
auto print_character_at = [this](size_t i) {
|
||||
StringBuilder builder;
|
||||
auto c = m_buffer[i];
|
||||
bool should_print_masked = isascii(c) && iscntrl(c) && c != '\n';
|
||||
bool should_print_masked = is_ascii_control(c) && c != '\n';
|
||||
bool should_print_caret = c < 64 && should_print_masked;
|
||||
if (should_print_caret)
|
||||
builder.appendff("^{:c}", c + 64);
|
||||
|
@ -1722,7 +1722,7 @@ Editor::VTState Editor::actual_rendered_string_length_step(StringMetrics& metric
|
|||
current_line.length = 0;
|
||||
return state;
|
||||
}
|
||||
if (isascii(c) && iscntrl(c) && c != '\n')
|
||||
if (is_ascii_control(c) && c != '\n')
|
||||
current_line.masked_chars.append({ index, 1, c < 64 ? 2u : 4u }); // if the character cannot be represented as ^c, represent it as \xbb.
|
||||
// FIXME: This will not support anything sophisticated
|
||||
++current_line.length;
|
||||
|
@ -1740,7 +1740,7 @@ Editor::VTState Editor::actual_rendered_string_length_step(StringMetrics& metric
|
|||
// FIXME: This does not support non-VT (aside from set-title) escapes
|
||||
return state;
|
||||
case Bracket:
|
||||
if (isdigit(c)) {
|
||||
if (is_ascii_digit(c)) {
|
||||
return BracketArgsSemi;
|
||||
}
|
||||
return state;
|
||||
|
@ -1748,7 +1748,7 @@ Editor::VTState Editor::actual_rendered_string_length_step(StringMetrics& metric
|
|||
if (c == ';') {
|
||||
return Bracket;
|
||||
}
|
||||
if (!isdigit(c))
|
||||
if (!is_ascii_digit(c))
|
||||
state = Free;
|
||||
return state;
|
||||
case Title:
|
||||
|
@ -1848,7 +1848,7 @@ Vector<size_t, 2> Editor::vt_dsr()
|
|||
m_incomplete_data.append(c);
|
||||
continue;
|
||||
case SawBracket:
|
||||
if (isdigit(c)) {
|
||||
if (is_ascii_digit(c)) {
|
||||
state = InFirstCoordinate;
|
||||
coordinate_buffer.append(c);
|
||||
continue;
|
||||
|
@ -1856,7 +1856,7 @@ Vector<size_t, 2> Editor::vt_dsr()
|
|||
m_incomplete_data.append(c);
|
||||
continue;
|
||||
case InFirstCoordinate:
|
||||
if (isdigit(c)) {
|
||||
if (is_ascii_digit(c)) {
|
||||
coordinate_buffer.append(c);
|
||||
continue;
|
||||
}
|
||||
|
@ -1872,7 +1872,7 @@ Vector<size_t, 2> Editor::vt_dsr()
|
|||
m_incomplete_data.append(c);
|
||||
continue;
|
||||
case SawSemicolon:
|
||||
if (isdigit(c)) {
|
||||
if (is_ascii_digit(c)) {
|
||||
state = InSecondCoordinate;
|
||||
coordinate_buffer.append(c);
|
||||
continue;
|
||||
|
@ -1880,7 +1880,7 @@ Vector<size_t, 2> Editor::vt_dsr()
|
|||
m_incomplete_data.append(c);
|
||||
continue;
|
||||
case InSecondCoordinate:
|
||||
if (isdigit(c)) {
|
||||
if (is_ascii_digit(c)) {
|
||||
coordinate_buffer.append(c);
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/FileStream.h>
|
||||
#include <AK/ScopeGuard.h>
|
||||
#include <AK/ScopedValueRollback.h>
|
||||
|
@ -11,7 +12,6 @@
|
|||
#include <AK/TemporaryChange.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibLine/Editor.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
@ -84,7 +84,7 @@ void Editor::cursor_left_word()
|
|||
for (;;) {
|
||||
if (m_cursor == 0)
|
||||
break;
|
||||
if (skipped_at_least_one_character && !isalnum(m_buffer[m_cursor - 1])) // stop *after* a non-alnum, but only if it changes the position
|
||||
if (skipped_at_least_one_character && !is_ascii_alphanumeric(m_buffer[m_cursor - 1])) // stop *after* a non-alnum, but only if it changes the position
|
||||
break;
|
||||
skipped_at_least_one_character = true;
|
||||
--m_cursor;
|
||||
|
@ -109,7 +109,7 @@ void Editor::cursor_right_word()
|
|||
for (;;) {
|
||||
if (m_cursor >= m_buffer.size())
|
||||
break;
|
||||
if (!isalnum(m_buffer[++m_cursor]))
|
||||
if (!is_ascii_alphanumeric(m_buffer[++m_cursor]))
|
||||
break;
|
||||
}
|
||||
m_buffer.take_last();
|
||||
|
@ -178,7 +178,7 @@ void Editor::erase_word_backwards()
|
|||
// A word here is space-separated. `foo=bar baz` is two words.
|
||||
bool has_seen_nonspace = false;
|
||||
while (m_cursor > 0) {
|
||||
if (isspace(m_buffer[m_cursor - 1])) {
|
||||
if (is_ascii_space(m_buffer[m_cursor - 1])) {
|
||||
if (has_seen_nonspace)
|
||||
break;
|
||||
} else {
|
||||
|
@ -375,27 +375,27 @@ void Editor::transpose_words()
|
|||
|
||||
// Move to end of word under (or after) caret.
|
||||
size_t cursor = m_cursor;
|
||||
while (cursor < m_buffer.size() && !isalnum(m_buffer[cursor]))
|
||||
while (cursor < m_buffer.size() && !is_ascii_alphanumeric(m_buffer[cursor]))
|
||||
++cursor;
|
||||
while (cursor < m_buffer.size() && isalnum(m_buffer[cursor]))
|
||||
while (cursor < m_buffer.size() && is_ascii_alphanumeric(m_buffer[cursor]))
|
||||
++cursor;
|
||||
|
||||
// Move left over second word and the space to its right.
|
||||
size_t end = cursor;
|
||||
size_t start = cursor;
|
||||
while (start > 0 && !isalnum(m_buffer[start - 1]))
|
||||
while (start > 0 && !is_ascii_alphanumeric(m_buffer[start - 1]))
|
||||
--start;
|
||||
while (start > 0 && isalnum(m_buffer[start - 1]))
|
||||
while (start > 0 && is_ascii_alphanumeric(m_buffer[start - 1]))
|
||||
--start;
|
||||
size_t start_second_word = start;
|
||||
|
||||
// Move left over space between the two words.
|
||||
while (start > 0 && !isalnum(m_buffer[start - 1]))
|
||||
while (start > 0 && !is_ascii_alphanumeric(m_buffer[start - 1]))
|
||||
--start;
|
||||
size_t start_gap = start;
|
||||
|
||||
// Move left over first word.
|
||||
while (start > 0 && isalnum(m_buffer[start - 1]))
|
||||
while (start > 0 && is_ascii_alphanumeric(m_buffer[start - 1]))
|
||||
--start;
|
||||
|
||||
if (start != start_gap) {
|
||||
|
@ -452,7 +452,7 @@ void Editor::erase_alnum_word_backwards()
|
|||
// A word here is contiguous alnums. `foo=bar baz` is three words.
|
||||
bool has_seen_alnum = false;
|
||||
while (m_cursor > 0) {
|
||||
if (!isalnum(m_buffer[m_cursor - 1])) {
|
||||
if (!is_ascii_alphanumeric(m_buffer[m_cursor - 1])) {
|
||||
if (has_seen_alnum)
|
||||
break;
|
||||
} else {
|
||||
|
@ -467,7 +467,7 @@ void Editor::erase_alnum_word_forwards()
|
|||
// A word here is contiguous alnums. `foo=bar baz` is three words.
|
||||
bool has_seen_alnum = false;
|
||||
while (m_cursor < m_buffer.size()) {
|
||||
if (!isalnum(m_buffer[m_cursor])) {
|
||||
if (!is_ascii_alphanumeric(m_buffer[m_cursor])) {
|
||||
if (has_seen_alnum)
|
||||
break;
|
||||
} else {
|
||||
|
@ -480,15 +480,15 @@ void Editor::erase_alnum_word_forwards()
|
|||
void Editor::case_change_word(Editor::CaseChangeOp change_op)
|
||||
{
|
||||
// A word here is contiguous alnums. `foo=bar baz` is three words.
|
||||
while (m_cursor < m_buffer.size() && !isalnum(m_buffer[m_cursor]))
|
||||
while (m_cursor < m_buffer.size() && !is_ascii_alphanumeric(m_buffer[m_cursor]))
|
||||
++m_cursor;
|
||||
size_t start = m_cursor;
|
||||
while (m_cursor < m_buffer.size() && isalnum(m_buffer[m_cursor])) {
|
||||
while (m_cursor < m_buffer.size() && is_ascii_alphanumeric(m_buffer[m_cursor])) {
|
||||
if (change_op == CaseChangeOp::Uppercase || (change_op == CaseChangeOp::Capital && m_cursor == start)) {
|
||||
m_buffer[m_cursor] = toupper(m_buffer[m_cursor]);
|
||||
m_buffer[m_cursor] = to_ascii_uppercase(m_buffer[m_cursor]);
|
||||
} else {
|
||||
VERIFY(change_op == CaseChangeOp::Lowercase || (change_op == CaseChangeOp::Capital && m_cursor > start));
|
||||
m_buffer[m_cursor] = tolower(m_buffer[m_cursor]);
|
||||
m_buffer[m_cursor] = to_ascii_lowercase(m_buffer[m_cursor]);
|
||||
}
|
||||
++m_cursor;
|
||||
m_refresh_needed = true;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue