From 5c9f648cc5781fa797bf765c0569b57ff9092851 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 15 Jul 2020 12:22:46 +0100 Subject: [PATCH] Full string utilities for char --- .gitignore | 1 + include/etl/algorithm.h | 16 +- include/etl/basic_string.h | 5 + include/etl/private/string_utilities_helper.h | 632 ++++------ include/etl/string_utilities.h | 541 ++++----- test/codeblocks/ETL.cbp | 3 + test/test_algorithm.cpp | 26 + test/test_string_utilities_char.cpp | 1078 ++++++++++------- test/vs2019/etl.vcxproj | 2 +- 9 files changed, 1116 insertions(+), 1188 deletions(-) diff --git a/.gitignore b/.gitignore index 1c467992..4b550b45 100644 --- a/.gitignore +++ b/.gitignore @@ -257,3 +257,4 @@ test/Logs build-test-Desktop_x86_windows_msvc2019_pe_32bit-Debug test/vs2019/.vs Corel Auto-Preserve +test/vs2019/Debug No Unit Tests diff --git a/include/etl/algorithm.h b/include/etl/algorithm.h index 1931ac44..d82038cb 100644 --- a/include/etl/algorithm.h +++ b/include/etl/algorithm.h @@ -939,11 +939,11 @@ namespace etl //*************************************************************************** // replace template - void replace(TIterator first, TIterator last, const T& old_value, const T& new_value) + ETL_CONSTEXPR14 void replace(TIterator first, TIterator last, const T& old_value, const T& new_value) { - while (first != last) + while (first != last) { - if (*first == old_value) + if (*first == old_value) { *first = new_value; } @@ -955,7 +955,7 @@ namespace etl //*************************************************************************** // replace_if template - void replace(TIterator first, TIterator last, TPredicate predicate, const T& new_value) + ETL_CONSTEXPR14 void replace_if(TIterator first, TIterator last, TPredicate predicate, const T& new_value) { while (first != last) { @@ -964,14 +964,14 @@ namespace etl *first = new_value; } - ++first + ++first; } } #else //*************************************************************************** // replace template - void replace(TIterator first, TIterator last, const T& old_value, const T& new_value) + ETL_CONSTEXPR14 void replace(TIterator first, TIterator last, const T& old_value, const T& new_value) { std::replace(first, last, old_value, new_value); } @@ -979,7 +979,7 @@ namespace etl //*************************************************************************** // replace_if template - void replace(TIterator first, TIterator last, TPredicate predicate, const T& new_value) + ETL_CONSTEXPR14 void replace_if(TIterator first, TIterator last, TPredicate predicate, const T& new_value) { std::replace_if(first, last, predicate, new_value); } @@ -1236,7 +1236,7 @@ namespace etl { #if ETL_CPP11_SUPPORTED return std::is_heap(first, last); -#else +#else typedef etl::less::value_type> compare; return private_heap::is_heap(first, last - first, compare()); #endif diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index 9a3b2852..efadab47 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -1148,6 +1148,11 @@ namespace etl //********************************************************************* iterator erase(iterator first, iterator last) { + if (first == last) + { + return first; + } + etl::copy(last, end(), first); size_t n_delete = etl::distance(first, last); diff --git a/include/etl/private/string_utilities_helper.h b/include/etl/private/string_utilities_helper.h index c599e092..a5fe9a17 100644 --- a/include/etl/private/string_utilities_helper.h +++ b/include/etl/private/string_utilities_helper.h @@ -36,11 +36,26 @@ SOFTWARE. #include "../string_view.h" #include "../algorithm.h" #include "../utility.h" +#include "../enum_type.h" #include namespace etl { + struct string_pad_direction + { + enum enum_type + { + LEFT, + RIGHT, + }; + + ETL_DECLARE_ENUM_TYPE(string_pad_direction, int) + ETL_ENUM_TYPE(LEFT, "left") + ETL_ENUM_TYPE(RIGHT, "right") + ETL_END_ENUM_TYPE + }; + namespace private_string_utilities { //*************************************************************************** @@ -73,36 +88,6 @@ namespace etl return TStringView(pbegin, view.end()); } - //*************************************************************************** - /// trim_from_left - /// Trim left of trim_characters - //*************************************************************************** - template - void trim_from_left(TIString& s, typename TIString::const_pointer trim_characters, size_t length) - { - size_t position = s.find_first_not_of(trim_characters, 0, length); - s.erase(0U, position); - } - - //*************************************************************************** - /// view_trim_left_of - /// Trim left of whitespace - //*************************************************************************** - template - TStringView view_trim_from_left(const TStringView& view, typename TStringView::const_pointer trim_characters, size_t length) - { - size_t first = view.find_first_not_of(trim_characters, 0, length); - - typename TStringView::const_pointer pbegin = view.end(); - - if (first != TStringView::npos) - { - pbegin = view.begin() + first; - } - - return TStringView(pbegin, view.end()); - } - //*************************************************************************** /// trim_left_delimiters /// Trim left, up to, but not including, delimiters. @@ -137,42 +122,6 @@ namespace etl return TStringView(pbegin, view.end()); } - //*************************************************************************** - /// trim_left_delimiters - /// Trim left, up to, but not including, delimiters. - //*************************************************************************** - template - void trim_left_delimiters(TIString& s, typename TIString::const_pointer delimiters, size_t length) - { - size_t p = s.find_first_of(delimiters, 0, length); - - if (p != TIString::npos) - { - s.erase(0, p); - } - } - - //*************************************************************************** - /// view_trim_left_delimiters - /// Trim left, up to, but not including, delimiters. - //*************************************************************************** - template - TStringView view_trim_left_delimiters(const TStringView& view, typename TStringView::const_pointer delimiters, size_t length) - { - size_t first = view.find_first_of(delimiters, 0, length); - - typename TStringView::const_pointer pbegin = view.end(); - - if (first != TStringView::npos) - { - pbegin = view.begin() + first; - } - - return TStringView(pbegin, view.end()); - } - - //********************************************************************************************************************************************************* - //*************************************************************************** /// trim_from_right /// Trim right of trim_characters @@ -202,35 +151,6 @@ namespace etl return TStringView(view.begin(), pend); } - //*************************************************************************** - /// trim_from_right - /// Trim right of trim_characters - //*************************************************************************** - template - void trim_from_right(TIString& s, typename TIString::const_pointer trim_characters, size_t length) - { - s.erase(s.find_last_not_of(trim_characters, TIString::npos, length) + 1); - } - - //*************************************************************************** - /// view_trim_from_right - /// Trim right of trim_characters - //*************************************************************************** - template - TStringView view_trim_from_right(const TStringView& view, typename TStringView::const_pointer trim_characters, size_t length) - { - size_t last = view.find_last_not_of(trim_characters, TStringView::npos, length) + 1; - - typename TStringView::const_pointer pend = view.begin(); - - if (last != TStringView::npos) - { - pend += last; - } - - return TStringView(view.begin(), pend); - } - //*************************************************************************** /// trim_right_delimiters //*************************************************************************** @@ -268,45 +188,6 @@ namespace etl return TStringView(view.begin(), pend); } - //*************************************************************************** - /// trim_right - /// Trim right, up to, but not including, delimiters. - //*************************************************************************** - template - void trim_right_delimiters(TIString& s, typename TIString::const_pointer delimiters, size_t length) - { - size_t p = s.find_last_of(delimiters, TIString::npos, length); - - if (p != TIString::npos) - { - ++p; - - if (p != s.size()) - { - s.erase(p); - } - } - } - - //*************************************************************************** - /// view_trim_right_delimiters - /// Trim right, up to, but not including, delimiters. - //*************************************************************************** - template - TStringView view_trim_right_delimiters(const TStringView& view, typename TStringView::const_pointer delimiters, size_t length) - { - size_t last = view.find_last_of(delimiters, 0, length) + 1; - - typename TStringView::const_pointer pend = view.begin(); - - if (last != TStringView::npos) - { - pend += last; - } - - return TStringView(view.begin(), pend); - } - //*************************************************************************** /// trim_from /// Trim left and right of trim_characters @@ -344,43 +225,6 @@ namespace etl return TStringView(pbegin, pend); } - //*************************************************************************** - /// trim_from - /// Trim left and right of trim_characters - //*************************************************************************** - template - void trim_from(TIString& s, typename TIString::const_pointer trim_characters, size_t length) - { - trim_from_left(s, trim_characters, length); - trim_from_right(s, trim_characters, length); - } - - //*************************************************************************** - /// trim_from - /// Trim left and right of trim_characters - //*************************************************************************** - template - TStringView view_trim_from(const TStringView& view, typename TStringView::const_pointer trim_characters, size_t length) - { - size_t first = view.find_first_not_of(trim_characters, 0, length); - size_t last = view.find_last_not_of(trim_characters, 0, length) + 1; - - typename TStringView::const_pointer pbegin = view.begin(); - typename TStringView::const_pointer pend = view.begin(); - - if (first != TStringView::npos) - { - pbegin += first; - } - - if (last != TStringView::npos) - { - pend += last; - } - - return TStringView(pbegin, pend); - } - //*************************************************************************** /// trim_delimiters /// Trim left and right of trim_characters @@ -418,27 +262,6 @@ namespace etl return TStringView(pbegin, pend); } - //*************************************************************************** - /// trim_delimiters - /// Trim left and right of trim_characters - //*************************************************************************** - template - void trim_delimiters(TIString& s, typename TIString::const_pointer delimiters, size_t length) - { - trim_left_delimiters(s, delimiters, length); - trim_right_delimiters(s, delimiters, length); - } - - //*************************************************************************** - /// trim_delimiters - /// Trim left and right of trim_characters - //*************************************************************************** - template - TStringView view_trim_delimiters(const TStringView& view, typename TStringView::const_pointer delimiters, size_t length) - { - return view; - } - //*************************************************************************** /// reverse /// Reverse a string @@ -493,6 +316,146 @@ namespace etl } } + //********************************************************************* + /// Find first of any of delimiters within the string + //********************************************************************* + template + TIterator find_first_of(TIterator first, TIterator last, TPointer delimiters) + { + TIterator itr(first); + + while (itr != last) + { + TPointer pd = delimiters; + + while (*pd != 0) + { + if (*itr == *pd) + { + return itr; + } + + ++pd; + } + + ++itr; + } + + return last; + } + + //********************************************************************* + /// Find first not of any of delimiters within the string + //********************************************************************* + template + TIterator find_first_not_of(TIterator first, TIterator last, TPointer delimiters) + { + TIterator itr(first); + + while (itr != last) + { + TPointer pd = delimiters; + + bool found = false; + + while (*pd != 0) + { + if (*itr == *pd) + { + found = true; + break; + } + + ++pd; + } + + if (!found) + { + return itr; + } + + ++itr; + } + + return last; + } + + //********************************************************************* + /// Find last of any of delimiters within the string + //********************************************************************* + template + TIterator find_last_of(TIterator first, TIterator last, TPointer delimiters) + { + if (first == last) + { + return last; + } + + TIterator itr(last - 1); + TIterator end(first - 1); + + while (itr != end) + { + TPointer pd = delimiters; + + while (*pd != 0) + { + if (*itr == *pd) + { + return itr; + } + + ++pd; + } + + --itr; + } + + return last; + } + + //********************************************************************* + /// Find last not of any of delimiters within the string + //********************************************************************* + template + TIterator find_last_not_of(TIterator first, TIterator last, TPointer delimiters) + { + if (first == last) + { + return last; + } + + TIterator itr(last - 1); + TIterator end(first - 1); + + while (itr != end) + { + TPointer pd = delimiters; + + bool found = false; + + while (*pd != 0) + { + if (*itr == *pd) + { + found = true; + break; + } + + ++pd; + } + + if (!found) + { + return itr; + } + + --itr; + } + + return last; + } + //*************************************************************************** /// get_token //*************************************************************************** @@ -502,252 +465,75 @@ namespace etl typename TIString::const_iterator first = last_view.end(); typename TIString::const_iterator last = last_view.end(); - // The last view was default constructed, so we must be looking for the first token. + // If the last view was default constructed then we must be looking for the first token. if (last_view.data() == ETL_NULLPTR) { first = s.begin(); - last = s.begin(); } // Look for the start of the next token. - size_t first_position = s.find_first_not_of(delimiters, std::distance(s.begin(), last)); - size_t last_position = s.find_first_of(delimiters, first_position); + first = find_first_not_of(first, s.end(), delimiters); + last = find_first_of(first, s.end(), delimiters); - if (first_position == TIString::npos) - { - first_position = s.size(); - } - - if (last_position == TIString::npos) - { - last_position = s.size(); - } - - return TStringView(s.begin() + first_position, s.begin() + last_position); + return TStringView(first, last); } - ////*************************************************************************** - ///// - ////*************************************************************************** - //template - //etl::string left(etl::string const& s, size_t n_chars) - //{ - // return s.substr(0, n_chars); - //} + //*************************************************************************** + /// pad_left + //*************************************************************************** + template + void pad_left(TIString& s, size_t required_size, typename TIString::value_type pad_char) + { + required_size = etl::min(required_size, s.capacity()); - ////*************************************************************************** - ///// - ////*************************************************************************** - //template - //etl::string right(etl::string const& s, size_t start) - //{ - // return s.substr(start); - //} + if (required_size > s.size()) + { + required_size -= s.size(); + s.insert(0U, required_size, pad_char); + } + } - //template - //void tokenize(etl::string const& s, etl::vector, max_num_tokens>& tokens, char const* delims, bool ignore_empty_strings) - //{ - // size_t delim; - // size_t O = 0; + //*************************************************************************** + /// pad_right + //*************************************************************************** + template + void pad_right(TIString& s, size_t required_size, typename TIString::value_type pad_char) + { + required_size = etl::min(required_size, s.capacity()); - // tokens.clear(); + if (required_size > s.size()) + { + required_size -= s.size(); + s.insert(s.size(), required_size, pad_char); + } + } - // delim = s.find_first_of(delims, O); + //*************************************************************************** + /// pad + //*************************************************************************** + template + void pad(TIString& s, size_t required_size, string_pad_direction pad_direction, typename TIString::value_type pad_char) + { + switch (int(pad_direction)) + { + case string_pad_direction::LEFT: + { + pad_left(s, required_size, pad_char); + break; + } - // while (delim != etl::string::npos) - // { - // etl::string tok(s.substr(O, delim - O)); - // trim(tok); - // if (!tok.empty() || !ignore_empty_strings) - // tokens.push_back(tok); - // O = delim + 1; + case string_pad_direction::RIGHT: + { + pad_right(s, required_size, pad_char); + break; + } - // delim = s.find_first_of(delims, O); - // } - // etl::string tok(s.substr(O)); - // trim(tok); - // if (!tok.empty() || !ignore_empty_strings) - // tokens.push_back(tok); - //} - - //template - //void tokenize(etl::string const& s, etl::vector, max_num_tokens>& tokens, char const* delims, bool ignore_empty_strings) - //{ - // size_t delim; - // size_t O = 0; - - // tokens.clear(); - - // delim = s.find_first_of(delims, O); - - // while (delim != etl::string::npos) - // { - // etl::string tok(s.substr(O, delim - O).c_str()); - // trim(tok); - // if (!tok.empty() || !ignore_empty_strings) - // tokens.push_back(tok); - // O = delim + 1; - - // delim = s.find_first_of(delims, O); - // } - // etl::string tok(s.substr(O).c_str()); - // trim(tok); - // if (!tok.empty() || !ignore_empty_strings) - // tokens.push_back(tok); - //} - - //template - //float to_float(etl::string const& s) - //{ - // return strtof(s.c_str(), 0); - //} - - //template - //unsigned to_unsigned(etl::string const& s, int radix) - //{ - // return strtoul(s.c_str(), 0, radix); - //} - - //template - //int to_int(etl::string const& s, int radix) - //{ - // return strtol(s.c_str(), 0, radix); - //} - - //template - //bool to_bool(etl::string const& s) - //{ - // return s[0] == 't' || s[0] == 'T'; - //} - - //template - //etl::string substr_between(etl::string const& s, const etl::string brackets) - //{ - // size_t l = s.find_first_of(brackets[0]); - // size_t r = s.find_last_of(brackets[1]); - - // if (l != etl::string::npos && r != etl::string::npos && r - l > 1) - // return s.substr(l + 1, r - l - 1); - // return etl::string(); - //} - - //template - //int array_subscript(etl::string const& s) - //{ - // etl::string n = substr_between(s); - // return n.empty() ? -1 : to_int(n); - //} - - //template - //etl::string from_float(float f, int digits) - //{ - // char buf[32]; - // char format[] = { '%','9','.',char('0' + char(digits)),'f',0 }; - // sprintf(buf, format, f); - // return etl::string(buf); - //} - - //template - //etl::string from_float(float f, char const* format_string) - //{ - // char buf[32]; - // sprintf(buf, format_string, f); - // return etl::string(buf); - //} - - //template - //etl::string from_unsigned(unsigned u, bool hex = false) - //{ - // char buf[16]; - // char format[] = { '%',hex ? 'x' : 'u',0 }; - // sprintf(buf, format, u); - // return etl::string(buf); - //} - - //template - //etl::string from_int(int i, bool hex = false) - //{ - // char buf[16]; - // char format[] = { '%',hex ? 'x' : 'i',0 }; - // sprintf(buf, format, i); - // return etl::string(buf); - //} - - //template - //etl::string from_bool(bool b, bool verbose) - //{ - // if (verbose) - // return b ? "true" : "false"; - // else - // return b ? "t" : "f"; - //} - - - //template - //void reverse(etl::string& s) - //{ - // std::reverse(s.begin(), s.end()); - //} - - //enum pad_dir_t - //{ - // pad_dir__right, - // pad_dir__left - //}; - - //template - //void pad(etl::string& s, size_t size, pad_dir_t dir, char pad_char) - //{ - // if (size > s.size()) - // { - // size -= s.size(); - - // if (dir == pad_dir__left) - // { - // reverse(s); - // s.reserve(s.size() + size); - // while (size--) - // s.push_back(pad_char); - // reverse(s); - // } - // else - // { - // s.reserve(s.size() + size); - // while (size--) - // s.push_back(pad_char); - // } - // } - //} - - //template - //void to_uppercase(etl::string& s) - //{ - // for (typename etl::string::iterator iter = s.begin(); iter != s.end(); ++iter) - // *iter = char(toupper(*iter)); - //} - - //template - //void to_lowercase(etl::string& s) - //{ - // for (typename etl::string::iterator iter = s.begin(); iter != s.end(); ++iter) - // *iter = char(tolower(*iter)); - //} - - //template - //void namify(etl::string& s) - //{ - // replace(s.begin(), s.end(), ' ', '_'); - // replace(s.begin(), s.end(), '/', '_'); - // replace(s.begin(), s.end(), '-', '_'); - // replace(s.begin(), s.end(), '.', '_'); - // replace(s.begin(), s.end(), '(', '_'); - // replace(s.begin(), s.end(), ')', '_'); - // replace(s.begin(), s.end(), ',', '_'); - // replace(s.begin(), s.end(), '[', '_'); - // replace(s.begin(), s.end(), ']', '_'); - // while (!s.empty() && s.back() == '_') - // s.pop_back(); - //} + default: + { + break; + } + } + } }; } diff --git a/include/etl/string_utilities.h b/include/etl/string_utilities.h index a87deea7..33a71a55 100644 --- a/include/etl/string_utilities.h +++ b/include/etl/string_utilities.h @@ -77,42 +77,6 @@ namespace etl return etl::private_string_utilities::view_trim_from_left(view, trim_characters); } - //*************************************************************************** - /// trim_from_left - /// Trim left of specified characters - //*************************************************************************** - inline void trim_from_left(etl::istring& s, etl::istring::const_pointer trim_characters, size_t length) - { - etl::private_string_utilities::trim_from_left(s, trim_characters, length); - } - - //*************************************************************************** - /// trim_from_left - /// View trim left of specified characters - //*************************************************************************** - inline etl::string_view trim_from_left(const etl::string_view& view, etl::istring::const_pointer trim_characters, size_t length) - { - return etl::private_string_utilities::view_trim_from_left(view, trim_characters, length); - } - - //*************************************************************************** - /// trim_from_left - /// Trim left of specified characters - //*************************************************************************** - inline void trim_from_left(etl::istring& s, const etl::istring& trim_characters) - { - etl::private_string_utilities::trim_from_left(s, trim_characters.c_str()); - } - - //*************************************************************************** - /// trim_from_left - /// View trim left of specified characters - //*************************************************************************** - inline etl::string_view trim_from_left(const etl::string_view& view, const etl::istring& trim_characters) - { - return etl::private_string_utilities::view_trim_from_left(view, trim_characters.c_str()); - } - //*************************************************************************** /// trim_left /// Trim left, up to, but not including, delimiters. @@ -131,44 +95,6 @@ namespace etl return etl::private_string_utilities::view_trim_left_delimiters(s, delimiters); } - //*************************************************************************** - /// trim_left - /// Trim left, up to, but not including, delimiters. - //*************************************************************************** - inline void trim_left(etl::istring& s, etl::istring::const_pointer delimiters, size_t length) - { - etl::private_string_utilities::trim_left_delimiters(s, delimiters, length); - } - - //*************************************************************************** - /// trim_left - /// Trim left, up to, but not including, delimiters. - //*************************************************************************** - inline etl::string_view trim_left(const etl::string_view& view, etl::string_view::const_pointer delimiters, size_t length) - { - return etl::private_string_utilities::view_trim_left_delimiters(view, delimiters, length); - } - - //*************************************************************************** - /// trim_left - /// Trim left, up to, but not including, delimiters. - //*************************************************************************** - inline void trim_left(etl::istring& s, const etl::istring& delimiters) - { - etl::private_string_utilities::trim_left_delimiters(s, delimiters.c_str()); - } - - //*************************************************************************** - /// trim_left - /// Trim left, up to, but not including, delimiters. - //*************************************************************************** - inline etl::string_view trim_left(const etl::string_view& view, const etl::istring& delimiters) - { - return etl::private_string_utilities::view_trim_left_delimiters(view, delimiters.c_str()); - } - - //********************************************************************************************************************************************************* - //*************************************************************************** /// trim_whitespace_right /// Trim right of whitespace @@ -205,42 +131,6 @@ namespace etl return etl::private_string_utilities::view_trim_from_right(view, trim_characters); } - //*************************************************************************** - /// trim_from_right - /// Trim right of specified characters - //*************************************************************************** - inline void trim_from_right(etl::istring& s, etl::istring::const_pointer trim_characters, size_t length) - { - etl::private_string_utilities::trim_from_right(s, trim_characters, length); - } - - //*************************************************************************** - /// trim_from_right - /// Trim right of specified characters - //*************************************************************************** - inline etl::string_view trim_from_right(const etl::string_view& view, etl::istring::const_pointer trim_characters, size_t length) - { - return etl::private_string_utilities::view_trim_from_right(view, trim_characters, length); - } - - //*************************************************************************** - /// trim_from_right - /// Trim right of specified characters - //*************************************************************************** - inline void trim_from_right(etl::istring& s, const etl::istring& trim_characters) - { - etl::private_string_utilities::trim_from_right(s, trim_characters.c_str()); - } - - //*************************************************************************** - /// trim_from_right - /// Trim right of specified characters - //*************************************************************************** - inline etl::string_view trim_from_right(const etl::string_view& view, const etl::istring& trim_characters) - { - return etl::private_string_utilities::view_trim_from_right(view, trim_characters.c_str()); - } - //*************************************************************************** /// trim_right //*************************************************************************** @@ -257,44 +147,6 @@ namespace etl return etl::private_string_utilities::view_trim_right_delimiters(view, delimiters); } - //*************************************************************************** - /// trim_right - /// Trim right, up to, but not including, delimiters. - //*************************************************************************** - inline void trim_right(etl::istring& s, etl::istring::const_pointer delimiters, size_t length) - { - etl::private_string_utilities::trim_right_delimiters(s, delimiters, length); - } - - //*************************************************************************** - /// trim_right - /// Trim right, up to, but not including, delimiters. - //*************************************************************************** - inline etl::string_view trim_right(const etl::string_view& view, etl::istring::const_pointer delimiters, size_t length) - { - etl::private_string_utilities::view_trim_right_delimiters(view, delimiters, length); - } - - //*************************************************************************** - /// trim_right - /// Trim right, up to, but not including, delimiters. - //*************************************************************************** - inline void trim_right(etl::istring& s, const etl::istring& delimiters) - { - etl::private_string_utilities::trim_right_delimiters(s, delimiters.c_str()); - } - - //*************************************************************************** - /// trim_right - /// Trim right, up to, but not including, delimiters. - //*************************************************************************** - inline etl::string_view trim_right(const etl::string_view& view, const etl::istring& delimiters) - { - return etl::private_string_utilities::view_trim_right_delimiters(view, delimiters.c_str()); - } - - //********************************************************************************************************************************************************* - //*************************************************************************** /// trim_whitespace /// Trim both ends of whitespace @@ -331,42 +183,6 @@ namespace etl return etl::private_string_utilities::view_trim_from(view, trim_characters); } - //*************************************************************************** - /// trim_from - /// Trim right of specified characters - //*************************************************************************** - inline void trim_from(etl::istring& s, etl::istring::const_pointer trim_characters, size_t length) - { - etl::private_string_utilities::trim_from(s, trim_characters, length); - } - - //*************************************************************************** - /// trim_from - /// Trim right of specified characters - //*************************************************************************** - inline etl::string_view trim_from(const etl::string_view& view, etl::istring::const_pointer trim_characters, size_t length) - { - return etl::private_string_utilities::view_trim_from(view, trim_characters, length); - } - - //*************************************************************************** - /// trim_from - /// Trim right of specified characters - //*************************************************************************** - inline void trim_from(etl::istring& s, const etl::istring& trim_characters) - { - etl::private_string_utilities::trim_from(s, trim_characters.c_str()); - } - - //*************************************************************************** - /// trim_from - /// Trim right of specified characters - //*************************************************************************** - inline etl::string_view trim_from(const etl::string_view& view, const etl::istring& trim_characters) - { - return etl::private_string_utilities::view_trim_from(view, trim_characters.c_str()); - } - //*************************************************************************** /// trim //*************************************************************************** @@ -383,168 +199,83 @@ namespace etl return etl::private_string_utilities::view_trim_delimiters(view, delimiters); } - //*************************************************************************** - /// trim - /// Trim right, up to, but not including, delimiters. - //*************************************************************************** - inline void trim(etl::istring& s, etl::istring::const_pointer delimiters, size_t length) - { - etl::private_string_utilities::trim_delimiters(s, delimiters, length); - } - - //*************************************************************************** - /// trim - /// Trim right, up to, but not including, delimiters. - //*************************************************************************** - inline etl::string_view trim(const etl::string_view& view, etl::istring::const_pointer delimiters, size_t length) - { - etl::private_string_utilities::view_trim_delimiters(view, delimiters, length); - } - - //*************************************************************************** - /// trim - /// Trim right, up to, but not including, delimiters. - //*************************************************************************** - inline void trim(etl::istring& s, const etl::istring& delimiters) - { - etl::private_string_utilities::trim_delimiters(s, delimiters.c_str()); - } - - //*************************************************************************** - /// trim - /// Trim right, up to, but not including, delimiters. - //*************************************************************************** - inline etl::string_view trim(const etl::string_view& view, const etl::istring& delimiters) - { - return etl::private_string_utilities::view_trim_delimiters(view, delimiters.c_str()); - } - //*************************************************************************** /// reverse /// Reverse the string //*************************************************************************** - void reverse(etl::istring& s) + inline void reverse(etl::istring& s) { etl::private_string_utilities::reverse(s); } + //*************************************************************************** + /// Get up to the first n characters. + //*************************************************************************** + inline void left_n(etl::istring& s, size_t n) + { + n = (n > s.size()) ? s.size() : n; + + s.erase(s.begin() + n, s.end()); + } + //*************************************************************************** /// Get a view of up to the first n characters. //*************************************************************************** - etl::string_view left(const etl::istring& s, size_t n) + inline etl::string_view left_n(etl::string_view view, size_t n) { - n = (n > s.size()) ? s.size() : n; + n = (n > view.size()) ? view.size() : n; - return etl::string_view(s.begin(), s.begin() + n); + return etl::string_view(view.begin(), view.begin() + n); } //*************************************************************************** - /// Get a view of up to the last n characters. + /// Get up to the last n characters. //*************************************************************************** - etl::string_view right(const etl::istring& s, size_t n) + inline void right_n(etl::istring& s, size_t n) { n = (n > s.size()) ? s.size() : n; - return etl::string_view(s.end() - n, s.end()); + s.erase(s.begin(), s.end() - n); } - //template - //void tokenize(etl::string const& s, etl::vector, max_num_tokens>& tokens, char const* delims, bool ignore_empty_strings) - //{ - // size_t delim; - // size_t O = 0; + //*************************************************************************** + /// Get a view of up to the last n characters. + //*************************************************************************** + inline etl::string_view right_n(etl::string_view view, size_t n) + { + n = (n > view.size()) ? view.size() : n; - // tokens.clear(); + return etl::string_view(view.end() - n, view.end()); + } - // delim = s.find_first_of(delims, O); + //*************************************************************************** + /// pad_left + //*************************************************************************** + inline void pad_left(etl::istring& s, size_t required_size, etl::istring::value_type pad_char) + { + etl::private_string_utilities::pad_left(s, required_size, pad_char); + } - // while (delim != etl::string::npos) - // { - // etl::string tok(s.substr(O, delim - O)); - // trim(tok); - // if (!tok.empty() || !ignore_empty_strings) - // tokens.push_back(tok); - // O = delim + 1; + //*************************************************************************** + /// pad_right + //*************************************************************************** + inline void pad_right(etl::istring& s, size_t required_size, etl::istring::value_type pad_char) + { + etl::private_string_utilities::pad_right(s, required_size, pad_char); + } - // delim = s.find_first_of(delims, O); - // } - // etl::string tok(s.substr(O)); - // trim(tok); - // if (!tok.empty() || !ignore_empty_strings) - // tokens.push_back(tok); - //} - - //template - //void tokenize(etl::string const& s, etl::vector, max_num_tokens>& tokens, char const* delims, bool ignore_empty_strings) - //{ - // size_t delim; - // size_t O = 0; - - // tokens.clear(); - - // delim = s.find_first_of(delims, O); - - // while (delim != etl::string::npos) - // { - // etl::string tok(s.substr(O, delim - O).c_str()); - // trim(tok); - // if (!tok.empty() || !ignore_empty_strings) - // tokens.push_back(tok); - // O = delim + 1; - - // delim = s.find_first_of(delims, O); - // } - // etl::string tok(s.substr(O).c_str()); - // trim(tok); - // if (!tok.empty() || !ignore_empty_strings) - // tokens.push_back(tok); - //} - - //template - //etl::string substr_between(etl::string const& s, const etl::string brackets) - //{ - // size_t l = s.find_first_of(brackets[0]); - // size_t r = s.find_last_of(brackets[1]); - - // if (l != etl::string::npos && r != etl::string::npos && r - l > 1) - // return s.substr(l + 1, r - l - 1); - // return etl::string(); - //} - - //enum pad_dir_t - //{ - // pad_dir__right, - // pad_dir__left - //}; - - //template - //void pad(etl::string& s, size_t size, pad_dir_t dir, char pad_char) - //{ - // if (size > s.size()) - // { - // size -= s.size(); - - // if (dir == pad_dir__left) - // { - // reverse(s); - // s.reserve(s.size() + size); - // while (size--) - // s.push_back(pad_char); - // reverse(s); - // } - // else - // { - // s.reserve(s.size() + size); - // while (size--) - // s.push_back(pad_char); - // } - // } - //} + //*************************************************************************** + /// pad + //*************************************************************************** + void pad(etl::istring& s, size_t required_size, string_pad_direction pad_direction, etl::istring::value_type pad_char) + { + etl::private_string_utilities::pad(s, required_size, pad_direction, pad_char); + } //*************************************************************************** /// to_upper_case //*************************************************************************** - void to_upper_case(etl::istring& s) + inline void to_upper_case(etl::istring& s) { etl::istring::iterator itr = s.begin(); @@ -558,7 +289,7 @@ namespace etl //*************************************************************************** /// to_lower_case //*************************************************************************** - void to_lower_case(etl::istring& s) + inline void to_lower_case(etl::istring& s) { etl::istring::iterator itr = s.begin(); @@ -572,7 +303,7 @@ namespace etl //*************************************************************************** /// to_sentence_case //*************************************************************************** - void to_sentence_case(etl::istring& s) + inline void to_sentence_case(etl::istring& s) { etl::istring::iterator itr = s.begin(); @@ -588,9 +319,9 @@ namespace etl //*************************************************************************** /// transform //*************************************************************************** - void transform(etl::istring& s, - const etl::pair* pairsbegin, - const etl::pair* pairsend) + inline void transform(etl::istring& s, + const etl::pair* pairsbegin, + const etl::pair* pairsend) { etl::private_string_utilities::transform_characters(s, pairsbegin, pairsend); } @@ -598,9 +329,9 @@ namespace etl //*************************************************************************** /// transform //*************************************************************************** - void transform(etl::istring& s, - const etl::pair* pairsbegin, - const etl::pair* pairsend) + inline void transform(etl::istring& s, + const etl::pair* pairsbegin, + const etl::pair* pairsend) { etl::private_string_utilities::transform_strings(s, pairsbegin, pairsend); } @@ -608,10 +339,170 @@ namespace etl //*************************************************************************** /// get_token //*************************************************************************** - etl::string_view get_token(const etl::istring& s, etl::istring::const_pointer delimiters, const string_view& last_view) + inline etl::string_view get_token(const etl::istring& s, etl::istring::const_pointer delimiters, const string_view& last_view) { return etl::private_string_utilities::get_token(s, delimiters, last_view); } + + //********************************************************************* + /// Find first of any of delimiters within the string + //********************************************************************* + inline etl::istring::iterator find_first_of(etl::istring::iterator first, etl::istring::iterator last, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_first_of(first, last, delimiters); + } + + //********************************************************************* + /// Find first of any of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_first_of(etl::istring::const_iterator first, etl::istring::const_iterator last, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_first_of(first, last, delimiters); + } + + //********************************************************************* + /// Find first of any of delimiters within the string + //********************************************************************* + inline etl::istring::iterator find_first_of(etl::istring& s, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_first_of(s.begin(), s.end(), delimiters); + } + + //********************************************************************* + /// Find first of any of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_first_of(const etl::istring& s, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_first_of(s.cbegin(), s.cend(), delimiters); + } + + //********************************************************************* + /// Find first of any of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_first_of(const etl::string_view& s, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_first_of(s.cbegin(), s.cend(), delimiters); + } + + //********************************************************************* + /// Find first not of delimiters within the string + //********************************************************************* + inline etl::istring::iterator find_first_not_of(etl::istring::iterator first, etl::istring::iterator last, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_first_not_of(first, last, delimiters); + } + + //********************************************************************* + /// Find first not of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_first_not_of(etl::istring::const_iterator first, etl::istring::const_iterator last, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_first_not_of(first, last, delimiters); + } + + //********************************************************************* + /// Find first not of delimiters within the string + //********************************************************************* + inline etl::istring::iterator find_first_not_of(etl::istring& s, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_first_not_of(s.begin(), s.end(), delimiters); + } + + //********************************************************************* + /// Find first not of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_first_not_of(const etl::istring& s, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_first_not_of(s.cbegin(), s.cend(), delimiters); + } + + //********************************************************************* + /// Find first not of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_first_not_of(const etl::string_view& view, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_first_not_of(view.cbegin(), view.cend(), delimiters); + } + + //********************************************************************* + /// Find last of any of delimiters within the string + //********************************************************************* + inline etl::istring::iterator find_last_of(etl::istring::iterator first, etl::istring::iterator last, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_last_of(first, last, delimiters); + } + + //********************************************************************* + /// Find last of any of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_last_of(etl::istring::const_iterator first, etl::istring::const_iterator last, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_last_of(first, last, delimiters); + } + + //********************************************************************* + /// Find last of any of delimiters within the string + //********************************************************************* + inline etl::istring::iterator find_last_of(etl::istring& s, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_last_of(s.begin(), s.end(), delimiters); + } + + //********************************************************************* + /// Find last of any of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_last_of(const etl::istring& s, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_last_of(s.cbegin(), s.cend(), delimiters); + } + + //********************************************************************* + /// Find last of any of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_last_of(const etl::string_view& view, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_last_of(view.cbegin(), view.cend(), delimiters); + } + + //********************************************************************* + /// Find last of any of delimiters within the string + //********************************************************************* + inline etl::istring::iterator find_last_not_of(etl::istring::iterator first, etl::istring::iterator last, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_last_not_of(first, last, delimiters); + } + + //********************************************************************* + /// Find last of any of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_last_not_of(etl::istring::const_iterator first, etl::istring::const_iterator last, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_last_not_of(first, last, delimiters); + } + + //********************************************************************* + /// Find last of any of delimiters within the string + //********************************************************************* + inline etl::istring::iterator find_last_not_of(etl::istring& s, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_last_not_of(s.begin(), s.end(), delimiters); + } + + //********************************************************************* + /// Find last of any of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_last_not_of(const etl::istring& s, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_last_not_of(s.cbegin(), s.cend(), delimiters); + } + + //********************************************************************* + /// Find last of any of delimiters within the string + //********************************************************************* + inline etl::istring::const_iterator find_last_not_of(const etl::string_view& view, etl::istring::const_pointer delimiters) + { + return etl::private_string_utilities::find_last_not_of(view.cbegin(), view.cend(), delimiters); + } } #endif \ No newline at end of file diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index fa6ddedf..e0da11ec 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -373,6 +373,7 @@ + @@ -435,6 +436,7 @@ + @@ -578,6 +580,7 @@ + diff --git a/test/test_algorithm.cpp b/test/test_algorithm.cpp index 86679682..8657383e 100644 --- a/test/test_algorithm.cpp +++ b/test/test_algorithm.cpp @@ -2004,5 +2004,31 @@ namespace CHECK_EQUAL(1, *etl::multimin_iter_compare(std::less(), &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], &i[6], &i[7])); CHECK_EQUAL(8, *etl::multimin_iter_compare(std::greater(), &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], &i[6], &i[7])); } + + //************************************************************************* + TEST(replace) + { + int data[] = { 1, 8, 2, 7, 2, 6, 2, 2, 10, 9 }; + int expected[] = { 1, 8, 0, 7, 0, 6, 0, 0, 10, 9 }; + + // Replace 2 with 0 + etl::replace(std::begin(data), std::end(data), 2, 0); + + bool is_same = std::equal(std::begin(data), std::end(data), std::begin(expected)); + CHECK(is_same); + } + + //************************************************************************* + TEST(replace_if) + { + int data[] = { 1, 8, 2, 7, 3, 6, 4, 5, 10, 9 }; + int expected[] = { 0, 8, 0, 7, 0, 6, 0, 0, 10, 9 }; + + // Replace <=5 with 0 + etl::replace_if(std::begin(data), std::end(data), std::bind(std::less_equal(), std::placeholders::_1, 5), 0); + + bool is_same = std::equal(std::begin(data), std::end(data), std::begin(expected)); + CHECK(is_same); + } }; } diff --git a/test/test_string_utilities_char.cpp b/test/test_string_utilities_char.cpp index 94e0f61a..6d26a5e3 100644 --- a/test/test_string_utilities_char.cpp +++ b/test/test_string_utilities_char.cpp @@ -209,145 +209,6 @@ namespace CHECK(expected == text); } - //************************************************************************* - TEST(test_view_trim_from_left_pointer_length_empty) - { - String text(STR("")); - String expected(STR("")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_left(textview, STR(" \t\n\r\f\v"), 6U); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_from_left_pointer_length) - { - String text(STR(" \t\n\r\f\vHello World\t\n\r\f\v ")); - String expected(STR("Hello World\t\n\r\f\v ")); - - etl::trim_from_left(text, STR(" \t\n\r\f\v"), 6U); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_from_left_pointer_length) - { - String text(STR(" \t\n\r\f\vHello World\t\n\r\f\v ")); - String expected(STR("Hello World\t\n\r\f\v ")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_left(textview, STR(" \t\n\r\f\v"), 6U); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_from_left_pointer_length_nothing_to_trim) - { - String text(STR("Hello World\t\n\r\f\v ")); - String expected(STR("Hello World\t\n\r\f\v ")); - - etl::trim_from_left(text, STR(" \t\n\r\f\v"), 6U); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_from_left_pointer_length_nothing_to_trim) - { - String text(STR("Hello World\t\n\r\f\v ")); - String expected(STR("Hello World\t\n\r\f\v ")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_left(textview, STR(" \t\n\r\f\v"), 6U); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_from_left_string_empty) - { - String text(STR("")); - String expected(STR("")); - - etl::trim_from_left(text, String(STR(" \t\n\r\f\v"))); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_from_left_string_empty) - { - String text(STR("")); - String expected(STR("")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_left(textview, String(STR(" \t\n\r\f\v"))); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_from_left_string) - { - String text(STR(" \t\n\r\f\vHello World\t\n\r\f\v ")); - String expected(STR("Hello World\t\n\r\f\v ")); - - etl::trim_from_left(text, String(STR(" \t\n\r\f\v"))); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_from_left_string) - { - String text(STR(" \t\n\r\f\vHello World\t\n\r\f\v ")); - String expected(STR("Hello World\t\n\r\f\v ")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_left(textview, String(STR(" \t\n\r\f\v"))); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_from_left_string_nothing_to_trim) - { - String text(STR("Hello World\t\n\r\f\v ")); - String expected(STR("Hello World\t\n\r\f\v ")); - - etl::trim_from_left(text, String(STR(" \t\n\r\f\v"))); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_from_left_string_nothing_to_trim) - { - String text(STR("Hello World\t\n\r\f\v ")); - String expected(STR("Hello World\t\n\r\f\v ")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_left(textview, String(STR(" \t\n\r\f\v"))); - - CHECK(expectedview == view); - } - //************************************************************************* TEST(test_trim_left_pointer_delimiters) { @@ -398,17 +259,6 @@ namespace CHECK(expectedview == view); } - //************************************************************************* - TEST(test_trim_left_string_delimiters) - { - String text(STR("qztfpHello Worldqztfp")); - String expected("Hello Worldqztfp"); - - etl::trim_left(text, String(STR("Hel"))); - - CHECK(expected == text); - } - //************************************************************************* TEST(test_view_trim_left_string_delimiters) { @@ -423,31 +273,6 @@ namespace CHECK(expectedview == view); } - //************************************************************************* - TEST(test_trim_left_string_delimiters_nothing_to_trim) - { - String text(STR("Hello Worldqztfp")); - String expected("Hello Worldqztfp"); - - etl::trim_left(text, String(STR("Hel"))); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_left_string_delimiters_nothing_to_trim) - { - String text(STR("Hello Worldqztfp")); - String expected("Hello Worldqztfp"); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_left(textview, String(STR("Hel"))); - - CHECK(expectedview == view); - } - //************************************************************************* TEST(test_trim_whitespace_right_empty) { @@ -598,156 +423,6 @@ namespace CHECK(expectedview == view); } - //************************************************************************* - TEST(test_trim_from_right_pointer_length_empty) - { - String text(STR("")); - String expected(STR("")); - - etl::trim_from_right(text, STR(" \t\n\r\f\v"), 6U); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_from_right_pointer_length_empty) - { - String text(STR("")); - String expected(STR("")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_right(textview, STR(" \t\n\r\f\v"), 6U); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_from_right_pointer_length) - { - String text(STR(" \t\n\r\f\vHello World\t\n\r\f\v ")); - String expected(STR(" \t\n\r\f\vHello World")); - - etl::trim_from_right(text, STR(" \t\n\r\f\v"), 6U); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_from_right_pointer_length) - { - String text(STR(" \t\n\r\f\vHello World\t\n\r\f\v ")); - String expected(STR(" \t\n\r\f\vHello World")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_right(textview, STR(" \t\n\r\f\v"), 6U); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_from_right_pointer_length_nothing_to_trim) - { - String text(STR(" \t\n\r\f\vHello World")); - String expected(STR(" \t\n\r\f\vHello World")); - - etl::trim_from_right(text, STR(" \t\n\r\f\v"), 6U); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_from_right_pointer_length_nothing_to_trim) - { - String text(STR(" \t\n\r\f\vHello World")); - String expected(STR(" \t\n\r\f\vHello World")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_right(textview, STR(" \t\n\r\f\v"), 6U); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_from_right_string_empty) - { - String text(STR("")); - String expected(STR("")); - - etl::trim_from_right(text, String(STR(" \t\n\r\f\v"))); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_from_right_string_empty) - { - String text(STR("")); - String expected(STR("")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_right(textview, String(STR(" \t\n\r\f\v"))); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_from_right_string) - { - String text(STR(" \t\n\r\f\vHello World\t\n\r\f\v ")); - String expected(STR(" \t\n\r\f\vHello World")); - - etl::trim_from_right(text, String(STR(" \t\n\r\f\v"))); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_from_right_string) - { - String text(STR(" \t\n\r\f\vHello World\t\n\r\f\v ")); - String expected(STR(" \t\n\r\f\vHello World")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_right(textview, String(STR(" \t\n\r\f\v"))); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_from_right_string_nothing_to_trim) - { - String text(STR(" \t\n\r\f\vHello World")); - String expected(STR(" \t\n\r\f\vHello World")); - - etl::trim_from_right(text, String(STR(" \t\n\r\f\v"))); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_from_right_string_nothing_to_trim) - { - String text(STR(" \t\n\r\f\vHello World")); - String expected(STR(" \t\n\r\f\vHello World")); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_from_right(textview, String(STR(" \t\n\r\f\v"))); - - CHECK(expectedview == view); - } - //************************************************************************* TEST(test_trim_right_pointer_delimiters) { @@ -798,56 +473,6 @@ namespace CHECK(expectedview == view); } - //************************************************************************* - TEST(test_trim_right_string_delimiters) - { - String text(STR("qztfpHello Worldqztfp")); - String expected("qztfpHello World"); - - etl::trim_right(text, String(STR("rld"))); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_right_string_delimiters) - { - String text(STR("qztfpHello Worldqztfp")); - String expected("qztfpHello World"); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_right(textview, STR("rld")); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_right_string_delimiters_nothing_to_trim) - { - String text(STR("qztfpHello World")); - String expected("qztfpHello World"); - - etl::trim_right(text, String(STR("rld"))); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_right_string_delimiters_nothing_to_trim) - { - String text(STR("qztfpHello World")); - String expected("qztfpHello World"); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim_right(textview, String(STR("rld"))); - - CHECK(expectedview == view); - } - //************************************************************************* TEST(test_trim_whitespace_empty) { @@ -1048,56 +673,6 @@ namespace CHECK(expectedview == view); } - //************************************************************************* - TEST(test_trim_string_delimiters) - { - String text(STR("qztfpHello Worldqztfp")); - String expected("Hello World"); - - etl::trim(text, String(STR("Hd"))); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_string_delimiters) - { - String text(STR("qztfpHello Worldqztfp")); - String expected("Hello World"); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim(textview, STR("Hd")); - - CHECK(expectedview == view); - } - - //************************************************************************* - TEST(test_trim_string_delimiters_nothing_to_trim) - { - String text(STR("Hello World")); - String expected("Hello World"); - - etl::trim(text, String(STR("Hd"))); - - CHECK(expected == text); - } - - //************************************************************************* - TEST(test_view_trim_string_delimiters_nothing_to_trim) - { - String text(STR("Hello World")); - String expected("Hello World"); - - StringView textview(text); - StringView expectedview(expected); - - StringView view = etl::trim(textview, String(STR("Hd"))); - - CHECK(expectedview == view); - } - //************************************************************************* TEST(test_reverse) { @@ -1109,6 +684,106 @@ namespace CHECK(expected == text); } + //************************************************************************* + TEST(test_right_n_view) + { + String text(STR("Hello World")); + String expected(STR("World")); + + StringView textview(text); + StringView expectedview(expected); + + StringView view = etl::right_n(textview, expected.size()); + + CHECK(expectedview == view); + } + + //************************************************************************* + TEST(test_right_n_view_excess) + { + String text(STR("Hello World")); + String expected(STR("Hello World")); + + StringView textview(text); + StringView expectedview(expected); + + StringView view = etl::right_n(textview, text.size() * 2U); + + CHECK(expectedview == view); + } + + //************************************************************************* + TEST(test_right_n_string) + { + String text(STR("Hello World")); + String expected(STR("World")); + + etl::right_n(text, 5U); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_right_n_string_excess) + { + String text(STR("Hello World")); + String expected(STR("Hello World")); + + etl::right_n(text, text.size() * 2U); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_left_n_view) + { + String text(STR("Hello World")); + String expected(STR("Hello")); + + StringView textview(text); + StringView expectedview(expected); + + StringView view = etl::left_n(textview, expected.size()); + + CHECK(expectedview == view); + } + + //************************************************************************* + TEST(test_left_n_view_excess) + { + String text(STR("Hello World")); + String expected(STR("Hello World")); + + StringView textview(text); + StringView expectedview(expected); + + StringView view = etl::left_n(textview, text.size() * 2U); + + CHECK(expectedview == view); + } + + //************************************************************************* + TEST(test_left_n_string) + { + String text(STR("Hello World")); + String expected(STR("Hello")); + + etl::left_n(text, expected.size()); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_left_n_string_excess) + { + String text(STR("Hello World")); + String expected(STR("Hello World")); + + etl::left_n(text, text.size() * 2U); + + CHECK(expected == text); + } + //************************************************************************* TEST(test_transform_characters) { @@ -1192,24 +867,565 @@ namespace //************************************************************************* TEST(test_get_token_pointer_delimiters) { - String text(STR(" The cat.sat, on;the:mat ")); + String text(STR(" The cat.sat, on;the:mat .,;:")); Vector tokens; StringView token; - do + while (token.end() != text.end()) { token = etl::get_token(text, STR(" .,;:"), token); if (!token.empty()) { - String s(token.begin(), token.end()); - tokens.push_back(s); + tokens.emplace_back(token.begin(), token.end()); } - - } while (!token.empty()); + } CHECK_EQUAL(6U, tokens.size()); } + + //************************************************************************* + TEST(test_pad_left) + { + String text(STR("Hello World")); + String expected(STR("xxxxHello World")); + + etl::pad_left(text, expected.size(), STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_pad_left_smaller) + { + String text(STR("Hello World")); + String expected(STR("Hello World")); + + etl::pad_left(text, text.size() - 1U, STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_pad_left_greater_than_capacity) + { + String text(STR("Hello World")); + String expected(text); + expected.insert(0U, text.capacity() - expected.size(), STR('x')); + + etl::pad_left(text, text.capacity() + 1U, STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_pad_right) + { + String text(STR("Hello World")); + String expected(STR("Hello Worldxxxx")); + + etl::pad_right(text, expected.size(), STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_pad_right_smaller) + { + String text(STR("Hello World")); + String expected(STR("Hello World")); + + etl::pad_right(text, text.size() - 1U, STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_pad_right_greater_than_capacity) + { + String text(STR("Hello World")); + String expected(text); + expected.insert(text.size(), text.capacity() - expected.size(), STR('x')); + + etl::pad_right(text, text.capacity() + 1U, STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_pad_select_left) + { + String text(STR("Hello World")); + String expected(STR("xxxxHello World")); + + etl::pad(text, expected.size(), etl::string_pad_direction::LEFT, STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_pad_select_left_smaller) + { + String text(STR("Hello World")); + String expected(STR("Hello World")); + + etl::pad(text, text.size() - 1U, etl::string_pad_direction::LEFT, STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_pad_select_left_greater_than_capacity) + { + String text(STR("Hello World")); + String expected(text); + expected.insert(0U, text.capacity() - expected.size(), STR('x')); + + etl::pad(text, text.capacity() + 1U, etl::string_pad_direction::LEFT, STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_pad_select_right) + { + String text(STR("Hello World")); + String expected(STR("Hello Worldxxxx")); + + etl::pad(text, expected.size(), etl::string_pad_direction::RIGHT, STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_pad_select_right_smaller) + { + String text(STR("Hello World")); + String expected(STR("Hello World")); + + etl::pad(text, text.size() - 1U, etl::string_pad_direction::RIGHT, STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_pad_select_right_greater_than_capacity) + { + String text(STR("Hello World")); + String expected(text); + expected.insert(text.size(), text.capacity() - expected.size(), STR('x')); + + etl::pad(text, text.capacity() + 1U, etl::string_pad_direction::RIGHT, STR('x')); + + CHECK(expected == text); + } + + //************************************************************************* + TEST(test_find_first_of_iterator_iterator) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_first_of(text.begin(), text.end(), STR("Hel")); + + CHECK_EQUAL(STR('H'), *itr); + } + + //************************************************************************* + TEST(test_find_first_of_iterator_iterator_not_found) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_first_of(text.begin(), text.end(), STR("xyz")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_first_of_const_iterator_const_iterator) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_first_of(text.cbegin(), text.cend(), STR("Hel")); + + CHECK_EQUAL(STR('H'), *itr); + } + + //************************************************************************* + TEST(test_find_first_of_const_iterator_const_iterator_not_found) + { + String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_first_of(text.begin(), text.end(), STR("xyz")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_first_of_string) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_first_of(text, STR("Hel")); + + CHECK_EQUAL(STR('H'), *itr); + } + + //************************************************************************* + TEST(test_find_first_of_string_not_found) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_first_of(text, STR("xyz")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_first_of_const_string) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_first_of(text, STR("Hel")); + + CHECK_EQUAL(STR('H'), *itr); + } + + //************************************************************************* + TEST(test_find_first_of_const_string_not_found) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_first_of(text, STR("xyz")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_first_of_string_view) + { + const String text(STR("abcHello Worldabc")); + StringView textview(text); + + String::const_iterator itr = etl::find_first_of(textview, STR("Hel")); + + CHECK_EQUAL(STR('H'), *itr); + } + + //************************************************************************* + TEST(test_find_first_of_string_view_not_found) + { + const String text(STR("abcHello Worldabc")); + StringView textview(text); + + String::const_iterator itr = etl::find_first_of(textview, STR("xyz")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_first_not_of_iterator_iterator) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_first_not_of(text.begin(), text.end(), STR("abc")); + + CHECK_EQUAL(STR('H'), *itr); + } + + //************************************************************************* + TEST(test_find_first_not_of_iterator_iterator_not_found) + { + String text(STR("abcHello Worldabc")); + String::iterator itr = etl::find_first_not_of(text.begin(), text.end(), STR("abcHello World")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_first_not_of_const_iterator_const_iterator) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_first_not_of(text.cbegin(), text.cend(), STR("abc")); + + CHECK_EQUAL(STR('H'), *itr); + } + + //************************************************************************* + TEST(test_find_first_not_of_const_iterator_const_iterator_not_found) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_first_not_of(text.cbegin(), text.cend(), STR("abcHello World")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_first_not_of_string) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_first_not_of(text, STR("abc")); + + CHECK_EQUAL(STR('H'), *itr); + } + + //************************************************************************* + TEST(test_find_first_not_of_string_not_found) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_first_not_of(text, STR("abcHello World")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_first_not_of_const_string) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_first_not_of(text, STR("abc")); + + CHECK_EQUAL(STR('H'), *itr); + } + + //************************************************************************* + TEST(test_find_first_not_of_const_string_not_found) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_first_not_of(text, STR("abcHello World")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_first_not_of_string_view) + { + const String text(STR("abcHello Worldabc")); + StringView textview(text); + + String::const_iterator itr = etl::find_first_not_of(textview, STR("abc")); + + CHECK_EQUAL(STR('H'), *itr); + } + + //************************************************************************* + TEST(test_find_first_not_of_string_view_not_found) + { + const String text(STR("abcHello Worldabc")); + StringView textview(text); + + String::const_iterator itr = etl::find_first_not_of(textview, STR("abcHello World")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_last_of_iterator_iterator) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_last_of(text.begin(), text.end(), STR("rld")); + + CHECK_EQUAL(STR('d'), *itr); + } + + //************************************************************************* + TEST(test_find_last_of_iterator_iterator_not_found) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_last_of(text.begin(), text.end(), STR("xyz")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_last_of_const_iterator_const_iterator) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_last_of(text.cbegin(), text.cend(), STR("rld")); + + CHECK_EQUAL(STR('d'), *itr); + } + + //************************************************************************* + TEST(test_find_last_of_const_iterator_const_iterator_not_found) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_last_of(text.cbegin(), text.cend(), STR("xyz")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_last_of_string) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_last_of(text, STR("rld")); + + CHECK_EQUAL(STR('d'), *itr); + } + + //************************************************************************* + TEST(test_find_last_of_string_not_found) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_last_of(text, STR("xyz")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_last_of_const_string) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_last_of(text, STR("rld")); + + CHECK_EQUAL(STR('d'), *itr); + } + + //************************************************************************* + TEST(test_find_last_of_const_string_not_found) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_last_of(text, STR("xyz")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_last_of_string_view) + { + String text(STR("abcHello Worldabc")); + StringView textview(text); + + String::const_iterator itr = etl::find_last_of(textview, STR("rld")); + + CHECK_EQUAL(STR('d'), *itr); + } + + //************************************************************************* + TEST(test_find_last_of_string_view_not_found) + { + String text(STR("abcHello Worldabc")); + StringView textview(text); + + String::const_iterator itr = etl::find_last_of(textview, STR("xyz")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_last_not_of_iterator_iterator) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_last_not_of(text.begin(), text.end(), STR("abc")); + + CHECK_EQUAL(STR('d'), *itr); + } + + //************************************************************************* + TEST(test_find_last_not_of_iterator_iterator_not_found) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_last_not_of(text.begin(), text.end(), STR("abcHello World")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_last_not_of_const_iterator_const_iterator) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_last_not_of(text.cbegin(), text.cend(), STR("abc")); + + CHECK_EQUAL(STR('d'), *itr); + } + + //************************************************************************* + TEST(test_find_last_not_of_const_iterator_const_iterator_not_found) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_last_not_of(text.cbegin(), text.cend(), STR("abcHello World")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_last_not_of_string) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_last_not_of(text, STR("abc")); + + CHECK_EQUAL(STR('d'), *itr); + } + + //************************************************************************* + TEST(test_find_last_not_of_string_not_found) + { + String text(STR("abcHello Worldabc")); + + String::iterator itr = etl::find_last_not_of(text, STR("abcHello World")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_last_not_of_const_string) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_last_not_of(text, STR("abc")); + + CHECK_EQUAL(STR('d'), *itr); + } + + //************************************************************************* + TEST(test_find_last_not_of_const_string_not_found) + { + const String text(STR("abcHello Worldabc")); + + String::const_iterator itr = etl::find_last_not_of(text, STR("abcHello World")); + + CHECK(text.end() == itr); + } + + //************************************************************************* + TEST(test_find_last_not_of_string_view) + { + String text(STR("abcHello Worldabc")); + StringView textview(text); + + String::const_iterator itr = etl::find_last_not_of(textview, STR("abc")); + + CHECK_EQUAL(STR('d'), *itr); + } + + //************************************************************************* + TEST(test_find_last_not_of_string_view_not_found) + { + String text(STR("abcHello Worldabc")); + StringView textview(text); + + String::const_iterator itr = etl::find_last_not_of(textview, STR("abcHello World")); + + CHECK(text.end() == itr); + } }; } diff --git a/test/vs2019/etl.vcxproj b/test/vs2019/etl.vcxproj index 9aed630d..6a0e77b3 100644 --- a/test/vs2019/etl.vcxproj +++ b/test/vs2019/etl.vcxproj @@ -157,7 +157,7 @@ Application true - v141_clang_c2 + ClangCL Unicode