refactor(linkparser): take string views (#6715)

Reviewed-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
Nerixyz
2026-01-05 23:53:58 +01:00
committed by GitHub
parent 6110c32916
commit 90373323ec
7 changed files with 20 additions and 14 deletions
+1
View File
@@ -84,6 +84,7 @@
- Dev: Prefer `std::expected` over `nonstd::expected_lite`. (#6693) - Dev: Prefer `std::expected` over `nonstd::expected_lite`. (#6693)
- Dev: Moved Twitch PubSub to liveupdates. (#6638) - Dev: Moved Twitch PubSub to liveupdates. (#6638)
- Dev: Refactored types used for emoji parsing. (#6714) - Dev: Refactored types used for emoji parsing. (#6714)
- Dev: `LinkParser` now parses `QStringView`s. (#6715)
## 2.5.4 ## 2.5.4
+1 -1
View File
@@ -23,7 +23,7 @@ static void BM_LinkParsing(benchmark::State &state)
// Make sure the TLDs are loaded // Make sure the TLDs are loaded
{ {
benchmark::DoNotOptimize(linkparser::parse("xd.com")); benchmark::DoNotOptimize(linkparser::parse(u"xd.com"));
} }
for (auto _ : state) for (auto _ : state)
+1 -1
View File
@@ -170,7 +170,7 @@ Q_ALWAYS_INLINE bool isValidDomainChar(char16_t c)
namespace chatterino::linkparser { namespace chatterino::linkparser {
std::optional<Parsed> parse(const QString &source) noexcept std::optional<Parsed> parse(QStringView source) noexcept
{ {
using SizeType = QString::size_type; using SizeType = QString::size_type;
+13 -5
View File
@@ -75,7 +75,9 @@ struct Parsed {
QStringView link; QStringView link;
/// Checks if the parsed link contains a prefix /// 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(); return this->link.begin() != source.begin();
} }
@@ -89,13 +91,17 @@ struct Parsed {
/// https://www.forsen.tv/commands /// https://www.forsen.tv/commands
/// (empty) /// (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()}; return {source.data(), this->link.begin()};
} }
/// Checks if the parsed link contains a suffix /// 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(); return this->link.end() != source.end();
} }
@@ -109,7 +115,9 @@ struct Parsed {
/// https://www.forsen.tv/commands /// https://www.forsen.tv/commands
/// (empty) /// (empty)
/// ``` /// ```
QStringView suffix(const QString &source) const noexcept ///
/// @param source The exact source string passed to parse()
QStringView suffix(QStringView source) const noexcept
{ {
return { return {
this->link.begin() + this->link.size(), this->link.begin() + this->link.size(),
@@ -125,6 +133,6 @@ struct Parsed {
/// views into @a source. /// views into @a source.
/// ///
/// For the accepted links, see Parsed. /// For the accepted links, see Parsed.
std::optional<Parsed> parse(const QString &source) noexcept; std::optional<Parsed> parse(QStringView source) noexcept;
} // namespace chatterino::linkparser } // namespace chatterino::linkparser
@@ -143,11 +143,8 @@ bool appendWhisperMessageWordsLocally(const QStringList &words)
b.emplace<EmoteElement>(emote, b.emplace<EmoteElement>(emote,
MessageElementFlag::EmojiAll); MessageElementFlag::EmojiAll);
} }
void operator()(QStringView stringView, void operator()(QStringView string, MessageBuilder &b) const
MessageBuilder &b) const
{ {
QString string =
stringView.toString(); // FIXME: use string view
auto link = linkparser::parse(string); auto link = linkparser::parse(string);
if (link) if (link)
{ {
@@ -155,7 +152,7 @@ bool appendWhisperMessageWordsLocally(const QStringList &words)
} }
else else
{ {
b.emplace<TextElement>(string, b.emplace<TextElement>(string.toString(),
MessageElementFlag::Text); MessageElementFlag::Text);
} }
} }
+1 -1
View File
@@ -1009,7 +1009,7 @@ void MessageBuilder::append(std::unique_ptr<MessageElement> element)
} }
void MessageBuilder::addLink(const linkparser::Parsed &parsedLink, void MessageBuilder::addLink(const linkparser::Parsed &parsedLink,
const QString &source) QStringView source)
{ {
QString lowercaseLinkString; QString lowercaseLinkString;
QString origLink = parsedLink.link.toString(); QString origLink = parsedLink.link.toString();
+1 -1
View File
@@ -139,7 +139,7 @@ public:
std::weak_ptr<const Message> weakOf(); std::weak_ptr<const Message> weakOf();
void append(std::unique_ptr<MessageElement> element); void append(std::unique_ptr<MessageElement> element);
void addLink(const linkparser::Parsed &parsedLink, const QString &source); void addLink(const linkparser::Parsed &parsedLink, QStringView source);
template <typename T, typename... Args> template <typename T, typename... Args>
T *emplace(Args &&...args) T *emplace(Args &&...args)