2016-02-17 16:05:41 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
|
2017-08-21 10:21:47 +02:00
|
|
|
namespace Internal {
|
2020-05-22 19:05:45 +02:00
|
|
|
constexpr int reverse_memcmp(const char *first, const char *second, size_t n)
|
2017-08-21 10:21:47 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
const char *currentFirst = first + n - 1;
|
|
|
|
|
const char *currentSecond = second + n - 1;
|
2017-08-21 10:21:47 +02:00
|
|
|
|
2020-05-22 19:05:45 +02:00
|
|
|
while (n > 0) {
|
2017-08-21 10:21:47 +02:00
|
|
|
// If the current characters differ, return an appropriately signed
|
|
|
|
|
// value; otherwise, keep searching backwards
|
2017-09-21 11:43:59 +02:00
|
|
|
int difference = *currentFirst - *currentSecond;
|
|
|
|
|
if (difference != 0)
|
|
|
|
|
return difference;
|
2017-08-21 10:21:47 +02:00
|
|
|
|
|
|
|
|
--currentFirst;
|
|
|
|
|
--currentSecond;
|
|
|
|
|
--n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-05-22 19:05:45 +02:00
|
|
|
} // namespace Internal
|
2017-08-21 10:21:47 +02:00
|
|
|
|
2020-05-22 19:05:45 +02:00
|
|
|
constexpr int reverseCompare(SmallStringView first, SmallStringView second) noexcept
|
2017-08-21 10:21:47 +02:00
|
|
|
{
|
2022-07-13 18:13:29 +02:00
|
|
|
int difference = Internal::reverse_memcmp(first.data(), second.data(), first.size());
|
2017-08-21 10:21:47 +02:00
|
|
|
|
2022-07-13 18:13:29 +02:00
|
|
|
if (difference == 0)
|
|
|
|
|
return int(first.size()) - int(second.size());
|
2017-08-21 10:21:47 +02:00
|
|
|
|
2022-07-13 18:13:29 +02:00
|
|
|
return difference;
|
2017-08-21 10:21:47 +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);
|
|
|
|
|
}
|