mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
AK: Add support for AK::StringView literals with operator""sv
A new operator, operator""sv was added as of C++17 to support string_view literals. This allows string_views to be constructed from string literals and with no runtime cost to find the string length. See: https://en.cppreference.com/w/cpp/string/basic_string_view/operator%22%22sv This change implements that functionality in AK::StringView. We do have to suppress some warnings about implementing reserved operators as we are essentially implementing STL functions in AK as we have no STL :).
This commit is contained in:
parent
a48d54dfc5
commit
31e1b08e15
Notes:
sideshowbarker
2024-07-18 21:57:38 +09:00
Author: https://github.com/bgianfo Commit: https://github.com/SerenityOS/serenity/commit/31e1b08e158 Pull-request: https://github.com/SerenityOS/serenity/pull/5503
@ -213,4 +213,9 @@ struct Traits<StringView> : public GenericTraits<String> {
|
||||
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE constexpr AK::StringView operator"" sv(const char* cstring, size_t length)
|
||||
{
|
||||
return AK::StringView(cstring, length);
|
||||
}
|
||||
|
||||
using AK::StringView;
|
||||
|
@ -59,6 +59,15 @@ TEST_CASE(compare_views)
|
||||
EXPECT_EQ(view1, "foo");
|
||||
}
|
||||
|
||||
TEST_CASE(string_view_literal_operator)
|
||||
{
|
||||
StringView literal_view = "foo"sv;
|
||||
String test_string = "foo";
|
||||
|
||||
EXPECT_EQ(literal_view.length(), test_string.length());
|
||||
EXPECT_EQ(literal_view, test_string);
|
||||
}
|
||||
|
||||
TEST_CASE(starts_with)
|
||||
{
|
||||
String test_string = "ABCDEF";
|
||||
|
@ -80,9 +80,9 @@ else()
|
||||
endif()
|
||||
|
||||
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
|
||||
add_compile_options(-fconcepts)
|
||||
add_compile_options(-fconcepts -Wno-literal-suffix)
|
||||
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
||||
add_compile_options(-Wno-overloaded-virtual)
|
||||
add_compile_options(-Wno-overloaded-virtual -Wno-user-defined-literals)
|
||||
endif()
|
||||
|
||||
if (ENABLE_ALL_THE_DEBUG_MACROS)
|
||||
|
@ -9,7 +9,7 @@ endif()
|
||||
|
||||
|
||||
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-overloaded-virtual")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-overloaded-virtual -Wno-user-defined-literals")
|
||||
|
||||
if (ENABLE_ADDRESS_SANITIZER)
|
||||
add_definitions(-fsanitize=address -fno-omit-frame-pointer)
|
||||
|
Loading…
Reference in New Issue
Block a user