1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 08:47:34 +00:00

StringView: Rename characters() to characters_without_null_termination().

This should make you think twice before trying to use the const char* from
a StringView as if it's a null-terminated string.
This commit is contained in:
Andreas Kling 2019-07-08 15:38:44 +02:00
parent 567551bc12
commit 0e75aba7c3
21 changed files with 57 additions and 46 deletions

View file

@ -41,7 +41,7 @@ public:
if (view.m_impl)
m_impl = *view.m_impl;
else
m_impl = StringImpl::create(view.characters(), view.length());
m_impl = StringImpl::create(view.characters_without_null_termination(), view.length());
}
String(const String& other)

View file

@ -12,7 +12,7 @@ const LogStream& operator<<(const LogStream& stream, const String& value)
const LogStream& operator<<(const LogStream& stream, const StringView& value)
{
stream.write(value.characters(), value.length());
stream.write(value.characters_without_null_termination(), value.length());
return stream;
}

View file

@ -198,7 +198,7 @@ bool String::starts_with(const StringView& str) const
return false;
if (str.length() > length())
return false;
return !memcmp(characters(), str.characters(), str.length());
return !memcmp(characters(), str.characters_without_null_termination(), str.length());
}
bool String::ends_with(const StringView& str) const
@ -209,7 +209,7 @@ bool String::ends_with(const StringView& str) const
return false;
if (str.length() > length())
return false;
return !memcmp(characters() + (length() - str.length()), str.characters(), str.length());
return !memcmp(characters() + (length() - str.length()), str.characters_without_null_termination(), str.length());
}
String String::repeated(char ch, int count)
@ -239,7 +239,7 @@ bool String::match_helper(const StringView& mask) const
return false;
const char* string_ptr = characters();
const char* mask_ptr = mask.characters();
const char* mask_ptr = mask.characters_without_null_termination();
const char* mask_end = mask_ptr + mask.length();
// Match string against mask directly unless we hit a *

View file

@ -21,7 +21,7 @@ void StringBuilder::append(const StringView& str)
if (str.is_empty())
return;
will_append(str.length());
memcpy(m_buffer.pointer() + m_length, str.characters(), str.length());
memcpy(m_buffer.pointer() + m_length, str.characters_without_null_termination(), str.length());
m_length += str.length();
}

View file

@ -24,7 +24,7 @@ Vector<StringView> StringView::split_view(const char separator) const
Vector<StringView> v;
ssize_t substart = 0;
for (ssize_t i = 0; i < length(); ++i) {
char ch = characters()[i];
char ch = characters_without_null_termination()[i];
if (ch == separator) {
ssize_t sublen = i - substart;
if (sublen != 0)
@ -35,7 +35,7 @@ Vector<StringView> StringView::split_view(const char separator) const
ssize_t taillen = length() - substart;
if (taillen != 0)
v.append(substring_view(substart, taillen));
if (characters()[length() - 1] == separator)
if (characters_without_null_termination()[length() - 1] == separator)
v.append(String::empty());
return v;
}
@ -50,7 +50,7 @@ StringView StringView::substring_view(int start, int length) const
StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
{
const char* remaining_characters = substring.characters();
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);
@ -59,7 +59,7 @@ StringView StringView::substring_view_starting_from_substring(const StringView&
StringView StringView::substring_view_starting_after_substring(const StringView& substring) const
{
const char* remaining_characters = substring.characters() + substring.length();
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);
@ -70,12 +70,12 @@ unsigned StringView::to_uint(bool& ok) const
{
unsigned value = 0;
for (ssize_t i = 0; i < length(); ++i) {
if (characters()[i] < '0' || characters()[i] > '9') {
if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
ok = false;
return 0;
}
value = value * 10;
value += characters()[i] - '0';
value += characters_without_null_termination()[i] - '0';
}
ok = true;
return value;

View file

@ -35,7 +35,7 @@ public:
bool is_null() const { return !m_characters; }
bool is_empty() const { return m_length == 0; }
const char* characters() const { return m_characters; }
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]; }