diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a0fd211..ee947a5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -84,6 +84,7 @@ - Dev: Prefer `std::expected` over `nonstd::expected_lite`. (#6693) - Dev: Moved Twitch PubSub to liveupdates. (#6638) - Dev: Refactored types used for emoji parsing. (#6714) +- Dev: `LinkParser` now parses `QStringView`s. (#6715) ## 2.5.4 diff --git a/benchmarks/src/LinkParser.cpp b/benchmarks/src/LinkParser.cpp index b38e15de..7724fba3 100644 --- a/benchmarks/src/LinkParser.cpp +++ b/benchmarks/src/LinkParser.cpp @@ -23,7 +23,7 @@ static void BM_LinkParsing(benchmark::State &state) // Make sure the TLDs are loaded { - benchmark::DoNotOptimize(linkparser::parse("xd.com")); + benchmark::DoNotOptimize(linkparser::parse(u"xd.com")); } for (auto _ : state) diff --git a/src/common/LinkParser.cpp b/src/common/LinkParser.cpp index 6e88fd17..9ee74f22 100644 --- a/src/common/LinkParser.cpp +++ b/src/common/LinkParser.cpp @@ -170,7 +170,7 @@ Q_ALWAYS_INLINE bool isValidDomainChar(char16_t c) namespace chatterino::linkparser { -std::optional parse(const QString &source) noexcept +std::optional parse(QStringView source) noexcept { using SizeType = QString::size_type; diff --git a/src/common/LinkParser.hpp b/src/common/LinkParser.hpp index 151fb083..401ffe52 100644 --- a/src/common/LinkParser.hpp +++ b/src/common/LinkParser.hpp @@ -75,7 +75,9 @@ struct Parsed { QStringView link; /// Checks if the parsed link contains a prefix - bool hasPrefix(const QString &source) const noexcept + /// + /// @param source The exact source string passed to parse() + bool hasPrefix(QStringView source) const noexcept { return this->link.begin() != source.begin(); } @@ -89,13 +91,17 @@ struct Parsed { /// https://www.forsen.tv/commands /// (empty) /// ``` - QStringView prefix(const QString &source) const noexcept + /// + /// @param source The exact source string passed to parse() + QStringView prefix(QStringView source) const noexcept { return {source.data(), this->link.begin()}; } /// Checks if the parsed link contains a suffix - bool hasSuffix(const QString &source) const noexcept + /// + /// @param source The exact source string passed to parse() + bool hasSuffix(QStringView source) const noexcept { return this->link.end() != source.end(); } @@ -109,7 +115,9 @@ struct Parsed { /// https://www.forsen.tv/commands /// (empty) /// ``` - QStringView suffix(const QString &source) const noexcept + /// + /// @param source The exact source string passed to parse() + QStringView suffix(QStringView source) const noexcept { return { this->link.begin() + this->link.size(), @@ -125,6 +133,6 @@ struct Parsed { /// views into @a source. /// /// For the accepted links, see Parsed. -std::optional parse(const QString &source) noexcept; +std::optional parse(QStringView source) noexcept; } // namespace chatterino::linkparser diff --git a/src/controllers/commands/builtin/twitch/SendWhisper.cpp b/src/controllers/commands/builtin/twitch/SendWhisper.cpp index 0c43fbd8..d6933567 100644 --- a/src/controllers/commands/builtin/twitch/SendWhisper.cpp +++ b/src/controllers/commands/builtin/twitch/SendWhisper.cpp @@ -143,11 +143,8 @@ bool appendWhisperMessageWordsLocally(const QStringList &words) b.emplace(emote, MessageElementFlag::EmojiAll); } - void operator()(QStringView stringView, - MessageBuilder &b) const + void operator()(QStringView string, MessageBuilder &b) const { - QString string = - stringView.toString(); // FIXME: use string view auto link = linkparser::parse(string); if (link) { @@ -155,7 +152,7 @@ bool appendWhisperMessageWordsLocally(const QStringList &words) } else { - b.emplace(string, + b.emplace(string.toString(), MessageElementFlag::Text); } } diff --git a/src/messages/MessageBuilder.cpp b/src/messages/MessageBuilder.cpp index da11e52a..52ae3dc8 100644 --- a/src/messages/MessageBuilder.cpp +++ b/src/messages/MessageBuilder.cpp @@ -1009,7 +1009,7 @@ void MessageBuilder::append(std::unique_ptr element) } void MessageBuilder::addLink(const linkparser::Parsed &parsedLink, - const QString &source) + QStringView source) { QString lowercaseLinkString; QString origLink = parsedLink.link.toString(); diff --git a/src/messages/MessageBuilder.hpp b/src/messages/MessageBuilder.hpp index 76345839..b5196888 100644 --- a/src/messages/MessageBuilder.hpp +++ b/src/messages/MessageBuilder.hpp @@ -139,7 +139,7 @@ public: std::weak_ptr weakOf(); void append(std::unique_ptr element); - void addLink(const linkparser::Parsed &parsedLink, const QString &source); + void addLink(const linkparser::Parsed &parsedLink, QStringView source); template T *emplace(Args &&...args)