Add name capture group support for regex triggers#4731
Add name capture group support for regex triggers#4731Delwing merged 4 commits intoMudlet:developmentfrom
Conversation
|
Hey there! Thanks for helping Mudlet improve. 🌟 Test versionsYou can directly test the changes here:
No need to install anything - just unzip and run. |
SlySven
left a comment
There was a problem hiding this comment.
I like the idea of this - not sure I can get my head around it at the moment though...
😵
| matchStatePair.second->conditionMatched(); | ||
| matchStatePair.second->multiCaptureList.push_back(captureList); | ||
| matchStatePair.second->multiCapturePosList.push_back(posList); | ||
| if (nameMatches != nullptr) { |
There was a problem hiding this comment.
Will remove it and test 👍🏻
|
|
||
| tabptr = name_table; | ||
| for (i = 0; i < namecount; i++) { | ||
| int n = (tabptr[0] << 8) | tabptr[1]; |
There was a problem hiding this comment.
Could you add a comment for this construction?
There was a problem hiding this comment.
Well... I can reference snippet that I've used in comment + my (hopefully) correct understanding of that.
| @@ -13653,10 +13653,11 @@ std::pair<bool, QString> TLuaInterpreter::discordApiEnabled(lua_State* L, bool w | |||
| } | |||
|
|
|||
There was a problem hiding this comment.
In order for people to be able to check in their scripts if the player is running a Mudlet that supports this - add namedmatches = true to mudlet.supports, similar to how coroutines are there already.
See also https://wiki.mudlet.org/w/Manual:Advanced_Lua#Coroutines
src/TTrigger.cpp
Outdated
| NameGroupMatches* ng = &nameGroups; | ||
| updateMultistates(regexNumber, captureList, posList, ng); |
There was a problem hiding this comment.
This would work just as well:
| NameGroupMatches* ng = &nameGroups; | |
| updateMultistates(regexNumber, captureList, posList, ng); | |
| updateMultistates(regexNumber, captureList, posList, &nameGroups); |
| #define REGEX_PROMPT 7 | ||
| #define MAX_CAPTURE_GROUPS 33 | ||
|
|
||
| using NameGroupMatches = QVector<QPair<QString, QString>>; |
There was a problem hiding this comment.
🤯 This is a new concept for me - anyone else not get it? https://stackoverflow.com/q/20790932/4805858 might help.
There was a problem hiding this comment.
There was a problem hiding this comment.
I wanted to have this generics more comprehensible.
src/TTrigger.cpp
Outdated
| tabptr = name_table; | ||
| for (i = 0; i < namecount; i++) { | ||
| int n = (tabptr[0] << 8) | tabptr[1]; | ||
| auto name = QString::asprintf("%*s", name_entry_size - 3, tabptr + 2).trimmed(); |
There was a problem hiding this comment.
Qt documentation says:
Warning: We do not recommend using
QString::asprintf()in new Qt code. Instead, consider usingQTextStreamorarg(), both of which support Unicode strings seamlessly and are type-safe. Here's an example that usesQTextStream:
QString result;
QTextStream(&result) << "pi = " << 3.14;
// result == "pi = 3.14"
For translations, especially if the strings contains more than one escape sequence, you should consider using thearg()function instead. This allows the order of the replacements to be controlled by the translator.This function was introduced in Qt 5.5.
See also
arg().
There was a problem hiding this comment.
Yes! I've read it, and was pretty aware of that. arg() was not alternative in this case, not accepting range I guess... My understanding of this part is a bit limited.
It is based on:
https://github.com/vmg/pcre/blob/master/pcredemo.c
There was a problem hiding this comment.
I guess I've found solution not to use asprintf
|
👍 You know named capture groups is a great way to simplify multiple trigger items with different numbers of arguments and them not being in the same position in different ones...! Something that really should be mentioned in the release announcement. |
|
|
||
| tabptr = name_table; | ||
| for (i = 0; i < namecount; i++) { | ||
| int n = (tabptr[0] << 8) | tabptr[1]; |
|
That was my main motivation - same handler function, different argument order :) Had just use case like that, so started digging :) |
|
Include that in the docs for sure as a practical example! |
|
Yep! Will do. |
Co-authored-by: Piotr Wilczynski <piotr.wilczynski@bisnode.com>

Brief overview of PR changes/additions
It populates matches with named capture groups:
https://www.regular-expressions.info/named.html
Some skeletal retrieval was there, but never went any further.
I've tested that on regexp trigger and tempRegexTrigger - I guess only those 2 need support for that.
Multimatches also are made to work.
Please take a special look at the retrieval code
TTrigger.ccp:352Motivation for adding to Mudlet
Multiple triggers matching similar thing can be hell if searched needle is swapping place.
Named groups are very convinent.
Other info (issues closed, discussion etc)
Quick test regexp