From 6f4c380d95429ef4615f0a9f40d6fec9e1469764 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 9 Dec 2019 17:45:40 +0100 Subject: [PATCH] AK: Use size_t for the length of strings Using int was a mistake. This patch changes String, StringImpl, StringView and StringBuilder to use size_t instead of int for lengths. Obviously a lot of code needs to change as a result of this. --- AK/BufferStream.h | 8 +- AK/JsonParser.cpp | 4 +- AK/JsonParser.h | 2 +- AK/String.cpp | 28 +-- AK/String.h | 14 +- AK/StringBuilder.cpp | 12 +- AK/StringBuilder.h | 12 +- AK/StringImpl.cpp | 18 +- AK/StringImpl.h | 18 +- AK/StringView.cpp | 28 +-- AK/StringView.h | 14 +- AK/Tests/TestString.cpp | 4 +- AK/URL.cpp | 2 +- AK/Utf8View.cpp | 2 +- Applications/Help/main.cpp | 4 +- Applications/Welcome/TextWidget.cpp | 14 +- DevTools/HackStudio/CppLexer.cpp | 4 +- DevTools/HackStudio/CppLexer.h | 8 +- Kernel/FileSystem/FileSystem.cpp | 2 +- Kernel/FileSystem/FileSystem.h | 4 +- Kernel/FileSystem/ProcFS.cpp | 8 +- Kernel/Net/LocalSocket.cpp | 2 +- Kernel/Process.cpp | 26 ++- Libraries/LibC/netdb.cpp | 4 +- Libraries/LibCore/CHttpJob.cpp | 2 +- .../LibCore/CProcessStatisticsReader.cpp | 2 +- Libraries/LibDraw/Font.cpp | 2 +- Libraries/LibGUI/GTextDocument.cpp | 91 +++++---- Libraries/LibGUI/GTextDocument.h | 30 +-- Libraries/LibGUI/GTextEditor.cpp | 184 +++++++++--------- Libraries/LibGUI/GTextEditor.h | 30 +-- Libraries/LibGUI/GTextPosition.h | 18 +- Libraries/LibGUI/GWindow.cpp | 2 +- Libraries/LibGUI/GWindow.h | 2 +- Libraries/LibHTML/CSS/StyleResolver.cpp | 8 +- Libraries/LibHTML/DOMTreeModel.cpp | 2 +- Libraries/LibHTML/Layout/LayoutText.cpp | 2 +- Libraries/LibHTML/Parser/CSSParser.cpp | 10 +- Libraries/LibHTML/Parser/HTMLParser.cpp | 6 +- Libraries/LibMarkdown/MDCodeBlock.cpp | 2 +- Libraries/LibMarkdown/MDHeading.cpp | 8 +- Libraries/LibMarkdown/MDList.cpp | 4 +- Libraries/LibMarkdown/MDText.cpp | 10 +- Libraries/LibPCIDB/Database.cpp | 4 +- Libraries/LibVT/Terminal.cpp | 2 +- Servers/TelnetServer/Client.cpp | 11 +- Servers/TelnetServer/Parser.cpp | 2 +- Shell/LineEditor.cpp | 54 ++--- Shell/LineEditor.h | 4 +- Shell/Parser.cpp | 4 +- Shell/main.cpp | 10 +- Userland/ls.cpp | 12 +- Userland/man.cpp | 2 +- Userland/md.cpp | 2 +- 54 files changed, 387 insertions(+), 377 deletions(-) diff --git a/AK/BufferStream.h b/AK/BufferStream.h index 66c532b68bb..ed839334b37 100644 --- a/AK/BufferStream.h +++ b/AK/BufferStream.h @@ -148,6 +148,12 @@ public: m_buffer[m_offset++] = (u8)(value >> 24); return *this; } + + BufferStream& operator<<(size_t value) + { + return *this << (u32)value; + } + BufferStream& operator>>(u32& value) { if (m_offset + sizeof(value) > unsigned(m_buffer.size())) { @@ -200,7 +206,7 @@ public: BufferStream& operator<<(const StringView& value) { - for (ssize_t i = 0; i < value.length(); ++i) + for (size_t i = 0; i < value.length(); ++i) m_buffer[m_offset++] = value[i]; return *this; } diff --git a/AK/JsonParser.cpp b/AK/JsonParser.cpp index 41b3c7a2b1e..774def6e4c7 100644 --- a/AK/JsonParser.cpp +++ b/AK/JsonParser.cpp @@ -47,7 +47,7 @@ String JsonParser::consume_quoted_string() Vector buffer; for (;;) { - int peek_index = m_index; + size_t peek_index = m_index; char ch = 0; for (;;) { if (peek_index == m_input.length()) @@ -104,7 +104,7 @@ String JsonParser::consume_quoted_string() return String::empty(); auto& last_string_starting_with_character = m_last_string_starting_with_character[(int)buffer.first()]; - if (last_string_starting_with_character.length() == buffer.size()) { + if (last_string_starting_with_character.length() == (size_t)buffer.size()) { if (!memcmp(last_string_starting_with_character.characters(), buffer.data(), buffer.size())) return last_string_starting_with_character; } diff --git a/AK/JsonParser.h b/AK/JsonParser.h index 7a65d1c6380..a1bd0bd1ac7 100644 --- a/AK/JsonParser.h +++ b/AK/JsonParser.h @@ -36,7 +36,7 @@ private: void consume_while(C); StringView m_input; - int m_index { 0 }; + size_t m_index { 0 }; String m_last_string_starting_with_character[256]; }; diff --git a/AK/String.cpp b/AK/String.cpp index 0b97b0da833..9ddc833dcdc 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -76,7 +76,7 @@ String String::isolated_copy() const return String(move(*impl)); } -String String::substring(int start, int length) const +String String::substring(size_t start, size_t length) const { if (!length) return {}; @@ -86,7 +86,7 @@ String String::substring(int start, int length) const return { characters() + start, length }; } -StringView String::substring_view(int start, int length) const +StringView String::substring_view(size_t start, size_t length) const { if (!length) return {}; @@ -101,23 +101,23 @@ Vector String::split(const char separator) const return split_limit(separator, 0); } -Vector String::split_limit(const char separator, int limit) const +Vector String::split_limit(const char separator, size_t limit) const { if (is_empty()) return {}; Vector v; - int substart = 0; - for (int i = 0; i < length() && (v.size() + 1) != limit; ++i) { + size_t substart = 0; + for (size_t i = 0; i < length() && ((size_t)v.size() + 1) != limit; ++i) { char ch = characters()[i]; if (ch == separator) { - int sublen = i - substart; + size_t sublen = i - substart; if (sublen != 0) v.append(substring(substart, sublen)); substart = i + 1; } } - int taillen = length() - substart; + size_t taillen = length() - substart; if (taillen != 0) v.append(substring(substart, taillen)); if (characters()[length() - 1] == separator) @@ -131,17 +131,17 @@ Vector String::split_view(const char separator, bool keep_empty) con return {}; Vector v; - int substart = 0; - for (int i = 0; i < length(); ++i) { + size_t substart = 0; + for (size_t i = 0; i < length(); ++i) { char ch = characters()[i]; if (ch == separator) { - int sublen = i - substart; + size_t sublen = i - substart; if (sublen != 0 || keep_empty) v.append(substring_view(substart, sublen)); substart = i + 1; } } - int taillen = length() - substart; + size_t taillen = length() - substart; if (taillen != 0 || keep_empty) v.append(substring_view(substart, taillen)); if (characters()[length() - 1] == separator && keep_empty) @@ -160,7 +160,7 @@ int String::to_int(bool& ok) const { bool negative = false; int value = 0; - int i = 0; + size_t i = 0; if (is_empty()) { ok = false; @@ -187,7 +187,7 @@ int String::to_int(bool& ok) const unsigned String::to_uint(bool& ok) const { unsigned value = 0; - for (int i = 0; i < length(); ++i) { + for (size_t i = 0; i < length(); ++i) { if (characters()[i] < '0' || characters()[i] > '9') { ok = false; return 0; @@ -250,7 +250,7 @@ bool String::ends_with(const StringView& str) const return !memcmp(characters() + (length() - str.length()), str.characters_without_null_termination(), str.length()); } -String String::repeated(char ch, int count) +String String::repeated(char ch, size_t count) { if (!count) return empty(); diff --git a/AK/String.h b/AK/String.h index 486d2e381ef..87842d923d2 100755 --- a/AK/String.h +++ b/AK/String.h @@ -59,7 +59,7 @@ public: { } - String(const char* cstring, int length, ShouldChomp shouldChomp = NoChomp) + String(const char* cstring, size_t length, ShouldChomp shouldChomp = NoChomp) : m_impl(StringImpl::create(cstring, length, shouldChomp)) { } @@ -89,7 +89,7 @@ public: CaseSensitive, }; - static String repeated(char, int count); + static String repeated(char, size_t count); bool matches(const StringView& pattern, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; // FIXME: These should be shared between String and StringView somehow! @@ -112,18 +112,18 @@ public: bool contains(const String&) const; - Vector split_limit(char separator, int limit) const; + Vector split_limit(char separator, size_t limit) const; Vector split(char separator) const; - String substring(int start, int length) const; + String substring(size_t start, size_t length) const; Vector split_view(char separator, bool keep_empty = false) const; - StringView substring_view(int start, int length) const; + StringView substring_view(size_t start, size_t length) const; bool is_null() const { return !m_impl; } bool is_empty() const { return length() == 0; } - int length() const { return m_impl ? m_impl->length() : 0; } + size_t length() const { return m_impl ? m_impl->length() : 0; } const char* characters() const { return m_impl ? m_impl->characters() : nullptr; } - char operator[](int i) const + char operator[](size_t i) const { ASSERT(m_impl); return (*m_impl)[i]; diff --git a/AK/StringBuilder.cpp b/AK/StringBuilder.cpp index 54100029ffa..b7f4767d5b0 100644 --- a/AK/StringBuilder.cpp +++ b/AK/StringBuilder.cpp @@ -5,15 +5,15 @@ namespace AK { -inline void StringBuilder::will_append(int size) +inline void StringBuilder::will_append(size_t size) { - if ((m_length + size) > m_buffer.size()) - m_buffer.grow(max((int)16, m_buffer.size() * 2 + size)); + if ((m_length + size) > (size_t)m_buffer.size()) + m_buffer.grow(max((size_t)16, (size_t)m_buffer.size() * 2 + size)); } -StringBuilder::StringBuilder(int initial_capacity) +StringBuilder::StringBuilder(size_t initial_capacity) { - m_buffer.grow(initial_capacity); + m_buffer.grow((int)initial_capacity); } void StringBuilder::append(const StringView& str) @@ -25,7 +25,7 @@ void StringBuilder::append(const StringView& str) m_length += str.length(); } -void StringBuilder::append(const char* characters, int length) +void StringBuilder::append(const char* characters, size_t length) { if (!length) return; diff --git a/AK/StringBuilder.h b/AK/StringBuilder.h index 9c4694bfdfc..fc2a25dbfa0 100644 --- a/AK/StringBuilder.h +++ b/AK/StringBuilder.h @@ -10,12 +10,12 @@ class StringBuilder { public: using OutputType = String; - explicit StringBuilder(int initial_capacity = 16); + explicit StringBuilder(size_t initial_capacity = 16); ~StringBuilder() {} void append(const StringView&); void append(char); - void append(const char*, int); + void append(const char*, size_t); void appendf(const char*, ...); void appendvf(const char*, va_list); @@ -27,14 +27,14 @@ public: StringView string_view() const; void clear(); - int length() const { return m_length; } - void trim(int count) { m_length -= count; } + size_t length() const { return m_length; } + void trim(size_t count) { m_length -= count; } private: - void will_append(int); + void will_append(size_t); ByteBuffer m_buffer; - int m_length { 0 }; + size_t m_length { 0 }; }; } diff --git a/AK/StringImpl.cpp b/AK/StringImpl.cpp index 5e8c6d9090c..1f67d0136b6 100644 --- a/AK/StringImpl.cpp +++ b/AK/StringImpl.cpp @@ -17,7 +17,7 @@ void dump_all_stringimpls() { unsigned i = 0; for (auto& it : *g_all_live_stringimpls) { - dbgprintf("%u: \"%s\"\n", i, (*it).characters()); + dbgprsize_tf("%u: \"%s\"\n", i, (*it).characters()); ++i; } } @@ -36,7 +36,7 @@ StringImpl& StringImpl::the_empty_stringimpl() return *s_the_empty_stringimpl; } -StringImpl::StringImpl(ConstructWithInlineBufferTag, int length) +StringImpl::StringImpl(ConstructWithInlineBufferTag, size_t length) : m_length(length) { #ifdef DEBUG_STRINGIMPL @@ -55,12 +55,12 @@ StringImpl::~StringImpl() #endif } -static inline int allocation_size_for_stringimpl(int length) +static inline size_t allocation_size_for_stringimpl(size_t length) { return sizeof(StringImpl) + (sizeof(char) * length) + sizeof(char); } -NonnullRefPtr StringImpl::create_uninitialized(int length, char*& buffer) +NonnullRefPtr StringImpl::create_uninitialized(size_t length, char*& buffer) { ASSERT(length); void* slot = kmalloc(allocation_size_for_stringimpl(length)); @@ -71,7 +71,7 @@ NonnullRefPtr StringImpl::create_uninitialized(int length, char*& bu return new_stringimpl; } -RefPtr StringImpl::create(const char* cstring, int length, ShouldChomp should_chomp) +RefPtr StringImpl::create(const char* cstring, size_t length, ShouldChomp should_chomp) { if (!cstring) return nullptr; @@ -133,7 +133,7 @@ static inline char to_ascii_uppercase(char c) NonnullRefPtr StringImpl::to_lowercase() const { - for (int i = 0; i < m_length; ++i) { + for (size_t i = 0; i < m_length; ++i) { if (!is_ascii_lowercase(characters()[i])) goto slow_path; } @@ -142,14 +142,14 @@ NonnullRefPtr StringImpl::to_lowercase() const slow_path: char* buffer; auto lowercased = create_uninitialized(m_length, buffer); - for (int i = 0; i < m_length; ++i) + for (size_t i = 0; i < m_length; ++i) buffer[i] = to_ascii_lowercase(characters()[i]); return lowercased; } NonnullRefPtr StringImpl::to_uppercase() const { - for (int i = 0; i < m_length; ++i) { + for (size_t i = 0; i < m_length; ++i) { if (!is_ascii_uppercase(characters()[i])) goto slow_path; } @@ -158,7 +158,7 @@ NonnullRefPtr StringImpl::to_uppercase() const slow_path: char* buffer; auto uppercased = create_uninitialized(m_length, buffer); - for (int i = 0; i < m_length; ++i) + for (size_t i = 0; i < m_length; ++i) buffer[i] = to_ascii_uppercase(characters()[i]); return uppercased; } diff --git a/AK/StringImpl.h b/AK/StringImpl.h index b2b085f81db..bcc825650b1 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -14,9 +14,9 @@ enum ShouldChomp { class StringImpl : public RefCounted { public: - static NonnullRefPtr create_uninitialized(int length, char*& buffer); + static NonnullRefPtr create_uninitialized(size_t length, char*& buffer); static RefPtr create(const char* cstring, ShouldChomp = NoChomp); - static RefPtr create(const char* cstring, int length, ShouldChomp = NoChomp); + static RefPtr create(const char* cstring, size_t length, ShouldChomp = NoChomp); NonnullRefPtr to_lowercase() const; NonnullRefPtr to_uppercase() const; @@ -29,11 +29,11 @@ public: ~StringImpl(); - int length() const { return m_length; } + size_t length() const { return m_length; } const char* characters() const { return &m_inline_buffer[0]; } - char operator[](int i) const + char operator[](size_t i) const { - ASSERT(i >= 0 && i < m_length); + ASSERT(i < m_length); return characters()[i]; } @@ -56,20 +56,20 @@ private: enum ConstructWithInlineBufferTag { ConstructWithInlineBuffer }; - StringImpl(ConstructWithInlineBufferTag, int length); + StringImpl(ConstructWithInlineBufferTag, size_t length); void compute_hash() const; - int m_length { 0 }; + size_t m_length { 0 }; mutable unsigned m_hash { 0 }; mutable bool m_has_hash { false }; char m_inline_buffer[0]; }; -inline constexpr u32 string_hash(const char* characters, int length) +inline constexpr u32 string_hash(const char* characters, size_t length) { u32 hash = 0; - for (int i = 0; i < length; ++i) { + for (size_t i = 0; i < length; ++i) { hash += (u32)characters[i]; hash += (hash << 10); hash ^= (hash >> 6); diff --git a/AK/StringView.cpp b/AK/StringView.cpp index 8e9a2aa41ea..12f39049e1e 100644 --- a/AK/StringView.cpp +++ b/AK/StringView.cpp @@ -12,7 +12,7 @@ StringView::StringView(const String& string) StringView::StringView(const ByteBuffer& buffer) : m_characters((const char*)buffer.data()) - , m_length(buffer.size()) + , m_length((size_t)buffer.size()) { } @@ -22,17 +22,17 @@ Vector StringView::split_view(const char separator, bool keep_empty) return {}; Vector v; - ssize_t substart = 0; - for (ssize_t i = 0; i < length(); ++i) { + size_t substart = 0; + for (size_t i = 0; i < length(); ++i) { char ch = characters_without_null_termination()[i]; if (ch == separator) { - ssize_t sublen = i - substart; + size_t sublen = i - substart; if (sublen != 0 || keep_empty) v.append(substring_view(substart, sublen)); substart = i + 1; } } - ssize_t taillen = length() - substart; + size_t taillen = length() - substart; if (taillen != 0 || keep_empty) v.append(substring_view(substart, taillen)); if (characters_without_null_termination()[length() - 1] == separator && keep_empty) @@ -49,10 +49,10 @@ Vector StringView::lines(bool consider_cr) const return split_view('\n', true); Vector v; - ssize_t substart = 0; + size_t substart = 0; bool last_ch_was_cr = false; bool split_view = false; - for (ssize_t i = 0; i < length(); ++i) { + for (size_t i = 0; i < length(); ++i) { char ch = characters_without_null_termination()[i]; if (ch == '\n') { split_view = true; @@ -67,13 +67,13 @@ Vector StringView::lines(bool consider_cr) const last_ch_was_cr = true; } if (split_view) { - ssize_t sublen = i - substart; + size_t sublen = i - substart; v.append(substring_view(substart, sublen)); substart = i + 1; } split_view = false; } - ssize_t taillen = length() - substart; + size_t taillen = length() - substart; if (taillen != 0) v.append(substring_view(substart, taillen)); return v; @@ -92,7 +92,7 @@ bool StringView::starts_with(const StringView& str) const return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length()); } -StringView StringView::substring_view(int start, int length) const +StringView StringView::substring_view(size_t start, size_t length) const { if (!length) return {}; @@ -105,7 +105,7 @@ StringView StringView::substring_view_starting_from_substring(const StringView& const char* remaining_characters = substring.characters_without_null_termination(); ASSERT(remaining_characters >= m_characters); ASSERT(remaining_characters <= m_characters + m_length); - int remaining_length = m_length - (remaining_characters - m_characters); + size_t remaining_length = m_length - (remaining_characters - m_characters); return { remaining_characters, remaining_length }; } @@ -114,7 +114,7 @@ StringView StringView::substring_view_starting_after_substring(const StringView& const char* remaining_characters = substring.characters_without_null_termination() + substring.length(); ASSERT(remaining_characters >= m_characters); ASSERT(remaining_characters <= m_characters + m_length); - int remaining_length = m_length - (remaining_characters - m_characters); + size_t remaining_length = m_length - (remaining_characters - m_characters); return { remaining_characters, remaining_length }; } @@ -122,7 +122,7 @@ int StringView::to_int(bool& ok) const { bool negative = false; int value = 0; - int i = 0; + size_t i = 0; if (is_empty()) { ok = false; @@ -149,7 +149,7 @@ int StringView::to_int(bool& ok) const unsigned StringView::to_uint(bool& ok) const { unsigned value = 0; - for (ssize_t i = 0; i < length(); ++i) { + for (size_t i = 0; i < length(); ++i) { if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') { ok = false; return 0; diff --git a/AK/StringView.h b/AK/StringView.h index 9e558947f0e..e8cdbbbd450 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -11,12 +11,12 @@ class StringImpl; class StringView { public: StringView() {} - StringView(const char* characters, int length) + StringView(const char* characters, size_t length) : m_characters(characters) , m_length(length) { } - StringView(const unsigned char* characters, int length) + StringView(const unsigned char* characters, size_t length) : m_characters((const char*)characters) , m_length(length) { @@ -36,14 +36,14 @@ public: bool is_null() const { return !m_characters; } bool is_empty() const { return m_length == 0; } const char* characters_without_null_termination() const { return m_characters; } - int length() const { return m_length; } - char operator[](int index) const { return m_characters[index]; } + size_t length() const { return m_length; } + char operator[](size_t index) const { return m_characters[index]; } unsigned hash() const; bool starts_with(const StringView&) const; - StringView substring_view(int start, int length) const; + StringView substring_view(size_t start, size_t length) const; Vector split_view(char, bool keep_empty = false) const; // Create a Vector of StringViews split by line endings. As of CommonMark @@ -81,7 +81,7 @@ public: return !cstring; if (!cstring) return false; - int other_length = strlen(cstring); + size_t other_length = strlen(cstring); if (m_length != other_length) return false; return !memcmp(m_characters, cstring, m_length); @@ -113,7 +113,7 @@ private: friend class String; const StringImpl* m_impl { nullptr }; const char* m_characters { nullptr }; - int m_length { 0 }; + size_t m_length { 0 }; }; } diff --git a/AK/Tests/TestString.cpp b/AK/Tests/TestString.cpp index 9a427d302e9..3ae8eb5c554 100644 --- a/AK/Tests/TestString.cpp +++ b/AK/Tests/TestString.cpp @@ -20,8 +20,8 @@ TEST_CASE(construct_contents) String test_string = "ABCDEF"; EXPECT(!test_string.is_empty()); EXPECT(!test_string.is_null()); - EXPECT_EQ(test_string.length(), 6); - EXPECT_EQ(test_string.length(), (int)strlen(test_string.characters())); + EXPECT_EQ(test_string.length(), 6u); + EXPECT_EQ(test_string.length(), strlen(test_string.characters())); EXPECT(test_string.characters() != nullptr); EXPECT(!strcmp(test_string.characters(), "ABCDEF")); diff --git a/AK/URL.cpp b/AK/URL.cpp index 96c7afe202a..a78ce8a9b49 100644 --- a/AK/URL.cpp +++ b/AK/URL.cpp @@ -31,7 +31,7 @@ bool URL::parse(const StringView& string) Vector buffer; State state { State::InProtocol }; - int index = 0; + size_t index = 0; auto peek = [&] { if (index >= string.length()) diff --git a/AK/Utf8View.cpp b/AK/Utf8View.cpp index d5b73f4319f..e4e42371436 100644 --- a/AK/Utf8View.cpp +++ b/AK/Utf8View.cpp @@ -30,7 +30,7 @@ const unsigned char* Utf8View::end_ptr() const Utf8CodepointIterator Utf8View::begin() const { - return { begin_ptr(), m_string.length() }; + return { begin_ptr(), (int)m_string.length() }; } Utf8CodepointIterator Utf8View::end() const diff --git a/Applications/Help/main.cpp b/Applications/Help/main.cpp index c21907ad7cd..391bb734014 100644 --- a/Applications/Help/main.cpp +++ b/Applications/Help/main.cpp @@ -56,7 +56,7 @@ int main(int argc, char* argv[]) go_forward_action->set_enabled(history.can_go_forward()); }; - auto open_page = [&](String path) { + auto open_page = [&](const String& path) { if (path.is_null()) { html_view->set_document(nullptr); return; @@ -73,7 +73,7 @@ int main(int argc, char* argv[]) return; } auto buffer = file->read_all(); - StringView source { (char*)buffer.data(), buffer.size() }; + StringView source { (const char*)buffer.data(), (size_t)buffer.size() }; MDDocument md_document; bool success = md_document.parse(source); diff --git a/Applications/Welcome/TextWidget.cpp b/Applications/Welcome/TextWidget.cpp index 3ca2d0d07d8..6da59a21638 100644 --- a/Applications/Welcome/TextWidget.cpp +++ b/Applications/Welcome/TextWidget.cpp @@ -66,20 +66,20 @@ void TextWidget::resize_event(GResizeEvent& event) void TextWidget::wrap_and_set_height() { Vector words; - int start = -1; - for (int i = 0; i < m_text.length(); i++) { + Optional start; + for (size_t i = 0; i < m_text.length(); i++) { auto ch = m_text[i]; if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { - if (start != -1) - words.append(m_text.substring(start, i - start)); + if (start.has_value()) + words.append(m_text.substring(start.value(), i - start.value())); start = -1; - } else if (start == -1) { + } else if (!start.has_value()) { start = i; } } - if (start != -1) - words.append(m_text.substring(start, m_text.length() - start)); + if (start.has_value()) + words.append(m_text.substring(start, m_text.length() - start.value())); auto rect = frame_inner_rect(); if (frame_thickness() > 0) diff --git a/DevTools/HackStudio/CppLexer.cpp b/DevTools/HackStudio/CppLexer.cpp index e92bdfb4880..bfcad0cf5dd 100644 --- a/DevTools/HackStudio/CppLexer.cpp +++ b/DevTools/HackStudio/CppLexer.cpp @@ -8,7 +8,7 @@ CppLexer::CppLexer(const StringView& input) { } -char CppLexer::peek(int offset) const +char CppLexer::peek(size_t offset) const { if ((m_index + offset) >= m_input.length()) return 0; @@ -191,7 +191,7 @@ Vector CppLexer::lex() { Vector tokens; - int token_start_index = 0; + size_t token_start_index = 0; CppPosition token_start_position; auto emit_token = [&](auto type) { diff --git a/DevTools/HackStudio/CppLexer.h b/DevTools/HackStudio/CppLexer.h index d2a61eda8da..5215a20d22d 100644 --- a/DevTools/HackStudio/CppLexer.h +++ b/DevTools/HackStudio/CppLexer.h @@ -25,8 +25,8 @@ __TOKEN(Identifier) struct CppPosition { - int line { -1 }; - int column { -1 }; + size_t line; + size_t column; }; struct CppToken { @@ -60,11 +60,11 @@ public: Vector lex(); private: - char peek(int offset = 0) const; + char peek(size_t offset = 0) const; char consume(); StringView m_input; - int m_index { 0 }; + size_t m_index { 0 }; CppPosition m_previous_position { 0, 0 }; CppPosition m_position { 0, 0 }; }; diff --git a/Kernel/FileSystem/FileSystem.cpp b/Kernel/FileSystem/FileSystem.cpp index bae0038a521..f803f3d6ff4 100644 --- a/Kernel/FileSystem/FileSystem.cpp +++ b/Kernel/FileSystem/FileSystem.cpp @@ -46,7 +46,7 @@ FS::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, u8 ft) name[name_length] = '\0'; } -FS::DirectoryEntry::DirectoryEntry(const char* n, int nl, InodeIdentifier i, u8 ft) +FS::DirectoryEntry::DirectoryEntry(const char* n, size_t nl, InodeIdentifier i, u8 ft) : name_length(nl) , inode(i) , file_type(ft) diff --git a/Kernel/FileSystem/FileSystem.h b/Kernel/FileSystem/FileSystem.h index fa43ba58bc5..33efc773187 100644 --- a/Kernel/FileSystem/FileSystem.h +++ b/Kernel/FileSystem/FileSystem.h @@ -50,9 +50,9 @@ public: // FIXME: This data structure is very clunky and unpleasant. Replace it with something nicer. struct DirectoryEntry { DirectoryEntry(const char* name, InodeIdentifier, u8 file_type); - DirectoryEntry(const char* name, int name_length, InodeIdentifier, u8 file_type); + DirectoryEntry(const char* name, size_t name_length, InodeIdentifier, u8 file_type); char name[256]; - int name_length { 0 }; + size_t name_length { 0 }; InodeIdentifier inode; u8 file_type { 0 }; }; diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 768e8beade8..5cd88759494 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -1085,11 +1085,11 @@ bool ProcFSInode::traverse_as_directory(Function __FI_Root_Start && entry.proc_file_type < __FI_Root_End) - callback({ entry.name, (int)strlen(entry.name), to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type), 0 }); + callback({ entry.name, strlen(entry.name), to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type), 0 }); } for (auto pid_child : Process::all_pids()) { char name[16]; - int name_length = sprintf(name, "%u", pid_child); + size_t name_length = (size_t)sprintf(name, "%u", pid_child); callback({ name, name_length, to_identifier(fsid(), PDI_Root, pid_child, FI_PID), 0 }); } break; @@ -1119,7 +1119,7 @@ bool ProcFSInode::traverse_as_directory(Functionlength() + 1)) + if ((size_t)size < (s_hostname->length() + 1)) return -ENAMETOOLONG; strcpy(buffer, s_hostname->characters()); return 0; @@ -1038,7 +1038,7 @@ int Process::sys$ttyname_r(int fd, char* buffer, ssize_t size) if (!description->is_tty()) return -ENOTTY; auto tty_name = description->tty()->tty_name(); - if (size < tty_name.length() + 1) + if ((size_t)size < tty_name.length() + 1) return -ERANGE; memcpy(buffer, tty_name.characters_without_null_termination(), tty_name.length()); buffer[tty_name.length()] = '\0'; @@ -1058,7 +1058,7 @@ int Process::sys$ptsname_r(int fd, char* buffer, ssize_t size) if (!master_pty) return -ENOTTY; auto pts_name = master_pty->pts_name(); - if (size < pts_name.length() + 1) + if ((size_t)size < pts_name.length() + 1) return -ERANGE; strcpy(buffer, pts_name.characters()); return 0; @@ -1347,7 +1347,7 @@ int Process::sys$getcwd(char* buffer, ssize_t size) if (!validate_write(buffer, size)) return -EFAULT; auto path = current_directory().absolute_path(); - if (size < path.length() + 1) + if ((size_t)size < path.length() + 1) return -ERANGE; strcpy(buffer, path.characters()); return 0; @@ -2991,11 +2991,14 @@ int Process::sys$join_thread(int tid, void** exit_value) int Process::sys$set_thread_name(int tid, const char* buffer, int buffer_size) { + if (buffer_size < 0) + return -EINVAL; + if (!validate_read(buffer, buffer_size)) return -EFAULT; const size_t max_thread_name_size = 64; - if (strnlen(buffer, buffer_size) > max_thread_name_size) + if (strnlen(buffer, (size_t)buffer_size) > max_thread_name_size) return -EINVAL; Thread* thread = nullptr; @@ -3015,7 +3018,7 @@ int Process::sys$set_thread_name(int tid, const char* buffer, int buffer_size) if (thread == ¤t->process().main_thread()) return -EINVAL; - thread->set_name({buffer, buffer_size}); + thread->set_name({ buffer, (size_t)buffer_size }); return 0; } @@ -3039,7 +3042,7 @@ int Process::sys$get_thread_name(int tid, char* buffer, int buffer_size) if (!thread) return -ESRCH; - if (thread->name().length() >= buffer_size) + if (thread->name().length() >= (size_t)buffer_size) return -ENAMETOOLONG; strncpy(buffer, thread->name().characters(), buffer_size); @@ -3112,10 +3115,13 @@ int Process::sys$ftruncate(int fd, off_t length) int Process::sys$watch_file(const char* path, int path_length) { + if (path_length < 0) + return -EINVAL; + if (!validate_read(path, path_length)) return -EFAULT; - auto custody_or_error = VFS::the().resolve_path({ path, path_length }, current_directory()); + auto custody_or_error = VFS::the().resolve_path({ path, (size_t)path_length }, current_directory()); if (custody_or_error.is_error()) return custody_or_error.error(); @@ -3335,10 +3341,10 @@ int Process::sys$get_process_name(char* buffer, int buffer_size) if (!validate_write(buffer, buffer_size)) return -EFAULT; - if (m_name.length() >= buffer_size) + if (m_name.length() >= (size_t)buffer_size) return -ENAMETOOLONG; - strncpy(buffer, m_name.characters(), buffer_size); + strncpy(buffer, m_name.characters(), (size_t)buffer_size); return 0; } diff --git a/Libraries/LibC/netdb.cpp b/Libraries/LibC/netdb.cpp index 75478768ea3..9a74e7d155b 100644 --- a/Libraries/LibC/netdb.cpp +++ b/Libraries/LibC/netdb.cpp @@ -81,7 +81,7 @@ hostent* gethostbyname(const char* name) return nullptr; } - ASSERT(nsent == line.length()); + ASSERT((size_t)nsent == line.length()); char buffer[1024]; int nrecv = read(fd, buffer, sizeof(buffer) - 1); @@ -144,7 +144,7 @@ hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type) return nullptr; } - ASSERT(nsent == line.length()); + ASSERT((size_t)nsent == line.length()); char buffer[1024]; int nrecv = read(fd, buffer, sizeof(buffer) - 1); diff --git a/Libraries/LibCore/CHttpJob.cpp b/Libraries/LibCore/CHttpJob.cpp index 39b50c218fc..8c7f739789f 100644 --- a/Libraries/LibCore/CHttpJob.cpp +++ b/Libraries/LibCore/CHttpJob.cpp @@ -101,7 +101,7 @@ void CHttpJob::on_socket_connected() } auto name = parts[0]; if (chomped_line.length() < name.length() + 2) { - fprintf(stderr, "CHttpJob: Malformed HTTP header: '%s' (%d)\n", chomped_line.characters(), chomped_line.length()); + fprintf(stderr, "CHttpJob: Malformed HTTP header: '%s' (%zu)\n", chomped_line.characters(), chomped_line.length()); return deferred_invoke([this](auto&) { did_fail(CNetworkJob::Error::ProtocolFailed); }); } auto value = chomped_line.substring(name.length() + 2, chomped_line.length() - name.length() - 2); diff --git a/Libraries/LibCore/CProcessStatisticsReader.cpp b/Libraries/LibCore/CProcessStatisticsReader.cpp index 83e06424511..2f48c49eb28 100644 --- a/Libraries/LibCore/CProcessStatisticsReader.cpp +++ b/Libraries/LibCore/CProcessStatisticsReader.cpp @@ -19,7 +19,7 @@ HashMap CProcessStatisticsReader::get_all() HashMap map; auto file_contents = file->read_all(); - auto json = JsonValue::from_string({ file_contents.data(), file_contents.size() }); + auto json = JsonValue::from_string({ file_contents.data(), (size_t)file_contents.size() }); json.as_array().for_each([&](auto& value) { const JsonObject& process_object = value.as_object(); CProcessStatistics process; diff --git a/Libraries/LibDraw/Font.cpp b/Libraries/LibDraw/Font.cpp index 684af5aeed1..561f78ca1db 100644 --- a/Libraries/LibDraw/Font.cpp +++ b/Libraries/LibDraw/Font.cpp @@ -154,7 +154,7 @@ bool Font::write_to_file(const StringView& path) header.glyph_height = m_glyph_height; header.type = 0; header.is_variable_width = !m_fixed_width; - memcpy(header.name, m_name.characters(), min(m_name.length(), 63)); + memcpy(header.name, m_name.characters(), min(m_name.length(), (size_t)63)); size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height; diff --git a/Libraries/LibGUI/GTextDocument.cpp b/Libraries/LibGUI/GTextDocument.cpp index 2cedd2c1bff..ce7f675ed37 100644 --- a/Libraries/LibGUI/GTextDocument.cpp +++ b/Libraries/LibGUI/GTextDocument.cpp @@ -22,17 +22,17 @@ void GTextDocument::set_text(const StringView& text) m_spans.clear(); remove_all_lines(); - int start_of_current_line = 0; + size_t start_of_current_line = 0; - auto add_line = [&](int current_position) { - int line_length = current_position - start_of_current_line; + auto add_line = [&](size_t current_position) { + size_t line_length = current_position - start_of_current_line; auto line = make(*this); if (line_length) line->set_text(*this, text.substring_view(start_of_current_line, current_position - start_of_current_line)); append_line(move(line)); start_of_current_line = current_position + 1; }; - int i = 0; + size_t i = 0; for (i = 0; i < text.length(); ++i) { if (text[i] == '\n') add_line(i); @@ -44,10 +44,10 @@ void GTextDocument::set_text(const StringView& text) client->document_did_set_text(); } -int GTextDocumentLine::first_non_whitespace_column() const +size_t GTextDocumentLine::first_non_whitespace_column() const { - for (int i = 0; i < length(); ++i) { - if (!isspace(m_text[i])) + for (size_t i = 0; i < length(); ++i) { + if (!isspace(m_text[(int)i])) return i; } return length(); @@ -78,12 +78,12 @@ void GTextDocumentLine::set_text(GTextDocument& document, const StringView& text clear(document); return; } - m_text.resize(text.length() + 1); + m_text.resize((int)text.length() + 1); memcpy(m_text.data(), text.characters_without_null_termination(), text.length() + 1); document.update_views({}); } -void GTextDocumentLine::append(GTextDocument& document, const char* characters, int length) +void GTextDocumentLine::append(GTextDocument& document, const char* characters, size_t length) { int old_length = m_text.size() - 1; m_text.resize(m_text.size() + length); @@ -102,31 +102,31 @@ void GTextDocumentLine::prepend(GTextDocument& document, char ch) insert(document, 0, ch); } -void GTextDocumentLine::insert(GTextDocument& document, int index, char ch) +void GTextDocumentLine::insert(GTextDocument& document, size_t index, char ch) { if (index == length()) { m_text.last() = ch; m_text.append(0); } else { - m_text.insert(index, move(ch)); + m_text.insert((int)index, move(ch)); } document.update_views({}); } -void GTextDocumentLine::remove(GTextDocument& document, int index) +void GTextDocumentLine::remove(GTextDocument& document, size_t index) { if (index == length()) { m_text.take_last(); m_text.last() = 0; } else { - m_text.remove(index); + m_text.remove((int)index); } document.update_views({}); } -void GTextDocumentLine::truncate(GTextDocument& document, int length) +void GTextDocumentLine::truncate(GTextDocument& document, size_t length) { - m_text.resize(length + 1); + m_text.resize((int)length + 1); m_text.last() = 0; document.update_views({}); } @@ -140,18 +140,18 @@ void GTextDocument::append_line(NonnullOwnPtr line) } } -void GTextDocument::insert_line(int line_index, NonnullOwnPtr line) +void GTextDocument::insert_line(size_t line_index, NonnullOwnPtr line) { - lines().insert(line_index, move(line)); + lines().insert((int)line_index, move(line)); if (m_client_notifications_enabled) { for (auto* client : m_clients) client->document_did_insert_line(line_index); } } -void GTextDocument::remove_line(int line_index) +void GTextDocument::remove_line(size_t line_index) { - lines().remove(line_index); + lines().remove((int)line_index); if (m_client_notifications_enabled) { for (auto* client : m_clients) client->document_did_remove_line(line_index); @@ -207,10 +207,10 @@ String GTextDocument::text_in_range(const GTextRange& a_range) const auto range = a_range.normalized(); StringBuilder builder; - for (int i = range.start().line(); i <= range.end().line(); ++i) { - auto& line = lines()[i]; - int selection_start_column_on_line = range.start().line() == i ? range.start().column() : 0; - int selection_end_column_on_line = range.end().line() == i ? range.end().column() : line.length(); + for (size_t i = range.start().line(); i <= range.end().line(); ++i) { + auto& line = this->line(i); + size_t selection_start_column_on_line = range.start().line() == i ? range.start().column() : 0; + size_t selection_end_column_on_line = range.end().line() == i ? range.end().column() : line.length(); builder.append(line.characters() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line); if (i != range.end().line()) builder.append('\n'); @@ -222,7 +222,7 @@ String GTextDocument::text_in_range(const GTextRange& a_range) const char GTextDocument::character_at(const GTextPosition& position) const { ASSERT(position.line() < line_count()); - auto& line = lines()[position.line()]; + auto& line = this->line(position.line()); if (position.column() == line.length()) return '\n'; return line.characters()[position.column()]; @@ -230,7 +230,7 @@ char GTextDocument::character_at(const GTextPosition& position) const GTextPosition GTextDocument::next_position_after(const GTextPosition& position, SearchShouldWrap should_wrap) const { - auto& line = lines()[position.line()]; + auto& line = this->line(position.line()); if (position.column() == line.length()) { if (position.line() == line_count() - 1) { if (should_wrap == SearchShouldWrap::Yes) @@ -247,12 +247,12 @@ GTextPosition GTextDocument::previous_position_before(const GTextPosition& posit if (position.column() == 0) { if (position.line() == 0) { if (should_wrap == SearchShouldWrap::Yes) { - auto& last_line = lines()[line_count() - 1]; + auto& last_line = this->line(line_count() - 1); return { line_count() - 1, last_line.length() }; } return {}; } - auto& prev_line = lines()[position.line() - 1]; + auto& prev_line = this->line(position.line() - 1); return { position.line() - 1, prev_line.length() }; } return { position.line(), position.column() - 1 }; @@ -267,7 +267,7 @@ GTextRange GTextDocument::find_next(const StringView& needle, const GTextPositio GTextPosition original_position = position; GTextPosition start_of_potential_match; - int needle_index = 0; + size_t needle_index = 0; do { auto ch = character_at(position); @@ -298,16 +298,16 @@ GTextRange GTextDocument::find_previous(const StringView& needle, const GTextPos GTextPosition original_position = position; GTextPosition end_of_potential_match; - int needle_index = needle.length() - 1; + size_t needle_index = needle.length() - 1; do { auto ch = character_at(position); if (ch == needle[needle_index]) { if (needle_index == needle.length() - 1) end_of_potential_match = position; - --needle_index; - if (needle_index < 0) + if (needle_index == 0) return { position, next_position_after(end_of_potential_match, should_wrap) }; + --needle_index; } else { if (needle_index < needle.length() - 1) position = end_of_potential_match; @@ -441,9 +441,8 @@ void GTextDocument::update_undo_timer() GTextPosition GTextDocument::insert_at(const GTextPosition& position, const StringView& text) { GTextPosition cursor = position; - for (int i = 0; i < text.length(); ++i) { + for (size_t i = 0; i < text.length(); ++i) cursor = insert_at(cursor, text[i]); - } return cursor; } @@ -451,7 +450,7 @@ GTextPosition GTextDocument::insert_at(const GTextPosition& position, char ch) { // FIXME: We need these from GTextEditor! bool m_automatic_indentation_enabled = true; - int m_soft_tab_width = 4; + size_t m_soft_tab_width = 4; bool at_head = position.column() == 0; bool at_tail = position.column() == line(position.line()).length(); @@ -459,9 +458,9 @@ GTextPosition GTextDocument::insert_at(const GTextPosition& position, char ch) if (at_tail || at_head) { String new_line_contents; if (m_automatic_indentation_enabled && at_tail) { - int leading_spaces = 0; + size_t leading_spaces = 0; auto& old_line = lines()[position.line()]; - for (int i = 0; i < old_line.length(); ++i) { + for (size_t i = 0; i < old_line.length(); ++i) { if (old_line.characters()[i] == ' ') ++leading_spaces; else @@ -471,9 +470,9 @@ GTextPosition GTextDocument::insert_at(const GTextPosition& position, char ch) new_line_contents = String::repeated(' ', leading_spaces); } - int row = position.line(); + size_t row = position.line(); Vector line_content; - for (int i = position.column(); i < line(row).length(); i++) + for (size_t i = position.column(); i < line(row).length(); i++) line_content.append(line(row).characters()[i]); insert_line(position.line() + (at_tail ? 1 : 0), make(*this, new_line_contents)); notify_did_change(); @@ -483,7 +482,7 @@ GTextPosition GTextDocument::insert_at(const GTextPosition& position, char ch) new_line->append(*this, line(position.line()).characters() + position.column(), line(position.line()).length() - position.column()); Vector line_content; - for (int i = 0; i < new_line->length(); i++) + for (size_t i = 0; i < new_line->length(); i++) line_content.append(new_line->characters()[i]); line(position.line()).truncate(*this, position.column()); insert_line(position.line() + 1, move(new_line)); @@ -491,9 +490,9 @@ GTextPosition GTextDocument::insert_at(const GTextPosition& position, char ch) return { position.line() + 1, 0 }; } if (ch == '\t') { - int next_soft_tab_stop = ((position.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width; - int spaces_to_insert = next_soft_tab_stop - position.column(); - for (int i = 0; i < spaces_to_insert; ++i) { + size_t next_soft_tab_stop = ((position.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width; + size_t spaces_to_insert = next_soft_tab_stop - position.column(); + for (size_t i = 0; i < spaces_to_insert; ++i) { line(position.line()).insert(*this, position.column(), ' '); } notify_did_change(); @@ -512,14 +511,14 @@ void GTextDocument::remove(const GTextRange& unnormalized_range) auto range = unnormalized_range.normalized(); // First delete all the lines in between the first and last one. - for (int i = range.start().line() + 1; i < range.end().line();) { + for (size_t i = range.start().line() + 1; i < range.end().line();) { remove_line(i); range.end().set_line(range.end().line() - 1); } if (range.start().line() == range.end().line()) { // Delete within same line. - auto& line = lines()[range.start().line()]; + auto& line = this->line(range.start().line()); bool whole_line_is_selected = range.start().column() == 0 && range.end().column() == line.length(); if (whole_line_is_selected) { @@ -535,8 +534,8 @@ void GTextDocument::remove(const GTextRange& unnormalized_range) } else { // Delete across a newline, merging lines. ASSERT(range.start().line() == range.end().line() - 1); - auto& first_line = lines()[range.start().line()]; - auto& second_line = lines()[range.end().line()]; + auto& first_line = line(range.start().line()); + auto& second_line = line(range.end().line()); auto before_selection = String(first_line.characters(), first_line.length()).substring(0, range.start().column()); auto after_selection = String(second_line.characters(), second_line.length()).substring(range.end().column(), second_line.length() - range.end().column()); StringBuilder builder(before_selection.length() + after_selection.length()); diff --git a/Libraries/LibGUI/GTextDocument.h b/Libraries/LibGUI/GTextDocument.h index 9e14b4aaafc..bbf24e1bec8 100644 --- a/Libraries/LibGUI/GTextDocument.h +++ b/Libraries/LibGUI/GTextDocument.h @@ -66,8 +66,8 @@ public: public: virtual ~Client(); virtual void document_did_append_line() = 0; - virtual void document_did_insert_line(int) = 0; - virtual void document_did_remove_line(int) = 0; + virtual void document_did_insert_line(size_t) = 0; + virtual void document_did_remove_line(size_t) = 0; virtual void document_did_remove_all_lines() = 0; virtual void document_did_change() = 0; virtual void document_did_set_text() = 0; @@ -79,9 +79,9 @@ public: return adopt(*new GTextDocument(client)); } - int line_count() const { return m_lines.size(); } - const GTextDocumentLine& line(int line_index) const { return m_lines[line_index]; } - GTextDocumentLine& line(int line_index) { return m_lines[line_index]; } + size_t line_count() const { return (size_t)m_lines.size(); } + const GTextDocumentLine& line(size_t line_index) const { return m_lines[(int)line_index]; } + GTextDocumentLine& line(size_t line_index) { return m_lines[(int)line_index]; } void set_spans(const Vector& spans) { m_spans = spans; } @@ -92,12 +92,12 @@ public: bool has_spans() const { return !m_spans.is_empty(); } const Vector& spans() const { return m_spans; } - void set_span_at_index(int index, GTextDocumentSpan span) { m_spans[index] = move(span); } + void set_span_at_index(size_t index, GTextDocumentSpan span) { m_spans[(int)index] = move(span); } void append_line(NonnullOwnPtr); - void remove_line(int line_index); + void remove_line(size_t line_index); void remove_all_lines(); - void insert_line(int line_index, NonnullOwnPtr); + void insert_line(size_t line_index, NonnullOwnPtr); void register_client(Client&); void unregister_client(Client&); @@ -156,18 +156,18 @@ public: explicit GTextDocumentLine(GTextDocument&); explicit GTextDocumentLine(GTextDocument&, const StringView&); - StringView view() const { return { characters(), length() }; } + StringView view() const { return { characters(), (size_t)length() }; } const char* characters() const { return m_text.data(); } - int length() const { return m_text.size() - 1; } + size_t length() const { return (size_t)m_text.size() - 1; } void set_text(GTextDocument&, const StringView&); void append(GTextDocument&, char); void prepend(GTextDocument&, char); - void insert(GTextDocument&, int index, char); - void remove(GTextDocument&, int index); - void append(GTextDocument&, const char*, int); - void truncate(GTextDocument&, int length); + void insert(GTextDocument&, size_t index, char); + void remove(GTextDocument&, size_t index); + void append(GTextDocument&, const char*, size_t); + void truncate(GTextDocument&, size_t length); void clear(GTextDocument&); - int first_non_whitespace_column() const; + size_t first_non_whitespace_column() const; private: // NOTE: This vector is null terminated. diff --git a/Libraries/LibGUI/GTextEditor.cpp b/Libraries/LibGUI/GTextEditor.cpp index 88afbd6b637..25bf3ff348f 100644 --- a/Libraries/LibGUI/GTextEditor.cpp +++ b/Libraries/LibGUI/GTextEditor.cpp @@ -97,30 +97,30 @@ GTextPosition GTextEditor::text_position_at(const Point& a_position) const position.move_by(-(m_horizontal_content_padding + ruler_width()), 0); position.move_by(-frame_thickness(), -frame_thickness()); - int line_index = -1; + size_t line_index = 0; if (is_line_wrapping_enabled()) { - for (int i = 0; i < lines().size(); ++i) { + for (size_t i = 0; i < line_count(); ++i) { auto& rect = m_line_visual_data[i].visual_rect; if (position.y() >= rect.top() && position.y() <= rect.bottom()) { line_index = i; break; } if (position.y() > rect.bottom()) - line_index = lines().size() - 1; + line_index = line_count() - 1; } } else { - line_index = position.y() / line_height(); + line_index = (size_t)(position.y() / line_height()); } - line_index = max(0, min(line_index, line_count() - 1)); + line_index = max((size_t)0, min(line_index, line_count() - 1)); - int column_index; + size_t column_index; switch (m_text_alignment) { case TextAlignment::CenterLeft: column_index = (position.x() + glyph_width() / 2) / glyph_width(); if (is_line_wrapping_enabled()) { - for_each_visual_line(line_index, [&](const Rect& rect, const StringView&, int start_of_line) { + for_each_visual_line(line_index, [&](const Rect& rect, const StringView&, size_t start_of_line) { if (rect.contains_vertically(position.y())) { column_index += start_of_line; return IterationDecision::Break; @@ -138,7 +138,7 @@ GTextPosition GTextEditor::text_position_at(const Point& a_position) const ASSERT_NOT_REACHED(); } - column_index = max(0, min(column_index, lines()[line_index].length())); + column_index = max((size_t)0, min(column_index, line(line_index).length())); return { line_index, column_index }; } @@ -155,7 +155,7 @@ void GTextEditor::doubleclick_event(GMouseEvent& event) auto start = text_position_at(event.position()); auto end = start; - auto& line = lines()[start.line()]; + auto& line = this->line(start.line()); if (!document().has_spans()) { while (start.column() > 0) { @@ -201,11 +201,11 @@ void GTextEditor::mousedown_event(GMouseEvent& event) if (is_multi_line()) { // select *current* line start = GTextPosition(m_cursor.line(), 0); - end = GTextPosition(m_cursor.line(), lines()[m_cursor.line()].length()); + end = GTextPosition(m_cursor.line(), line(m_cursor.line()).length()); } else { // select *whole* line start = GTextPosition(0, 0); - end = GTextPosition(line_count() - 1, lines()[line_count() - 1].length()); + end = GTextPosition(line_count() - 1, line(line_count() - 1).length()); } m_selection.set(start, end); @@ -266,7 +266,7 @@ int GTextEditor::ruler_width() const return 5 * font().glyph_width('x') + 4; } -Rect GTextEditor::ruler_content_rect(int line_index) const +Rect GTextEditor::ruler_content_rect(size_t line_index) const { if (!m_ruler_visible) return {}; @@ -318,14 +318,14 @@ void GTextEditor::paint_event(GPaintEvent& event) if (m_ruler_visible) painter.translate(ruler_width(), 0); - int first_visible_line = text_position_at(event.rect().top_left()).line(); - int last_visible_line = text_position_at(event.rect().bottom_right()).line(); + size_t first_visible_line = text_position_at(event.rect().top_left()).line(); + size_t last_visible_line = text_position_at(event.rect().bottom_right()).line(); auto selection = normalized_selection(); bool has_selection = selection.is_valid(); if (m_ruler_visible) { - for (int i = first_visible_line; i <= last_visible_line; ++i) { + for (size_t i = first_visible_line; i <= last_visible_line; ++i) { bool is_current_line = i == m_cursor.line(); auto ruler_line_rect = ruler_content_rect(i); painter.draw_text( @@ -345,12 +345,12 @@ void GTextEditor::paint_event(GPaintEvent& event) }; painter.add_clip_rect(text_clip_rect); - for (int line_index = first_visible_line; line_index <= last_visible_line; ++line_index) { - auto& line = lines()[line_index]; + for (size_t line_index = first_visible_line; line_index <= last_visible_line; ++line_index) { + auto& line = this->line(line_index); bool physical_line_has_selection = has_selection && line_index >= selection.start().line() && line_index <= selection.end().line(); - int first_visual_line_with_selection = -1; - int last_visual_line_with_selection = -1; + size_t first_visual_line_with_selection = 0; + size_t last_visual_line_with_selection = 0; if (physical_line_has_selection) { if (selection.start().line() < line_index) first_visual_line_with_selection = 0; @@ -363,11 +363,11 @@ void GTextEditor::paint_event(GPaintEvent& event) last_visual_line_with_selection = visual_line_containing(line_index, selection.end().column()); } - int selection_start_column_within_line = selection.start().line() == line_index ? selection.start().column() : 0; - int selection_end_column_within_line = selection.end().line() == line_index ? selection.end().column() : line.length(); + size_t selection_start_column_within_line = selection.start().line() == line_index ? selection.start().column() : 0; + size_t selection_end_column_within_line = selection.end().line() == line_index ? selection.end().column() : line.length(); - int visual_line_index = 0; - for_each_visual_line(line_index, [&](const Rect& visual_line_rect, const StringView& visual_line_text, int start_of_visual_line) { + size_t visual_line_index = 0; + for_each_visual_line(line_index, [&](const Rect& visual_line_rect, const StringView& visual_line_text, size_t start_of_visual_line) { if (is_multi_line() && line_index == m_cursor.line()) painter.fill_rect(visual_line_rect, Color(230, 230, 230)); #ifdef DEBUG_GTEXTEDITOR @@ -379,7 +379,7 @@ void GTextEditor::paint_event(GPaintEvent& event) } else { int advance = font().glyph_width(' ') + font().glyph_spacing(); Rect character_rect = { visual_line_rect.location(), { font().glyph_width(' '), line_height() } }; - for (int i = 0; i < visual_line_text.length(); ++i) { + for (size_t i = 0; i < visual_line_text.length(); ++i) { const Font* font = &this->font(); Color color; Optional background_color; @@ -426,8 +426,8 @@ void GTextEditor::paint_event(GPaintEvent& event) painter.fill_rect(selection_rect, Color::from_rgb(0x955233)); - int start_of_selection_within_visual_line = max(0, selection_start_column_within_line - start_of_visual_line); - int end_of_selection_within_visual_line = selection_end_column_within_line - start_of_visual_line; + size_t start_of_selection_within_visual_line = max((size_t)0, selection_start_column_within_line - start_of_visual_line); + size_t end_of_selection_within_visual_line = selection_end_column_within_line - start_of_visual_line; StringView visual_selected_text { visual_line_text.characters_without_null_termination() + start_of_selection_within_visual_line, @@ -465,14 +465,14 @@ void GTextEditor::toggle_selection_if_needed_for_event(const GKeyEvent& event) void GTextEditor::select_all() { GTextPosition start_of_document { 0, 0 }; - GTextPosition end_of_document { line_count() - 1, lines()[line_count() - 1].length() }; + GTextPosition end_of_document { line_count() - 1, line(line_count() - 1).length() }; m_selection.set(start_of_document, end_of_document); did_update_selection(); set_cursor(end_of_document); update(); } -void GTextEditor::get_selection_line_boundaries(int& first_line, int& last_line) +void GTextEditor::get_selection_line_boundaries(size_t& first_line, size_t& last_line) { auto selection = normalized_selection(); if (!selection.is_valid()) { @@ -488,15 +488,15 @@ void GTextEditor::get_selection_line_boundaries(int& first_line, int& last_line) void GTextEditor::move_selected_lines_up() { - int first_line; - int last_line; + size_t first_line; + size_t last_line; get_selection_line_boundaries(first_line, last_line); if (first_line == 0) return; auto& lines = document().lines(); - lines.insert(last_line, lines.take(first_line - 1)); + lines.insert((int)last_line, lines.take((int)first_line - 1)); m_cursor = { first_line - 1, 0 }; if (has_selection()) { @@ -510,15 +510,15 @@ void GTextEditor::move_selected_lines_up() void GTextEditor::move_selected_lines_down() { - int first_line; - int last_line; + size_t first_line; + size_t last_line; get_selection_line_boundaries(first_line, last_line); auto& lines = document().lines(); - if (last_line >= (lines.size() - 1)) + if (last_line >= (size_t)(lines.size() - 1)) return; - lines.insert(first_line, lines.take(last_line + 1)); + lines.insert((int)first_line, lines.take((int)last_line + 1)); m_cursor = { first_line + 1, 0 }; if (has_selection()) { @@ -538,14 +538,14 @@ void GTextEditor::sort_selected_lines() if (!has_selection()) return; - int first_line; - int last_line; + size_t first_line; + size_t last_line; get_selection_line_boundaries(first_line, last_line); auto& lines = document().lines(); - auto start = lines.begin() + first_line; - auto end = lines.begin() + last_line + 1; + auto start = lines.begin() + (int)first_line; + auto end = lines.begin() + (int)last_line + 1; quick_sort(start, end, [](auto& a, auto& b) { return strcmp(a.characters(), b.characters()) < 0; @@ -577,8 +577,8 @@ void GTextEditor::keydown_event(GKeyEvent& event) move_selected_lines_up(); return; } - int new_line = m_cursor.line() - 1; - int new_column = min(m_cursor.column(), lines()[new_line].length()); + size_t new_line = m_cursor.line() - 1; + size_t new_column = min(m_cursor.column(), line(new_line).length()); toggle_selection_if_needed_for_event(event); set_cursor(new_line, new_column); if (event.shift() && m_selection.start().is_valid()) { @@ -589,13 +589,13 @@ void GTextEditor::keydown_event(GKeyEvent& event) return; } if (event.key() == KeyCode::Key_Down) { - if (m_cursor.line() < (lines().size() - 1)) { + if (m_cursor.line() < (line_count() - 1)) { if (event.ctrl() && event.shift()) { move_selected_lines_down(); return; } - int new_line = m_cursor.line() + 1; - int new_column = min(m_cursor.column(), lines()[new_line].length()); + size_t new_line = m_cursor.line() + 1; + size_t new_column = min(m_cursor.column(), line(new_line).length()); toggle_selection_if_needed_for_event(event); set_cursor(new_line, new_column); if (event.shift() && m_selection.start().is_valid()) { @@ -607,8 +607,8 @@ void GTextEditor::keydown_event(GKeyEvent& event) } if (event.key() == KeyCode::Key_PageUp) { if (m_cursor.line() > 0) { - int new_line = max(0, m_cursor.line() - visible_content_rect().height() / line_height()); - int new_column = min(m_cursor.column(), lines()[new_line].length()); + size_t new_line = max((size_t)0, m_cursor.line() - (size_t)visible_content_rect().height() / (size_t)line_height()); + size_t new_column = min(m_cursor.column(), line(new_line).length()); toggle_selection_if_needed_for_event(event); set_cursor(new_line, new_column); if (event.shift() && m_selection.start().is_valid()) { @@ -619,7 +619,7 @@ void GTextEditor::keydown_event(GKeyEvent& event) return; } if (event.key() == KeyCode::Key_PageDown) { - if (m_cursor.line() < (lines().size() - 1)) { + if (m_cursor.line() < (line_count() - 1)) { int new_line = min(line_count() - 1, m_cursor.line() + visible_content_rect().height() / line_height()); int new_column = min(m_cursor.column(), lines()[new_line].length()); toggle_selection_if_needed_for_event(event); @@ -699,7 +699,7 @@ void GTextEditor::keydown_event(GKeyEvent& event) return; } if (!event.ctrl() && event.key() == KeyCode::Key_Home) { - int first_nonspace_column = current_line().first_non_whitespace_column(); + size_t first_nonspace_column = current_line().first_non_whitespace_column(); toggle_selection_if_needed_for_event(event); if (m_cursor.column() == first_nonspace_column) set_cursor(m_cursor.line(), 0); @@ -773,7 +773,7 @@ void GTextEditor::keydown_event(GKeyEvent& event) } if (m_cursor.column() == 0 && m_cursor.line() != 0) { // Backspace at column 0; merge with previous line - int previous_length = line(m_cursor.line() - 1).length(); + size_t previous_length = line(m_cursor.line() - 1).length(); GTextRange erased_range({ m_cursor.line() - 1, previous_length }, m_cursor); execute("\n", erased_range); return; @@ -806,10 +806,10 @@ void GTextEditor::delete_current_line() GTextPosition start; GTextPosition end; - if (m_cursor.line() == 0 && lines().size() == 1) { + if (m_cursor.line() == 0 && line_count() == 1) { start = { 0, 0 }; end = { 0, line(0).length() }; - } else if (m_cursor.line() == lines().size() - 1) { + } else if (m_cursor.line() == line_count() - 1) { start = { m_cursor.line() - 1, line(m_cursor.line()).length() }; end = { m_cursor.line(), line(m_cursor.line()).length() }; } else { @@ -845,11 +845,11 @@ void GTextEditor::do_delete() int GTextEditor::content_x_for_position(const GTextPosition& position) const { - auto& line = lines()[position.line()]; + auto& line = this->line(position.line()); int x_offset = -1; switch (m_text_alignment) { case TextAlignment::CenterLeft: - for_each_visual_line(position.line(), [&](const Rect&, const StringView& view, int start_of_visual_line) { + for_each_visual_line(position.line(), [&](const Rect&, const StringView& view, size_t start_of_visual_line) { if (position.column() >= start_of_visual_line && ((position.column() - start_of_visual_line) <= view.length())) { x_offset = (position.column() - start_of_visual_line) * glyph_width(); return IterationDecision::Break; @@ -882,7 +882,7 @@ Rect GTextEditor::content_rect_for_position(const GTextPosition& position) const } Rect rect; - for_each_visual_line(position.line(), [&](const Rect& visual_line_rect, const StringView& view, int start_of_visual_line) { + for_each_visual_line(position.line(), [&](const Rect& visual_line_rect, const StringView& view, size_t start_of_visual_line) { if (position.column() >= start_of_visual_line && ((position.column() - start_of_visual_line) <= view.length())) { // NOTE: We have to subtract the horizontal padding here since it's part of the visual line rect // *and* included in what we get from content_x_for_position(). @@ -904,7 +904,7 @@ Rect GTextEditor::cursor_content_rect() const return content_rect_for_position(m_cursor); } -Rect GTextEditor::line_widget_rect(int line_index) const +Rect GTextEditor::line_widget_rect(size_t line_index) const { auto rect = line_content_rect(line_index); rect.set_x(frame_thickness()); @@ -920,8 +920,8 @@ void GTextEditor::scroll_position_into_view(const GTextPosition& position) auto rect = content_rect_for_position(position); if (position.column() == 0) rect.set_x(content_x_for_position({ position.line(), 0 }) - 2); - else if (position.column() == lines()[position.line()].length()) - rect.set_x(content_x_for_position({ position.line(), lines()[position.line()].length() }) + 2); + else if (position.column() == line(position.line()).length()) + rect.set_x(content_x_for_position({ position.line(), line(position.line()).length() }) + 2); scroll_into_view(rect, true, true); } @@ -930,11 +930,11 @@ void GTextEditor::scroll_cursor_into_view() scroll_position_into_view(m_cursor); } -Rect GTextEditor::line_content_rect(int line_index) const +Rect GTextEditor::line_content_rect(size_t line_index) const { - auto& line = lines()[line_index]; + auto& line = this->line(line_index); if (is_single_line()) { - Rect line_rect = { content_x_for_position({ line_index, 0 }), 0, line.length() * glyph_width(), font().glyph_height() + 2 }; + Rect line_rect = { content_x_for_position({ line_index, 0 }), 0, (int)line.length() * glyph_width(), font().glyph_height() + 2 }; line_rect.center_vertically_within({ {}, frame_inner_rect().size() }); return line_rect; } @@ -942,8 +942,8 @@ Rect GTextEditor::line_content_rect(int line_index) const return m_line_visual_data[line_index].visual_rect; return { content_x_for_position({ line_index, 0 }), - line_index * line_height(), - line.length() * glyph_width(), + (int)line_index * line_height(), + (int)line.length() * glyph_width(), line_height() }; } @@ -953,7 +953,7 @@ void GTextEditor::update_cursor() update(line_widget_rect(m_cursor.line())); } -void GTextEditor::set_cursor(int line, int column) +void GTextEditor::set_cursor(size_t line, size_t column) { set_cursor({ line, column }); } @@ -964,15 +964,15 @@ void GTextEditor::set_cursor(const GTextPosition& a_position) GTextPosition position = a_position; - if (position.line() >= lines().size()) - position.set_line(lines().size() - 1); + if (position.line() >= line_count()) + position.set_line(line_count() - 1); if (position.column() > lines()[position.line()].length()) position.set_column(lines()[position.line()].length()); if (m_cursor != position) { // NOTE: If the old cursor is no longer valid, repaint everything just in case. - auto old_cursor_line_rect = m_cursor.line() < lines().size() + auto old_cursor_line_rect = m_cursor.line() < line_count() ? line_widget_rect(m_cursor.line()) : rect(); m_cursor = position; @@ -1015,9 +1015,9 @@ bool GTextEditor::write_to_file(const StringView& path) // Compute the final file size and ftruncate() to make writing fast. // FIXME: Remove this once the kernel is smart enough to do this instead. off_t file_size = 0; - for (int i = 0; i < lines().size(); ++i) - file_size += lines()[i].length(); - file_size += lines().size() - 1; + for (size_t i = 0; i < line_count(); ++i) + file_size += line(i).length(); + file_size += line_count() - 1; int rc = ftruncate(fd, file_size); if (rc < 0) { @@ -1025,8 +1025,8 @@ bool GTextEditor::write_to_file(const StringView& path) return false; } - for (int i = 0; i < lines().size(); ++i) { - auto& line = lines()[i]; + for (size_t i = 0; i < line_count(); ++i) { + auto& line = this->line(i); if (line.length()) { ssize_t nwritten = write(fd, line.characters(), line.length()); if (nwritten < 0) { @@ -1035,7 +1035,7 @@ bool GTextEditor::write_to_file(const StringView& path) return false; } } - if (i != lines().size() - 1) { + if (i != line_count() - 1) { char ch = '\n'; ssize_t nwritten = write(fd, &ch, 1); if (nwritten != 1) { @@ -1053,8 +1053,8 @@ bool GTextEditor::write_to_file(const StringView& path) String GTextEditor::text() const { StringBuilder builder; - for (int i = 0; i < line_count(); ++i) { - auto& line = lines()[i]; + for (size_t i = 0; i < line_count(); ++i) { + auto& line = this->line(i); builder.append(line.characters(), line.length()); if (i != line_count() - 1) builder.append('\n'); @@ -1234,7 +1234,7 @@ void GTextEditor::clear_selection() void GTextEditor::recompute_all_visual_lines() { int y_offset = 0; - for (int line_index = 0; line_index < line_count(); ++line_index) { + for (size_t line_index = 0; line_index < line_count(); ++line_index) { recompute_visual_lines(line_index); m_line_visual_data[line_index].visual_rect.set_y(y_offset); y_offset += m_line_visual_data[line_index].visual_rect.height(); @@ -1246,18 +1246,18 @@ void GTextEditor::recompute_all_visual_lines() void GTextEditor::ensure_cursor_is_valid() { auto new_cursor = m_cursor; - if (new_cursor.line() >= lines().size()) - new_cursor.set_line(lines().size() - 1); - if (new_cursor.column() > lines()[new_cursor.line()].length()) - new_cursor.set_column(lines()[new_cursor.line()].length()); + if (new_cursor.line() >= line_count()) + new_cursor.set_line(line_count() - 1); + if (new_cursor.column() > line(new_cursor.line()).length()) + new_cursor.set_column(line(new_cursor.line()).length()); if (m_cursor != new_cursor) set_cursor(new_cursor); } -int GTextEditor::visual_line_containing(int line_index, int column) const +size_t GTextEditor::visual_line_containing(size_t line_index, size_t column) const { - int visual_line_index = 0; - for_each_visual_line(line_index, [&](const Rect&, const StringView& view, int start_of_visual_line) { + size_t visual_line_index = 0; + for_each_visual_line(line_index, [&](const Rect&, const StringView& view, size_t start_of_visual_line) { if (column >= start_of_visual_line && ((column - start_of_visual_line) < view.length())) return IterationDecision::Break; ++visual_line_index; @@ -1266,7 +1266,7 @@ int GTextEditor::visual_line_containing(int line_index, int column) const return visual_line_index; } -void GTextEditor::recompute_visual_lines(int line_index) +void GTextEditor::recompute_visual_lines(size_t line_index) { auto& line = document().line(line_index); auto& visual_data = m_line_visual_data[line_index]; @@ -1278,7 +1278,7 @@ void GTextEditor::recompute_visual_lines(int line_index) if (is_line_wrapping_enabled()) { int line_width_so_far = 0; - for (int i = 0; i < line.length(); ++i) { + for (size_t i = 0; i < line.length(); ++i) { auto ch = line.characters()[i]; auto glyph_width = font().glyph_width(ch); if ((line_width_so_far + glyph_width) > available_width) { @@ -1299,11 +1299,11 @@ void GTextEditor::recompute_visual_lines(int line_index) } template -void GTextEditor::for_each_visual_line(int line_index, Callback callback) const +void GTextEditor::for_each_visual_line(size_t line_index, Callback callback) const { auto editor_visible_text_rect = visible_text_rect_in_inner_coordinates(); - int start_of_line = 0; - int visual_line_index = 0; + size_t start_of_line = 0; + size_t visual_line_index = 0; auto& line = document().line(line_index); auto& visual_data = m_line_visual_data[line_index]; @@ -1312,7 +1312,7 @@ void GTextEditor::for_each_visual_line(int line_index, Callback callback) const auto visual_line_view = StringView(line.characters() + start_of_line, visual_line_break - start_of_line); Rect visual_line_rect { visual_data.visual_rect.x(), - visual_data.visual_rect.y() + (visual_line_index * line_height()), + visual_data.visual_rect.y() + ((int)visual_line_index * line_height()), font().width(visual_line_view), line_height() }; @@ -1357,7 +1357,7 @@ void GTextEditor::document_did_append_line() update(); } -void GTextEditor::document_did_remove_line(int line_index) +void GTextEditor::document_did_remove_line(size_t line_index) { m_line_visual_data.remove(line_index); recompute_all_visual_lines(); @@ -1371,7 +1371,7 @@ void GTextEditor::document_did_remove_all_lines() update(); } -void GTextEditor::document_did_insert_line(int line_index) +void GTextEditor::document_did_insert_line(size_t line_index) { m_line_visual_data.insert(line_index, make()); recompute_all_visual_lines(); @@ -1387,7 +1387,7 @@ void GTextEditor::document_did_change() void GTextEditor::document_did_set_text() { m_line_visual_data.clear(); - for (int i = 0; i < m_document->line_count(); ++i) + for (size_t i = 0; i < m_document->line_count(); ++i) m_line_visual_data.append(make()); document_did_change(); } @@ -1405,7 +1405,7 @@ void GTextEditor::set_document(GTextDocument& document) m_document->unregister_client(*this); m_document = document; m_line_visual_data.clear(); - for (int i = 0; i < m_document->line_count(); ++i) { + for (size_t i = 0; i < m_document->line_count(); ++i) { m_line_visual_data.append(make()); } m_cursor = { 0, 0 }; diff --git a/Libraries/LibGUI/GTextEditor.h b/Libraries/LibGUI/GTextEditor.h index 83d1db038f1..202be7e84ba 100644 --- a/Libraries/LibGUI/GTextEditor.h +++ b/Libraries/LibGUI/GTextEditor.h @@ -56,7 +56,7 @@ public: void set_text(const StringView&); void scroll_cursor_into_view(); void scroll_position_into_view(const GTextPosition&); - int line_count() const { return document().line_count(); } + size_t line_count() const { return document().line_count(); } int line_spacing() const { return m_line_spacing; } int line_height() const { return font().glyph_height() + m_line_spacing; } GTextPosition cursor() const { return m_cursor; } @@ -99,7 +99,7 @@ public: void add_custom_context_menu_action(GAction&); - void set_cursor(int line, int column); + void set_cursor(size_t line, size_t column); void set_cursor(const GTextPosition&); protected: @@ -130,8 +130,8 @@ private: // ^GTextDocument::Client virtual void document_did_append_line() override; - virtual void document_did_insert_line(int) override; - virtual void document_did_remove_line(int) override; + virtual void document_did_insert_line(size_t) override; + virtual void document_did_remove_line(size_t) override; virtual void document_did_remove_all_lines() override; virtual void document_did_change() override; virtual void document_did_set_text() override; @@ -142,19 +142,19 @@ private: void update_content_size(); void did_change(); - Rect line_content_rect(int item_index) const; - Rect line_widget_rect(int line_index) const; + Rect line_content_rect(size_t item_index) const; + Rect line_widget_rect(size_t line_index) const; Rect cursor_content_rect() const; Rect content_rect_for_position(const GTextPosition&) const; void update_cursor(); const NonnullOwnPtrVector& lines() const { return document().lines(); } NonnullOwnPtrVector& lines() { return document().lines(); } - GTextDocumentLine& line(int index) { return document().line(index); } - const GTextDocumentLine& line(int index) const { return document().line(index); } + GTextDocumentLine& line(size_t index) { return document().line(index); } + const GTextDocumentLine& line(size_t index) const { return document().line(index); } GTextDocumentLine& current_line() { return line(m_cursor.line()); } const GTextDocumentLine& current_line() const { return line(m_cursor.line()); } int ruler_width() const; - Rect ruler_content_rect(int line) const; + Rect ruler_content_rect(size_t line) const; void toggle_selection_if_needed_for_event(const GKeyEvent&); void insert_at_cursor_or_replace_selection(const StringView&); void delete_selection(); @@ -165,13 +165,13 @@ private: void recompute_all_visual_lines(); void ensure_cursor_is_valid(); void flush_pending_change_notification_if_needed(); - void get_selection_line_boundaries(int& first_line, int& last_line); + void get_selection_line_boundaries(size_t& first_line, size_t& last_line); void move_selected_lines_up(); void move_selected_lines_down(); void sort_selected_lines(); - int visual_line_containing(int line_index, int column) const; - void recompute_visual_lines(int line_index); + size_t visual_line_containing(size_t line_index, size_t column) const; + void recompute_visual_lines(size_t line_index); template inline void execute(Args&&... args) @@ -193,7 +193,7 @@ private: bool m_line_wrapping_enabled { false }; bool m_readonly { false }; int m_line_spacing { 4 }; - int m_soft_tab_width { 4 }; + size_t m_soft_tab_width { 4 }; int m_horizontal_content_padding { 2 }; GTextRange m_selection; OwnPtr m_context_menu; @@ -209,10 +209,10 @@ private: RefPtr m_document; template - void for_each_visual_line(int line_index, Callback) const; + void for_each_visual_line(size_t line_index, Callback) const; struct LineVisualData { - Vector visual_line_breaks; + Vector visual_line_breaks; Rect visual_rect; }; diff --git a/Libraries/LibGUI/GTextPosition.h b/Libraries/LibGUI/GTextPosition.h index 186681d085c..34fdb36201a 100644 --- a/Libraries/LibGUI/GTextPosition.h +++ b/Libraries/LibGUI/GTextPosition.h @@ -6,32 +6,32 @@ class GTextPosition { public: GTextPosition() {} - GTextPosition(int line, int column) + GTextPosition(size_t line, size_t column) : m_line(line) , m_column(column) { } - bool is_valid() const { return m_line >= 0 && m_column >= 0; } + bool is_valid() const { return m_line != 0xffffffffu && m_column != 0xffffffffu; } - int line() const { return m_line; } - int column() const { return m_column; } + size_t line() const { return m_line; } + size_t column() const { return m_column; } - void set_line(int line) { m_line = line; } - void set_column(int column) { m_column = column; } + void set_line(size_t line) { m_line = line; } + void set_column(size_t column) { m_column = column; } bool operator==(const GTextPosition& other) const { return m_line == other.m_line && m_column == other.m_column; } bool operator!=(const GTextPosition& other) const { return m_line != other.m_line || m_column != other.m_column; } bool operator<(const GTextPosition& other) const { return m_line < other.m_line || (m_line == other.m_line && m_column < other.m_column); } private: - int m_line { -1 }; - int m_column { -1 }; + size_t m_line { 0xffffffff }; + size_t m_column { 0xffffffff }; }; inline const LogStream& operator<<(const LogStream& stream, const GTextPosition& value) { if (!value.is_valid()) return stream << "GTextPosition(Invalid)"; - return stream << String::format("(%d,%d)", value.line(), value.column()); + return stream << String::format("(%zu,%zu)", value.line(), value.column()); } diff --git a/Libraries/LibGUI/GWindow.cpp b/Libraries/LibGUI/GWindow.cpp index 91db8d9de41..556103ff615 100644 --- a/Libraries/LibGUI/GWindow.cpp +++ b/Libraries/LibGUI/GWindow.cpp @@ -338,7 +338,7 @@ void GWindow::paint_keybinds() continue; auto& widget = *keypair.value; bool could_be_keybind = true; - for (int i = 0; i < m_entered_keybind.length(); ++i) { + for (size_t i = 0; i < m_entered_keybind.length(); ++i) { if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) { could_be_keybind = false; break; diff --git a/Libraries/LibGUI/GWindow.h b/Libraries/LibGUI/GWindow.h index ff11e7e10a9..82575d4a14a 100644 --- a/Libraries/LibGUI/GWindow.h +++ b/Libraries/LibGUI/GWindow.h @@ -175,7 +175,7 @@ private: bool m_show_titlebar { true }; bool m_keybind_mode { false }; String m_entered_keybind; - int m_max_keybind_length { 0 }; + size_t m_max_keybind_length { 0 }; HashMap> m_keyboard_activation_targets; bool m_layout_pending { false }; }; diff --git a/Libraries/LibHTML/CSS/StyleResolver.cpp b/Libraries/LibHTML/CSS/StyleResolver.cpp index da73828fdb7..632b52fc31e 100644 --- a/Libraries/LibHTML/CSS/StyleResolver.cpp +++ b/Libraries/LibHTML/CSS/StyleResolver.cpp @@ -100,17 +100,17 @@ static Vector split_on_whitespace(const StringView& string) return {}; Vector v; - int substart = 0; - for (int i = 0; i < string.length(); ++i) { + size_t substart = 0; + for (size_t i = 0; i < string.length(); ++i) { char ch = string.characters_without_null_termination()[i]; if (isspace(ch)) { - int sublen = i - substart; + size_t sublen = i - substart; if (sublen != 0) v.append(string.substring_view(substart, sublen)); substart = i + 1; } } - int taillen = string.length() - substart; + size_t taillen = string.length() - substart; if (taillen != 0) v.append(string.substring_view(substart, taillen)); return v; diff --git a/Libraries/LibHTML/DOMTreeModel.cpp b/Libraries/LibHTML/DOMTreeModel.cpp index 580487645d0..61df349c9d5 100644 --- a/Libraries/LibHTML/DOMTreeModel.cpp +++ b/Libraries/LibHTML/DOMTreeModel.cpp @@ -68,7 +68,7 @@ int DOMTreeModel::column_count(const GModelIndex&) const static String with_whitespace_collapsed(const StringView& string) { StringBuilder builder; - for (int i = 0; i < string.length(); ++i) { + for (size_t i = 0; i < string.length(); ++i) { if (isspace(string[i])) { builder.append(' '); while (i < string.length()) { diff --git a/Libraries/LibHTML/Layout/LayoutText.cpp b/Libraries/LibHTML/Layout/LayoutText.cpp index a3a2ec14f8f..b085bb5f60e 100644 --- a/Libraries/LibHTML/Layout/LayoutText.cpp +++ b/Libraries/LibHTML/Layout/LayoutText.cpp @@ -20,7 +20,7 @@ LayoutText::~LayoutText() static bool is_all_whitespace(const String& string) { - for (int i = 0; i < string.length(); ++i) { + for (size_t i = 0; i < string.length(); ++i) { if (!isspace(string[i])) return false; } diff --git a/Libraries/LibHTML/Parser/CSSParser.cpp b/Libraries/LibHTML/Parser/CSSParser.cpp index ad865ecfbc7..786c713591a 100644 --- a/Libraries/LibHTML/Parser/CSSParser.cpp +++ b/Libraries/LibHTML/Parser/CSSParser.cpp @@ -135,15 +135,15 @@ public: bool next_is(const char* str) const { - int len = strlen(str); - for (int i = 0; i < len; ++i) { + size_t len = strlen(str); + for (size_t i = 0; i < len; ++i) { if (peek(i) != str[i]) return false; } return true; } - char peek(int offset = 0) const + char peek(size_t offset = 0) const { if ((index + offset) < css.length()) return css[index + offset]; @@ -169,7 +169,7 @@ public: bool consume_whitespace_or_comments() { - int original_index = index; + size_t original_index = index; bool in_comment = false; for (; index < css.length(); ++index) { char ch = peek(); @@ -582,7 +582,7 @@ private: CurrentRule current_rule; Vector buffer; - int index = 0; + size_t index = 0; StringView css; }; diff --git a/Libraries/LibHTML/Parser/HTMLParser.cpp b/Libraries/LibHTML/Parser/HTMLParser.cpp index 722350929fc..07c23d723c2 100644 --- a/Libraries/LibHTML/Parser/HTMLParser.cpp +++ b/Libraries/LibHTML/Parser/HTMLParser.cpp @@ -129,8 +129,8 @@ static bool parse_html_document(const StringView& html, Document& document, Pare } }; - for (int i = 0; i < html.length(); ++i) { - auto peek = [&](int offset) -> char { + for (size_t i = 0; i < html.length(); ++i) { + auto peek = [&](size_t offset) -> char { if (i + offset >= html.length()) return '\0'; return html[i + offset]; @@ -356,7 +356,7 @@ RefPtr parse_html_document(const StringView& html, const URL& url) String escape_html_entities(const StringView& html) { StringBuilder builder; - for (int i = 0; i < html.length(); ++i) { + for (size_t i = 0; i < html.length(); ++i) { if (html[i] == '<') builder.append("<"); else if (html[i] == '>') diff --git a/Libraries/LibMarkdown/MDCodeBlock.cpp b/Libraries/LibMarkdown/MDCodeBlock.cpp index 61d518e177c..cf5c01fdad9 100644 --- a/Libraries/LibMarkdown/MDCodeBlock.cpp +++ b/Libraries/LibMarkdown/MDCodeBlock.cpp @@ -33,7 +33,7 @@ String MDCodeBlock::render_to_html() const builder.appendf("", style_language.characters()); // TODO: This should also be done in other places. - for (int i = 0; i < m_code.length(); i++) + for (size_t i = 0; i < m_code.length(); i++) if (m_code[i] == '<') builder.append("<"); else if (m_code[i] == '>') diff --git a/Libraries/LibMarkdown/MDHeading.cpp b/Libraries/LibMarkdown/MDHeading.cpp index 37c15db49b9..77e8a70ac62 100644 --- a/Libraries/LibMarkdown/MDHeading.cpp +++ b/Libraries/LibMarkdown/MDHeading.cpp @@ -38,14 +38,14 @@ bool MDHeading::parse(Vector::ConstIterator& lines) const StringView& line = *lines; - for (m_level = 0; m_level < line.length(); m_level++) - if (line[m_level] != '#') + for (m_level = 0; m_level < (int)line.length(); m_level++) + if (line[(size_t)m_level] != '#') break; - if (m_level >= line.length() || line[m_level] != ' ') + if (m_level >= (int)line.length() || line[(size_t)m_level] != ' ') return false; - StringView title_view = line.substring_view(m_level + 1, line.length() - m_level - 1); + StringView title_view = line.substring_view((size_t)m_level + 1, line.length() - (size_t)m_level - 1); bool success = m_text.parse(title_view); ASSERT(success); diff --git a/Libraries/LibMarkdown/MDList.cpp b/Libraries/LibMarkdown/MDList.cpp index c6072b98f6b..096d8caac54 100644 --- a/Libraries/LibMarkdown/MDList.cpp +++ b/Libraries/LibMarkdown/MDList.cpp @@ -49,7 +49,7 @@ bool MDList::parse(Vector::ConstIterator& lines) break; bool appears_unordered = false; - int offset = 0; + size_t offset = 0; if (line.length() > 2) if (line[1] == ' ' && (line[0] == '*' || line[0] == '-')) { appears_unordered = true; @@ -57,7 +57,7 @@ bool MDList::parse(Vector::ConstIterator& lines) } bool appears_ordered = false; - for (int i = 0; i < 10 && i < line.length(); i++) { + for (size_t i = 0; i < 10 && i < line.length(); i++) { char ch = line[i]; if ('0' <= ch && ch <= '9') continue; diff --git a/Libraries/LibMarkdown/MDText.cpp b/Libraries/LibMarkdown/MDText.cpp index 3912e122b11..795a74da1fc 100644 --- a/Libraries/LibMarkdown/MDText.cpp +++ b/Libraries/LibMarkdown/MDText.cpp @@ -4,7 +4,7 @@ static String unescape(const StringView& text) { StringBuilder builder; - for (int i = 0; i < text.length(); ++i) { + for (size_t i = 0; i < text.length(); ++i) { if (text[i] == '\\' && i != text.length() - 1) { builder.append(text[i + 1]); i++; @@ -125,10 +125,10 @@ String MDText::render_for_terminal() const bool MDText::parse(const StringView& str) { Style current_style; - int current_span_start = 0; + size_t current_span_start = 0; int first_span_in_the_current_link = -1; - auto append_span_if_needed = [&](int offset) { + auto append_span_if_needed = [&](size_t offset) { if (current_span_start != offset) { Span span { unescape(str.substring_view(current_span_start, offset - current_span_start)), @@ -138,7 +138,7 @@ bool MDText::parse(const StringView& str) } }; - for (int offset = 0; offset < str.length(); offset++) { + for (size_t offset = 0; offset < str.length(); offset++) { char ch = str[offset]; bool is_escape = ch == '\\'; @@ -179,7 +179,7 @@ bool MDText::parse(const StringView& str) offset++; ASSERT(str[offset] == '('); offset++; - int start_of_href = offset; + size_t start_of_href = offset; do offset++; diff --git a/Libraries/LibPCIDB/Database.cpp b/Libraries/LibPCIDB/Database.cpp index 6568a988c20..dc86af6ad25 100644 --- a/Libraries/LibPCIDB/Database.cpp +++ b/Libraries/LibPCIDB/Database.cpp @@ -89,12 +89,12 @@ u8 parse_hex_digit(char digit) } template -T parse_hex(StringView str, int count) +T parse_hex(StringView str, size_t count) { ASSERT(str.length() >= count); T res = 0; - for (int i = 0; i < count; i++) + for (size_t i = 0; i < count; i++) res = (res << 4) + parse_hex_digit(str[i]); return res; diff --git a/Libraries/LibVT/Terminal.cpp b/Libraries/LibVT/Terminal.cpp index 1de5206acdd..61b72447d46 100644 --- a/Libraries/LibVT/Terminal.cpp +++ b/Libraries/LibVT/Terminal.cpp @@ -837,7 +837,7 @@ void Terminal::on_char(u8 ch) void Terminal::inject_string(const StringView& str) { - for (int i = 0; i < str.length(); ++i) + for (size_t i = 0; i < str.length(); ++i) on_char(str[i]); } diff --git a/Servers/TelnetServer/Client.cpp b/Servers/TelnetServer/Client.cpp index 76d476113b6..459c8c10293 100644 --- a/Servers/TelnetServer/Client.cpp +++ b/Servers/TelnetServer/Client.cpp @@ -1,3 +1,4 @@ +#include "Client.h" #include #include #include @@ -7,8 +8,7 @@ #include #include #include - -#include "Client.h" +#include Client::Client(int id, RefPtr socket, int ptm_fd) : m_id(id) @@ -56,7 +56,7 @@ void Client::drain_pty() quit(); return; } - send_data(StringView(buffer, nread)); + send_data(StringView(buffer, (size_t)nread)); } void Client::handle_data(const StringView& data) @@ -105,7 +105,7 @@ void Client::handle_error() void Client::send_data(StringView data) { bool fast = true; - for (int i = 0; i < data.length(); i++) { + for (size_t i = 0; i < data.length(); i++) { u8 c = data[i]; if (c == '\n' || c == 0xff) fast = false; @@ -117,7 +117,7 @@ void Client::send_data(StringView data) } StringBuilder builder; - for (int i = 0; i < data.length(); i++) { + for (size_t i = 0; i < data.length(); i++) { u8 c = data[i]; switch (c) { @@ -136,7 +136,6 @@ void Client::send_data(StringView data) m_socket->write(builder.to_string()); } - void Client::send_command(Command command) { send_commands({ command }); diff --git a/Servers/TelnetServer/Parser.cpp b/Servers/TelnetServer/Parser.cpp index 6c05ce7e2e4..09d2250efaf 100644 --- a/Servers/TelnetServer/Parser.cpp +++ b/Servers/TelnetServer/Parser.cpp @@ -6,7 +6,7 @@ void Parser::write(const StringView& data) { - for (int i = 0; i < data.length(); i++) { + for (size_t i = 0; i < data.length(); i++) { u8 ch = data[i]; switch (m_state) { diff --git a/Shell/LineEditor.cpp b/Shell/LineEditor.cpp index 7a7f404f552..af4878bc4fa 100644 --- a/Shell/LineEditor.cpp +++ b/Shell/LineEditor.cpp @@ -21,7 +21,7 @@ void LineEditor::add_to_history(const String& line) void LineEditor::clear_line() { - for (int i = 0; i < m_cursor; ++i) + for (size_t i = 0; i < m_cursor; ++i) fputc(0x8, stdout); fputs("\033[K", stdout); fflush(stdout); @@ -31,10 +31,10 @@ void LineEditor::clear_line() void LineEditor::append(const String& string) { - m_buffer.append(string.characters(), string.length()); + m_buffer.append(string.characters(), (int)string.length()); fputs(string.characters(), stdout); fflush(stdout); - m_cursor = m_buffer.size(); + m_cursor = (size_t)m_buffer.size(); } void LineEditor::cache_path() @@ -68,9 +68,9 @@ void LineEditor::cache_path() quick_sort(m_path.begin(), m_path.end(), AK::is_less_than); } -void LineEditor::cut_mismatching_chars(String& completion, const String& program, int token_length) +void LineEditor::cut_mismatching_chars(String& completion, const String& program, size_t token_length) { - int i = token_length; + size_t i = token_length; while (i < completion.length() && i < program.length() && completion[i] == program[i]) ++i; completion = completion.substring(0, i); @@ -139,17 +139,17 @@ String LineEditor::get_line(const String& prompt) } auto do_delete = [&] { - if (m_cursor == m_buffer.size()) { + if (m_cursor == (size_t)m_buffer.size()) { fputc('\a', stdout); fflush(stdout); return; } - m_buffer.remove(m_cursor - 1); + m_buffer.remove((int)m_cursor - 1); fputs("\033[3~", stdout); fflush(stdout); vt_save_cursor(); vt_clear_to_end_of_line(); - for (int i = m_cursor; i < m_buffer.size(); ++i) + for (size_t i = m_cursor; i < (size_t)m_buffer.size(); ++i) fputc(m_buffer[i], stdout); vt_restore_cursor(); }; @@ -195,7 +195,7 @@ String LineEditor::get_line(const String& prompt) m_state = InputState::Free; continue; case 'C': // right - if (m_cursor < m_buffer.size()) { + if (m_cursor < (size_t)m_buffer.size()) { ++m_cursor; fputs("\033[C", stdout); fflush(stdout); @@ -204,17 +204,17 @@ String LineEditor::get_line(const String& prompt) continue; case 'H': if (m_cursor > 0) { - fprintf(stdout, "\033[%dD", m_cursor); + fprintf(stdout, "\033[%zuD", m_cursor); fflush(stdout); m_cursor = 0; } m_state = InputState::Free; continue; case 'F': - if (m_cursor < m_buffer.size()) { - fprintf(stdout, "\033[%dC", m_buffer.size() - m_cursor); + if (m_cursor < (size_t)m_buffer.size()) { + fprintf(stdout, "\033[%zuC", (size_t)m_buffer.size() - m_cursor); fflush(stdout); - m_cursor = m_buffer.size(); + m_cursor = (size_t)m_buffer.size(); } m_state = InputState::Free; continue; @@ -264,13 +264,13 @@ String LineEditor::get_line(const String& prompt) fflush(stdout); return; } - m_buffer.remove(m_cursor - 1); + m_buffer.remove((int)m_cursor - 1); --m_cursor; putchar(8); vt_save_cursor(); vt_clear_to_end_of_line(); - for (int i = m_cursor; i < m_buffer.size(); ++i) - fputc(m_buffer[i], stdout); + for (size_t i = m_cursor; i < (size_t)m_buffer.size(); ++i) + fputc(m_buffer[(int)i], stdout); vt_restore_cursor(); }; @@ -281,7 +281,7 @@ String LineEditor::get_line(const String& prompt) if (ch == g.termios.c_cc[VWERASE]) { bool has_seen_nonspace = false; while (m_cursor > 0) { - if (isspace(m_buffer[m_cursor - 1])) { + if (isspace(m_buffer[(int)m_cursor - 1])) { if (has_seen_nonspace) break; } else { @@ -301,14 +301,14 @@ String LineEditor::get_line(const String& prompt) fputs(prompt.characters(), stdout); for (int i = 0; i < m_buffer.size(); ++i) fputc(m_buffer[i], stdout); - if (m_cursor < m_buffer.size()) - printf("\033[%dD", m_buffer.size() - m_cursor); // Move cursor N steps left. + if (m_cursor < (size_t)m_buffer.size()) + printf("\033[%zuD", (size_t)m_buffer.size() - m_cursor); // Move cursor N steps left. fflush(stdout); continue; } if (ch == 0x01) { // ^A if (m_cursor > 0) { - printf("\033[%dD", m_cursor); + printf("\033[%zuD", m_cursor); fflush(stdout); m_cursor = 0; } @@ -322,10 +322,10 @@ String LineEditor::get_line(const String& prompt) continue; } if (ch == 0x05) { // ^E - if (m_cursor < m_buffer.size()) { - printf("\033[%dC", m_buffer.size() - m_cursor); + if (m_cursor < (size_t)m_buffer.size()) { + printf("\033[%zuC", (size_t)m_buffer.size() - m_cursor); fflush(stdout); - m_cursor = m_buffer.size(); + m_cursor = (size_t)m_buffer.size(); } continue; } @@ -337,17 +337,17 @@ String LineEditor::get_line(const String& prompt) return string; } - if (m_cursor == m_buffer.size()) { + if (m_cursor == (size_t)m_buffer.size()) { m_buffer.append(ch); ++m_cursor; continue; } vt_save_cursor(); vt_clear_to_end_of_line(); - for (int i = m_cursor; i < m_buffer.size(); ++i) - fputc(m_buffer[i], stdout); + for (size_t i = m_cursor; i < (size_t)m_buffer.size(); ++i) + fputc(m_buffer[(int)i], stdout); vt_restore_cursor(); - m_buffer.insert(m_cursor, move(ch)); + m_buffer.insert((int)m_cursor, move(ch)); ++m_cursor; } } diff --git a/Shell/LineEditor.h b/Shell/LineEditor.h index 3c2f64d0d89..2f502d35414 100644 --- a/Shell/LineEditor.h +++ b/Shell/LineEditor.h @@ -23,14 +23,14 @@ public: private: void clear_line(); void append(const String&); - void cut_mismatching_chars(String& completion, const String& program, int token_length); + void cut_mismatching_chars(String& completion, const String& program, size_t token_length); void tab_complete_first_token(); void vt_save_cursor(); void vt_restore_cursor(); void vt_clear_to_end_of_line(); Vector m_buffer; - int m_cursor { 0 }; + size_t m_cursor { 0 }; // FIXME: This should be something more take_first()-friendly. Vector m_history; diff --git a/Shell/Parser.cpp b/Shell/Parser.cpp index 8cf09e5c9df..bdb8911b6ab 100644 --- a/Shell/Parser.cpp +++ b/Shell/Parser.cpp @@ -48,7 +48,7 @@ void Parser::begin_redirect_write(int fd) Vector Parser::parse() { - for (int i = 0; i < m_input.length(); ++i) { + for (size_t i = 0; i < m_input.length(); ++i) { char ch = m_input.characters()[i]; switch (m_state) { case State::Free: @@ -107,7 +107,7 @@ Vector Parser::parse() // redirection from zsh-style multi-digit fd, such as {10}>file if (ch == '{') { bool is_multi_fd_redirection = false; - int redir_end = i + 1; + size_t redir_end = i + 1; while (redir_end < m_input.length()) { char lookahead_ch = m_input.characters()[redir_end]; diff --git a/Shell/main.cpp b/Shell/main.cpp index 916693332b8..0b98cdb7814 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -457,7 +457,7 @@ struct CommandTimer { static bool is_glob(const StringView& s) { - for (int i = 0; i < s.length(); i++) { + for (size_t i = 0; i < s.length(); i++) { char c = s.characters_without_null_termination()[i]; if (c == '*' || c == '?') return true; @@ -469,19 +469,19 @@ static Vector split_path(const StringView& path) { Vector parts; - ssize_t substart = 0; - for (ssize_t i = 0; i < path.length(); i++) { + size_t substart = 0; + for (size_t i = 0; i < path.length(); i++) { char ch = path.characters_without_null_termination()[i]; if (ch != '/') continue; - ssize_t sublen = i - substart; + size_t sublen = i - substart; if (sublen != 0) parts.append(path.substring_view(substart, sublen)); parts.append(path.substring_view(i, 1)); substart = i + 1; } - ssize_t taillen = path.length() - substart; + size_t taillen = path.length() - substart; if (taillen != 0) parts.append(path.substring_view(substart, taillen)); diff --git a/Userland/ls.cpp b/Userland/ls.cpp index fff7188ba26..96f00b68205 100644 --- a/Userland/ls.cpp +++ b/Userland/ls.cpp @@ -28,8 +28,8 @@ static bool flag_human_readable = false; static bool flag_sort_by_timestamp = false; static bool flag_reverse_sort = false; -static int terminal_rows = 0; -static int terminal_columns = 0; +static size_t terminal_rows = 0; +static size_t terminal_columns = 0; static bool output_is_terminal = false; int main(int argc, char** argv) @@ -326,7 +326,7 @@ int do_file_system_object_short(const char* path) } Vector names; - int longest_name = 0; + size_t longest_name = 0; while (di.has_next()) { String name = di.next_path(); names.append(name); @@ -335,7 +335,7 @@ int do_file_system_object_short(const char* path) } quick_sort(names.begin(), names.end(), [](auto& a, auto& b) { return a < b; }); - int printed_on_row = 0; + size_t printed_on_row = 0; int nprinted; for (int i = 0; i < names.size(); ++i) { auto& name = names[i]; @@ -352,10 +352,10 @@ int do_file_system_object_short(const char* path) // The offset must be at least 2 because: // - With each file an aditional char is printed e.g. '@','*'. // - Each filename must be separated by a space. - int column_width = longest_name + (offset > 0 ? offset : 2); + size_t column_width = longest_name + (offset > 0 ? offset : 2); printed_on_row += column_width; - for (int j = nprinted; i != (names.size() - 1) && j < column_width; ++j) + for (int j = nprinted; i != (names.size() - 1) && j < (int)column_width; ++j) printf(" "); if ((printed_on_row + column_width) >= terminal_columns) { printf("\n"); diff --git a/Userland/man.cpp b/Userland/man.cpp index e9ffdc823ed..8278cd2cc3a 100644 --- a/Userland/man.cpp +++ b/Userland/man.cpp @@ -57,7 +57,7 @@ int main(int argc, char* argv[]) } dbg() << "Loading man page from " << file->filename(); auto buffer = file->read_all(); - String source { (char*)buffer.data(), buffer.size() }; + String source { (const char*)buffer.data(), (size_t)buffer.size() }; printf("%s(%s)\t\tSerenity manual\n", name.characters(), section.characters()); diff --git a/Userland/md.cpp b/Userland/md.cpp index cf76c6f89d7..31ef0fe4219 100644 --- a/Userland/md.cpp +++ b/Userland/md.cpp @@ -30,7 +30,7 @@ int main(int argc, char* argv[]) auto buffer = file->read_all(); dbg() << "Read size " << buffer.size(); - String input { (char*)buffer.data(), buffer.size() }; + String input { (const char*)buffer.data(), (size_t)buffer.size() }; MDDocument document; success = document.parse(input);