Genivia / RE-flex

A high-performance C++ regex library and lexical analyzer generator with Unicode support. Extends Flex++ with Unicode support, indent/dedent anchors, lazy quantifiers, functions for lex and syntax error reporting and more. Seamlessly integrates with Bison and other parsers.
https://www.genivia.com/doc/reflex/html
BSD 3-Clause "New" or "Revised" License
522 stars 85 forks source link

Question: Matcher.matches() does not return group capture index. #128

Closed jakob-stark closed 2 years ago

jakob-stark commented 2 years ago

The manual states:

The matches() method returns a nonzero "accept" value (the size_t accept() group capture index value or the value 1 if no groups are used) if the given input from begin to the end matches the specified pattern.

Almost the same is said for the scan() method. Now consider the following program

#include <iostream>
#include <reflex/matcher.h>

int main(int argc, char * argv[]) {
    auto pattern = reflex::Pattern("(cat)|(dog)");

    std::cout << reflex::Matcher(pattern, "cat").matches() << std::endl;
    std::cout << reflex::Matcher(pattern, "cat").scan()    << std::endl;

    std::cout << reflex::Matcher(pattern, "dog").matches() << std::endl;
    std::cout << reflex::Matcher(pattern, "dog").scan()    << std::endl;
}

It prints out

1
1
1
2

So the scan() method actually returns the group capture index value as stated in the manual, but the matches() method does not. Is this behaviour intended?

genivia-inc commented 2 years ago

I believe this behavior may have changed sometime, because matches() was just intended to report a match yes/no. The doc isn't updated. But I agree it is nicer to have the group capture index. Will update the repo with the change to return capture index.

Note that you can also get the capture index afterwards with Matcher::accept().

jakob-stark commented 2 years ago

Thanks for the answer and also for the awesome project. I guess I'm just fine to use Matcher::accept() but would also appreciate a future change. After all the return value still could be used as a boolean value if it returned the group capture index.

genivia-inc commented 2 years ago

Fixed in v3.2.2.