c9s / r3

libr3 is a high-performance path dispatching library. It compiles your route paths into a prefix tree (trie). By using the constructed prefix trie in the start-up time, you may dispatch your routes with efficiency
http://c9s.github.com/r3/bench.html
MIT License
815 stars 83 forks source link

Support for multiple params within a route #118

Closed MartB closed 11 months ago

MartB commented 5 years ago

Is it possible to create something like:

/api/username:\\w+/:action:\\w+and have it properly match username and action, so i can use these as params in my code?

Im currently reading the data back from the tokenlist and the stuff it returns is a mess.

[0] is: test/test/
[1] is: test/

this is what i would expect from a dumb regex parser but not from a path dispatch solution.

generated by the following code:

const auto tokens = entry.get()->vars.tokens;

for (int i = 0; i < tokens.size; i++) {
    printf("[%d] is: %s\n", i, tokens.entries[i]);
}

int ret = *static_cast<int*>(matched_node.data());
std::cout << "match path ret: " << ret << std::endl;
bjosv commented 11 months ago

Interesting. I'm not sure what version you tested on and/or if you have some extra code layer (since you have a entry.get()) but the following example seems to work fine on the 2.0 branch. The problem you probably faced is that you need to take the length into account when print out the entry.

    R3Node *n = r3_tree_create(10);
    r3_tree_insert_path(n, "/api/{username:\\w+}/{action:\\w+}", NULL);
    r3_tree_compile(n, NULL);

    match_entry *entry = match_entry_create("/api/sesame/open");

    R3Node *matched = r3_tree_match_entry(n, entry);

    for (int i = 0; i < entry->vars.tokens.size; i++) {
    // Only print given length
    printf("[%d] is: %.*s\n", i, entry->vars.tokens.entries[i].len, entry->vars.tokens.entries[i].base);
    }

    match_entry_free(entry);
    r3_tree_free(n);
[0] is: sesame
[1] is: open

Do still have this problem or do you have any objections if we close this issue?

MartB commented 11 months ago

Will revisit once i get time to work on that old project again, but we can surely close this issue now :+1: