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
https://c9s.github.io/r3/bench.html
MIT License
815 stars 83 forks source link

Improve route slug parser #92

Open c9s opened 9 years ago

c9s commented 9 years ago
c9s commented 8 years ago

@karantin2020 just to confirm, are the items above completed?

karantin2020 commented 8 years ago

Routes contain only slug names:

struct _R3Route {
    char * path;
    int    path_len;

    char **slugs;
    int    slugs_len;
    int    slugs_cap;
...
typedef struct {
    str_array * vars;
    const char * path; // current path to dispatch
    int    path_len; // the length of the current path
    int    request_method;  // current request method

    void * data; // R3Route ptr

    char * host; // the request host
    int    host_len;

    char * remote_addr;
    int    remote_addr_len;
} match_entry;

typedef struct _str_array {
  char **slugs;
  int    slugs_len;
  char **tokens;
  int    len;
  int    cap;
} str_array;

Access to slug names and it's values is through match_entry->vars. Slug names are pointers to routers field slugs. They do not copy by value and do not create again.

The second point was not realized full. ...so we can iterate the slugs in the matched route - it is incorrect way to access slugs. Routes created only once and there is no need to change it in life time. Because slug values depend on every match path it is better to save that values in match_entry struct. So iterate slugs with match_entry->vars.

For details see examples/routing.c.