Skip to content

selectCaptureGroup will work with named capture groups#5131

Merged
Delwing merged 7 commits intoMudlet:developmentfrom
Delwing:select-capture-group-with-named
Jul 31, 2021
Merged

selectCaptureGroup will work with named capture groups#5131
Delwing merged 7 commits intoMudlet:developmentfrom
Delwing:select-capture-group-with-named

Conversation

@Delwing
Copy link
Copy Markdown
Contributor

@Delwing Delwing commented Apr 10, 2021

Brief overview of PR changes/additions

Make it possible to use selectCaptureGroup with name parameter

Motivation for adding to Mudlet

Consistency.

Other info (issues closed, discussion etc)

closes #5107

Release post highlight

@Delwing Delwing requested a review from a team as a code owner April 10, 2021 12:08
@Delwing Delwing requested review from a team April 10, 2021 12:08
@add-deployment-links
Copy link
Copy Markdown

add-deployment-links bot commented Apr 10, 2021

Hey there! Thanks for helping Mudlet improve. 🌟

Test versions

You can directly test the changes here:

No need to install anything - just unzip and run.
Let us know if it works well, and if it doesn't, please give details.

@Delwing Delwing changed the title add support for selecting capture groups by their name selectCaptureGroup will work with named capture groups Apr 10, 2021
Copy link
Copy Markdown
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

src/TTrigger.cpp Outdated
int n = (tabptr[0] << 8) | tabptr[1];
auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed();
auto capture = QString::fromUtf8(subject + ovector[2*n], ovector[2*n+1] - ovector[2*n]);
auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed().remove(QStringLiteral("\u0000"));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]

auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed().remove(QStringLiteral("\u0000"));
                                      ^

src/TTrigger.cpp Outdated
auto capture = QString::fromUtf8(subject + ovector[2*n], ovector[2*n+1] - ovector[2*n]);
auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed().remove(QStringLiteral("\u0000"));
qDebug() << name;
char* substring_start = subject + ovector[2*n];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]

char* substring_start = subject + ovector[2*n];
                                ^

src/TTrigger.cpp Outdated
auto capture = QString::fromUtf8(subject + ovector[2*n], ovector[2*n+1] - ovector[2*n]);
auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed().remove(QStringLiteral("\u0000"));
qDebug() << name;
char* substring_start = subject + ovector[2*n];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: do not use array subscript when the index is not an integer constant expression; use gsl::at() instead [cppcoreguidelines-pro-bounds-constant-array-index]

char* substring_start = subject + ovector[2*n];
                                  ^

src/TTrigger.cpp Outdated
auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed().remove(QStringLiteral("\u0000"));
qDebug() << name;
char* substring_start = subject + ovector[2*n];
int substring_length = ovector[2*n+1] - ovector[2*n];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: do not use array subscript when the index is not an integer constant expression; use gsl::at() instead [cppcoreguidelines-pro-bounds-constant-array-index]

int substring_length = ovector[2*n+1] - ovector[2*n];
                       ^

src/TTrigger.cpp Outdated
auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed().remove(QStringLiteral("\u0000"));
qDebug() << name;
char* substring_start = subject + ovector[2*n];
int substring_length = ovector[2*n+1] - ovector[2*n];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: do not use array subscript when the index is not an integer constant expression; use gsl::at() instead [cppcoreguidelines-pro-bounds-constant-array-index]

int substring_length = ovector[2*n+1] - ovector[2*n];
                                        ^

src/TTrigger.cpp Outdated
int n = (tabptr[0] << 8) | tabptr[1];
auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed();
auto capture = QString::fromUtf8(subject + ovector[2*n], ovector[2*n+1] - ovector[2*n]);
auto name = QString::fromUtf8(tabptr + 2, name_entry_size - 3).trimmed().remove(QStringLiteral("\u0000"));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add a comment for the remove()? It's purpose not immediately clear.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, just added.
it does not propagate to Lua, but I've noticed that this null is somehow added sometimes to name of capture group.
Might be better way to deal with it - but I don't know it - this for sue helps.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You are dealing with data from a C library and it's outputs are going to be \0 terminated const char[] C strings. The name_entry_size value is going to be a strìng length argument and if you are trying to return partial string extraction with the original strings you should be referring to it (I believe) with a &tabptr[2] rather than tabptr + 2. Indeed given that as a first argument to the QString::fromUtf8(...) method I believe it is smart enough to remove just the first 2 characters from whatever it is that the original tabptr pointed to without having to specify an explicit length of string to consider as a second argument (and thereby not needing the QString::remove(...) either...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

remove remark - I've already managed to figure that one out, by removing second argument all was fixed and remove could be deleted.
I've also just changed first argument as suggested.

Copy link
Copy Markdown
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

src/TTrigger.cpp Outdated
int n = (tabptr[0] << 8) | tabptr[1];
auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed();
auto capture = QString::fromUtf8(subject + ovector[2*n], ovector[2*n+1] - ovector[2*n]);
auto name = QString::fromUtf8(tabptr + 2, name_entry_size - 3).trimmed().remove(QStringLiteral("\u0000"));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]

auto name = QString::fromUtf8(tabptr + 2, name_entry_size - 3).trimmed().remove(QStringLiteral("\u0000"));
                                     ^

Copy link
Copy Markdown
Member

@vadi2 vadi2 left a comment

Choose a reason for hiding this comment

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

Nice improvement! A bit more polish needed - it seems to get confused about the captures:

image

test-trig.zip

@Delwing
Copy link
Copy Markdown
Contributor Author

Delwing commented Apr 12, 2021

Sure, will do corrections.

Copy link
Copy Markdown
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

src/TTrigger.cpp Outdated
}
}

int namecount;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

int namecount;
    ^
              = 0

src/TTrigger.cpp Outdated
}

int namecount;
int name_entry_size;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

int name_entry_size;
    ^
                    = 0

src/TTrigger.cpp Outdated

int namecount;
int name_entry_size;
char* tabptr;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

char* tabptr;
      ^
             = nullptr

src/TTrigger.cpp Outdated
auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed();
auto capture = QString::fromUtf8(subject + ovector[2*n], ovector[2*n+1] - ovector[2*n]);
auto name = QString::fromUtf8(&tabptr[2]).trimmed();
auto* substring_start = subject + ovector[2*n];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]

auto* substring_start = subject + ovector[2*n];
                                ^

src/TTrigger.cpp Outdated
auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed();
auto capture = QString::fromUtf8(subject + ovector[2*n], ovector[2*n+1] - ovector[2*n]);
auto name = QString::fromUtf8(&tabptr[2]).trimmed();
auto* substring_start = subject + ovector[2*n];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: do not use array subscript when the index is not an integer constant expression; use gsl::at() instead [cppcoreguidelines-pro-bounds-constant-array-index]

auto* substring_start = subject + ovector[2*n];
                                  ^

src/TTrigger.cpp Outdated
auto capture = QString::fromUtf8(subject + ovector[2*n], ovector[2*n+1] - ovector[2*n]);
auto name = QString::fromUtf8(&tabptr[2]).trimmed();
auto* substring_start = subject + ovector[2*n];
auto substring_length = ovector[2*n+1] - ovector[2*n];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: do not use array subscript when the index is not an integer constant expression; use gsl::at() instead [cppcoreguidelines-pro-bounds-constant-array-index]

auto substring_length = ovector[2*n+1] - ovector[2*n];
                        ^

src/TTrigger.cpp Outdated
auto capture = QString::fromUtf8(subject + ovector[2*n], ovector[2*n+1] - ovector[2*n]);
auto name = QString::fromUtf8(&tabptr[2]).trimmed();
auto* substring_start = subject + ovector[2*n];
auto substring_length = ovector[2*n+1] - ovector[2*n];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: do not use array subscript when the index is not an integer constant expression; use gsl::at() instead [cppcoreguidelines-pro-bounds-constant-array-index]

auto substring_length = ovector[2*n+1] - ovector[2*n];
                                         ^

Copy link
Copy Markdown
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

src/TTrigger.cpp Outdated
int n = (tabptr[0] << 8) | tabptr[1];
auto name = QString::fromUtf8( tabptr + 2, name_entry_size - 3).trimmed();
auto capture = QString::fromUtf8(subject + ovector[2*n], ovector[2*n+1] - ovector[2*n]);
auto name = QString::fromUtf8(&tabptr[2]).trimmed();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]

auto name = QString::fromUtf8(&tabptr[2]).trimmed();
                               ^

@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Apr 15, 2021

We can add a lint ignore line to this code, not much to be done about it.

@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Apr 15, 2021

Example - //NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)

@Delwing
Copy link
Copy Markdown
Contributor Author

Delwing commented Apr 15, 2021

Do I have to do it for every line or can be done over scope block?

@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Apr 15, 2021

It doesn't look like scope blocks are supported: https://clang.llvm.org/extra/clang-tidy/

We could turn it off globally, but that doesn't seem such a good idea.

@SlySven
Copy link
Copy Markdown
Member

SlySven commented Apr 15, 2021

Oh, I just found out what the gsl referred to is - it is the Guidelines-Support Library and https://joshpeterson.github.io/using-span-with-argv gives some examples of using it for C strings...

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented May 2, 2021

clang-tidy review says "All clean, LGTM! 👍"

@Delwing
Copy link
Copy Markdown
Contributor Author

Delwing commented May 2, 2021

@vadi2 took ma a while, but got finally chance to lean on that :)

@vadi2 vadi2 assigned vadi2 and unassigned Delwing May 2, 2021
@vadi2
Copy link
Copy Markdown
Member

vadi2 commented May 2, 2021

Life happens, thanks for following up on it! I'll look at reviewing it.

@Delwing Delwing enabled auto-merge (squash) June 14, 2021 13:38
@Delwing
Copy link
Copy Markdown
Contributor Author

Delwing commented Jun 14, 2021

@vadi2 if you have any time to look at it please do :)

@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Jun 14, 2021

Hey I'm away on leave, see if you can get anyone else to review it and feel free to merge when it's good.

@Delwing Delwing merged commit da12bf9 into Mudlet:development Jul 31, 2021
Copy link
Copy Markdown
Member

@vadi2 vadi2 left a comment

Choose a reason for hiding this comment

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

Looks good! Could you just cover off #5131 (comment) and then merge?

@vadi2 vadi2 assigned Delwing and unassigned vadi2 Jul 31, 2021
@vadi2
Copy link
Copy Markdown
Member

vadi2 commented Jul 31, 2021

@Delwing Didn't notice the automerge trigger. Could you still make that small code improvement?

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.

selectCaptureGroup() enhancement to support named captures

3 participants