getMatch parses the passed url and tries to match it against the route segments and determine the parameter positions
(detectionPath, path string, params *[maxParams]string, partialCheck bool)
| 511 | |
| 512 | // getMatch parses the passed url and tries to match it against the route segments and determine the parameter positions |
| 513 | func (parser *routeParser) getMatch(detectionPath, path string, params *[maxParams]string, partialCheck bool) bool { //nolint:revive // Accepting a bool param is fine here |
| 514 | originalDetectionPath := detectionPath |
| 515 | var i, paramsIterator, partLen int |
| 516 | for _, segment := range parser.segs { |
| 517 | partLen = len(detectionPath) |
| 518 | // check const segment |
| 519 | if !segment.IsParam { |
| 520 | i = segment.Length |
| 521 | // is optional part or the const part must match with the given string |
| 522 | // check if the end of the segment is an optional slash |
| 523 | // the unsigned compare proves 0 <= i <= len(detectionPath), keeping detectionPath[:i] bounds-check free |
| 524 | if segment.HasOptionalSlash && partLen == i-1 && detectionPath == segment.Const[:i-1] { |
| 525 | i-- |
| 526 | } else if uint(i) > uint(len(detectionPath)) || detectionPath[:i] != segment.Const { |
| 527 | return false |
| 528 | } |
| 529 | } else { |
| 530 | // determine parameter length |
| 531 | i = findParamLen(detectionPath, segment) |
| 532 | if !segment.IsOptional && i == 0 { |
| 533 | return false |
| 534 | } |
| 535 | // take over the params positions |
| 536 | params[paramsIterator] = path[:i] |
| 537 | |
| 538 | if !segment.IsOptional || i != 0 { |
| 539 | // check constraint |
| 540 | for _, c := range segment.Constraints { |
| 541 | if matched := c.matchConstraint(params[paramsIterator]); !matched { |
| 542 | return false |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | paramsIterator++ |
| 548 | } |
| 549 | |
| 550 | // reduce founded part from the string |
| 551 | if partLen > 0 { |
| 552 | detectionPath, path = detectionPath[i:], path[i:] |
| 553 | } |
| 554 | } |
| 555 | if detectionPath != "" { |
| 556 | if !partialCheck { |
| 557 | return false |
| 558 | } |
| 559 | consumedLength := len(originalDetectionPath) - len(detectionPath) |
| 560 | if !hasPartialMatchBoundary(originalDetectionPath, consumedLength) { |
| 561 | return false |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return true |
| 566 | } |
| 567 | |
| 568 | // findParamLen for the expressjs wildcard behavior (right to left greedy) |
| 569 | // look at the other segments and take what is left for the wildcard from right to left |