diff --git a/src/string_view.h b/src/string_view.h new file mode 100644 index 00000000..c5e95603 --- /dev/null +++ b/src/string_view.h @@ -0,0 +1,875 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2017 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef __ETL_STRING_VIEW__ +#define __ETL_STRING_VIEW__ + +#include "platform.h" +#include "memory.h" +#include "iterator.h" +#include "error_handler.h" +#include "exception.h" +#include "char_traits.h" +#include "integral_limits.h" +#include "hash.h" + +#include + +///\defgroup array array +/// A wrapper for arrays +///\ingroup containers + +#undef ETL_FILE +#define ETL_FILE "42" + +#ifdef ETL_COMPILER_MICROSOFT +#undef min +#undef max +#endif + +namespace etl +{ + //*************************************************************************** + /// The base class for basic_string_view exceptions. + //*************************************************************************** + class string_view_exception : public exception + { + public: + + string_view_exception(string_type reason_, string_type file_name_, numeric_type line_number_) + : exception(reason_, file_name_, line_number_) + { + } + }; + + //*************************************************************************** + ///\ingroup stack + /// The exception thrown when the index is out of bounds. + //*************************************************************************** + class string_view_bounds : public string_view_exception + { + public: + + string_view_bounds(string_type file_name_, numeric_type line_number_) + : string_view_exception(ETL_ERROR_TEXT("basic_string_view:bounds", ETL_FILE"A"), file_name_, line_number_) + { + } + }; + + //*************************************************************************** + ///\ingroup stack + /// The exception thrown when the view is uninitialised. + //*************************************************************************** + class string_view_uninitialised : public string_view_exception + { + public: + + string_view_uninitialised(string_type file_name_, numeric_type line_number_) + : string_view_exception(ETL_ERROR_TEXT("basic_string_view:uninitialised", ETL_FILE"B"), file_name_, line_number_) + { + } + }; + + //*************************************************************************** + /// String view. + //*************************************************************************** + template > + class basic_string_view + { + public: + + typedef T value_type; + typedef TTraits traits_type; + typedef std::size_t size_type; + typedef const T& const_reference; + typedef const T* const_pointer; + typedef const T* const_iterator; + typedef std::reverse_iterator const_reverse_iterator; + + enum + { + npos = etl::integral_limits::max + }; + + //************************************************************************* + /// Default constructor. + //************************************************************************* + basic_string_view() + : mbegin(nullptr), + mend(nullptr) + { + } + + //************************************************************************* + /// Construct from T*. + //************************************************************************* + basic_string_view(const T* begin_) + : mbegin(begin_), + mend(begin_ + TTraits::length(begin_)) + { + } + + //************************************************************************* + /// Construct from pointer range. + //************************************************************************* + basic_string_view(const T* begin_, const T* end_) + : mbegin(begin_), + mend(end_) + { + } + + //************************************************************************* + /// Construct from iterator/size. + //************************************************************************* + template ::value, void>::type> + basic_string_view(const T* begin_, TSize size_) + : mbegin(begin_), + mend(begin_ + size_) + { + } + + //************************************************************************* + /// Copy constructor + //************************************************************************* + basic_string_view(const basic_string_view& other) + : mbegin(other.mbegin), + mend(other.mend) + { + } + + //************************************************************************* + /// Returns a const reference to the first element. + //************************************************************************* + const_reference front() const + { + return *mbegin; + } + + //************************************************************************* + /// Returns a const reference to the last element. + //************************************************************************* + const_reference back() const + { + return *(mend - 1); + } + + //************************************************************************* + /// Returns a const pointer to the first element of the internal storage. + //************************************************************************* + const_pointer data() const + { + return mbegin; + } + + //************************************************************************* + /// Returns a const iterator to the beginning of the array. + //************************************************************************* + const_iterator begin() const + { + return mbegin; + } + + //************************************************************************* + /// Returns a const iterator to the beginning of the array. + //************************************************************************* + const_iterator cbegin() const + { + return mbegin; + } + + //************************************************************************* + /// Returns a const iterator to the end of the array. + //************************************************************************* + const_iterator end() const + { + return mend; + } + + //************************************************************************* + // Returns a const iterator to the end of the array. + //************************************************************************* + const_iterator cend() const + { + return mend; + } + + //************************************************************************* + /// Returns a const reverse iterator to the reverse beginning of the array. + //************************************************************************* + const_reverse_iterator rbegin() const + { + return const_reverse_iterator(mend); + } + + //************************************************************************* + /// Returns a const reverse iterator to the reverse beginning of the array. + //************************************************************************* + const_reverse_iterator crbegin() const + { + return const_reverse_iterator(mend); + } + + //************************************************************************* + /// Returns a const reverse iterator to the end of the array. + //************************************************************************* + const_reverse_iterator rend() const + { + return const_reverse_iterator(mbegin); + } + + //************************************************************************* + /// Returns a const reverse iterator to the end of the array. + //************************************************************************* + const_reverse_iterator crend() const + { + return const_reverse_iterator(mbegin); + } + + //************************************************************************* + // Capacity + //************************************************************************* + + //************************************************************************* + /// Returns true if the array size is zero. + //************************************************************************* + bool empty() const + { + return (mbegin == mend); + } + + //************************************************************************* + /// Returns the size of the array. + //************************************************************************* + size_t size() const + { + return (mend - mbegin); + } + + //************************************************************************* + /// Returns the size of the array. + //************************************************************************* + size_t length() const + { + return size(); + } + + //************************************************************************* + /// Returns the maximum possible size of the array. + //************************************************************************* + size_t max_size() const + { + return size(); + } + + //************************************************************************* + /// Assign from a view. + //************************************************************************* + etl::basic_string_view& operator=(const etl::basic_string_view& other) + { + mbegin = other.mbegin; + mend = other.mend; + return *this; + } + + //************************************************************************* + /// Assign from iterators + //************************************************************************* + template ::value, void>::type> + void assign(TIterator begin_, TIterator end_) + { + mbegin = etl::addressof(*begin_); + mend = etl::addressof(*begin_) + std::distance(begin_, end_); + } + + //************************************************************************* + /// Assign from iterator and size. + //************************************************************************* + template ::value, void>::type> + void assign(TIterator begin_, TSize size_) + { + mbegin = etl::addressof(*begin_); + mend = etl::addressof(*begin_) + size_; + } + + //************************************************************************* + /// Returns a const reference to the indexed value. + //************************************************************************* + const_reference operator[](size_t i) const + { + return mbegin[i]; + } + + //************************************************************************* + /// Returns a const reference to the indexed value. + //************************************************************************* + const_reference at(size_t i) const + { + ETL_ASSERT((mbegin != nullptr && mend != nullptr), ETL_ERROR(string_view_uninitialised)); + ETL_ASSERT(i < size(), ETL_ERROR(string_view_bounds)); + return mbegin[i]; + } + + //************************************************************************* + /// Swaps with another basic_string_view. + //************************************************************************* + void swap(basic_string_view& other) + { + std::swap(mbegin, other.mbegin); + std::swap(mend, other.mend); + } + + //************************************************************************* + /// Copies characters + //************************************************************************* + size_type copy(T* destination, size_type count, size_type position = 0) const + { + size_t n = 0; + + if (position < size()) + { + n = std::min(count, size() - position); + + std::copy(mbegin + position, mbegin + position + n, destination); + } + + return n; + } + + //************************************************************************* + /// Returns a substring + //************************************************************************* + basic_string_view substr(size_type position = 0, size_type count = npos) const + { + basic_string_view view; + + if (position < size()) + { + size_t n = std::min(count, size() - position); + + view = basic_string_view(mbegin + position, mbegin + position + n); + } + + return view; + } + + //************************************************************************* + /// Shrinks the view by moving its start forward. + //************************************************************************* + void remove_prefix(size_type n) + { + mbegin += n; + } + + //************************************************************************* + /// Shrinks the view by moving its end backward. + //************************************************************************* + void remove_suffix(size_type n) + { + mend -= n; + } + + //************************************************************************* + /// Compares two views + //************************************************************************* + int compare(basic_string_view view) const + { + return (*this == view) ? 0 : ((*this > view) ? 1 : -1); + } + + int compare(size_type position, size_type count, basic_string_view view) const + { + return substr(position, count).compare(view); + } + + int compare(size_type position1, size_type count1, + basic_string_view view, + size_type position2, size_type count2) const + { + return substr(position1, count1).compare(view.substr(position2, count2)); + } + + int compare(const T* text) const + { + return compare(etl::basic_string_view(text)); + } + + int compare(size_type position, size_type count, const T* text) const + { + return substr(position, count).compare(etl::basic_string_view(text)); + } + + int compare(size_type position, size_type count1, const T* text, size_type count2) const + { + return substr(position, count1).compare(etl::basic_string_view(text, count2)); + } + + //************************************************************************* + /// Checks if the string view starts with the given prefix + //************************************************************************* + bool starts_with(etl::basic_string_view view) const + { + return (size() >= view.size()) && + (compare(0, view.size(), view) == 0); + } + + bool starts_with(T c) const + { + return !empty() && (front() == c); + } + + bool starts_with(const T* text) const + { + size_t lengthtext = TTraits::length(text); + + return (size() >= lengthtext) && + (compare(0, lengthtext, text) == 0); + } + + //************************************************************************* + /// Checks if the string view ends with the given suffix + //************************************************************************* + bool ends_with(etl::basic_string_view view) const + { + return (size() >= view.size()) && + (compare(size() - view.size(), npos, view) == 0); + } + + bool ends_with(T c) const + { + return !empty() && (back() == c); + } + + bool ends_with(const T* text) const + { + size_t lengthtext = TTraits::length(text); + size_t lengthview = size(); + + return (lengthview >= lengthtext) && + (compare(lengthview - lengthtext, lengthtext, text) == 0); + } + + //************************************************************************* + /// Find characters in the view + //************************************************************************* + size_type find(etl::basic_string_view view, size_type position = 0) const + { + if ((size() < view.size())) + { + return npos; + } + + const_iterator iposition = std::search(begin() + position, end(), view.begin(), view.end()); + + if (iposition == end()) + { + return npos; + } + else + { + return std::distance(begin(), iposition); + } + } + + size_type find(T c, size_type position = 0) const + { + return find(etl::basic_string_view(&c, 1), position); + } + + size_type find(const T* text, size_type position, size_type count) const + { + return find(etl::basic_string_view(text, count), position); + } + + size_type find(const T* text, size_type position = 0) const + { + return find(etl::basic_string_view(text), position); + } + + //************************************************************************* + /// Find the last occurrence of a substring + //************************************************************************* + size_type rfind(etl::basic_string_view view, size_type position = npos) const + { + if ((size() < view.size())) + { + return npos; + } + + position = std::min(position, size()); + + const_iterator iposition = std::find_end(begin(), + begin() + position, + view.begin(), + view.end()); + + if (iposition == end()) + { + return npos; + } + else + { + return std::distance(begin(), iposition); + } + } + + size_type rfind(T c, size_type position = npos) const + { + return rfind(etl::basic_string_view(&c, 1), position); + } + + size_type rfind(const T* text, size_type position, size_type count) const + { + return rfind(etl::basic_string_view(text, count), position); + } + + size_type rfind(const T* text, size_type position = npos) const + { + return rfind(etl::basic_string_view(text), position); + } + + //************************************************************************* + /// Find first occurrence of characters + //************************************************************************* + size_type find_first_of(etl::basic_string_view view, size_type position = 0) const + { + const size_t lengthtext = size(); + + if (position < lengthtext) + { + for (size_t i = position; i < lengthtext; ++i) + { + const size_t lengthview = view.size(); + + for (size_t j = 0; j < lengthview; ++j) + { + if (mbegin[i] == view[j]) + { + return i; + } + } + } + } + + return npos; + } + + size_type find_first_of(T c, size_type position = 0) const + { + return find_first_of(etl::basic_string_view(&c, 1), position); + } + + size_type find_first_of(const T* text, size_type position, size_type count) const + { + return find_first_of(etl::basic_string_view(text, count), position); + } + + size_type find_first_of(const T* text, size_type position = 0) const + { + return find_first_of(etl::basic_string_view(text), position); + } + + //************************************************************************* + /// Find last occurrence of characters + //************************************************************************* + size_type find_last_of(etl::basic_string_view view, size_type position = npos) const + { + if (empty()) + { + return npos; + } + + position = std::min(position, size() - 1); + + const_reverse_iterator it = rbegin() + size() - position - 1; + + while (it != rend()) + { + const size_t viewlength = view.size(); + + for (size_t j = 0; j < viewlength; ++j) + { + if (mbegin[position] == view[j]) + { + return position; + } + } + + ++it; + --position; + } + + return npos; + } + + size_type find_last_of(T c, size_type position = npos) const + { + return find_last_of(etl::basic_string_view(&c, 1), position);; + } + + size_type find_last_of(const T* text, size_type position, size_type count) const + { + return find_last_of(etl::basic_string_view(text, count), position);; + } + + size_type find_last_of(const T* text, size_type position = npos) const + { + return find_last_of(etl::basic_string_view(text), position);; + } + + //************************************************************************* + /// Find first absence of characters + //************************************************************************* + size_type find_first_not_of(etl::basic_string_view view, size_type position = 0) const + { + const size_t lengthtext = size(); + + if (position < lengthtext) + { + for (size_t i = position; i < lengthtext; ++i) + { + bool found = false; + + const size_t viewlength = view.size(); + + for (size_t j = 0; j < viewlength; ++j) + { + if (mbegin[i] == view[j]) + { + found = true; + } + } + + if (!found) + { + return i; + } + } + } + + return npos; + } + + size_type find_first_not_of(T c, size_type position = 0) const + { + return find_first_not_of(etl::basic_string_view(&c, 1), position); + } + + size_type find_first_not_of(const T* text, size_type position, size_type count) const + { + return find_first_not_of(etl::basic_string_view(text, count), position); + } + + size_type find_first_not_of(const T* text, size_type position = 0) const + { + return find_first_not_of(etl::basic_string_view(text), position); + } + + //************************************************************************* + /// Find last absence of characters + //************************************************************************* + size_type find_last_not_of(etl::basic_string_view view, size_type position = npos) const + { + if (empty()) + { + return npos; + } + + position = std::min(position, size() - 1); + + const_reverse_iterator it = rbegin() + size() - position - 1; + + while (it != rend()) + { + bool found = false; + + const size_t viewlength = view.size(); + + for (size_t j = 0; j < viewlength; ++j) + { + if (mbegin[position] == view[j]) + { + found = true; + } + } + + if (!found) + { + return position; + } + + ++it; + --position; + } + + return npos; + } + + size_type find_last_not_of(T c, size_type position = npos) const + { + return find_last_not_of(etl::basic_string_view(&c, 1), position);; + } + + size_type find_last_not_of(const T* text, size_type position, size_type count) const + { + return find_last_not_of(etl::basic_string_view(text, count), position);; + } + + size_type find_last_not_of(const T* text, size_type position = npos) const + { + return find_last_not_of(etl::basic_string_view(text), position);; + } + + //************************************************************************* + /// Equality for array views. + //************************************************************************* + friend bool operator == (const etl::basic_string_view& lhs, const etl::basic_string_view& rhs) + { + return (lhs.size() == rhs.size()) && + std::equal(lhs.begin(), lhs.end(), rhs.begin()); + } + + //************************************************************************* + /// Inequality for array views. + //************************************************************************* + friend bool operator != (const etl::basic_string_view& lhs, const etl::basic_string_view& rhs) + { + return !(lhs == rhs); + } + + //************************************************************************* + /// Less-than for array views. + //************************************************************************* + friend bool operator < (const etl::basic_string_view& lhs, const etl::basic_string_view& rhs) + { + return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); + } + + //************************************************************************* + /// Greater-than for array views. + //************************************************************************* + friend bool operator > (const etl::basic_string_view& lhs, const etl::basic_string_view& rhs) + { + return rhs < lhs; + } + + //************************************************************************* + /// Less-than-equal for array views. + //************************************************************************* + friend bool operator <= (const etl::basic_string_view& lhs, const etl::basic_string_view& rhs) + { + return !(lhs > rhs); + } + + //************************************************************************* + /// Greater-than-equal for array views. + //************************************************************************* + friend bool operator >= (const etl::basic_string_view& lhs, const etl::basic_string_view& rhs) + { + return !(lhs < rhs); + } + + private: + + const T* mbegin; + const T* mend; + }; + + typedef etl::basic_string_view string_view; + typedef etl::basic_string_view wstring_view; + typedef etl::basic_string_view u16string_view; + typedef etl::basic_string_view u32string_view; + + //************************************************************************* + /// Hash function. + //************************************************************************* +#if ETL_8BIT_SUPPORT + template <> + struct hash + { + size_t operator()(const etl::string_view& text) const + { + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); + } + }; + + template <> + struct hash + { + size_t operator()(const etl::wstring_view& text) const + { + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); + } + }; + + template <> + struct hash + { + size_t operator()(const etl::u16string_view& text) const + { + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); + } + }; + + template <> + struct hash + { + size_t operator()(const etl::u32string_view& text) const + { + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); + } + }; +#endif +} + +//************************************************************************* +/// Swaps the values. +//************************************************************************* +template > +void swap(etl::basic_string_view& lhs, etl::basic_string_view& rhs) +{ + lhs.swap(rhs); +} + +#ifdef ETL_COMPILER_MICROSOFT +#define min(a,b) (((a) < (b)) ? (a) : (b)) +#define max(a,b) (((a) > (b)) ? (a) : (b)) +#endif + +#undef ETL_FILE + +#endif + diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index 69b1be84..4d976d13 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -190,6 +190,7 @@ + @@ -359,6 +360,7 @@ + diff --git a/test/codeblocks/ETL.layout b/test/codeblocks/ETL.layout index aab3c739..3bd0bf67 100644 --- a/test/codeblocks/ETL.layout +++ b/test/codeblocks/ETL.layout @@ -2,239 +2,114 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -242,154 +117,29 @@ - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -397,134 +147,399 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - diff --git a/test/test_string_view.cpp b/test/test_string_view.cpp new file mode 100644 index 00000000..7d2f796c --- /dev/null +++ b/test/test_string_view.cpp @@ -0,0 +1,873 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2017 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#include "UnitTest++.h" + +#include "string_view.h" +#include "cstring.h" +#include "wstring.h" +#include "u16string.h" +#include "u32string.h" +#include "hash.h" + +#include +#include +#include +#include +#include + +namespace +{ + typedef etl::string_view View; + + static const size_t SIZE = 10; + + etl::string<11> etltext = "Hello World"; + std::string text = "Hello World"; + std::string text_smaller = "Hello Worlc"; + std::string text_shorter = "Hello Worl"; + std::string text_different = "Goodbye!!!!"; + + char ctext[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' }; + char* pctext = ctext; + + std::ostream& operator << (std::ostream& os, const View& view) + { + os << uintptr_t(view.begin()) << " " << uintptr_t(view.end()); + return os; + } + + SUITE(test_string_view) + { + //************************************************************************* + TEST(test_default_constructor) + { + View view; + + CHECK_EQUAL(0U, view.size()); + CHECK_EQUAL(0U, view.max_size()); + CHECK(view.empty()); + } + + //************************************************************************* + TEST(test_constructor_pointer_range) + { + View view(pctext, pctext + strlen(pctext)); + + CHECK_EQUAL(text.size(), view.size()); + CHECK_EQUAL(text.size(), view.max_size()); + + bool isEqual = std::equal(view.begin(), view.end(), text.begin()); + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_constructor_pointer_size) + { + View view(pctext, strlen(pctext)); + + CHECK_EQUAL(text.size(), view.size()); + CHECK_EQUAL(text.size(), view.max_size()); + + bool isEqual = std::equal(view.begin(), view.end(), text.begin()); + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_constructor_pointer) + { + View view(pctext); + + CHECK_EQUAL(text.size(), view.size()); + CHECK_EQUAL(text.size(), view.max_size()); + + bool isEqual = std::equal(view.begin(), view.end(), text.begin()); + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_constructor_literal) + { + View view("Hello World"); + + CHECK_EQUAL(text.size(), view.size()); + CHECK_EQUAL(text.size(), view.max_size()); + + bool isEqual = std::equal(view.begin(), view.end(), text.begin()); + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_assign_from_string_view) + { + View view1(text.c_str()); + View view2; + + view2 = view1; + + CHECK_EQUAL(view1.size(), view2.size()); + CHECK_EQUAL(view1.max_size(), view2.max_size()); + + bool isEqual; + + isEqual = std::equal(view1.begin(), view1.end(), view2.begin()); + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_assign_from_iterator_range) + { + View view; + + view.assign(text.c_str(), text.size()); + + CHECK_EQUAL(text.size(), view.size()); + CHECK_EQUAL(text.size(), view.max_size()); + + bool isEqual; + + isEqual = std::equal(text.begin(), text.end(), view.begin()); + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_begin_end) + { + View view(text.c_str(), text.size()); + + CHECK_EQUAL(view.begin(), view.cbegin()); + CHECK_EQUAL(view.rbegin().base(), view.crbegin().base()); + CHECK_EQUAL(view.end(), view.cend()); + CHECK_EQUAL(view.rend().base(), view.crend().base()); + } + + //************************************************************************* + TEST(test_front_back) + { + View view(text.c_str(), text.size()); + + CHECK_EQUAL(text.front(), view.front()); + CHECK_EQUAL(text.back(), view.back()); + } + + //************************************************************************* + TEST(test_data) + { + View view(text.c_str(), text.size()); + + CHECK_EQUAL(text.data(), view.data()); + } + + //************************************************************************* + TEST(test_index_operator) + { + View view(text.c_str(), text.size()); + + for (size_t i = 0; i < text.size(); ++i) + { + CHECK_EQUAL(text[i], view[i]); + } + } + + //************************************************************************* + TEST(test_at) + { + View view(text.c_str(), text.size()); + + for (size_t i = 0; i < text.size(); ++i) + { + CHECK_EQUAL(text[i], view.at(i)); + } + } + + //************************************************************************* + TEST(test_at_uninitialised_exception) + { + View view; + + CHECK_THROW(view.at(0), etl::string_view_uninitialised); + } + + //************************************************************************* + TEST(test_at_bounds_exception) + { + View view(text.c_str(), text.size()); + + CHECK_THROW(view.at(view.size()), etl::string_view_bounds); + } + + //************************************************************************* + TEST(test_non_member_same) + { + View view1(text.c_str(), text.size()); + View view2(text.c_str(), text.size()); + + CHECK(view1 == view2); + CHECK(view1 <= view2); + CHECK(view1 >= view2); + CHECK(!(view1 > view2)); + CHECK(!(view1 < view2)); + } + + //************************************************************************* + TEST(test_non_member_smaller) + { + View view1(text.c_str(), text.size()); + View view2(text_smaller.c_str(), text_smaller.size()); + + CHECK(!(view1 == view2)); + CHECK(!(view1 <= view2)); + CHECK(view2 <= view1); + CHECK(view1 >= view2); + CHECK(!(view2 >= view1)); + CHECK(view1 > view2); + CHECK(!(view2 > view1)); + CHECK(!(view1 < view2)); + CHECK(view2 < view1); + } + + //************************************************************************* + TEST(test_non_member_shorter) + { + View view1(text.c_str(), text.size()); + View view2(text_shorter.c_str(), text_shorter.size()); + + CHECK(!(view1 == view2)); + CHECK(!(view1 <= view2)); + CHECK(view2 <= view1); + CHECK(view1 >= view2); + CHECK(!(view2 >= view1)); + CHECK(view1 > view2); + CHECK(!(view2 > view1)); + CHECK(!(view1 < view2)); + CHECK(view2 < view1); + } + + //************************************************************************* + TEST(test_empty) + { + View view1(text.c_str(), text.c_str()); + CHECK(view1.empty()); + + View view2(text.c_str(), text.c_str() + 1); + CHECK(!view2.empty()); + } + + //************************************************************************* + TEST(test_swap) + { + View view1(text.c_str()); + View view2(text_smaller.c_str()); + + std::swap(view1, view2); + + CHECK_EQUAL(text.size(), view1.size()); + CHECK_EQUAL(text_smaller.size(), view2.size()); + + bool isEqual; + + isEqual = std::equal(view1.begin(), view1.end(), text_smaller.begin()); + CHECK(isEqual); + + isEqual = std::equal(view2.begin(), view2.end(), text.begin()); + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_remove_prefix_suffix) + { + std::string original = "Hello World"; + std::string prefix = "llo World"; + std::string prefixsuffix = "llo Wor"; + View view(original.c_str()); + + bool isEqual; + + view.remove_prefix(2); + isEqual = std::equal(view.begin(), view.end(), prefix.begin()); + CHECK(isEqual); + + view.remove_suffix(2); + isEqual = std::equal(view.begin(), view.end(), prefixsuffix.begin()); + CHECK(isEqual); + } + + //************************************************************************* + TEST(test_copy) + { + View view(text.c_str()); + std::array destination; + std::array blank; + size_t count; + + blank.fill(0); + + // Full text. + destination.fill(0); + count = view.copy(destination.data(), text.size(), 0); + CHECK_EQUAL(text.size(), count); + CHECK_EQUAL(text.size(), strlen(destination.data())); + CHECK_ARRAY_EQUAL(text.data(), destination.data(), text.size()); + + // From position 2, count OK. + destination.fill(0); + count = view.copy(destination.data(), text.size() - 2, 2); + CHECK_EQUAL(text.size() - 2, count); + CHECK_EQUAL(text.size() - 2, strlen(destination.data())); + CHECK_ARRAY_EQUAL(text.data() + 2, destination.data(), text.size() - 2); + + // From position 2, count too large. + destination.fill(0); + count = view.copy(destination.data(), text.size(), 2); + CHECK_EQUAL(text.size() - 2, count); + CHECK_EQUAL(text.size() - 2, strlen(destination.data())); + CHECK_ARRAY_EQUAL(text.data() + 2, destination.data(), text.size() - 2); + + // Position too large. + destination.fill(0); + count = view.copy(destination.data(), text.size(), 11); + CHECK_EQUAL(0U, count); + CHECK_ARRAY_EQUAL(blank.data(), destination.data(), text.size()); + + // From position 2, short range. + destination.fill(0); + count = view.copy(destination.data(), text.size() - 4, 2); + CHECK_EQUAL(text.size() - 4, count); + CHECK_EQUAL(text.size() - 4, strlen(destination.data())); + CHECK_ARRAY_EQUAL(text.data() + 2, destination.data(), text.size() - 4); + } + + //************************************************************************* + TEST(test_substr) + { + View view1(text.c_str()); + View view2(text.c_str() + 2, text.size() - 2); + View view3; + View view4(text.c_str() + 2, text.size() - 2); + View sub; + + // Whole string. + sub = view1.substr(0, text.size()); + CHECK_EQUAL(view1, sub); + + // From from +2 to -2. + sub = view1.substr(2, text.size() - 2); + CHECK_EQUAL(view2, sub); + + // Position too large. + sub = view1.substr(text.size(), 2); + CHECK_EQUAL(view3, sub); + + // Count too large. + sub = view1.substr(2, text.size()); + CHECK_EQUAL(view4, sub); + } + + //************************************************************************* + TEST(test_compare_basic_string_view) + { + View view(text.c_str()); + View view_smaller(text_smaller.c_str()); + View view_shorter(text_shorter.c_str()); + + CHECK_EQUAL(0, view.compare(view)); + CHECK_EQUAL(-1, view_smaller.compare(view)); + CHECK_EQUAL(-1, view_shorter.compare(view)); + CHECK_EQUAL(1, view.compare(view_smaller)); + CHECK_EQUAL(1, view.compare(view_shorter)); + } + + //************************************************************************* + TEST(test_compare_position_count_view) + { + View view(text.c_str()); + View view_smaller(text_smaller.c_str()); + View view_shorter(text_shorter.c_str()); + + std::string text_long = std::string("xx") + text + std::string("xx"); + View view_long(text_long.c_str()); + + std::string text_smaller_long = std::string("xx") + text_smaller + std::string("xx"); + View view_smaller_long(text_smaller_long.c_str()); + + std::string text_shorter_long = std::string("xx") + text_shorter + std::string("xx"); + View view_shorter_long(text_shorter_long.c_str()); + + CHECK_EQUAL(0, view_long.compare(2, view.size(), view)); + CHECK_EQUAL(-1, view_smaller_long.compare(2, view.size(), view)); + CHECK_EQUAL(1, view_long.compare(2, view.size(), view_smaller)); + CHECK_EQUAL(1, view_long.compare(2, view.size(), view_shorter)); + } + + //************************************************************************* + TEST(test_compare_position_count_view_position_count) + { + View view(text.c_str()); + + std::string text_long = std::string("xx") + text + std::string("xx"); + View view_long(text_long.c_str()); + + std::string text_smaller_long = std::string("xx") + text_smaller + std::string("xx"); + View view_smaller_long(text_smaller_long.c_str()); + + std::string text_shorter_long = std::string("xx") + text_shorter + std::string("xx"); + View view_shorter_long(text_shorter_long.c_str()); + + CHECK_EQUAL(0, view_long.compare(2, view.size(), view_long, 2, view.size())); + CHECK_EQUAL(-1, view_smaller_long.compare(2, view.size(), view_long, 2, view.size())); + CHECK_EQUAL(-1, view_shorter_long.compare(2, text_shorter.size(), view_long, 2, view.size())); + CHECK_EQUAL(1, view_long.compare(2, view.size(), view_smaller_long, 2, view.size())); + CHECK_EQUAL(1, view_long.compare(2, view.size(), view_shorter_long, 2, text_shorter.size())); + } + + //************************************************************************* + TEST(test_compare_text) + { + View view(text.c_str()); + View view_smaller(text_smaller.c_str()); + View view_shorter(text_shorter.c_str()); + + CHECK_EQUAL(0, view.compare(view.data())); + CHECK_EQUAL(-1, view_smaller.compare(view.data())); + CHECK_EQUAL(1, view.compare(view_smaller.data())); + CHECK_EQUAL(-1, view_shorter.compare(view.data())); + CHECK_EQUAL(1, view.compare(view_shorter.data())); + } + + //************************************************************************* + TEST(test_compare_position_count_text) + { + View view(text.c_str()); + View view_smaller(text_smaller.c_str()); + View view_shorter(text_shorter.c_str()); + + std::string text_long = std::string("xx") + text + std::string("xx"); + View view_long(text_long.c_str()); + + std::string text_smaller_long = std::string("xx") + text_smaller + std::string("xx"); + View view_smaller_long(text_smaller_long.c_str()); + + std::string text_shorter_long = std::string("xx") + text_shorter + std::string("xx"); + View view_shorter_long(text_shorter_long.c_str()); + + CHECK_EQUAL(0, view_long.compare(2, view.size(), view.data())); + CHECK_EQUAL(-1, view_smaller_long.compare(2, view.size(), view_long.data())); + CHECK_EQUAL(-1, view_shorter_long.compare(2, text_shorter.size(), view_long.data())); + CHECK_EQUAL(1, view_long.compare(2, view.size(), view_smaller.data())); + CHECK_EQUAL(1, view_long.compare(2, view.size(), view_shorter.data())); + } + + //************************************************************************* + TEST(test_compare_position_count_text_count) + { + View view(text.c_str()); + View view_smaller(text_smaller.c_str()); + View view_shorter(text_shorter.c_str()); + + std::string text_long = std::string("xx") + text + std::string("xx"); + View view_long(text_long.c_str()); + + std::string text_smaller_long = std::string("xx") + text_smaller + std::string("xx"); + View view_smaller_long(text_smaller_long.c_str()); + + std::string text_shorter_long = std::string("xx") + text_shorter + std::string("xx"); + View view_shorter_long(text_shorter_long.c_str()); + + CHECK_EQUAL(0, view_long.compare(2, view.size(), view.data(), view.size())); + CHECK_EQUAL(-1, view_smaller_long.compare(2, view.size(), view_long.data(), view_long.size())); + CHECK_EQUAL(-1, view_shorter_long.compare(2, text_shorter.size(), view_long.data(), view_long.size())); + CHECK_EQUAL(1, view_long.compare(2, view.size(), view_smaller.data(), view_smaller.size())); + CHECK_EQUAL(1, view_long.compare(2, view.size(), view_shorter.data(), view_shorter.size())); + } + + //************************************************************************* + TEST(test_starts_with) + { + View view(text.c_str()); + View start("Hello"); + View notstart("Gello"); + View notstartlong("Hello Worldxxxxxx"); + + CHECK(view.starts_with(start)); + CHECK(!view.starts_with(notstart)); + CHECK(!view.starts_with(notstartlong)); + + CHECK(view.starts_with('H')); + CHECK(!view.starts_with('G')); + + CHECK(view.starts_with("Hello")); + CHECK(!view.starts_with("Gello")); + CHECK(!view.starts_with("Hello Worldxxxxxx")); + } + + //************************************************************************* + TEST(test_ends_with) + { + View view(text.c_str()); + View end("World"); + View notend("Xorld"); + View notendlong("Hello Worldxxxxxx"); + + CHECK(view.ends_with(end)); + CHECK(!view.ends_with(notend)); + CHECK(!view.ends_with(notendlong)); + + CHECK(view.ends_with('d')); + CHECK(!view.ends_with('e')); + + CHECK(view.ends_with("World")); + CHECK(!view.ends_with("Xorld")); + CHECK(!view.ends_with("Hello Worldxxxxxx")); + } + + //************************************************************************* + TEST(test_find) + { + const char* s1 = "Hello"; + const char* s2 = "llo Wor"; + const char* s3 = "World"; + const char* s4 = "Xorld"; + const char* s5 = "Hello Worldxxxxxx"; + + View view(text.c_str()); + View v1(s1); + View v2(s2); + View v3(s3); + View v4(s4); + View v5(s5); + + CHECK_EQUAL(0U, view.find(v1)); + CHECK_EQUAL(2U, view.find(v2)); + CHECK_EQUAL(2U, view.find(v2, 2)); + CHECK_EQUAL(6U, view.find(v3)); + CHECK_EQUAL(View::npos, view.find(v3, 7)); + CHECK_EQUAL(View::npos, view.find(v4)); + CHECK_EQUAL(View::npos, view.find(v5)); + + CHECK_EQUAL(0U, view.find('H')); + CHECK_EQUAL(2U, view.find('l')); + CHECK_EQUAL(2U, view.find('l', 2)); + CHECK_EQUAL(View::npos, view.find('X')); + + CHECK_EQUAL(0U, view.find(s1)); + CHECK_EQUAL(2U, view.find(s2)); + CHECK_EQUAL(2U, view.find(s2, 2)); + CHECK_EQUAL(6U, view.find(s3)); + CHECK_EQUAL(View::npos, view.find(s3, 7)); + CHECK_EQUAL(View::npos, view.find(s4)); + CHECK_EQUAL(View::npos, view.find(s5)); + + CHECK_EQUAL(0U, view.find(s1, 0, 3)); + CHECK_EQUAL(2U, view.find(s2, 0, 3)); + CHECK_EQUAL(2U, view.find(s2, 2, 3)); + CHECK_EQUAL(6U, view.find(s3, 0, 3)); + CHECK_EQUAL(View::npos, view.find(s3, 7, 3)); + CHECK_EQUAL(View::npos, view.find(s4, 0, 3)); + CHECK_EQUAL(View::npos, view.find(s5, 0, 15)); + } + + //************************************************************************* + TEST(test_rfind) + { + const char* s1 = "ab"; + const char* s2 = "cd"; + const char* s3 = "bcd"; + const char* s4 = "abcdabcdabcd"; + + View view("abcdabcdab"); + View v1(s1); + View v2(s2); + View v3(s3); + View v4(s4); + + CHECK_EQUAL(8U, view.rfind(v1)); + CHECK_EQUAL(6U, view.rfind(v2)); + CHECK_EQUAL(2U, view.rfind(v2, 5)); + CHECK_EQUAL(View::npos, view.rfind(v4)); + + CHECK_EQUAL(8U, view.rfind('a')); + CHECK_EQUAL(6U, view.rfind('c')); + CHECK_EQUAL(2U, view.rfind('c', 5)); + + CHECK_EQUAL(8U, view.rfind(s1)); + CHECK_EQUAL(6U, view.rfind(s2)); + CHECK_EQUAL(2U, view.rfind(s2, 5)); + CHECK_EQUAL(View::npos, view.rfind(s4)); + + CHECK_EQUAL(1U, view.rfind(s3, 5, 2)); + CHECK_EQUAL(View::npos, view.rfind(s4, 0, 11)); + } + + //************************************************************************* + TEST(test_find_first_of) + { + const char* s1 = "l"; + const char* s2 = "o"; + const char* s3 = "W"; + const char* s4 = "Wol"; + const char* s5 = "wOL"; + + View view("Hello World"); + View v1(s1); + View v2(s2); + View v3(s3); + View v4(s4); + View v5(s5); + + CHECK_EQUAL(2U, view.find_first_of(v1)); + CHECK_EQUAL(4U, view.find_first_of(v2)); + CHECK_EQUAL(6U, view.find_first_of(v3)); + CHECK_EQUAL(2U, view.find_first_of(v4)); + CHECK_EQUAL(View::npos, view.find_first_of(v5)); + + CHECK_EQUAL(3U, view.find_first_of(v1, 3)); + CHECK_EQUAL(4U, view.find_first_of(v2, 2)); + CHECK_EQUAL(View::npos, view.find_first_of(v3, 7)); + CHECK_EQUAL(6U, view.find_first_of(v4, 5)); + CHECK_EQUAL(View::npos, view.find_first_of(v5, 2)); + + CHECK_EQUAL(2U, view.find_first_of('l')); + CHECK_EQUAL(4U, view.find_first_of('o')); + CHECK_EQUAL(6U, view.find_first_of('W')); + CHECK_EQUAL(View::npos, view.find_first_of('w')); + + CHECK_EQUAL(2U, view.find_first_of(s1)); + CHECK_EQUAL(4U, view.find_first_of(s2)); + CHECK_EQUAL(6U, view.find_first_of(s3)); + CHECK_EQUAL(2U, view.find_first_of(s4)); + CHECK_EQUAL(View::npos, view.find_first_of(s5)); + + CHECK_EQUAL(3U, view.find_first_of(s1, 3)); + CHECK_EQUAL(4U, view.find_first_of(s2, 2)); + CHECK_EQUAL(View::npos, view.find_first_of(s3, 7)); + CHECK_EQUAL(6U, view.find_first_of(s4, 5)); + CHECK_EQUAL(View::npos, view.find_first_of(s5, 2)); + + CHECK_EQUAL(3U, view.find_first_of(s1, 3, 1)); + CHECK_EQUAL(4U, view.find_first_of(s2, 2, 1)); + CHECK_EQUAL(View::npos, view.find_first_of(s3, 7, 1)); + CHECK_EQUAL(4U, view.find_first_of(s4, 0, 2)); + CHECK_EQUAL(View::npos, view.find_first_of(s5, 2, 3)); + } + + //************************************************************************* + TEST(test_find_last_of) + { + const char* s1 = "l"; + const char* s2 = "o"; + const char* s3 = "W"; + const char* s4 = "Wol"; + const char* s5 = "wOL"; + + View view("Hello World"); + View v1(s1); + View v2(s2); + View v3(s3); + View v4(s4); + View v5(s5); + + CHECK_EQUAL(9U, view.find_last_of(v1)); + CHECK_EQUAL(7U, view.find_last_of(v2)); + CHECK_EQUAL(6U, view.find_last_of(v3)); + CHECK_EQUAL(9U, view.find_last_of(v4)); + CHECK_EQUAL(View::npos, view.find_last_of(v5)); + + CHECK_EQUAL(3U, view.find_last_of(v1, 3)); + CHECK_EQUAL(View::npos, view.find_last_of(v2, 2)); + CHECK_EQUAL(6U, view.find_last_of(v3, 7)); + CHECK_EQUAL(4U, view.find_last_of(v4, 5)); + CHECK_EQUAL(View::npos, view.find_last_of(v5, 2)); + + CHECK_EQUAL(9U, view.find_last_of('l')); + CHECK_EQUAL(7U, view.find_last_of('o')); + CHECK_EQUAL(6U, view.find_last_of('W')); + CHECK_EQUAL(View::npos, view.find_last_of('w')); + + CHECK_EQUAL(9U, view.find_last_of(s1)); + CHECK_EQUAL(7U, view.find_last_of(s2)); + CHECK_EQUAL(6U, view.find_last_of(s3)); + CHECK_EQUAL(9U, view.find_last_of(s4)); + CHECK_EQUAL(View::npos, view.find_last_of(s5)); + + CHECK_EQUAL(3U, view.find_last_of(s1, 3)); + CHECK_EQUAL(View::npos, view.find_last_of(s2, 2)); + CHECK_EQUAL(6U, view.find_last_of(s3, 7)); + CHECK_EQUAL(4U, view.find_last_of(s4, 5)); + CHECK_EQUAL(View::npos, view.find_last_of(s5, 2)); + + CHECK_EQUAL(3U, view.find_last_of(s1, 3, 1)); + CHECK_EQUAL(View::npos, view.find_last_of(s2, 2, 1)); + CHECK_EQUAL(6U, view.find_last_of(s3, 7, 1)); + CHECK_EQUAL(4U, view.find_last_of(s4, 5, 2)); + CHECK_EQUAL(View::npos, view.find_last_of(s5, 2, 3)); + } + + //************************************************************************* + TEST(test_find_first_not_of) + { + const char* s1 = "H"; + const char* s2 = "eH"; + const char* s3 = "leH"; + const char* s4 = " olleH"; + const char* s5 = "wOL"; + const char* s6 = "Helo Wrd"; + + View view("Hello World"); + View v1(s1); + View v2(s2); + View v3(s3); + View v4(s4); + View v5(s5); + View v6(s6); + + CHECK_EQUAL(1U, view.find_first_not_of(v1)); + CHECK_EQUAL(2U, view.find_first_not_of(v2)); + CHECK_EQUAL(4U, view.find_first_not_of(v3)); + CHECK_EQUAL(6U, view.find_first_not_of(v4)); + CHECK_EQUAL(0U, view.find_first_not_of(v5)); + CHECK_EQUAL(View::npos, view.find_first_not_of(v6)); + + CHECK_EQUAL(3U, view.find_first_not_of(v1, 3)); + CHECK_EQUAL(2U, view.find_first_not_of(v2, 2)); + CHECK_EQUAL(7U, view.find_first_not_of(v3, 7)); + CHECK_EQUAL(6U, view.find_first_not_of(v4, 5)); + CHECK_EQUAL(2U, view.find_first_not_of(v5, 2)); + CHECK_EQUAL(View::npos, view.find_first_not_of(v6, 0)); + + CHECK_EQUAL(1U, view.find_first_not_of('H')); + CHECK_EQUAL(0U, view.find_first_not_of('e')); + + CHECK_EQUAL(1U, view.find_first_not_of(s1)); + CHECK_EQUAL(2U, view.find_first_not_of(s2)); + CHECK_EQUAL(4U, view.find_first_not_of(s3)); + CHECK_EQUAL(6U, view.find_first_not_of(s4)); + CHECK_EQUAL(0U, view.find_first_not_of(s5)); + CHECK_EQUAL(View::npos, view.find_first_not_of(s6)); + + CHECK_EQUAL(3U, view.find_first_not_of(s1, 3)); + CHECK_EQUAL(2U, view.find_first_not_of(s2, 2)); + CHECK_EQUAL(7U, view.find_first_not_of(s3, 7)); + CHECK_EQUAL(6U, view.find_first_not_of(s4, 5)); + CHECK_EQUAL(2U, view.find_first_not_of(s5, 2)); + CHECK_EQUAL(View::npos, view.find_first_not_of(s6, 0)); + + CHECK_EQUAL(3U, view.find_first_not_of(s1, 3, 1)); + CHECK_EQUAL(2U, view.find_first_not_of(s2, 2, 1)); + CHECK_EQUAL(7U, view.find_first_not_of(s3, 7, 1)); + CHECK_EQUAL(6U, view.find_first_not_of(s4, 5, 2)); + CHECK_EQUAL(2U, view.find_first_not_of(s5, 2, 3)); + CHECK_EQUAL(View::npos, view.find_first_not_of(s6, 0, 8)); + } + + //************************************************************************* + TEST(test_find_last_not_of) + { + const char* s1 = "d"; + const char* s2 = "dl"; + const char* s3 = "dlr"; + const char* s4 = "dlroW"; + const char* s5 = "DLROw"; + const char* s6 = "Helo Wrd"; + + View view("Hello World"); + View v1(s1); + View v2(s2); + View v3(s3); + View v4(s4); + View v5(s5); + View v6(s6); + + CHECK_EQUAL(9U, view.find_last_not_of(v1)); + CHECK_EQUAL(8U, view.find_last_not_of(v2)); + CHECK_EQUAL(7U, view.find_last_not_of(v3)); + CHECK_EQUAL(5U, view.find_last_not_of(v4)); + CHECK_EQUAL(10U, view.find_last_not_of(v5)); + CHECK_EQUAL(View::npos, view.find_last_not_of(v6)); + + CHECK_EQUAL(9U, view.find_last_not_of(v1, 10)); + CHECK_EQUAL(8U, view.find_last_not_of(v2, 9)); + CHECK_EQUAL(7U, view.find_last_not_of(v3, 8)); + CHECK_EQUAL(5U, view.find_last_not_of(v4, 8)); + CHECK_EQUAL(8U, view.find_last_not_of(v5, 8)); + CHECK_EQUAL(View::npos, view.find_last_not_of(v6, 10)); + + CHECK_EQUAL(9U, view.find_last_not_of('d')); + CHECK_EQUAL(10U, view.find_last_not_of('l')); + + CHECK_EQUAL(9U, view.find_last_not_of(s1)); + CHECK_EQUAL(8U, view.find_last_not_of(s2)); + CHECK_EQUAL(7U, view.find_last_not_of(s3)); + CHECK_EQUAL(5U, view.find_last_not_of(s4)); + CHECK_EQUAL(10U, view.find_last_not_of(s5)); + CHECK_EQUAL(View::npos, view.find_last_not_of(s6)); + + CHECK_EQUAL(9U, view.find_last_not_of(s1, 10)); + CHECK_EQUAL(8U, view.find_last_not_of(s2, 9)); + CHECK_EQUAL(7U, view.find_last_not_of(s3, 8)); + CHECK_EQUAL(5U, view.find_last_not_of(s4, 8)); + CHECK_EQUAL(8U, view.find_last_not_of(s5, 8)); + CHECK_EQUAL(View::npos, view.find_last_not_of(s6, 10)); + + CHECK_EQUAL(9U, view.find_last_not_of(s1, 10, 1)); + CHECK_EQUAL(9U, view.find_last_not_of(s2, 9, 1)); + CHECK_EQUAL(8U, view.find_last_not_of(s3, 9, 2)); + CHECK_EQUAL(7U, view.find_last_not_of(s4, 8, 3)); + CHECK_EQUAL(8U, view.find_last_not_of(s5, 8, 5)); + CHECK_EQUAL(View::npos, view.find_last_not_of(s6, 10, 8)); + } + + //************************************************************************* + TEST(test_hash) + { + typedef etl::string<11> Text; + typedef etl::wstring<11> WText; + typedef etl::u16string<11> U16Text; + typedef etl::u32string<11> U32Text; + + typedef etl::string_view View; + typedef etl::wstring_view WView; + typedef etl::u16string_view U16View; + typedef etl::u32string_view U32View; + + Text text("Hello World"); + WText wtext(L"Hello World"); + U16Text u16text(u"Hello World"); + U32Text u32text(U"Hello World"); + + View view(text.data()); + WView wview(wtext.data()); + U16View u16view(u16text.data()); + U32View u32view(u32text.data()); + + CHECK_EQUAL(etl::hash()(text), etl::hash()(view)); + CHECK_EQUAL(etl::hash()(wtext), etl::hash()(wview)); + CHECK_EQUAL(etl::hash()(u16text), etl::hash()(u16view)); + CHECK_EQUAL(etl::hash()(u32text), etl::hash()(u32view)); + } + }; +} diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index b04d4c24..cef80540 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -78,6 +78,8 @@ false + + Console @@ -162,6 +164,7 @@ + @@ -443,6 +446,7 @@ + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index 685c1998..4ddc59f6 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -564,6 +564,9 @@ ETL\Containers + + ETL\Containers + @@ -941,6 +944,9 @@ Source Files + + Source Files +