@@ -233,70 +233,62 @@ pub const RoutingTrie = struct {
233
233
print_node (& self .root , 0 );
234
234
}
235
235
236
- pub fn get_route (
236
+ pub fn get_bundle (
237
237
self : Self ,
238
238
path : []const u8 ,
239
239
captures : []Capture ,
240
240
queries : * QueryMap ,
241
241
) ! ? FoundBundle {
242
242
var capture_idx : usize = 0 ;
243
-
244
- queries .clear ();
245
243
const query_pos = std .mem .indexOfScalar (u8 , path , '?' );
246
244
var iter = std .mem .tokenizeScalar (u8 , path [0.. (query_pos orelse path .len )], '/' );
247
245
248
246
var current = self .root ;
249
247
250
248
slash_loop : while (iter .next ()) | chunk | {
251
- const fragment = Token { .fragment = chunk };
252
-
253
- // If it is the fragment, match it here.
254
- if (current .children .get (fragment )) | child | {
255
- current = child ;
256
- continue ;
257
- }
258
-
259
- var matched = false ;
260
- for (std .meta .tags (TokenMatch )) | token_type | {
261
- const token = Token { .match = token_type };
262
- if (current .children .get (token )) | child | {
263
- matched = true ;
264
- switch (token_type ) {
265
- .signed = > if (std .fmt .parseInt (i64 , chunk , 10 )) | value | {
266
- captures [capture_idx ] = Capture { .signed = value };
267
- } else | _ | continue ,
268
- .unsigned = > if (std .fmt .parseInt (u64 , chunk , 10 )) | value | {
269
- captures [capture_idx ] = Capture { .unsigned = value };
270
- } else | _ | continue ,
271
- .float = > if (std .fmt .parseFloat (f64 , chunk )) | value | {
272
- captures [capture_idx ] = Capture { .float = value };
273
- } else | _ | continue ,
274
- .string = > captures [capture_idx ] = Capture { .string = chunk },
275
- // This ends the matching sequence and claims everything.
276
- // Does not claim the query values.
277
- .remaining = > {
278
- const rest = iter .buffer [(iter .index - chunk .len ).. ];
279
- captures [capture_idx ] = Capture { .remaining = rest };
280
-
281
- current = child ;
282
- capture_idx += 1 ;
283
-
284
- break :slash_loop ;
285
- },
286
- }
287
-
288
- current = child ;
289
- capture_idx += 1 ;
249
+ var child_iter = current .children .iterator ();
250
+ child_loop : while (child_iter .next ()) | entry | {
251
+ const token = entry .key_ptr .* ;
252
+ const child = entry .value_ptr .* ;
253
+
254
+ switch (token ) {
255
+ .fragment = > | inner | if (std .mem .eql (u8 , inner , chunk )) {
256
+ current = child ;
257
+ continue :slash_loop ;
258
+ },
259
+ .match = > | kind | {
260
+ switch (kind ) {
261
+ .signed = > if (std .fmt .parseInt (i64 , chunk , 10 )) | value | {
262
+ captures [capture_idx ] = Capture { .signed = value };
263
+ } else | _ | continue :child_loop ,
264
+ .unsigned = > if (std .fmt .parseInt (u64 , chunk , 10 )) | value | {
265
+ captures [capture_idx ] = Capture { .unsigned = value };
266
+ } else | _ | continue :child_loop ,
267
+ .float = > if (std .fmt .parseFloat (f64 , chunk )) | value | {
268
+ captures [capture_idx ] = Capture { .float = value };
269
+ } else | _ | continue :child_loop ,
270
+ .string = > captures [capture_idx ] = Capture { .string = chunk },
271
+ .remaining = > {
272
+ const rest = iter .buffer [(iter .index - chunk .len ).. ];
273
+ captures [capture_idx ] = Capture { .remaining = rest };
274
+
275
+ current = child ;
276
+ capture_idx += 1 ;
277
+
278
+ break :slash_loop ;
279
+ },
280
+ }
290
281
291
- if (capture_idx > captures .len ) return error .TooManyCaptures ;
292
- break ;
282
+ current = child ;
283
+ capture_idx += 1 ;
284
+ if (capture_idx > captures .len ) return error .TooManyCaptures ;
285
+ continue :slash_loop ;
286
+ },
293
287
}
294
288
}
295
289
296
290
// If we failed to match, this is an invalid route.
297
- if (! matched ) {
298
- return null ;
299
- }
291
+ return null ;
300
292
}
301
293
302
294
if (query_pos ) | pos | {
@@ -416,17 +408,17 @@ test "Routing with Paths" {
416
408
417
409
var captures : [8 ]Capture = [_ ]Capture {undefined } ** 8 ;
418
410
419
- try testing .expectEqual (null , try s .get_route ("/item/name" , captures [0.. ], & q ));
411
+ try testing .expectEqual (null , try s .get_bundle ("/item/name" , captures [0.. ], & q ));
420
412
421
413
{
422
- const captured = (try s .get_route ("/item/name/HELLO" , captures [0.. ], & q )).? ;
414
+ const captured = (try s .get_bundle ("/item/name/HELLO" , captures [0.. ], & q )).? ;
423
415
424
416
try testing .expectEqual (Route .init ("/item/name/%s" ), captured .bundle .route );
425
417
try testing .expectEqualStrings ("HELLO" , captured .captures [0 ].string );
426
418
}
427
419
428
420
{
429
- const captured = (try s .get_route ("/item/2112.22121/price_float" , captures [0.. ], & q )).? ;
421
+ const captured = (try s .get_bundle ("/item/2112.22121/price_float" , captures [0.. ], & q )).? ;
430
422
431
423
try testing .expectEqual (Route .init ("/item/%f/price_float" ), captured .bundle .route );
432
424
try testing .expectEqual (2112.22121 , captured .captures [0 ].float );
@@ -447,27 +439,27 @@ test "Routing with Remaining" {
447
439
448
440
var captures : [8 ]Capture = [_ ]Capture {undefined } ** 8 ;
449
441
450
- try testing .expectEqual (null , try s .get_route ("/item/name" , captures [0.. ], & q ));
442
+ try testing .expectEqual (null , try s .get_bundle ("/item/name" , captures [0.. ], & q ));
451
443
452
444
{
453
- const captured = (try s .get_route ("/item/name/HELLO" , captures [0.. ], & q )).? ;
445
+ const captured = (try s .get_bundle ("/item/name/HELLO" , captures [0.. ], & q )).? ;
454
446
try testing .expectEqual (Route .init ("/item/name/%r" ), captured .bundle .route );
455
447
try testing .expectEqualStrings ("HELLO" , captured .captures [0 ].remaining );
456
448
}
457
449
{
458
- const captured = (try s .get_route ("/item/name/THIS/IS/A/FILE/SYSTEM/PATH.html" , captures [0.. ], & q )).? ;
450
+ const captured = (try s .get_bundle ("/item/name/THIS/IS/A/FILE/SYSTEM/PATH.html" , captures [0.. ], & q )).? ;
459
451
try testing .expectEqual (Route .init ("/item/name/%r" ), captured .bundle .route );
460
452
try testing .expectEqualStrings ("THIS/IS/A/FILE/SYSTEM/PATH.html" , captured .captures [0 ].remaining );
461
453
}
462
454
463
455
{
464
- const captured = (try s .get_route ("/item/2112.22121/price_float" , captures [0.. ], & q )).? ;
456
+ const captured = (try s .get_bundle ("/item/2112.22121/price_float" , captures [0.. ], & q )).? ;
465
457
try testing .expectEqual (Route .init ("/item/%f/price_float" ), captured .bundle .route );
466
458
try testing .expectEqual (2112.22121 , captured .captures [0 ].float );
467
459
}
468
460
469
461
{
470
- const captured = (try s .get_route ("/item/100/price/283.21" , captures [0.. ], & q )).? ;
462
+ const captured = (try s .get_bundle ("/item/100/price/283.21" , captures [0.. ], & q )).? ;
471
463
try testing .expectEqual (Route .init ("/item/%i/price/%f" ), captured .bundle .route );
472
464
try testing .expectEqual (100 , captured .captures [0 ].signed );
473
465
try testing .expectEqual (283.21 , captured .captures [1 ].float );
@@ -488,10 +480,10 @@ test "Routing with Queries" {
488
480
489
481
var captures : [8 ]Capture = [_ ]Capture {undefined } ** 8 ;
490
482
491
- try testing .expectEqual (null , try s .get_route ("/item/name" , captures [0.. ], & q ));
483
+ try testing .expectEqual (null , try s .get_bundle ("/item/name" , captures [0.. ], & q ));
492
484
493
485
{
494
- const captured = (try s .get_route ("/item/name/HELLO?name=muki&food=waffle" , captures [0.. ], & q )).? ;
486
+ const captured = (try s .get_bundle ("/item/name/HELLO?name=muki&food=waffle" , captures [0.. ], & q )).? ;
495
487
try testing .expectEqual (Route .init ("/item/name/%r" ), captured .bundle .route );
496
488
try testing .expectEqualStrings ("HELLO" , captured .captures [0 ].remaining );
497
489
try testing .expectEqual (2 , q .dirty ());
@@ -501,15 +493,15 @@ test "Routing with Queries" {
501
493
502
494
{
503
495
// Purposefully bad format with no keys or values.
504
- const captured = (try s .get_route ("/item/2112.22121/price_float?" , captures [0.. ], & q )).? ;
496
+ const captured = (try s .get_bundle ("/item/2112.22121/price_float?" , captures [0.. ], & q )).? ;
505
497
try testing .expectEqual (Route .init ("/item/%f/price_float" ), captured .bundle .route );
506
498
try testing .expectEqual (2112.22121 , captured .captures [0 ].float );
507
499
try testing .expectEqual (0 , q .dirty ());
508
500
}
509
501
510
502
{
511
503
// Purposefully bad format with incomplete key/value pair.
512
- const captured = (try s .get_route ("/item/100/price/283.21?help" , captures [0.. ], & q )).? ;
504
+ const captured = (try s .get_bundle ("/item/100/price/283.21?help" , captures [0.. ], & q )).? ;
513
505
try testing .expectEqual (Route .init ("/item/%i/price/%f" ), captured .bundle .route );
514
506
try testing .expectEqual (100 , captured .captures [0 ].signed );
515
507
try testing .expectEqual (283.21 , captured .captures [1 ].float );
@@ -518,7 +510,7 @@ test "Routing with Queries" {
518
510
519
511
{
520
512
// Purposefully have too many queries.
521
- const captured = try s .get_route (
513
+ const captured = try s .get_bundle (
522
514
"/item/100/price/283.21?a=1&b=2&c=3&d=4&e=5&f=6&g=7&h=8&i=9&j=10&k=11" ,
523
515
captures [0.. ],
524
516
& q ,
0 commit comments