2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2016-02-17 16:05:41 +01:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2020-05-22 19:05:45 +02:00
|
|
|
#include "smallstringfwd.h"
|
2016-02-17 16:05:41 +01:00
|
|
|
#include "smallstringiterator.h"
|
|
|
|
|
|
2017-08-17 15:20:36 +02:00
|
|
|
#include <QString>
|
2016-02-17 16:05:41 +01:00
|
|
|
|
|
|
|
|
#include <cstring>
|
2017-07-05 12:11:02 +02:00
|
|
|
#include <string>
|
2021-03-22 17:59:07 +01:00
|
|
|
#include <string_view>
|
2016-02-17 16:05:41 +01:00
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
|
2018-01-17 11:49:06 +01:00
|
|
|
template <typename String>
|
|
|
|
|
using enable_if_has_char_data_pointer = typename std::enable_if_t<
|
|
|
|
|
std::is_same<
|
|
|
|
|
std::remove_const_t<
|
|
|
|
|
std::remove_pointer_t<
|
2018-05-31 11:31:59 +02:00
|
|
|
decltype(std::declval<const String>().data())
|
2018-01-17 11:49:06 +01:00
|
|
|
>
|
|
|
|
|
>, char>::value
|
|
|
|
|
, int>;
|
|
|
|
|
|
2021-03-22 17:59:07 +01:00
|
|
|
class SmallStringView : public std::string_view
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2021-03-22 17:59:07 +01:00
|
|
|
using std::string_view::string_view;
|
2021-02-18 17:36:36 +01:00
|
|
|
|
|
|
|
|
constexpr SmallStringView(const_iterator begin, const_iterator end) noexcept
|
2021-03-22 17:59:07 +01:00
|
|
|
: std::string_view{std::addressof(*begin), static_cast<std::size_t>(std::distance(begin, end))}
|
2021-02-18 17:36:36 +01:00
|
|
|
{}
|
2016-02-17 16:05:41 +01:00
|
|
|
|
2021-03-25 11:52:22 +01:00
|
|
|
#ifdef Q_CC_MSVC
|
2021-03-22 17:59:07 +01:00
|
|
|
constexpr SmallStringView(const char *const begin, const char *const end) noexcept
|
|
|
|
|
: std::string_view{begin, static_cast<std::size_t>(std::distance(begin, end))}
|
2021-02-18 17:36:36 +01:00
|
|
|
{}
|
2021-03-22 17:59:07 +01:00
|
|
|
#endif
|
2018-03-26 17:10:17 +02:00
|
|
|
|
2020-05-22 19:05:45 +02:00
|
|
|
template<typename String, typename Utils::enable_if_has_char_data_pointer<String> = 0>
|
|
|
|
|
constexpr SmallStringView(const String &string) noexcept
|
2021-03-22 17:59:07 +01:00
|
|
|
: std::string_view{string.data(), static_cast<std::size_t>(string.size())}
|
2019-01-31 10:30:58 +01:00
|
|
|
{}
|
2017-08-29 12:54:10 +02:00
|
|
|
|
2020-07-31 11:00:19 +02:00
|
|
|
static constexpr SmallStringView fromUtf8(const char *const characterPointer)
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
2020-05-22 19:05:45 +02:00
|
|
|
return SmallStringView(characterPointer);
|
2016-02-17 16:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-22 17:59:07 +01:00
|
|
|
constexpr size_type isEmpty() const noexcept { return empty(); }
|
2017-09-21 11:34:41 +02:00
|
|
|
|
2017-11-16 17:48:53 +01:00
|
|
|
constexpr
|
|
|
|
|
SmallStringView mid(size_type position) const noexcept
|
|
|
|
|
{
|
|
|
|
|
return SmallStringView(data() + position, size() - position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr
|
|
|
|
|
SmallStringView mid(size_type position, size_type length) const noexcept
|
|
|
|
|
{
|
|
|
|
|
return SmallStringView(data() + position, length);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 19:05:45 +02:00
|
|
|
constexpr20 operator std::string() const { return std::string(data(), size()); }
|
2017-07-03 11:12:00 +02:00
|
|
|
|
2017-08-17 15:20:36 +02:00
|
|
|
explicit operator QString() const
|
|
|
|
|
{
|
|
|
|
|
return QString::fromUtf8(data(), int(size()));
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-25 20:16:58 +02:00
|
|
|
explicit operator QByteArray() const
|
|
|
|
|
{
|
|
|
|
|
return QByteArray(data(), int(size()));
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 11:00:19 +02:00
|
|
|
constexpr bool startsWith(SmallStringView subStringToSearch) const noexcept
|
2017-07-31 17:27:46 +02:00
|
|
|
{
|
|
|
|
|
if (size() >= subStringToSearch.size())
|
2021-03-22 17:59:07 +01:00
|
|
|
return !std::char_traits<char>::compare(data(),
|
2020-05-22 19:05:45 +02:00
|
|
|
subStringToSearch.data(),
|
|
|
|
|
subStringToSearch.size());
|
2017-07-31 17:27:46 +02:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 19:05:45 +02:00
|
|
|
constexpr bool startsWith(char characterToSearch) const noexcept
|
2017-07-31 17:27:46 +02:00
|
|
|
{
|
2021-03-22 17:59:07 +01:00
|
|
|
return *begin() == characterToSearch;
|
2017-07-31 17:27:46 +02:00
|
|
|
}
|
2022-07-05 14:45:16 +02:00
|
|
|
|
|
|
|
|
constexpr bool endsWith(SmallStringView ending) const noexcept
|
|
|
|
|
{
|
|
|
|
|
return size() >= ending.size() && std::equal(ending.rbegin(), ending.rend(), rbegin());
|
|
|
|
|
}
|
2016-02-17 16:05:41 +01:00
|
|
|
};
|
|
|
|
|
|
2022-07-13 18:13:29 +02:00
|
|
|
constexpr bool operator!=(SmallStringView first, SmallStringView second) noexcept
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
2022-07-13 18:13:29 +02:00
|
|
|
return std::string_view{first} != std::string_view{second};
|
2016-02-17 16:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-13 18:13:29 +02:00
|
|
|
constexpr bool operator==(SmallStringView first, SmallStringView second) noexcept
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
2022-07-13 18:13:29 +02:00
|
|
|
return std::string_view{first} == std::string_view{second};
|
2016-02-17 16:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-13 18:13:29 +02:00
|
|
|
constexpr bool operator<(SmallStringView first, SmallStringView second) noexcept
|
2017-08-21 10:21:47 +02:00
|
|
|
{
|
2022-07-13 18:13:29 +02:00
|
|
|
return std::string_view{first} < std::string_view{second};
|
|
|
|
|
}
|
2017-08-21 10:21:47 +02:00
|
|
|
|
2022-07-13 18:13:29 +02:00
|
|
|
constexpr bool operator>(SmallStringView first, SmallStringView second) noexcept
|
|
|
|
|
{
|
|
|
|
|
return std::string_view{first} > std::string_view{second};
|
|
|
|
|
}
|
2017-08-21 10:21:47 +02:00
|
|
|
|
2022-07-13 18:13:29 +02:00
|
|
|
constexpr bool operator<=(SmallStringView first, SmallStringView second) noexcept
|
|
|
|
|
{
|
|
|
|
|
return std::string_view{first} <= std::string_view{second};
|
2017-08-21 10:21:47 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-13 18:13:29 +02:00
|
|
|
constexpr bool operator>=(SmallStringView first, SmallStringView second) noexcept
|
2017-08-29 12:54:10 +02:00
|
|
|
{
|
2022-07-13 18:13:29 +02:00
|
|
|
return std::string_view{first} >= std::string_view{second};
|
2017-08-29 12:54:10 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-13 18:13:29 +02:00
|
|
|
constexpr int compare(SmallStringView first, SmallStringView second) noexcept
|
2017-08-29 12:54:10 +02:00
|
|
|
{
|
2022-07-13 18:13:29 +02:00
|
|
|
return first.compare(second);
|
2017-08-29 12:54:10 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 16:05:41 +01:00
|
|
|
} // namespace Utils
|
2017-08-17 15:19:46 +02:00
|
|
|
|
|
|
|
|
constexpr Utils::SmallStringView operator""_sv(const char *const string, size_t size)
|
|
|
|
|
{
|
|
|
|
|
return Utils::SmallStringView(string, size);
|
|
|
|
|
}
|