diff --git a/.gitignore b/.gitignore index 77faf521..b0d7a389 100644 --- a/.gitignore +++ b/.gitignore @@ -259,3 +259,4 @@ test/vs2019/.vs Corel Auto-Preserve test/vs2019/Debug No Unit Tests test/kdevelopbuild +.vscode/settings.json diff --git a/include/etl/string.h b/include/etl/string.h index 86c1dbc4..c92b70d5 100644 --- a/include/etl/string.h +++ b/include/etl/string.h @@ -36,8 +36,10 @@ SOFTWARE. #include "string_view.h" #include "hash.h" +#include + #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL -#include + #include #endif #include "private/minmax_push.h" @@ -260,7 +262,7 @@ namespace etl //*************************************************************************** /// A string implementation that uses a fixed size buffer. - /// A specilisation that requires an external buffer to be specified. + /// A specialisation that requires an external buffer to be specified. ///\ingroup string //*************************************************************************** template <> @@ -332,7 +334,7 @@ namespace etl /// Constructor, from null terminated text. ///\param text The initial text of the string. //************************************************************************* - string(const value_type* text, value_type* buffer, size_type buffer_size) + string(const char* text, char* buffer, size_type buffer_size) : istring(buffer, buffer_size - 1U) { // Is the initial text at the same address as the buffer? @@ -476,17 +478,45 @@ namespace etl }; #endif - //*************************************************************************** - /// Make string from string literal or char array - //*************************************************************************** - template - etl::string make_string(const char(&text)[MAX_SIZE]) + namespace private_string_char { - return etl::string(text, MAX_SIZE - 1); + //*********************************** + template + struct make_string_helper + { + typedef etl::string type; + + static type make(const char(&text)[ARRAY_SIZE]) + { + return etl::string(text, ARRAY_SIZE - 1); + } + }; + + //*********************************** + template<> + struct make_string_helper<1U> + { + typedef etl::string<0> type; + + static type make(const char(&text)[1U]) + { + static char c; + return etl::string<0U>(text, &c, 1U); + } + }; } //*************************************************************************** - /// Make string with max capacity from string literal or char array + /// Make string from string literal or array + //*************************************************************************** + template + etl::string make_string(const char(&text)[ARRAY_SIZE]) + { + return private_string_char::make_string_helper::make(text); + } + + //*************************************************************************** + /// Make string with max capacity from string literal or array //*************************************************************************** template etl::string make_string_with_capacity(const char(&text)[SIZE]) diff --git a/include/etl/string_utilities.h b/include/etl/string_utilities.h index d17c9a5b..b1371fef 100644 --- a/include/etl/string_utilities.h +++ b/include/etl/string_utilities.h @@ -796,6 +796,38 @@ namespace etl } } } + + //*************************************************************************** + /// to_upper_case + //*************************************************************************** + template + void to_upper_case(TString& s) + { + etl::transform(s.begin(), s.end(), s.begin(), ::toupper); + } + + //*************************************************************************** + /// to_lower_case + //*************************************************************************** + template + void to_lower_case(TString& s) + { + etl::transform(s.begin(), s.end(), s.begin(), ::tolower); + } + + //*************************************************************************** + /// to_sentence_case + //*************************************************************************** + template + void to_sentence_case(TString& s) + { + typename TString::iterator itr = s.begin(); + + *itr = typename TString::value_type(::toupper(*itr)); + ++itr; + + etl::transform(itr, s.end(), itr, ::tolower); + } } #endif \ No newline at end of file diff --git a/include/etl/u16string.h b/include/etl/u16string.h index 4843d6a3..593692a8 100644 --- a/include/etl/u16string.h +++ b/include/etl/u16string.h @@ -246,7 +246,7 @@ namespace etl //*************************************************************************** /// A u16string implementation that uses a fixed size buffer. - /// A specilisation that requires an external buffer to be specified. + /// A specialisation that requires an external buffer to be specified. ///\ingroup u16string //*************************************************************************** template <> @@ -461,17 +461,45 @@ namespace etl }; #endif - //*************************************************************************** - /// Make u16string from UTF-8 string literal or char16_t array - //*************************************************************************** - template - etl::u16string make_string(const char16_t (&text) [MAX_SIZE]) + namespace private_u16string { - return etl::u16string(text, MAX_SIZE - 1); + //*********************************** + template + struct make_string_helper + { + typedef etl::u16string type; + + static type make(const char16_t(&text)[ARRAY_SIZE]) + { + return etl::u16string(text, ARRAY_SIZE - 1); + } + }; + + //*********************************** + template<> + struct make_string_helper<1U> + { + typedef etl::u16string<0> type; + + static type make(const char16_t(&text)[1U]) + { + static char16_t c; + return etl::u16string<0U>(text, &c, 1U); + } + }; } //*************************************************************************** - /// Make string with max capacity from string literal or char array + /// Make string from string literal or array + //*************************************************************************** + template + etl::u16string make_string(const char16_t(&text)[ARRAY_SIZE]) + { + return private_u16string::make_string_helper::make(text); + } + + //*************************************************************************** + /// Make string with max capacity from string literal or array //*************************************************************************** template etl::u16string make_string_with_capacity(const char16_t(&text)[SIZE]) diff --git a/include/etl/u32string.h b/include/etl/u32string.h index 95f128cf..fcc3d420 100644 --- a/include/etl/u32string.h +++ b/include/etl/u32string.h @@ -246,7 +246,7 @@ namespace etl //*************************************************************************** /// A u32string implementation that uses a fixed size buffer. - /// A specilisation that requires an external buffer to be specified. + /// A specialisation that requires an external buffer to be specified. ///\ingroup u32string //*************************************************************************** template <> @@ -461,17 +461,45 @@ namespace etl }; #endif - //*************************************************************************** - /// Make u32string from UTF-16 string literal or char32_t array - //*************************************************************************** - template - etl::u32string make_string(const char32_t (&text) [MAX_SIZE]) + namespace private_u32string { - return etl::u32string(text, MAX_SIZE - 1); + //*********************************** + template + struct make_string_helper + { + typedef etl::u32string type; + + static type make(const char32_t(&text)[ARRAY_SIZE]) + { + return etl::u32string(text, ARRAY_SIZE - 1); + } + }; + + //*********************************** + template<> + struct make_string_helper<1U> + { + typedef etl::u32string<0> type; + + static type make(const char32_t(&text)[1U]) + { + static char32_t c; + return etl::u32string<0U>(text, &c, 1U); + } + }; } //*************************************************************************** - /// Make string with max capacity from string literal or char array + /// Make string from string literal or array + //*************************************************************************** + template + etl::u32string make_string(const char32_t(&text)[ARRAY_SIZE]) + { + return private_u32string::make_string_helper::make(text); + } + + //*************************************************************************** + /// Make string with max capacity from string literal or array //*************************************************************************** template etl::u32string make_string_with_capacity(const char32_t(&text)[SIZE]) diff --git a/include/etl/wstring.h b/include/etl/wstring.h index c56d34b6..77be86cb 100644 --- a/include/etl/wstring.h +++ b/include/etl/wstring.h @@ -246,7 +246,7 @@ namespace etl //*************************************************************************** /// A wstring implementation that uses a fixed size buffer. - /// A specilisation that requires an external buffer to be specified. + /// A specialisation that requires an external buffer to be specified. ///\ingroup wstring //*************************************************************************** template <> @@ -461,13 +461,41 @@ namespace etl }; #endif - //*************************************************************************** - /// Make wstring from wide string literal or wchar_t array - //*************************************************************************** - template - etl::wstring make_string(const wchar_t (&text) [MAX_SIZE]) + namespace private_wstring { - return etl::wstring(text, MAX_SIZE - 1); + //*********************************** + template + struct make_string_helper + { + typedef etl::wstring type; + + static type make(const wchar_t(&text)[ARRAY_SIZE]) + { + return etl::wstring(text, ARRAY_SIZE - 1); + } + }; + + //*********************************** + template<> + struct make_string_helper<1U> + { + typedef etl::wstring<0> type; + + static type make(const wchar_t(&text)[1U]) + { + static wchar_t c; + return etl::wstring<0U>(text, &c, 1U); + } + }; + } + + //*************************************************************************** + /// Make string from string literal or array + //*************************************************************************** + template + etl::wstring make_string(const wchar_t(&text)[ARRAY_SIZE]) + { + return private_wstring::make_string_helper::make(text); } //*************************************************************************** diff --git a/test/test_make_string.cpp b/test/test_make_string.cpp index 3e82e057..a0e25d30 100644 --- a/test/test_make_string.cpp +++ b/test/test_make_string.cpp @@ -53,6 +53,27 @@ namespace { }; + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_make_string_empty) + { + size_t length = strlen(""); + + auto ctext = etl::make_string(""); + auto wtext = etl::make_string(L""); + auto u16text = etl::make_string(u""); + auto u32text = etl::make_string(U""); + + CHECK_EQUAL(length, ctext.max_size()); + CHECK_EQUAL(length, wtext.max_size()); + CHECK_EQUAL(length, u16text.max_size()); + CHECK_EQUAL(length, u32text.max_size()); + + CHECK(Equal(std::string(""), ctext)); + CHECK(Equal(std::wstring(L""), wtext)); + CHECK(Equal(std::u16string(u""), u16text)); + CHECK(Equal(std::u32string(U""), u32text)); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_make_string) { @@ -69,9 +90,9 @@ namespace CHECK_EQUAL(length, u32text.max_size()); CHECK(Equal(std::string("Hello World"), ctext)); - CHECK(Equal(std::wstring(L"Hello World"), ctext)); - CHECK(Equal(std::u16string(u"Hello World"), ctext)); - CHECK(Equal(std::u32string(U"Hello World"), ctext)); + CHECK(Equal(std::wstring(L"Hello World"), wtext)); + CHECK(Equal(std::u16string(u"Hello World"), u16text)); + CHECK(Equal(std::u32string(U"Hello World"), u32text)); } //************************************************************************* @@ -110,9 +131,9 @@ namespace #endif CHECK(Equal(std::string("Hello World"), ctext)); - CHECK(Equal(std::wstring(L"Hello World"), ctext)); - CHECK(Equal(std::u16string(u"Hello World"), ctext)); - CHECK(Equal(std::u32string(U"Hello World"), ctext)); + CHECK(Equal(std::wstring(L"Hello World"), wtext)); + CHECK(Equal(std::u16string(u"Hello World"), u16text)); + CHECK(Equal(std::u32string(U"Hello World"), u32text)); } //************************************************************************* diff --git a/test/test_string_utilities.cpp b/test/test_string_utilities.cpp index e9151c29..1ee0d6d7 100644 --- a/test/test_string_utilities.cpp +++ b/test/test_string_utilities.cpp @@ -1618,5 +1618,38 @@ namespace CHECK(textview.end() == itr); } + + //************************************************************************* + TEST(test_to_upper_case) + { + String text(STR("HeLlO WoRlD!")); + String expected(STR("HELLO WORLD!")); + + etl::to_upper_case(text); + + CHECK(text == expected); + } + + //************************************************************************* + TEST(test_to_lower_case) + { + String text(STR("HeLlO WoRlD!")); + String expected(STR("hello world!")); + + etl::to_lower_case(text); + + CHECK(text == expected); + } + + //************************************************************************* + TEST(test_to_sentence_case) + { + String text(STR("HeLlO WoRlD!")); + String expected(STR("Hello world!")); + + etl::to_sentence_case(text); + + CHECK(text == expected); + } }; } diff --git a/test/test_string_utilities_std.cpp b/test/test_string_utilities_std.cpp index 1b711c38..e9025fde 100644 --- a/test/test_string_utilities_std.cpp +++ b/test/test_string_utilities_std.cpp @@ -1571,5 +1571,38 @@ namespace CHECK(textview.end() == itr); } + + //************************************************************************* + TEST(test_to_upper_case) + { + String text(STR("HeLlO WoRlD!")); + String expected(STR("HELLO WORLD!")); + + etl::to_upper_case(text); + + CHECK(text == expected); + } + + //************************************************************************* + TEST(test_to_lower_case) + { + String text(STR("HeLlO WoRlD!")); + String expected(STR("hello world!")); + + etl::to_lower_case(text); + + CHECK(text == expected); + } + + //************************************************************************* + TEST(test_to_sentence_case) + { + String text(STR("HeLlO WoRlD!")); + String expected(STR("Hello world!")); + + etl::to_sentence_case(text); + + CHECK(text == expected); + } }; }