diff --git a/include/etl/string.h b/include/etl/string.h index a1415e2e..a84c4d3a 100644 --- a/include/etl/string.h +++ b/include/etl/string.h @@ -43,6 +43,19 @@ SOFTWARE. namespace etl { +#if ETL_USING_CPP11 + inline namespace literals + { + inline namespace string_literals + { + constexpr etl::string_view operator ""_sv(const char* str, size_t length) noexcept + { + return etl::string_view{ str, length }; + } + } + } +#endif + typedef etl::ibasic_string istring; //*************************************************************************** diff --git a/include/etl/u16string.h b/include/etl/u16string.h index 88f5303b..9be06986 100644 --- a/include/etl/u16string.h +++ b/include/etl/u16string.h @@ -41,6 +41,19 @@ SOFTWARE. namespace etl { +#if ETL_USING_CPP11 + inline namespace literals + { + inline namespace string_literals + { + constexpr etl::u16string_view operator ""_sv(const char16_t* str, size_t length) noexcept + { + return etl::u16string_view{ str, length }; + } + } + } +#endif + typedef ibasic_string iu16string; //*************************************************************************** diff --git a/include/etl/u32string.h b/include/etl/u32string.h index 1dda3291..783f4901 100644 --- a/include/etl/u32string.h +++ b/include/etl/u32string.h @@ -41,6 +41,19 @@ SOFTWARE. namespace etl { +#if ETL_USING_CPP11 + inline namespace literals + { + inline namespace string_literals + { + constexpr etl::u32string_view operator ""_sv(const char32_t* str, size_t length) noexcept + { + return etl::u32string_view{ str, length }; + } + } + } +#endif + typedef ibasic_string iu32string; //*************************************************************************** diff --git a/include/etl/wstring.h b/include/etl/wstring.h index cde74be3..22caaae6 100644 --- a/include/etl/wstring.h +++ b/include/etl/wstring.h @@ -41,6 +41,19 @@ SOFTWARE. namespace etl { +#if ETL_USING_CPP11 + inline namespace literals + { + inline namespace string_literals + { + constexpr etl::wstring_view operator ""_sv(const wchar_t* str, size_t length) noexcept + { + return etl::wstring_view{ str, length }; + } + } + } +#endif + typedef ibasic_string iwstring; //*************************************************************************** diff --git a/test/test_string_view.cpp b/test/test_string_view.cpp index 86c9eefc..9cf0e1e7 100644 --- a/test/test_string_view.cpp +++ b/test/test_string_view.cpp @@ -1013,5 +1013,26 @@ namespace CHECK(etl::hash()(u16text) == etl::hash()(u16view)); CHECK(etl::hash()(u32text) == etl::hash()(u32view)); } + + //************************************************************************* + TEST(string_view_literal) + { + typedef etl::string_view View; + typedef etl::wstring_view WView; + typedef etl::u16string_view U16View; + typedef etl::u32string_view U32View; + + using namespace etl::literals::string_literals; + + View view = "Hello World"_sv; + WView wview = L"Hello World"_sv; + U16View u16view = u"Hello World"_sv; + U32View u32view = U"Hello World"_sv; + + CHECK_TRUE((view == View{ "Hello World", etl::strlen("Hello World") })); + CHECK_TRUE((wview == WView{ L"Hello World", etl::strlen(L"Hello World") })); + CHECK_TRUE((u16view == U16View{ u"Hello World", etl::strlen(u"Hello World") })); + CHECK_TRUE((u32view == U32View{ U"Hello World", etl::strlen(U"Hello World") })); + } }; }