AK: Make Utf8View constructors inline and remove C string constructor
Using StringView instead of C strings is basically always preferable. The only reason to use a C string is because you are calling a C API.
This commit is contained in:
parent
291dbff2e1
commit
1be4cbd639
Notes:
sideshowbarker
2024-07-18 03:43:17 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/1be4cbd6395
9 changed files with 36 additions and 42 deletions
|
@ -11,21 +11,6 @@
|
|||
|
||||
namespace AK {
|
||||
|
||||
Utf8View::Utf8View(const String& string)
|
||||
: m_string(string)
|
||||
{
|
||||
}
|
||||
|
||||
Utf8View::Utf8View(const StringView& string)
|
||||
: m_string(string)
|
||||
{
|
||||
}
|
||||
|
||||
Utf8View::Utf8View(const char* string)
|
||||
: m_string(string)
|
||||
{
|
||||
}
|
||||
|
||||
const unsigned char* Utf8View::begin_ptr() const
|
||||
{
|
||||
return (const unsigned char*)m_string.characters_without_null_termination();
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
|
@ -53,9 +54,17 @@ public:
|
|||
using Iterator = Utf8CodePointIterator;
|
||||
|
||||
Utf8View() = default;
|
||||
explicit Utf8View(const String&);
|
||||
explicit Utf8View(const StringView&);
|
||||
explicit Utf8View(const char*);
|
||||
|
||||
explicit Utf8View(String& string)
|
||||
: m_string(string.view())
|
||||
{
|
||||
}
|
||||
|
||||
explicit Utf8View(StringView string)
|
||||
: m_string(string)
|
||||
{
|
||||
}
|
||||
|
||||
~Utf8View() = default;
|
||||
|
||||
explicit Utf8View(String&&) = delete;
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
TEST_CASE(decode_ascii)
|
||||
{
|
||||
Utf8View utf8 { "Hello World!11" };
|
||||
Utf8View utf8 { "Hello World!11"sv };
|
||||
EXPECT(utf8.validate());
|
||||
|
||||
u32 expected[] = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 49, 49 };
|
||||
|
@ -28,7 +28,7 @@ TEST_CASE(decode_ascii)
|
|||
|
||||
TEST_CASE(decode_utf8)
|
||||
{
|
||||
Utf8View utf8 { "Привет, мир! 😀 γειά σου κόσμος こんにちは世界" };
|
||||
Utf8View utf8 { "Привет, мир! 😀 γειά σου κόσμος こんにちは世界"sv };
|
||||
size_t valid_bytes;
|
||||
EXPECT(utf8.validate(valid_bytes));
|
||||
EXPECT(valid_bytes == (size_t)utf8.byte_length());
|
||||
|
@ -52,29 +52,29 @@ TEST_CASE(validate_invalid_ut8)
|
|||
{
|
||||
size_t valid_bytes;
|
||||
char invalid_utf8_1[] = { 42, 35, (char)182, 9, 0 };
|
||||
Utf8View utf8_1 { invalid_utf8_1 };
|
||||
Utf8View utf8_1 { StringView { invalid_utf8_1 } };
|
||||
EXPECT(!utf8_1.validate(valid_bytes));
|
||||
EXPECT(valid_bytes == 2);
|
||||
|
||||
char invalid_utf8_2[] = { 42, 35, (char)208, (char)208, 0 };
|
||||
Utf8View utf8_2 { invalid_utf8_2 };
|
||||
Utf8View utf8_2 { StringView { invalid_utf8_2 } };
|
||||
EXPECT(!utf8_2.validate(valid_bytes));
|
||||
EXPECT(valid_bytes == 2);
|
||||
|
||||
char invalid_utf8_3[] = { (char)208, 0 };
|
||||
Utf8View utf8_3 { invalid_utf8_3 };
|
||||
Utf8View utf8_3 { StringView { invalid_utf8_3 } };
|
||||
EXPECT(!utf8_3.validate(valid_bytes));
|
||||
EXPECT(valid_bytes == 0);
|
||||
|
||||
char invalid_utf8_4[] = { (char)208, 35, 0 };
|
||||
Utf8View utf8_4 { invalid_utf8_4 };
|
||||
Utf8View utf8_4 { StringView { invalid_utf8_4 } };
|
||||
EXPECT(!utf8_4.validate(valid_bytes));
|
||||
EXPECT(valid_bytes == 0);
|
||||
}
|
||||
|
||||
TEST_CASE(iterate_utf8)
|
||||
{
|
||||
Utf8View view("Some weird characters \u00A9\u266A\uA755");
|
||||
Utf8View view("Some weird characters \u00A9\u266A\uA755"sv);
|
||||
Utf8CodePointIterator iterator = view.begin();
|
||||
|
||||
EXPECT(*iterator == 'S');
|
||||
|
@ -113,7 +113,7 @@ TEST_CASE(decode_invalid_ut8)
|
|||
// Test case 1 : Getting an extension byte as first byte of the code point
|
||||
{
|
||||
char raw_data[] = { 'a', 'b', (char)0xA0, 'd', 0 };
|
||||
Utf8View view { raw_data };
|
||||
Utf8View view { StringView { raw_data } };
|
||||
u32 expected_characters[] = { 'a', 'b', 0xFFFD, 'd' };
|
||||
String expected_underlying_bytes[] = { "a", "b", "\xA0", "d" };
|
||||
size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]);
|
||||
|
@ -131,7 +131,7 @@ TEST_CASE(decode_invalid_ut8)
|
|||
// Test case 2 : Getting a non-extension byte when an extension byte is expected
|
||||
{
|
||||
char raw_data[] = { 'a', 'b', (char)0xC0, 'd', 'e', 0 };
|
||||
Utf8View view { raw_data };
|
||||
Utf8View view { StringView { raw_data } };
|
||||
u32 expected_characters[] = { 'a', 'b', 0xFFFD, 'd', 'e' };
|
||||
String expected_underlying_bytes[] = { "a", "b", "\xC0", "d", "e" };
|
||||
size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]);
|
||||
|
@ -149,7 +149,7 @@ TEST_CASE(decode_invalid_ut8)
|
|||
// Test case 3 : Not enough bytes before the end of the string
|
||||
{
|
||||
char raw_data[] = { 'a', 'b', (char)0x90, 'd', 0 };
|
||||
Utf8View view { raw_data };
|
||||
Utf8View view { StringView { raw_data } };
|
||||
u32 expected_characters[] = { 'a', 'b', 0xFFFD, 'd' };
|
||||
String expected_underlying_bytes[] = { "a", "b", "\x90", "d" };
|
||||
size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]);
|
||||
|
@ -167,7 +167,7 @@ TEST_CASE(decode_invalid_ut8)
|
|||
// Test case 4 : Not enough bytes at the end of the string
|
||||
{
|
||||
char raw_data[] = { 'a', 'b', 'c', (char)0x90, 0 };
|
||||
Utf8View view { raw_data };
|
||||
Utf8View view { StringView { raw_data } };
|
||||
u32 expected_characters[] = { 'a', 'b', 'c', 0xFFFD };
|
||||
String expected_underlying_bytes[] = { "a", "b", "c", "\x90" };
|
||||
size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]);
|
||||
|
@ -185,33 +185,33 @@ TEST_CASE(decode_invalid_ut8)
|
|||
|
||||
TEST_CASE(trim)
|
||||
{
|
||||
Utf8View whitespace { " " };
|
||||
Utf8View whitespace { " "sv };
|
||||
{
|
||||
Utf8View view { "word" };
|
||||
Utf8View view { "word"sv };
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Both).as_string(), "word");
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Left).as_string(), "word");
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Right).as_string(), "word");
|
||||
}
|
||||
{
|
||||
Utf8View view { " word" };
|
||||
Utf8View view { " word"sv };
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Both).as_string(), "word");
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Left).as_string(), "word");
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Right).as_string(), " word");
|
||||
}
|
||||
{
|
||||
Utf8View view { "word " };
|
||||
Utf8View view { "word "sv };
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Both).as_string(), "word");
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Left).as_string(), "word ");
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Right).as_string(), "word");
|
||||
}
|
||||
{
|
||||
Utf8View view { " word " };
|
||||
Utf8View view { " word "sv };
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Both).as_string(), "word");
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Left).as_string(), "word ");
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Right).as_string(), " word");
|
||||
}
|
||||
{
|
||||
Utf8View view { "\u180E" };
|
||||
Utf8View view { "\u180E"sv };
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Both).as_string(), "\u180E");
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Left).as_string(), "\u180E");
|
||||
EXPECT_EQ(view.trim(whitespace, TrimMode::Right).as_string(), "\u180E");
|
||||
|
|
|
@ -256,7 +256,7 @@ TEST_CASE(char_utf8)
|
|||
Regex<PosixExtended> re("😀");
|
||||
RegexResult result;
|
||||
|
||||
EXPECT_EQ((result = match(Utf8View { "Привет, мир! 😀 γειά σου κόσμος 😀 こんにちは世界" }, re, PosixFlags::Global)).success, true);
|
||||
EXPECT_EQ((result = match(Utf8View { "Привет, мир! 😀 γειά σου κόσμος 😀 こんにちは世界"sv }, re, PosixFlags::Global)).success, true);
|
||||
EXPECT_EQ(result.count, 2u);
|
||||
}
|
||||
|
||||
|
|
|
@ -187,7 +187,7 @@ void WindowServerConnection::key_down(i32 window_id, u32 code_point, u32 key, u3
|
|||
key_event->m_key = Key_Invalid;
|
||||
key_event->m_modifiers = 0;
|
||||
|
||||
Utf8View m_utf8_view(emoji_input_dialog->selected_emoji_text().characters());
|
||||
Utf8View m_utf8_view(emoji_input_dialog->selected_emoji_text());
|
||||
u32 emoji_code_point = *m_utf8_view.begin();
|
||||
|
||||
key_event->m_code_point = emoji_code_point;
|
||||
|
|
|
@ -533,7 +533,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::pad_end)
|
|||
return pad_string(global_object, move(string), PadPlacement::End);
|
||||
}
|
||||
|
||||
static const Utf8View whitespace_characters = Utf8View("\x09\x0A\x0B\x0C\x0D\x20\xC2\xA0\xE1\x9A\x80\xE2\x80\x80\xE2\x80\x81\xE2\x80\x82\xE2\x80\x83\xE2\x80\x84\xE2\x80\x85\xE2\x80\x86\xE2\x80\x87\xE2\x80\x88\xE2\x80\x89\xE2\x80\x8A\xE2\x80\xAF\xE2\x81\x9F\xE3\x80\x80\xE2\x80\xA8\xE2\x80\xA9\xEF\xBB\xBF");
|
||||
static Utf8View const whitespace_characters = Utf8View("\x09\x0A\x0B\x0C\x0D\x20\xC2\xA0\xE1\x9A\x80\xE2\x80\x80\xE2\x80\x81\xE2\x80\x82\xE2\x80\x83\xE2\x80\x84\xE2\x80\x85\xE2\x80\x86\xE2\x80\x87\xE2\x80\x88\xE2\x80\x89\xE2\x80\x8A\xE2\x80\xAF\xE2\x81\x9F\xE3\x80\x80\xE2\x80\xA8\xE2\x80\xA9\xEF\xBB\xBF"sv);
|
||||
|
||||
// 22.1.3.29 String.prototype.trim ( ), https://tc39.es/ecma262/#sec-string.prototype.trim
|
||||
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::trim)
|
||||
|
|
|
@ -81,7 +81,7 @@ Vector<u32> CharacterMapFile::read_map(const JsonObject& json, const String& nam
|
|||
} else if (key_value.length() == 1) {
|
||||
buffer[i] = key_value.characters()[0];
|
||||
} else {
|
||||
Utf8View m_utf8_view(key_value.characters());
|
||||
Utf8View m_utf8_view(key_value);
|
||||
buffer[i] = *m_utf8_view.begin();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -234,7 +234,7 @@ public:
|
|||
if (size == -1)
|
||||
return false;
|
||||
|
||||
m_lines.extend(wrap_line(Utf8View(line), m_width));
|
||||
m_lines.extend(wrap_line(Utf8View { StringView { line } }, m_width));
|
||||
free(line);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -196,13 +196,13 @@ int main(int argc, char** argv)
|
|||
return status;
|
||||
}
|
||||
|
||||
static int print_escaped(const char* name)
|
||||
static int print_escaped(StringView name)
|
||||
{
|
||||
int printed = 0;
|
||||
|
||||
Utf8View utf8_name(name);
|
||||
if (utf8_name.validate()) {
|
||||
printf("%s", name);
|
||||
out("{}", name);
|
||||
return utf8_name.length();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue