1818import java .util .stream .Collectors ;
1919import java .util .stream .Stream ;
2020
21+ /**
22+ * Sync: 20-01-20
23+ * Commit: 17fb1065d2b256d20f68bed0b7bca6c2942aff49
24+ */
2125class Chi implements RouteTree {
2226 private static final byte ntStatic = 0 ;// /home
2327 private static final byte ntRegexp = 1 ; // /{id:[0-9]+}
@@ -379,9 +383,8 @@ Node addChild(Node child, ZeroCopyString search) {
379383 // Search prefix contains a param, regexp or wildcard
380384
381385 if (segTyp == ntRegexp ) {
382- rex = Pattern .compile (seg .rexPat .toString ());
383386 child .prefix = seg .rexPat ;
384- child .rex = rex ;
387+ child .rex = Pattern . compile ( seg . rexPat . toString ()) ;
385388 }
386389
387390 if (segStartIdx == 0 ) {
@@ -399,7 +402,7 @@ Node addChild(Node child, ZeroCopyString search) {
399402 child .tail = seg .tail ; // for params, we set the tail
400403
401404 if (segStartIdx != search .length ()) {
402- // deploy static edge for the remaining part, split the end.
405+ // add static edge for the remaining part, split the end.
403406 // its not possible to have adjacent param nodes, so its certainly
404407 // going to be a static node next.
405408
@@ -418,7 +421,7 @@ Node addChild(Node child, ZeroCopyString search) {
418421 child .prefix = search .substring (0 , segStartIdx );
419422 child .rex = null ;
420423
421- // deploy the param edge node
424+ // add the param edge node
422425 search = search .substring (segStartIdx );
423426
424427 Node nn = new Node ().typ (segTyp ).label (search .charAt (0 )).tail (seg .tail );
@@ -491,81 +494,82 @@ void setEndpoint(String method, Route route) {
491494
492495 // Recursive edge traversal by checking all nodeTyp groups along the way.
493496 // It's like searching through a multi-dimensional radix trie.
494- Route findRoute (RouterMatch rctx , String method , ZeroCopyString pattern ) {
497+ Route findRoute (RouterMatch rctx , String method , ZeroCopyString path ) {
495498 Node n = this ;
496499 Node nn = n ;
497500
501+ ZeroCopyString search = path ;
502+
498503 for (int ntyp = 0 ; ntyp < NODE_SIZE ; ntyp ++) {
499504 Node [] nds = nn .children [ntyp ];
500505 if (nds != null ) {
501506 Node xn = null ;
502- ZeroCopyString search = pattern ;
507+ ZeroCopyString xsearch = search ;
503508
504509 char label = search .length () > 0 ? search .charAt (0 ) : ZERO_CHAR ;
505510
506511 switch (ntyp ) {
507512 case ntStatic :
508513 xn = findEdge (nds , label );
509- if (xn == null || !search .startsWith (xn .prefix )) {
514+ if (xn == null || !xsearch .startsWith (xn .prefix )) {
510515 continue ;
511516 }
512- search = search .substring (xn .prefix .length ());
517+ xsearch = xsearch .substring (xn .prefix .length ());
513518 break ;
514519
515520 case ntParam :
516521 case ntRegexp :
517522 // short-circuit and return no matching route for empty param values
518- if (search .length () == 0 ) {
523+ if (xsearch .length () == 0 ) {
519524 continue ;
520525 }
521-
522526 // serially loop through each node grouped by the tail delimiter
523527 for (int idx = 0 ; idx < nds .length ; idx ++) {
524528 xn = nds [idx ];
525529
526530 // label for param nodes is the delimiter byte
527- int p = search .indexOf (xn .tail );
531+ int p = xsearch .indexOf (xn .tail );
528532
529- if (p <= 0 ) {
533+ if (p < 0 ) {
530534 if (xn .tail == '/' ) {
531- p = search .length ();
535+ p = xsearch .length ();
532536 } else {
533537 continue ;
534538 }
535539 }
536540
537- if (ntyp == ntRegexp ) {
538- if (!xn .rex .matcher (search .substring (0 , p ).toString ()).matches ()) {
541+ if (ntyp == ntRegexp && xn . rex != null ) {
542+ if (!xn .rex .matcher (xsearch .substring (0 , p ).toString ()).matches ()) {
539543 continue ;
540544 }
541- } else if (search .substring (0 , p ).indexOf ('/' ) != -1 ) {
545+ } else if (xsearch .substring (0 , p ).indexOf ('/' ) != -1 ) {
542546 // avoid a newRuntimeRoute across path segments
543547 continue ;
544548 }
545549
546550 // rctx.routeParams.Values = append(rctx.routeParams.Values, xsearch[:p])
547- rctx .value (search .substring (0 , p ));
548- search = search .substring (p );
551+ rctx .value (xsearch .substring (0 , p ));
552+ xsearch = xsearch .substring (p );
549553 break ;
550554 }
551555 break ;
552556
553557 default :
554558 // catch-all nodes
555559 // rctx.routeParams.Values = append(rctx.routeParams.Values, search)
556- if (search .length () > 0 ) {
557- rctx .value (search );
560+ if (xsearch .length () > 0 ) {
561+ rctx .value (xsearch );
558562 }
559563 xn = nds [0 ];
560- search = ZeroCopyString .EMPTY ;
564+ xsearch = ZeroCopyString .EMPTY ;
561565 }
562566
563567 if (xn == null ) {
564568 continue ;
565569 }
566570
567571 // did we returnType it yet?
568- if (search .length () == 0 ) {
572+ if (xsearch .length () == 0 ) {
569573 if (xn .isLeaf ()) {
570574 Route h = xn .endpoints .get (method );
571575 if (h != null ) {
@@ -581,7 +585,7 @@ Route findRoute(RouterMatch rctx, String method, ZeroCopyString pattern) {
581585 }
582586
583587 // recursively returnType the next node..
584- Route fin = xn .findRoute (rctx , method , search );
588+ Route fin = xn .findRoute (rctx , method , xsearch );
585589 if (fin != null ) {
586590 return fin ;
587591 }
0 commit comments