Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Redundant Parsing of Links #4507

Merged
merged 4 commits into from
Apr 22, 2023

Conversation

Nerixyz
Copy link
Contributor

@Nerixyz Nerixyz commented Apr 3, 2023

Pull request checklist:

  • CHANGELOG.md was updated, if applicable

Description

This is a followup to #4436. While refactoring the LinkParser, I noticed that when matching/adding links, they're essentially parsed three times - first by the link-parser, then with up to three regexes and lastly by another regex.

All the regex-parsing can be eliminated:

  • The ftp and spotify regexes aren't needed, because these types of links will never be parsed by the LinkParser.
  • The https? regex can be removed, because the LinkParser knows whether the link contains a protocol or not.
  • The last regex can be removed, because the LinkParser knows where the host part of the link is (sidenote: the current regex is even wrong, if you have a link such as chatterino.com#FOO, then it will display as chatterino.com#foo).

I updated the tests to match the new format and added some tests that check the case-insensitivity of the parser. This doesn't change the parsing inside LinkParser yet.

@Nerixyz Nerixyz changed the title Improve Link Handling Remove Redundant Parsing of Links Apr 3, 2023
@pajlada pajlada enabled auto-merge (squash) April 22, 2023 22:17
@pajlada pajlada merged commit 95e7426 into Chatterino:master Apr 22, 2023
15 checks passed
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

@@ -67,7 +67,7 @@ namespace {

LinkParser::LinkParser(const QString &unparsedString)
{
this->match_ = unparsedString;
ParsedLink result;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'result' of type 'chatterino::ParsedLink' can be declared 'const' [misc-const-correctness]

Suggested change
ParsedLink result;
ParsedLink const result;

@@ -92,8 +94,10 @@ LinkParser::LinkParser(const QString &unparsedString)

// Host `a.b.c.com`
QStringRef host = l;
ParsedLink::StringView rest;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'rest' is not initialized [cppcoreguidelines-init-variables]

Suggested change
ParsedLink::StringView rest;
ParsedLink::StringView rest = 0;

bool lastWasDot = true;
bool inIpv6 = false;
bool hasMatch = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'hasMatch' of type 'bool' can be declared 'const' [misc-const-correctness]

Suggested change
bool hasMatch = false;
bool const hasMatch = false;

@@ -2,19 +2,31 @@

#include <QString>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'QString' file not found [clang-diagnostic-error]

#include <QString>
         ^

namespace chatterino {

struct ParsedLink {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: constructor does not initialize these fields: protocol, host, rest, source [cppcoreguidelines-pro-type-member-init]

src/common/LinkParser.hpp:14:

-     StringView protocol;
-     StringView host;
-     StringView rest;
-     QString source;
+     StringView protocol{};
+     StringView host{};
+     StringView rest{};
+     QString source{};

@Nerixyz Nerixyz mentioned this pull request Apr 23, 2023
@Nerixyz Nerixyz deleted the refactor/link-handling branch October 6, 2023 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants