compile-time-regular-expressions icon indicating copy to clipboard operation
compile-time-regular-expressions copied to clipboard

feature request for range api (match.position())

Open aaangeletakis opened this issue 3 years ago • 1 comments

The STL has a match_results::position() function that returns the position of the match in the string. https://www.cplusplus.com/reference/regex/match_results/position/

This could possibly be implemented via subtracting 2 pointers.

const char * str = ...
const char * match = ...
int position = match - str;

Example use

#include <ctre.hpp>
#include <iostream>

const char * input = "123,456,768";
//extern const char * input;

int main(void){
    auto matches = ctre::range<"([0-9]+),?">(input);
    for (auto match: matches) {
        std::cout << "Match found at " << std::string_view{match.get<0>()} << "\n";
        //std::cout << "Position at" << match.position() << "\n";
    }
    return 0;
}

Compiler Explorer

aaangeletakis avatar Mar 26 '21 18:03 aaangeletakis

Turns out you can get the position via a view

#include "ctre.hpp"
#include <iostream>

const char * input = "123,456,768";
//extern const char * input;

int main(void){
    static constexpr ctll::fixed_string pat = "([0-9]+),?";
    auto matches = ctre::range<pat>(input);
    for (auto match: matches) {
        auto view = match.to_view();
        std::cout << view.data() - input << " [" << view << "]\n";
    }
    return 0;
}

aaangeletakis avatar Apr 06 '21 00:04 aaangeletakis