Updated strings

Re-introduced case utilities
Fixed make_string for empty strings
This commit is contained in:
John Wellbelove 2020-11-06 12:58:32 +00:00
parent 76850b7037
commit 36d2152486
9 changed files with 273 additions and 39 deletions

1
.gitignore vendored
View File

@ -259,3 +259,4 @@ test/vs2019/.vs
Corel Auto-Preserve
test/vs2019/Debug No Unit Tests
test/kdevelopbuild
.vscode/settings.json

View File

@ -36,8 +36,10 @@ SOFTWARE.
#include "string_view.h"
#include "hash.h"
#include <ctype.h>
#if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
#include <initializer_list>
#include <initializer_list>
#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<const size_t MAX_SIZE>
etl::string<MAX_SIZE - 1> make_string(const char(&text)[MAX_SIZE])
namespace private_string_char
{
return etl::string<MAX_SIZE - 1>(text, MAX_SIZE - 1);
//***********************************
template<size_t ARRAY_SIZE = 1U>
struct make_string_helper
{
typedef etl::string<ARRAY_SIZE> type;
static type make(const char(&text)[ARRAY_SIZE])
{
return etl::string<ARRAY_SIZE - 1>(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<size_t ARRAY_SIZE>
etl::string<ARRAY_SIZE - 1> make_string(const char(&text)[ARRAY_SIZE])
{
return private_string_char::make_string_helper<ARRAY_SIZE>::make(text);
}
//***************************************************************************
/// Make string with max capacity from string literal or array
//***************************************************************************
template<const size_t MAX_SIZE, const size_t SIZE>
etl::string<MAX_SIZE> make_string_with_capacity(const char(&text)[SIZE])

View File

@ -796,6 +796,38 @@ namespace etl
}
}
}
//***************************************************************************
/// to_upper_case
//***************************************************************************
template <typename TString>
void to_upper_case(TString& s)
{
etl::transform(s.begin(), s.end(), s.begin(), ::toupper);
}
//***************************************************************************
/// to_lower_case
//***************************************************************************
template <typename TString>
void to_lower_case(TString& s)
{
etl::transform(s.begin(), s.end(), s.begin(), ::tolower);
}
//***************************************************************************
/// to_sentence_case
//***************************************************************************
template <typename TString>
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

View File

@ -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<const size_t MAX_SIZE>
etl::u16string<MAX_SIZE - 1> make_string(const char16_t (&text) [MAX_SIZE])
namespace private_u16string
{
return etl::u16string<MAX_SIZE - 1>(text, MAX_SIZE - 1);
//***********************************
template<size_t ARRAY_SIZE = 1U>
struct make_string_helper
{
typedef etl::u16string<ARRAY_SIZE> type;
static type make(const char16_t(&text)[ARRAY_SIZE])
{
return etl::u16string<ARRAY_SIZE - 1>(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<size_t ARRAY_SIZE>
etl::u16string<ARRAY_SIZE - 1> make_string(const char16_t(&text)[ARRAY_SIZE])
{
return private_u16string::make_string_helper<ARRAY_SIZE>::make(text);
}
//***************************************************************************
/// Make string with max capacity from string literal or array
//***************************************************************************
template<const size_t MAX_SIZE, const size_t SIZE>
etl::u16string<MAX_SIZE> make_string_with_capacity(const char16_t(&text)[SIZE])

View File

@ -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<const size_t MAX_SIZE>
etl::u32string<MAX_SIZE - 1> make_string(const char32_t (&text) [MAX_SIZE])
namespace private_u32string
{
return etl::u32string<MAX_SIZE - 1>(text, MAX_SIZE - 1);
//***********************************
template<size_t ARRAY_SIZE = 1U>
struct make_string_helper
{
typedef etl::u32string<ARRAY_SIZE> type;
static type make(const char32_t(&text)[ARRAY_SIZE])
{
return etl::u32string<ARRAY_SIZE - 1>(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<size_t ARRAY_SIZE>
etl::u32string<ARRAY_SIZE - 1> make_string(const char32_t(&text)[ARRAY_SIZE])
{
return private_u32string::make_string_helper<ARRAY_SIZE>::make(text);
}
//***************************************************************************
/// Make string with max capacity from string literal or array
//***************************************************************************
template<const size_t MAX_SIZE, const size_t SIZE>
etl::u32string<MAX_SIZE> make_string_with_capacity(const char32_t(&text)[SIZE])

View File

@ -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<const size_t MAX_SIZE>
etl::wstring<MAX_SIZE - 1> make_string(const wchar_t (&text) [MAX_SIZE])
namespace private_wstring
{
return etl::wstring<MAX_SIZE - 1>(text, MAX_SIZE - 1);
//***********************************
template<size_t ARRAY_SIZE = 1U>
struct make_string_helper
{
typedef etl::wstring<ARRAY_SIZE> type;
static type make(const wchar_t(&text)[ARRAY_SIZE])
{
return etl::wstring<ARRAY_SIZE - 1>(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<size_t ARRAY_SIZE>
etl::wstring<ARRAY_SIZE - 1> make_string(const wchar_t(&text)[ARRAY_SIZE])
{
return private_wstring::make_string_helper<ARRAY_SIZE>::make(text);
}
//***************************************************************************

View File

@ -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));
}
//*************************************************************************

View File

@ -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);
}
};
}

View File

@ -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);
}
};
}