Commit 8640ef19 authored by Marco Bubke's avatar Marco Bubke
Browse files

Utils: Convert to SmallStringView for comparison



Change-Id: I38d9716225b81091e8e75b26c9c2258a2aefa987
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 7c0331ab
...@@ -34,9 +34,9 @@ namespace Sqlite { ...@@ -34,9 +34,9 @@ namespace Sqlite {
class SQLITE_EXPORT SqliteException class SQLITE_EXPORT SqliteException
{ {
public: public:
SqliteException(Utils::SmallString &&whatErrorHasHappen, SqliteException(const char *whatErrorHasHappen,
Utils::SmallString &&sqliteErrorMessage = Utils::SmallString()) Utils::SmallString &&sqliteErrorMessage = Utils::SmallString())
: m_whatErrorHasHappen(std::move(whatErrorHasHappen)), : m_whatErrorHasHappen(whatErrorHasHappen),
m_sqliteErrorMessage(std::move(sqliteErrorMessage)) m_sqliteErrorMessage(std::move(sqliteErrorMessage))
{ {
} }
...@@ -44,7 +44,7 @@ public: ...@@ -44,7 +44,7 @@ public:
void printWarning() const; void printWarning() const;
private: private:
Utils::SmallString m_whatErrorHasHappen; const char *m_whatErrorHasHappen;
Utils::SmallString m_sqliteErrorMessage; Utils::SmallString m_sqliteErrorMessage;
}; };
......
...@@ -242,11 +242,13 @@ public: ...@@ -242,11 +242,13 @@ public:
return SmallStringView(data(), size()); return SmallStringView(data(), size());
} }
explicit
operator QString() const operator QString() const
{ {
return toQString(); return toQString();
} }
explicit
operator std::string() const operator std::string() const
{ {
return std::string(data(), size()); return std::string(data(), size());
...@@ -589,100 +591,6 @@ public: ...@@ -589,100 +591,6 @@ public:
return *(data() + index); return *(data() + index);
} }
template<size_type ArraySize>
friend bool operator==(const BasicSmallString& first, const char(&second)[ArraySize]) noexcept
{
return first == SmallStringView(second);
}
template<size_type ArraySize>
friend bool operator==(const char(&first)[ArraySize], const BasicSmallString& second) noexcept
{
return second == first;
}
template<typename Type,
typename = std::enable_if_t<std::is_pointer<Type>::value>
>
friend bool operator==(const BasicSmallString& first, Type second) noexcept
{
return first == SmallStringView(second);
}
template<typename Type,
typename = std::enable_if_t<std::is_pointer<Type>::value>
>
friend bool operator==(Type first, const BasicSmallString& second) noexcept
{
return second == first;
}
friend bool operator==(const BasicSmallString& first, const SmallStringView& second) noexcept
{
return first.size() == second.size()
&& std::memcmp(first.data(), second.data(), first.size()) == 0;
}
friend bool operator==(const SmallStringView& first, const BasicSmallString& second) noexcept
{
return second == first;
}
friend bool operator!=(const BasicSmallString& first, const SmallStringView& second) noexcept
{
return !(first == second);
}
friend bool operator!=(const SmallStringView& first, const BasicSmallString& second) noexcept
{
return second == first;
}
friend bool operator!=(const BasicSmallString& first, const BasicSmallString& second) noexcept
{
return !(first == second);
}
template<size_type ArraySize>
friend bool operator!=(const BasicSmallString& first, const char(&second)[ArraySize]) noexcept
{
return !(first == second);
}
template<size_type ArraySize>
friend bool operator!=(const char(&first)[ArraySize], const BasicSmallString& second) noexcept
{
return second != first;
}
template<typename Type,
typename = std::enable_if_t<std::is_pointer<Type>::value>
>
friend bool operator!=(const BasicSmallString& first, Type second) noexcept
{
return !(first == second);
}
template<typename Type,
typename = std::enable_if_t<std::is_pointer<Type>::value>
>
friend bool operator!=(Type first, const BasicSmallString& second) noexcept
{
return second != first;
}
friend bool operator<(const BasicSmallString& first, SmallStringView second) noexcept
{
return first.size() < second.size()
|| (first.size() == second.size() && std::memcmp(first.data(), second.data(), first.size()) < 0);
}
friend bool operator<(SmallStringView first, const BasicSmallString& second) noexcept
{
return first.size() < second.size()
|| (first.size() == second.size() && std::memcmp(first.data(), second.data(), first.size()) < 0);
}
friend BasicSmallString operator+(const BasicSmallString &first, const BasicSmallString &second) friend BasicSmallString operator+(const BasicSmallString &first, const BasicSmallString &second)
{ {
BasicSmallString text; BasicSmallString text;
...@@ -994,29 +902,6 @@ template<template<uint> class String, uint Size> ...@@ -994,29 +902,6 @@ template<template<uint> class String, uint Size>
using isSameString = std::is_same<std::remove_reference_t<std::remove_cv_t<String<Size>>>, using isSameString = std::is_same<std::remove_reference_t<std::remove_cv_t<String<Size>>>,
BasicSmallString<Size>>; BasicSmallString<Size>>;
template<template<uint> class String,
uint SizeOne,
uint SizeTwo,
typename = std::enable_if_t<isSameString<String, SizeOne>::value
|| isSameString<String, SizeTwo>::value>>
bool operator==(const String<SizeOne> &first, const String<SizeTwo> &second) noexcept
{
return first.size() == second.size()
&& std::memcmp(first.data(), second.data(), first.size()) == 0;
}
template<template<uint> class String,
uint SizeOne,
uint SizeTwo,
typename = std::enable_if_t<isSameString<String, SizeOne>::value
|| isSameString<String, SizeTwo>::value>>
bool operator<(const String<SizeOne> &first, const String<SizeTwo> &second) noexcept
{
return first.size() < second.size()
|| (first.size() == second.size() && std::memcmp(first.data(), second.data(), first.size()) < 0);
}
template<typename Key, template<typename Key,
typename Value, typename Value,
typename Hash = std::hash<Key>, typename Hash = std::hash<Key>,
......
...@@ -29,9 +29,15 @@ namespace Utils { ...@@ -29,9 +29,15 @@ namespace Utils {
using uint = unsigned int; using uint = unsigned int;
class SmallStringView;
template <uint Size> template <uint Size>
class BasicSmallString; class BasicSmallString;
using SmallString = BasicSmallString<31>; using SmallString = BasicSmallString<31>;
using PathString = BasicSmallString<190>; using PathString = BasicSmallString<190>;
inline
int compare(SmallStringView first, SmallStringView second) noexcept;
inline
int reverseCompare(SmallStringView first, SmallStringView second) noexcept;
} // namespace Utils } // namespace Utils
...@@ -41,6 +41,7 @@ struct SmallStringIterator : public std::iterator<Category, Type, DistanceType, ...@@ -41,6 +41,7 @@ struct SmallStringIterator : public std::iterator<Category, Type, DistanceType,
{ {
SmallStringIterator() noexcept = default; SmallStringIterator() noexcept = default;
constexpr
SmallStringIterator(Pointer ptr) noexcept : pointer_(ptr) SmallStringIterator(Pointer ptr) noexcept : pointer_(ptr)
{ {
} }
......
...@@ -56,12 +56,12 @@ public: ...@@ -56,12 +56,12 @@ public:
{ {
} }
const char *data() const const char *data() const noexcept
{ {
return Q_LIKELY(isShortString()) ? m_data.shortString.string : m_data.allocated.data.pointer; return Q_LIKELY(isShortString()) ? m_data.shortString.string : m_data.allocated.data.pointer;
} }
size_type size() const size_type size() const noexcept
{ {
return Q_LIKELY(isShortString()) ? m_data.shortString.shortStringSize : m_data.allocated.data.size; return Q_LIKELY(isShortString()) ? m_data.shortString.shortStringSize : m_data.allocated.data.size;
} }
......
...@@ -138,7 +138,8 @@ public: ...@@ -138,7 +138,8 @@ public:
QStringList qStringList; QStringList qStringList;
qStringList.reserve(int(Base::size())); qStringList.reserve(int(Base::size()));
std::copy(Base::begin(), Base::end(), std::back_inserter(qStringList)); for (const auto &entry : *this)
qStringList.push_back(QString(entry));
return qStringList; return qStringList;
} }
......
...@@ -67,6 +67,12 @@ public: ...@@ -67,6 +67,12 @@ public:
{ {
} }
SmallStringView(const std::string &string) noexcept
: m_pointer(string.data()),
m_size(string.size())
{
}
static static
SmallStringView fromUtf8(const char *const characterPointer) SmallStringView fromUtf8(const char *const characterPointer)
{ {
...@@ -74,19 +80,19 @@ public: ...@@ -74,19 +80,19 @@ public:
} }
constexpr constexpr
const char *data() const const char *data() const noexcept
{ {
return m_pointer; return m_pointer;
} }
constexpr constexpr
size_type size() const size_type size() const noexcept
{ {
return m_size; return m_size;
} }
constexpr constexpr
size_type isEmpty() const size_type isEmpty() const noexcept
{ {
return m_size == 0; return m_size == 0;
} }
...@@ -140,19 +146,19 @@ private: ...@@ -140,19 +146,19 @@ private:
}; };
inline inline
bool operator==(const SmallStringView& first, const SmallStringView& second) noexcept bool operator==(SmallStringView first, SmallStringView second) noexcept
{ {
return first.size() == second.size() && std::memcmp(first.data(), second.data(), first.size()) == 0; return first.size() == second.size() && std::memcmp(first.data(), second.data(), first.size()) == 0;
} }
inline inline
bool operator!=(const SmallStringView& first, const SmallStringView& second) noexcept bool operator!=(SmallStringView first, SmallStringView second) noexcept
{ {
return !(first == second); return !(first == second);
} }
inline inline
int compare(const SmallStringView& first, const SmallStringView& second) noexcept int compare(SmallStringView first, SmallStringView second) noexcept
{ {
int sizeDifference = int(first.size() - second.size()); int sizeDifference = int(first.size() - second.size());
...@@ -162,6 +168,18 @@ int compare(const SmallStringView& first, const SmallStringView& second) noexcep ...@@ -162,6 +168,18 @@ int compare(const SmallStringView& first, const SmallStringView& second) noexcep
return sizeDifference; return sizeDifference;
} }
inline
bool operator<(SmallStringView first, SmallStringView second) noexcept
{
return compare(first, second) < 0;
}
inline
bool operator>(SmallStringView first, SmallStringView second) noexcept
{
return second < first;
}
namespace Internal { namespace Internal {
inline inline
int reverse_memcmp(const char *first, const char *second, size_t n) int reverse_memcmp(const char *first, const char *second, size_t n)
...@@ -187,7 +205,7 @@ int reverse_memcmp(const char *first, const char *second, size_t n) ...@@ -187,7 +205,7 @@ int reverse_memcmp(const char *first, const char *second, size_t n)
} }
inline inline
int reverseCompare(const SmallStringView& first, const SmallStringView& second) noexcept int reverseCompare(SmallStringView first, SmallStringView second) noexcept
{ {
int sizeDifference = int(first.size() - second.size()); int sizeDifference = int(first.size() - second.size());
......
...@@ -43,7 +43,7 @@ void PchManagerClient::alive() ...@@ -43,7 +43,7 @@ void PchManagerClient::alive()
void PchManagerClient::precompiledHeadersUpdated(ClangBackEnd::PrecompiledHeadersUpdatedMessage &&message) void PchManagerClient::precompiledHeadersUpdated(ClangBackEnd::PrecompiledHeadersUpdatedMessage &&message)
{ {
for (const ClangBackEnd::ProjectPartPch &projectPartPch : message.projectPartPchs()) for (const ClangBackEnd::ProjectPartPch &projectPartPch : message.projectPartPchs())
precompiledHeaderUpdated(projectPartPch.id(), projectPartPch.path()); precompiledHeaderUpdated(QString(projectPartPch.id()), QString(projectPartPch.path()));
} }
void PchManagerClient::precompiledHeaderRemoved(const QString &projectPartId) void PchManagerClient::precompiledHeaderRemoved(const QString &projectPartId)
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
#pragma once #pragma once
#include <clangpchmanager_global.h> #include "clangpchmanager_global.h"
#include <filecontainerv2.h> #include <filecontainerv2.h>
......
...@@ -52,9 +52,9 @@ void ClangQueryHoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorW ...@@ -52,9 +52,9 @@ void ClangQueryHoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorW
Contexts contexts = m_highligher->contextsForLineAndColumn(uint(line), uint(column)); Contexts contexts = m_highligher->contextsForLineAndColumn(uint(line), uint(column));
if (!messages.empty()) if (!messages.empty())
setToolTip(QString("%1: %2").arg(messages[0].errorTypeText()).arg(messages[0].arguments().join(", "))); setToolTip(QString("%1: %2").arg(QString(messages[0].errorTypeText())).arg(QString(messages[0].arguments().join(", "))));
else if (!contexts.empty()) else if (!contexts.empty())
setToolTip(QString("%1: %2").arg(contexts[0].contextTypeText()).arg(contexts[0].arguments().join(", "))); setToolTip(QString("%1: %2").arg(QString(contexts[0].contextTypeText())).arg(QString(contexts[0].arguments().join(", "))));
} }
} // namespace ClangRefactoring } // namespace ClangRefactoring
...@@ -154,7 +154,7 @@ void RefactoringClient::addSearchResult(const ClangBackEnd::SourceRangeWithTextC ...@@ -154,7 +154,7 @@ void RefactoringClient::addSearchResult(const ClangBackEnd::SourceRangeWithTextC
std::unordered_map<uint, QString> &filePaths) std::unordered_map<uint, QString> &filePaths)
{ {
m_searchHandle->addResult(filePaths[sourceRangeWithText.fileHash()], m_searchHandle->addResult(filePaths[sourceRangeWithText.fileHash()],
sourceRangeWithText.text(), QString(sourceRangeWithText.text()),
{{int(sourceRangeWithText.start().line()), {{int(sourceRangeWithText.start().line()),
int(sourceRangeWithText.start().column() - 1), int(sourceRangeWithText.start().column() - 1),
int(sourceRangeWithText.start().offset())}, int(sourceRangeWithText.start().offset())},
......
...@@ -66,24 +66,24 @@ template <typename Source, ...@@ -66,24 +66,24 @@ template <typename Source,
typename Target> typename Target>
void append(Target &target, const Source &source) void append(Target &target, const Source &source)
{ {
using ValueType = typename Target::value_type;
Source clonedSource = source.clone(); Source clonedSource = source.clone();
target.reserve(target.size() + source.size()); target.reserve(target.size() + source.size());
std::move(clonedSource.begin(), for(auto &&entry : clonedSource)
clonedSource.end(), target.push_back(ValueType(std::move(entry)));
std::back_inserter(target));
} }
template <typename Source, template <typename Source,
typename Target> typename Target>
void append(Target &target, Source &source) void append(Target &target, Source &source)
{ {
using ValueType = typename Target::value_type;
target.reserve(target.size() + source.size()); target.reserve(target.size() + source.size());
std::move(source.begin(), for(auto &&entry : source)
source.end(), target.push_back(ValueType(entry));
std::back_inserter(target));
} }
template <typename GetterFunction> template <typename GetterFunction>
...@@ -469,14 +469,14 @@ std::vector<uint> PchCreator::generateProjectPartPchIncludes( ...@@ -469,14 +469,14 @@ std::vector<uint> PchCreator::generateProjectPartPchIncludes(
auto jointFile = generateFileWithContent(jointedFilePath, jointedFileContent); auto jointFile = generateFileWithContent(jointedFilePath, jointedFileContent);
Utils::SmallStringVector arguments = generateProjectPartCommandLine(projectPart); Utils::SmallStringVector arguments = generateProjectPartCommandLine(projectPart);
arguments.push_back(jointedFilePath); arguments.push_back(jointedFilePath);
FilePath filePath(jointedFilePath.clone()); FilePath filePath{Utils::PathString(jointedFilePath)};
IncludeCollector collector(m_filePathCache); IncludeCollector collector(m_filePathCache);
collector.setExcludedIncludes(generateProjectPartHeaderAndSourcePaths(projectPart)); collector.setExcludedIncludes(generateProjectPartHeaderAndSourcePaths(projectPart));
collector.addFile(filePath.directory(), collector.addFile(std::string(filePath.directory()),
filePath.name(), std::string(filePath.name()),
{}, {},
arguments); arguments);
...@@ -581,11 +581,11 @@ std::unique_ptr<QFile> PchCreator::generateFileWithContent( ...@@ -581,11 +581,11 @@ std::unique_ptr<QFile> PchCreator::generateFileWithContent(
const Utils::SmallString &filePath, const Utils::SmallString &filePath,
const Utils::SmallString &content) const Utils::SmallString &content)
{ {
std::unique_ptr<QFile> precompiledIncludeFile(new QFile(filePath)); std::unique_ptr<QFile> precompiledIncludeFile(new QFile(QString(filePath)));
precompiledIncludeFile->open(QIODevice::WriteOnly); precompiledIncludeFile->open(QIODevice::WriteOnly);
precompiledIncludeFile->write(content.data(), content.size()); precompiledIncludeFile->write(content.data(), qint64(content.size()));
precompiledIncludeFile->close(); precompiledIncludeFile->close();
return precompiledIncludeFile; return precompiledIncludeFile;
......
...@@ -70,8 +70,8 @@ unittest_public: ...@@ -70,8 +70,8 @@ unittest_public:
Process *processPointer = process.get(); Process *processPointer = process.get();
process->setProcessChannelMode(QProcess::ForwardedChannels); process->setProcessChannelMode(QProcess::ForwardedChannels);
process->setArguments(compilerArguments); process->setArguments(QStringList(compilerArguments));
process->setProgram(m_environment.clangCompilerPath()); process->setProgram(QString(m_environment.clangCompilerPath()));
connectProcess(processPointer, std::move(projectPartPch)); connectProcess(processPointer, std::move(projectPartPch));
......
...@@ -50,10 +50,10 @@ ClangQueryGatherer::createSourceRangesForSource( ...@@ -50,10 +50,10 @@ ClangQueryGatherer::createSourceRangesForSource(
{ {
ClangQuery clangQuery(*filePathCache, std::move(query)); ClangQuery clangQuery(*filePathCache, std::move(query));
clangQuery.addFile(source.filePath().directory(), clangQuery.addFile(std::string(source.filePath().directory()),
source.filePath().name(), std::string(source.filePath().name()),
source.takeUnsavedFileContent(), std::string(source.takeUnsavedFileContent()),
source.takeCommandLineArguments()); std::vector<std::string>(source.takeCommandLineArguments()));
clangQuery.addUnsavedFiles(unsaved); clangQuery.addUnsavedFiles(unsaved);
......
...@@ -68,7 +68,7 @@ void ClangTool::addFiles(const Container &filePaths, ...@@ -68,7 +68,7 @@ void ClangTool::addFiles(const Container &filePaths,
auto fileNameBegin = found.base(); auto fileNameBegin = found.base();