forked from qt-creator/qt-creator
Utils: Rebase SmallStringView on std::string_view
There are still some methods missing but with C++20 we can maybe remove SmallStringView. Change-Id: I65a1eacda0a07cec824f1837e385faa01fc825e9 Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -39,14 +39,13 @@ class AbstractFilePathView : public Utils::SmallStringView
|
|||||||
public:
|
public:
|
||||||
constexpr AbstractFilePathView() = default;
|
constexpr AbstractFilePathView() = default;
|
||||||
explicit AbstractFilePathView(const char *const string, const size_type size) noexcept
|
explicit AbstractFilePathView(const char *const string, const size_type size) noexcept
|
||||||
: Utils::SmallStringView(string, size),
|
: AbstractFilePathView{Utils::SmallStringView{string, size}}
|
||||||
m_slashIndex(lastSlashIndex(*this))
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit AbstractFilePathView(Utils::SmallStringView filePath)
|
explicit AbstractFilePathView(Utils::SmallStringView filePath)
|
||||||
: Utils::SmallStringView(filePath),
|
: Utils::SmallStringView(filePath)
|
||||||
m_slashIndex(lastSlashIndex(filePath))
|
, m_slashIndex(lastSlashIndex(filePath))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,9 +90,10 @@ public:
|
|||||||
constexpr char separator = Utils::HostOsInfo::isWindowsHost() ? WindowsSlash : '/';
|
constexpr char separator = Utils::HostOsInfo::isWindowsHost() ? WindowsSlash : '/';
|
||||||
auto foundReverse = std::find(filePath.rbegin(), filePath.rend(), separator);
|
auto foundReverse = std::find(filePath.rbegin(), filePath.rend(), separator);
|
||||||
auto found = foundReverse.base();
|
auto found = foundReverse.base();
|
||||||
--found;
|
|
||||||
|
|
||||||
return std::distance(filePath.begin(), found);
|
auto distance = std::distance(filePath.begin(), found);
|
||||||
|
|
||||||
|
return distance - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend bool operator==(const AbstractFilePathView &first, const AbstractFilePathView &second)
|
friend bool operator==(const AbstractFilePathView &first, const AbstractFilePathView &second)
|
||||||
|
|||||||
@@ -64,8 +64,8 @@ template<uint Size>
|
|||||||
class BasicSmallString
|
class BasicSmallString
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using iterator = SmallStringView::iterator;
|
using const_iterator = Internal::SmallStringIterator<std::random_access_iterator_tag, const char>;
|
||||||
using const_iterator = SmallStringView::const_iterator;
|
using iterator = Internal::SmallStringIterator<std::random_access_iterator_tag, char>;
|
||||||
using reverse_iterator = std::reverse_iterator<iterator>;
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
||||||
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
||||||
using size_type = std::size_t;
|
using size_type = std::size_t;
|
||||||
@@ -116,11 +116,12 @@ public:
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
explicit BasicSmallString(const_iterator begin, const_iterator end)
|
explicit BasicSmallString(const_iterator begin, const_iterator end)
|
||||||
: BasicSmallString(SmallStringView{begin, end})
|
: BasicSmallString{std::addressof(*begin), static_cast<std::size_t>(std::distance(begin, end))}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
explicit BasicSmallString(iterator begin, iterator end)
|
explicit BasicSmallString(iterator begin, iterator end)
|
||||||
: BasicSmallString(SmallStringView{begin, end})
|
|
||||||
|
: BasicSmallString{std::addressof(*begin), static_cast<std::size_t>(std::distance(begin, end))}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template<typename Type, typename = std::enable_if_t<std::is_pointer<Type>::value>>
|
template<typename Type, typename = std::enable_if_t<std::is_pointer<Type>::value>>
|
||||||
|
|||||||
@@ -32,6 +32,7 @@
|
|||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
|
|
||||||
@@ -45,45 +46,24 @@ using enable_if_has_char_data_pointer = typename std::enable_if_t<
|
|||||||
>, char>::value
|
>, char>::value
|
||||||
, int>;
|
, int>;
|
||||||
|
|
||||||
class SmallStringView
|
class SmallStringView : public std::string_view
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using const_iterator = Internal::SmallStringIterator<std::random_access_iterator_tag, const char>;
|
using std::string_view::string_view;
|
||||||
using iterator = Internal::SmallStringIterator<std::random_access_iterator_tag, char>;
|
|
||||||
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
|
||||||
using size_type = std::size_t;
|
|
||||||
|
|
||||||
constexpr SmallStringView() = default;
|
|
||||||
|
|
||||||
constexpr SmallStringView(const char *characterPointer) noexcept
|
|
||||||
: m_pointer(characterPointer)
|
|
||||||
, m_size(std::char_traits<char>::length(characterPointer))
|
|
||||||
{}
|
|
||||||
|
|
||||||
constexpr SmallStringView(const char *const string, const size_type size) noexcept
|
|
||||||
: m_pointer(string)
|
|
||||||
, m_size(size)
|
|
||||||
{}
|
|
||||||
|
|
||||||
constexpr SmallStringView(const char *const begin, const char *const end) noexcept
|
|
||||||
: m_pointer(begin)
|
|
||||||
, m_size(static_cast<std::size_t>(std::distance(begin, end)))
|
|
||||||
{}
|
|
||||||
|
|
||||||
constexpr SmallStringView(const_iterator begin, const_iterator end) noexcept
|
constexpr SmallStringView(const_iterator begin, const_iterator end) noexcept
|
||||||
: m_pointer(begin.data())
|
: std::string_view{std::addressof(*begin), static_cast<std::size_t>(std::distance(begin, end))}
|
||||||
, m_size(std::size_t(end - begin))
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
constexpr SmallStringView(iterator begin, iterator end) noexcept
|
#ifdef Q_OS_WINDOWS
|
||||||
: m_pointer(begin.data())
|
constexpr SmallStringView(const char *const begin, const char *const end) noexcept
|
||||||
, m_size(std::size_t(end - begin))
|
: std::string_view{begin, static_cast<std::size_t>(std::distance(begin, end))}
|
||||||
{}
|
{}
|
||||||
|
#endif
|
||||||
|
|
||||||
template<typename String, typename Utils::enable_if_has_char_data_pointer<String> = 0>
|
template<typename String, typename Utils::enable_if_has_char_data_pointer<String> = 0>
|
||||||
constexpr SmallStringView(const String &string) noexcept
|
constexpr SmallStringView(const String &string) noexcept
|
||||||
: m_pointer(string.data())
|
: std::string_view{string.data(), static_cast<std::size_t>(string.size())}
|
||||||
, m_size(string.size())
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
static constexpr SmallStringView fromUtf8(const char *const characterPointer)
|
static constexpr SmallStringView fromUtf8(const char *const characterPointer)
|
||||||
@@ -91,29 +71,7 @@ public:
|
|||||||
return SmallStringView(characterPointer);
|
return SmallStringView(characterPointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr
|
constexpr size_type isEmpty() const noexcept { return empty(); }
|
||||||
const char *data() const noexcept
|
|
||||||
{
|
|
||||||
return m_pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr
|
|
||||||
size_type size() const noexcept
|
|
||||||
{
|
|
||||||
return m_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr
|
|
||||||
size_type isEmpty() const noexcept
|
|
||||||
{
|
|
||||||
return m_size == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr
|
|
||||||
size_type empty() const noexcept
|
|
||||||
{
|
|
||||||
return m_size == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr
|
constexpr
|
||||||
SmallStringView mid(size_type position) const noexcept
|
SmallStringView mid(size_type position) const noexcept
|
||||||
@@ -127,28 +85,6 @@ public:
|
|||||||
return SmallStringView(data() + position, length);
|
return SmallStringView(data() + position, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr
|
|
||||||
const_iterator begin() const noexcept
|
|
||||||
{
|
|
||||||
return data();
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr
|
|
||||||
const_iterator end() const noexcept
|
|
||||||
{
|
|
||||||
return data() + size();
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr const_reverse_iterator rbegin() const noexcept
|
|
||||||
{
|
|
||||||
return const_reverse_iterator(end());
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr const_reverse_iterator rend() const noexcept
|
|
||||||
{
|
|
||||||
return const_reverse_iterator(begin());
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr20 operator std::string() const { return std::string(data(), size()); }
|
constexpr20 operator std::string() const { return std::string(data(), size()); }
|
||||||
|
|
||||||
explicit operator QString() const
|
explicit operator QString() const
|
||||||
@@ -159,7 +95,7 @@ public:
|
|||||||
constexpr bool startsWith(SmallStringView subStringToSearch) const noexcept
|
constexpr bool startsWith(SmallStringView subStringToSearch) const noexcept
|
||||||
{
|
{
|
||||||
if (size() >= subStringToSearch.size())
|
if (size() >= subStringToSearch.size())
|
||||||
return !std::char_traits<char>::compare(m_pointer,
|
return !std::char_traits<char>::compare(data(),
|
||||||
subStringToSearch.data(),
|
subStringToSearch.data(),
|
||||||
subStringToSearch.size());
|
subStringToSearch.size());
|
||||||
|
|
||||||
@@ -168,16 +104,8 @@ public:
|
|||||||
|
|
||||||
constexpr bool startsWith(char characterToSearch) const noexcept
|
constexpr bool startsWith(char characterToSearch) const noexcept
|
||||||
{
|
{
|
||||||
return m_pointer[0] == characterToSearch;
|
return *begin() == characterToSearch;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr char back() const { return m_pointer[m_size - 1]; }
|
|
||||||
|
|
||||||
constexpr char operator[](std::size_t index) { return m_pointer[index]; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
const char *m_pointer = "";
|
|
||||||
size_type m_size = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr bool operator==(SmallStringView first, SmallStringView second) noexcept
|
constexpr bool operator==(SmallStringView first, SmallStringView second) noexcept
|
||||||
|
|||||||
Reference in New Issue
Block a user