Loading...
Searching...
No Matches
string_view.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2017 John Wellbelove
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_INCLUDED
#define ETL_STRING_VIEW_INCLUDED
#include "platform.h"
#include "algorithm.h"
#include "basic_string.h"
#include "char_traits.h"
#include "error_handler.h"
#include "exception.h"
#include "hash.h"
#include "integral_limits.h"
#include "iterator.h"
#include "memory.h"
#include "private/minmax_push.h"
#if ETL_USING_STL && ETL_USING_CPP17
#include <string_view>
#endif
#if ETL_USING_STD_OSTREAM
#include <ostream>
#endif
#include <stdint.h>
namespace etl
{
//***************************************************************************
//***************************************************************************
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_)
{
}
};
//***************************************************************************
//***************************************************************************
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_STRING_VIEW_FILE_ID"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
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_STRING_VIEW_FILE_ID"B"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class string_view_empty : public string_view_exception
{
public:
string_view_empty(string_type file_name_, numeric_type line_number_)
: string_view_exception(ETL_ERROR_TEXT("basic_string_view:empty", ETL_STRING_VIEW_FILE_ID"C"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
template <typename T, typename TTraits = etl::char_traits<T> >
class basic_string_view
{
public:
typedef T value_type;
typedef TTraits traits_type;
typedef size_t size_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef const T* iterator;
typedef const T* const_iterator;
typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
enum
{
npos = etl::integral_limits<size_t>::max
};
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR basic_string_view() ETL_NOEXCEPT
: mbegin(ETL_NULLPTR)
, mend(ETL_NULLPTR)
{
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR basic_string_view(const etl::ibasic_string<T>& str) ETL_NOEXCEPT
: mbegin(str.begin())
, mend(str.end())
{
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 ETL_EXPLICIT_STRING_FROM_CHAR basic_string_view(const T* begin_) ETL_NOEXCEPT
: mbegin(begin_)
, mend(begin_ + TTraits::length(begin_))
{
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR basic_string_view(const T* begin_, const T* end_) ETL_NOEXCEPT
: mbegin(begin_)
, mend(end_)
{
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR basic_string_view(const T* begin_, size_t size_) ETL_NOEXCEPT
: mbegin(begin_)
, mend(begin_ + size_)
{
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR basic_string_view(const basic_string_view& other) ETL_NOEXCEPT
: mbegin(other.mbegin)
, mend(other.mend)
{
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR const_reference front() const
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return !empty() ? *mbegin : throw(ETL_ERROR(string_view_empty));
#else
ETL_ASSERT_CHECK_EXTRA(!empty(), ETL_ERROR(string_view_empty));
return *mbegin;
#endif
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR const_reference back() const
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
return !empty() ? *(mend - 1) : throw(ETL_ERROR(string_view_empty));
#else
ETL_ASSERT_CHECK_EXTRA(!empty(), ETL_ERROR(string_view_empty));
return *(mend - 1);
#endif
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
{
return mbegin;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
{
return mbegin;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
{
return mbegin;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
{
return mend;
}
//*************************************************************************
// Returns a const iterator to the end of the array.
//*************************************************************************
ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT
{
return mend;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
{
return const_reverse_iterator(mend);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
{
return const_reverse_iterator(mend);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
{
return const_reverse_iterator(mbegin);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
{
return const_reverse_iterator(mbegin);
}
//*************************************************************************
// Capacity
//*************************************************************************
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
{
return (mbegin == mend);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
{
return static_cast<size_t>(mend - mbegin);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR size_t length() const ETL_NOEXCEPT
{
return size();
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
{
return size();
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 etl::basic_string_view<T, TTraits>& operator=(const etl::basic_string_view<T, TTraits>& other) ETL_NOEXCEPT
{
mbegin = other.mbegin;
mend = other.mend;
return *this;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 void assign(const_pointer begin_, const_pointer end_) ETL_NOEXCEPT
{
mbegin = begin_;
mend = end_;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 void assign(const_pointer begin_, size_t size_) ETL_NOEXCEPT
{
mbegin = begin_;
mend = begin_ + size_;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR)
{
#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_INDEX_OPERATOR
return i < size() ? mbegin[i] : throw(ETL_ERROR(string_view_bounds));
#else
ETL_ASSERT_CHECK_INDEX_OPERATOR(i < size(), ETL_ERROR(string_view_bounds));
return mbegin[i];
#endif
}
//*************************************************************************
//*************************************************************************
const_reference at(size_t i) const
{
ETL_ASSERT((mbegin != ETL_NULLPTR && mend != ETL_NULLPTR), ETL_ERROR(string_view_uninitialised));
ETL_ASSERT(i < size(), ETL_ERROR(string_view_bounds));
return mbegin[i];
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 void swap(basic_string_view& other) ETL_NOEXCEPT
{
using ETL_OR_STD::swap; // Allow ADL
swap(mbegin, other.mbegin);
swap(mend, other.mend);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type copy(T* destination, size_type count, size_type position = 0) const ETL_NOEXCEPT
{
size_t n = 0UL;
if (position < size())
{
n = etl::min(count, size() - position);
etl::mem_copy(mbegin + position, n, destination);
}
return n;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 basic_string_view substr(size_type position = 0, size_type count = npos) const ETL_NOEXCEPT
{
basic_string_view view = basic_string_view();
if (position < size())
{
size_t n = etl::min(count, size() - position);
view = basic_string_view(mbegin + position, mbegin + position + n);
}
return view;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 void remove_prefix(size_type n) ETL_NOEXCEPT
{
mbegin += n;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 void remove_suffix(size_type n) ETL_NOEXCEPT
{
mend -= n;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 int compare(basic_string_view<T, TTraits> view) const ETL_NOEXCEPT
{
return (*this == view) ? 0 : ((*this > view) ? 1 : -1);
}
ETL_CONSTEXPR14 int compare(size_type position, size_type count, basic_string_view view) const ETL_NOEXCEPT
{
return substr(position, count).compare(view);
}
ETL_CONSTEXPR14 int compare(size_type position1, size_type count1, basic_string_view view, size_type position2, size_type count2) const
ETL_NOEXCEPT
{
return substr(position1, count1).compare(view.substr(position2, count2));
}
ETL_CONSTEXPR14 int compare(const T* text) const ETL_NOEXCEPT
{
const T* view_itr = mbegin;
const T* text_itr = text;
while (view_itr != mend && *text_itr != T(0))
{
if (*view_itr < *text_itr)
{
return -1;
}
else if (*view_itr > *text_itr)
{
return 1;
}
++view_itr;
++text_itr;
}
if ((view_itr == mend) && (*text_itr == T(0)))
{
return 0;
}
else if (view_itr == mend)
{
return -1;
}
else
{
return 1;
}
}
ETL_CONSTEXPR14 int compare(size_type position, size_type count, const T* text) const ETL_NOEXCEPT
{
return substr(position, count).compare(text);
}
ETL_CONSTEXPR14 int compare(size_type position, size_type count1, const T* text, size_type count2) const ETL_NOEXCEPT
{
return substr(position, count1).compare(etl::basic_string_view<T, TTraits>(text, count2));
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 bool starts_with(etl::basic_string_view<T, TTraits> view) const ETL_NOEXCEPT
{
return (size() >= view.size()) && (compare(0, view.size(), view) == 0);
}
ETL_CONSTEXPR14 bool starts_with(T c) const ETL_NOEXCEPT
{
return !empty() && (front() == c);
}
ETL_CONSTEXPR14 bool starts_with(const T* text) const ETL_NOEXCEPT
{
size_t lengthtext = TTraits::length(text);
return (size() >= lengthtext) && (compare(0, lengthtext, text) == 0);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 bool ends_with(etl::basic_string_view<T, TTraits> view) const ETL_NOEXCEPT
{
return (size() >= view.size()) && (compare(size() - view.size(), npos, view) == 0);
}
ETL_CONSTEXPR14 bool ends_with(T c) const
{
return !empty() && (back() == c);
}
ETL_CONSTEXPR14 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);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type find(etl::basic_string_view<T, TTraits> view, size_type position = 0) const ETL_NOEXCEPT
{
if ((size() < view.size()))
{
return npos;
}
const_iterator iposition = etl::search(begin() + position, end(), view.begin(), view.end());
if (iposition == end())
{
return npos;
}
else
{
return static_cast<size_type>(etl::distance(begin(), iposition));
}
}
ETL_CONSTEXPR14 size_type find(T c, size_type position = 0) const ETL_NOEXCEPT
{
return find(etl::basic_string_view<T, TTraits>(&c, 1), position);
}
ETL_CONSTEXPR14 size_type find(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
{
return find(etl::basic_string_view<T, TTraits>(text, count), position);
}
ETL_CONSTEXPR14 size_type find(const T* text, size_type position = 0) const ETL_NOEXCEPT
{
return find(etl::basic_string_view<T, TTraits>(text), position);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type rfind(etl::basic_string_view<T, TTraits> view, size_type position = npos) const ETL_NOEXCEPT
{
if ((size() < view.size()))
{
return npos;
}
position = etl::min(position, size() - view.size());
const_iterator iposition = etl::find_end(begin(), begin() + position + view.size(), view.begin(), view.end());
if (iposition == end())
{
return npos;
}
else
{
return static_cast<size_type>(etl::distance(begin(), iposition));
}
}
ETL_CONSTEXPR14 size_type rfind(T c, size_type position = npos) const ETL_NOEXCEPT
{
return rfind(etl::basic_string_view<T, TTraits>(&c, 1), position);
}
ETL_CONSTEXPR14 size_type rfind(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
{
return rfind(etl::basic_string_view<T, TTraits>(text, count), position);
}
ETL_CONSTEXPR14 size_type rfind(const T* text, size_type position = npos) const ETL_NOEXCEPT
{
return rfind(etl::basic_string_view<T, TTraits>(text), position);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type find_first_of(etl::basic_string_view<T, TTraits> view, size_type position = 0) const ETL_NOEXCEPT
{
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 = 0UL; j < lengthview; ++j)
{
if (mbegin[i] == view[j])
{
return i;
}
}
}
}
return npos;
}
ETL_CONSTEXPR14 size_type find_first_of(T c, size_type position = 0) const ETL_NOEXCEPT
{
return find_first_of(etl::basic_string_view<T, TTraits>(&c, 1), position);
}
ETL_CONSTEXPR14 size_type find_first_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
{
return find_first_of(etl::basic_string_view<T, TTraits>(text, count), position);
}
ETL_CONSTEXPR14 size_type find_first_of(const T* text, size_type position = 0) const ETL_NOEXCEPT
{
return find_first_of(etl::basic_string_view<T, TTraits>(text), position);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type find_last_of(etl::basic_string_view<T, TTraits> view, size_type position = npos) const ETL_NOEXCEPT
{
if (empty())
{
return npos;
}
position = etl::min(position, size() - 1);
const_reverse_iterator it = rbegin() + static_cast<ptrdiff_t>(size() - position - 1);
while (it != rend())
{
const size_t viewlength = view.size();
for (size_t j = 0UL; j < viewlength; ++j)
{
if (mbegin[position] == view[j])
{
return position;
}
}
++it;
--position;
}
return npos;
}
ETL_CONSTEXPR14 size_type find_last_of(T c, size_type position = npos) const ETL_NOEXCEPT
{
return find_last_of(etl::basic_string_view<T, TTraits>(&c, 1), position);
}
ETL_CONSTEXPR14 size_type find_last_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
{
return find_last_of(etl::basic_string_view<T, TTraits>(text, count), position);
}
ETL_CONSTEXPR14 size_type find_last_of(const T* text, size_type position = npos) const ETL_NOEXCEPT
{
return find_last_of(etl::basic_string_view<T, TTraits>(text), position);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type find_first_not_of(etl::basic_string_view<T, TTraits> view, size_type position = 0) const ETL_NOEXCEPT
{
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 = 0UL; j < viewlength; ++j)
{
if (mbegin[i] == view[j])
{
found = true;
break;
}
}
if (!found)
{
return i;
}
}
}
return npos;
}
ETL_CONSTEXPR14 size_type find_first_not_of(T c, size_type position = 0) const ETL_NOEXCEPT
{
return find_first_not_of(etl::basic_string_view<T, TTraits>(&c, 1), position);
}
ETL_CONSTEXPR14 size_type find_first_not_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
{
return find_first_not_of(etl::basic_string_view<T, TTraits>(text, count), position);
}
ETL_CONSTEXPR14 size_type find_first_not_of(const T* text, size_type position = 0) const ETL_NOEXCEPT
{
return find_first_not_of(etl::basic_string_view<T, TTraits>(text), position);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 size_type find_last_not_of(etl::basic_string_view<T, TTraits> view, size_type position = npos) const ETL_NOEXCEPT
{
if (empty())
{
return npos;
}
position = etl::min(position, size() - 1);
const_reverse_iterator it = rbegin() + static_cast<ptrdiff_t>(size() - position - 1);
while (it != rend())
{
bool found = false;
const size_t viewlength = view.size();
for (size_t j = 0UL; j < viewlength; ++j)
{
if (mbegin[position] == view[j])
{
found = true;
break;
}
}
if (!found)
{
return position;
}
++it;
--position;
}
return npos;
}
ETL_CONSTEXPR14 size_type find_last_not_of(T c, size_type position = npos) const ETL_NOEXCEPT
{
return find_last_not_of(etl::basic_string_view<T, TTraits>(&c, 1), position);
}
ETL_CONSTEXPR14 size_type find_last_not_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
{
return find_last_not_of(etl::basic_string_view<T, TTraits>(text, count), position);
}
ETL_CONSTEXPR14 size_type find_last_not_of(const T* text, size_type position = npos) const ETL_NOEXCEPT
{
return find_last_not_of(etl::basic_string_view<T, TTraits>(text), position);
}
//*********************************************************************
//*********************************************************************
bool contains(const etl::basic_string_view<T, TTraits>& view) const ETL_NOEXCEPT
{
return find(view) != npos;
}
//*********************************************************************
//*********************************************************************
bool contains(const_pointer s) const ETL_NOEXCEPT
{
return find(s) != npos;
}
//*********************************************************************
//*********************************************************************
bool contains(value_type c) const ETL_NOEXCEPT
{
return find(c) != npos;
}
//*************************************************************************
//*************************************************************************
friend ETL_CONSTEXPR14 bool operator==(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
{
return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
}
//*************************************************************************
//*************************************************************************
friend ETL_CONSTEXPR14 bool operator!=(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
{
return !(lhs == rhs);
}
//*************************************************************************
//*************************************************************************
friend ETL_CONSTEXPR14 bool operator<(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
{
return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
//*************************************************************************
//*************************************************************************
friend ETL_CONSTEXPR14 bool operator>(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
{
return rhs < lhs;
}
//*************************************************************************
//*************************************************************************
friend ETL_CONSTEXPR14 bool operator<=(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
{
return !(lhs > rhs);
}
//*************************************************************************
//*************************************************************************
friend ETL_CONSTEXPR14 bool operator>=(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
{
return !(lhs < rhs);
}
private:
const_pointer mbegin;
const_pointer mend;
};
typedef etl::basic_string_view<char> string_view;
typedef etl::basic_string_view<wchar_t> wstring_view;
typedef etl::basic_string_view<char8_t> u8string_view;
typedef etl::basic_string_view<char16_t> u16string_view;
typedef etl::basic_string_view<char32_t> u32string_view;
//*************************************************************************
//*************************************************************************
template <size_t Array_Size>
ETL_CONSTEXPR14 string_view make_string_view(const char (&text)[Array_Size]) ETL_NOEXCEPT
{
size_t length = etl::char_traits<char>::length(text, Array_Size - 1U);
return string_view(text, length);
}
//***********************************
template <size_t Array_Size>
ETL_CONSTEXPR14 wstring_view make_string_view(const wchar_t (&text)[Array_Size]) ETL_NOEXCEPT
{
size_t length = etl::char_traits<wchar_t>::length(text, Array_Size - 1U);
return wstring_view(text, length);
}
//***********************************
template <size_t Array_Size>
ETL_CONSTEXPR14 u8string_view make_string_view(const char8_t (&text)[Array_Size]) ETL_NOEXCEPT
{
size_t length = etl::char_traits<char8_t>::length(text, Array_Size - 1U);
return u8string_view(text, length);
}
//***********************************
template <size_t Array_Size>
ETL_CONSTEXPR14 u16string_view make_string_view(const char16_t (&text)[Array_Size]) ETL_NOEXCEPT
{
size_t length = etl::char_traits<char16_t>::length(text, Array_Size - 1U);
return u16string_view(text, length);
}
//***********************************
template <size_t Array_Size>
ETL_CONSTEXPR14 u32string_view make_string_view(const char32_t (&text)[Array_Size]) ETL_NOEXCEPT
{
size_t length = etl::char_traits<char32_t>::length(text, Array_Size - 1U);
return u32string_view(text, length);
}
//*************************************************************************
//*************************************************************************
#if ETL_USING_8BIT_TYPES
template <>
struct hash<etl::string_view>
{
size_t operator()(const etl::string_view& text) const
{
return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
reinterpret_cast<const uint8_t*>(text.data() + text.size()));
}
};
template <>
struct hash<etl::wstring_view>
{
size_t operator()(const etl::wstring_view& text) const
{
return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
reinterpret_cast<const uint8_t*>(text.data() + text.size()));
}
};
template <>
struct hash<etl::u16string_view>
{
size_t operator()(const etl::u16string_view& text) const
{
return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
reinterpret_cast<const uint8_t*>(text.data() + text.size()));
}
};
template <>
struct hash<etl::u32string_view>
{
size_t operator()(const etl::u32string_view& text) const
{
return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
reinterpret_cast<const uint8_t*>(text.data() + text.size()));
}
};
#endif
} // namespace etl
//*************************************************************************
//*************************************************************************
template <typename T, typename TTraits >
void swap(etl::basic_string_view<T, TTraits>& lhs, etl::basic_string_view<T, TTraits>& rhs) ETL_NOEXCEPT
{
lhs.swap(rhs);
}
template <typename T>
void swap(etl::basic_string_view<T, etl::char_traits<T> >& lhs, etl::basic_string_view<T, etl::char_traits<T> >& rhs) ETL_NOEXCEPT
{
lhs.swap(rhs);
}
//*************************************************************************
//*************************************************************************
#if ETL_USING_STD_OSTREAM
template <typename T>
std::basic_ostream<T, std::char_traits<T> >& operator<<(std::basic_ostream<T, std::char_traits<T> >& os,
etl::basic_string_view<T, etl::char_traits<T> > text)
{
os.write(text.data(), static_cast<std::streamsize>(text.size()));
return os;
}
#endif
#include "private/minmax_pop.h"
#endif
algorithm.h
basic_string.h
char_traits.h
etl::basic_string_view
String view.
Definition string_view.h:117
etl::basic_string_view::find
ETL_CONSTEXPR14 size_type find(etl::basic_string_view< T, TTraits > view, size_type position=0) const ETL_NOEXCEPT
Find characters in the view.
Definition string_view.h:548
etl::basic_string_view::basic_string_view
ETL_CONSTEXPR basic_string_view() ETL_NOEXCEPT
Default constructor.
Definition string_view.h:139
etl::basic_string_view::cbegin
ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition string_view.h:239
etl::basic_string_view::size
ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
Returns the size of the array.
Definition string_view.h:307
etl::basic_string_view::rend
ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition string_view.h:279
etl::basic_string_view::crend
ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition string_view.h:287
etl::basic_string_view::basic_string_view
ETL_CONSTEXPR basic_string_view(const basic_string_view &other) ETL_NOEXCEPT
Copy constructor.
Definition string_view.h:184
etl::basic_string_view::operator<=
friend ETL_CONSTEXPR14 bool operator<=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Less-than-equal for string_view.
Definition string_view.h:874
etl::basic_string_view::operator=
ETL_CONSTEXPR14 etl::basic_string_view< T, TTraits > & operator=(const etl::basic_string_view< T, TTraits > &other) ETL_NOEXCEPT
Assign from a view.
Definition string_view.h:331
etl::basic_string_view::find_last_not_of
ETL_CONSTEXPR14 size_type find_last_not_of(etl::basic_string_view< T, TTraits > view, size_type position=npos) const ETL_NOEXCEPT
Find last absence of characters.
Definition string_view.h:762
etl::basic_string_view::starts_with
ETL_CONSTEXPR14 bool starts_with(etl::basic_string_view< T, TTraits > view) const ETL_NOEXCEPT
Checks if the string view starts with the given prefix.
Definition string_view.h:507
etl::basic_string_view::operator>=
friend ETL_CONSTEXPR14 bool operator>=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Greater-than-equal for string_view.
Definition string_view.h:882
etl::basic_string_view::remove_prefix
ETL_CONSTEXPR14 void remove_prefix(size_type n) ETL_NOEXCEPT
Shrinks the view by moving its start forward.
Definition string_view.h:429
etl::basic_string_view::find_first_of
ETL_CONSTEXPR14 size_type find_first_of(etl::basic_string_view< T, TTraits > view, size_type position=0) const ETL_NOEXCEPT
Find first occurrence of characters.
Definition string_view.h:624
etl::basic_string_view::data
ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
Returns a const pointer to the first element of the internal storage.
Definition string_view.h:223
etl::basic_string_view::contains
bool contains(const etl::basic_string_view< T, TTraits > &view) const ETL_NOEXCEPT
Checks that the view is within this string.
Definition string_view.h:818
etl::basic_string_view::max_size
ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
Returns the maximum possible size of the array.
Definition string_view.h:323
etl::basic_string_view::operator!=
friend ETL_CONSTEXPR14 bool operator!=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Inequality for string_view.
Definition string_view.h:850
etl::basic_string_view::length
ETL_CONSTEXPR size_t length() const ETL_NOEXCEPT
Returns the size of the array.
Definition string_view.h:315
etl::basic_string_view::ends_with
ETL_CONSTEXPR14 bool ends_with(etl::basic_string_view< T, TTraits > view) const ETL_NOEXCEPT
Checks if the string view ends with the given suffix.
Definition string_view.h:527
etl::basic_string_view::find_last_of
ETL_CONSTEXPR14 size_type find_last_of(etl::basic_string_view< T, TTraits > view, size_type position=npos) const ETL_NOEXCEPT
Find last occurrence of characters.
Definition string_view.h:665
etl::basic_string_view::swap
ETL_CONSTEXPR14 void swap(basic_string_view &other) ETL_NOEXCEPT
Swaps with another basic_string_view.
Definition string_view.h:384
etl::basic_string_view::contains
bool contains(const_pointer s) const ETL_NOEXCEPT
Checks that text is within this string.
Definition string_view.h:826
etl::basic_string_view::operator<
friend ETL_CONSTEXPR14 bool operator<(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Less-than for string_view.
Definition string_view.h:858
etl::basic_string_view::back
ETL_CONSTEXPR const_reference back() const
Definition string_view.h:210
etl::basic_string_view::compare
ETL_CONSTEXPR14 int compare(basic_string_view< T, TTraits > view) const ETL_NOEXCEPT
Compares two views.
Definition string_view.h:445
etl::basic_string_view::end
ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
Returns a const iterator to the end of the array.
Definition string_view.h:247
etl::basic_string_view::at
const_reference at(size_t i) const
Returns a const reference to the indexed value.
Definition string_view.h:374
etl::basic_string_view::remove_suffix
ETL_CONSTEXPR14 void remove_suffix(size_type n) ETL_NOEXCEPT
Shrinks the view by moving its end backward.
Definition string_view.h:437
etl::basic_string_view::assign
ETL_CONSTEXPR14 void assign(const_pointer begin_, const_pointer end_) ETL_NOEXCEPT
Assign from iterators.
Definition string_view.h:341
etl::basic_string_view::contains
bool contains(value_type c) const ETL_NOEXCEPT
Checks that character is within this string.
Definition string_view.h:834
etl::basic_string_view::basic_string_view
ETL_CONSTEXPR basic_string_view(const T *begin_, size_t size_) ETL_NOEXCEPT
Construct from pointer/size.
Definition string_view.h:175
etl::basic_string_view::basic_string_view
ETL_CONSTEXPR basic_string_view(const etl::ibasic_string< T > &str) ETL_NOEXCEPT
Construct from string.
Definition string_view.h:148
etl::basic_string_view::assign
ETL_CONSTEXPR14 void assign(const_pointer begin_, size_t size_) ETL_NOEXCEPT
Assign from iterator and size.
Definition string_view.h:350
etl::basic_string_view::operator>
friend ETL_CONSTEXPR14 bool operator>(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Greater-than for string_view.
Definition string_view.h:866
etl::basic_string_view::find_first_not_of
ETL_CONSTEXPR14 size_type find_first_not_of(etl::basic_string_view< T, TTraits > view, size_type position=0) const ETL_NOEXCEPT
Find first absence of characters.
Definition string_view.h:713
etl::basic_string_view::basic_string_view
ETL_CONSTEXPR14 ETL_EXPLICIT_STRING_FROM_CHAR basic_string_view(const T *begin_) ETL_NOEXCEPT
Construct from T*.
Definition string_view.h:157
etl::basic_string_view::operator==
friend ETL_CONSTEXPR14 bool operator==(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Equality for string_view.
Definition string_view.h:842
etl::basic_string_view::begin
ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition string_view.h:231
etl::basic_string_view::front
ETL_CONSTEXPR const_reference front() const
Definition string_view.h:195
etl::basic_string_view::crbegin
ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
Returns a const reverse iterator to the reverse beginning of the array.
Definition string_view.h:271
etl::basic_string_view::substr
ETL_CONSTEXPR14 basic_string_view substr(size_type position=0, size_type count=npos) const ETL_NOEXCEPT
Returns a substring.
Definition string_view.h:412
etl::basic_string_view::operator[]
ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS||ETL_NOT_CHECKING_INDEX_OPERATOR)
Definition string_view.h:361
etl::basic_string_view::rfind
ETL_CONSTEXPR14 size_type rfind(etl::basic_string_view< T, TTraits > view, size_type position=npos) const ETL_NOEXCEPT
Find the last occurrence of a substring.
Definition string_view.h:585
etl::basic_string_view::copy
ETL_CONSTEXPR14 size_type copy(T *destination, size_type count, size_type position=0) const ETL_NOEXCEPT
Copies characters.
Definition string_view.h:395
etl::basic_string_view::rbegin
ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
Returns a const reverse iterator to the reverse beginning of the array.
Definition string_view.h:263
etl::basic_string_view::empty
ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
Returns true if the array size is zero.
Definition string_view.h:299
etl::basic_string_view::basic_string_view
ETL_CONSTEXPR basic_string_view(const T *begin_, const T *end_) ETL_NOEXCEPT
Construct from pointer range.
Definition string_view.h:166
etl::ibasic_string
Definition basic_string.h:335
etl::size_type
etl::string_view_bounds
Definition string_view.h:76
etl::string_view_empty
The exception thrown when the view is empty.
Definition string_view.h:103
etl::string_view_uninitialised
Definition string_view.h:90
error_handler.h
exception.h
etl::const_iterator
ETL_ASSERT
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
etl::exception::exception
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
etl::integral_limits
Definition integral_limits.h:517
hash.h
integral_limits.h
iterator.h
memory.h
minmax_pop.h
minmax_push.h
etl
Definition absolute.h:40
etl::make_string_view
ETL_CONSTEXPR14 string_view make_string_view(const char(&text)[Array_Size]) ETL_NOEXCEPT
make_string_view.
Definition string_view.h:903
etl::mem_copy
T * mem_copy(const T *sb, const T *se, T *db) ETL_NOEXCEPT
Definition memory.h:3260
etl::cend
TContainer::const_iterator cend(const TContainer &container)
Definition iterator.h:1186
platform.h
swap
void swap(etl::basic_string_view< T, TTraits > &lhs, etl::basic_string_view< T, TTraits > &rhs) ETL_NOEXCEPT
Swaps the values.
Definition string_view.h:996
operator<<
std::basic_ostream< T, std::char_traits< T > > & operator<<(std::basic_ostream< T, std::char_traits< T > > &os, etl::basic_string_view< T, etl::char_traits< T > > text)
Operator overload to write to std basic_ostream.
Definition string_view.h:1012
etl::char_traits
Character traits for any character type.
Definition char_traits.h:128