Skip to content

Commit 1c43f6f

Browse files
committed
CString.isEmpty() for String.utf8() returns true when String has a single character
https://bugs.webkit.org/show_bug.cgi?id=303428 Reviewed by Darin Adler. isEmpty was accounting for a null terminator in the CStringBuffer length but it is not like that. Test: Tools/TestWebKitAPI/Tests/WTF/CString.cpp * Source/WTF/wtf/text/CString.h: * Tools/TestWebKitAPI/Tests/WTF/CString.cpp: (TEST(WTF, CStringEmptyRegularConstructor)): (TEST(WTF, CStringOnyByte)): Canonical link: https://commits.webkit.org/303840@main
1 parent e2f5609 commit 1c43f6f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Source/WTF/wtf/text/CString.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class CString final {
9595
size_t length() const;
9696

9797
bool isNull() const { return !m_buffer; }
98-
bool isEmpty() const { return isNull() || m_buffer->length() <= 1; }
98+
bool isEmpty() const { return isNull() || !m_buffer->length(); }
9999
bool isSafeToSendToAnotherThread() const;
100100

101101
CStringBuffer* buffer() const LIFETIME_BOUND { return m_buffer.get(); }

Tools/TestWebKitAPI/Tests/WTF/CString.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ TEST(WTF, CStringEmptyRegularConstructor)
8787
ASSERT_STREQ(referenceString, stringWithLength.data());
8888
}
8989

90+
TEST(WTF, CStringOneByte)
91+
{
92+
const char* referenceString = "W";
93+
94+
CString string(referenceString);
95+
ASSERT_FALSE(string.isNull());
96+
ASSERT_FALSE(string.isEmpty());
97+
ASSERT_EQ(string.length(), strlen(referenceString));
98+
ASSERT_STREQ(referenceString, string.data());
99+
100+
CString stringWithLength(std::span { referenceString, 1 });
101+
ASSERT_FALSE(stringWithLength.isNull());
102+
ASSERT_FALSE(stringWithLength.isEmpty());
103+
ASSERT_EQ(stringWithLength.length(), strlen(referenceString));
104+
ASSERT_STREQ(referenceString, stringWithLength.data());
105+
}
106+
90107
TEST(WTF, CStringUninitializedConstructor)
91108
{
92109
std::span<char> buffer;

0 commit comments

Comments
 (0)