Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions astro/content/docs/clients/cpp.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,38 @@ auto match = pattern->match("https://example.com/books/123");
auto matched = pattern->test("https://example.com/books/123");
```

## URLPattern List

A URLPattern list compiles a set of pathname patterns together, so that finding the
matching route is one lookup instead of a loop over `url_pattern::exec`. It takes the
same regex provider as `parse_url_pattern`, and only routes with regexp groups or
modifiers reach it: static, `:param` and `*` routes are matched without a regex engine.

```cpp
// Same provider as above.
std::vector<std::string_view> routes = {"/", "/users/:id", "/users/me",
"/files/*", "/posts/(\\d+)"};
auto list = ada::parse_url_pattern_list<v8_regex_provider>(routes);

if (!list) { return EXIT_FAILURE; }

// Match a pathname, for example url.get_pathname()
auto m = list->match("/users/42");
// m.route_index == 1
// m.captures[0] is the ":id" value as a slice of the input: offset 7, length 2
// list->group_names(1)[0] == "id"

auto r = list->match("/posts/7");
// r.route_index == 4 and r.regexp_route == true: matched through the provider
// r.regexp_groups[0] == "7", as returned by regex_search
```

The most specific route wins: a literal segment beats `:param`, which beats `*`, and
between equally specific routes the one added first wins, as in routers such as
find-my-way and Express. `parse_url_pattern_list` also accepts a base URL,
`url_pattern_options` and existing `ada::url_pattern` objects. Only the pathname is
matched; the API is experimental.

## C Wrapper

Ada provides a C interface for use from C or other languages that can bind to C
Expand Down
Loading