Skip to content

Commit

Permalink
AK: Use size_t for the length of strings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
awesomekling committed Dec 9, 2019
1 parent 1726c17 commit 6f4c380
Show file tree
Hide file tree
Showing 54 changed files with 387 additions and 377 deletions.
8 changes: 7 additions & 1 deletion AK/BufferStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ class BufferStream {
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())) {
Expand Down Expand Up @@ -200,7 +206,7 @@ class BufferStream {

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;
}
Expand Down
4 changes: 2 additions & 2 deletions AK/JsonParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ String JsonParser::consume_quoted_string()
Vector<char, 1024> buffer;

for (;;) {
int peek_index = m_index;
size_t peek_index = m_index;
char ch = 0;
for (;;) {
if (peek_index == m_input.length())
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion AK/JsonParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class JsonParser {
void consume_while(C);

StringView m_input;
int m_index { 0 };
size_t m_index { 0 };

String m_last_string_starting_with_character[256];
};
Expand Down
28 changes: 14 additions & 14 deletions AK/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand All @@ -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 {};
Expand All @@ -101,23 +101,23 @@ Vector<String> String::split(const char separator) const
return split_limit(separator, 0);
}

Vector<String> String::split_limit(const char separator, int limit) const
Vector<String> String::split_limit(const char separator, size_t limit) const
{
if (is_empty())
return {};

Vector<String> 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)
Expand All @@ -131,17 +131,17 @@ Vector<StringView> String::split_view(const char separator, bool keep_empty) con
return {};

Vector<StringView> 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)
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 7 additions & 7 deletions AK/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class String {
{
}

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))
{
}
Expand Down Expand Up @@ -89,7 +89,7 @@ class String {
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!
Expand All @@ -112,18 +112,18 @@ class String {

bool contains(const String&) const;

Vector<String> split_limit(char separator, int limit) const;
Vector<String> split_limit(char separator, size_t limit) const;
Vector<String> split(char separator) const;
String substring(int start, int length) const;
String substring(size_t start, size_t length) const;

Vector<StringView> 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];
Expand Down
12 changes: 6 additions & 6 deletions AK/StringBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions AK/StringBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -27,14 +27,14 @@ class StringBuilder {
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 };
};

}
Expand Down
18 changes: 9 additions & 9 deletions AK/StringImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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
Expand All @@ -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> StringImpl::create_uninitialized(int length, char*& buffer)
NonnullRefPtr<StringImpl> StringImpl::create_uninitialized(size_t length, char*& buffer)
{
ASSERT(length);
void* slot = kmalloc(allocation_size_for_stringimpl(length));
Expand All @@ -71,7 +71,7 @@ NonnullRefPtr<StringImpl> StringImpl::create_uninitialized(int length, char*& bu
return new_stringimpl;
}

RefPtr<StringImpl> StringImpl::create(const char* cstring, int length, ShouldChomp should_chomp)
RefPtr<StringImpl> StringImpl::create(const char* cstring, size_t length, ShouldChomp should_chomp)
{
if (!cstring)
return nullptr;
Expand Down Expand Up @@ -133,7 +133,7 @@ static inline char to_ascii_uppercase(char c)

NonnullRefPtr<StringImpl> 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;
}
Expand All @@ -142,14 +142,14 @@ NonnullRefPtr<StringImpl> 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> 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;
}
Expand All @@ -158,7 +158,7 @@ NonnullRefPtr<StringImpl> 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;
}
Expand Down
18 changes: 9 additions & 9 deletions AK/StringImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ enum ShouldChomp {

class StringImpl : public RefCounted<StringImpl> {
public:
static NonnullRefPtr<StringImpl> create_uninitialized(int length, char*& buffer);
static NonnullRefPtr<StringImpl> create_uninitialized(size_t length, char*& buffer);
static RefPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
static RefPtr<StringImpl> create(const char* cstring, int length, ShouldChomp = NoChomp);
static RefPtr<StringImpl> create(const char* cstring, size_t length, ShouldChomp = NoChomp);
NonnullRefPtr<StringImpl> to_lowercase() const;
NonnullRefPtr<StringImpl> to_uppercase() const;

Expand All @@ -29,11 +29,11 @@ class StringImpl : public RefCounted<StringImpl> {

~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];
}

Expand All @@ -56,20 +56,20 @@ class StringImpl : public RefCounted<StringImpl> {
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);
Expand Down
Loading

0 comments on commit 6f4c380

Please sign in to comment.