Skip to content
Myr

chore: update tree (#2371)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by thinkerou · committed by GitHub · parent 3b5e861 · Browse files · 1 file+126 added−127 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

1 file+126 added−127 removed

  • other:+126 added−127 removed

1 file+126 added−127 removed

modified:tree.go+126 added-127 removed
117 unmodified lines
118 newPos := pos119 for ; newPos > 0 && cs[newPos-1].priority < prio; newPos-- {120 // Swap node positions121 cs[newPos-1], cs[newPos] = cs[newPos], cs[newPos-1]122122 }123124 // Build new index char string125 if newPos != pos {
431 unmodified lines
557558 // Use a static sized buffer on the stack in the common case.559 // If the path is too long, allocate a buffer on the heap instead.560 buf := make([]byte, 0, stackBufSize)562 if l := len(path) + 1; l > stackBufSize {563 buf = make([]byte, 0, l)561 if length := len(path) + 1; length > stackBufSize {562 buf = make([]byte, 0, length)563 }564565 ciPath := n.findCaseInsensitivePathRec(566 path,
31 unmodified lines
598 oldPath := path599 path = path[npLen:]600 ciPath = append(ciPath, n.path...)601603 if len(path) > 0 {604 // If this node does not have a wildcard (param or catchAll) child,605 // we can just look up the next child node and continue to walk down606 // the tree607 if !n.wildChild {608 // Skip rune bytes already processed609 rb = shiftNRuneBytes(rb, npLen)602 if len(path) == 0 {603 // We should have reached the node containing the handle.604 // Check if this node has a handle registered.605 if n.handlers != nil {606 return ciPath607 }608611 if rb[0] != 0 {612 // Old rune not finished613 idxc := rb[0]614 for i, c := range []byte(n.indices) {615 if c == idxc {616 // continue with child node617 n = n.children[i]618 npLen = len(n.path)619 continue walk609 // No handle found.610 // Try to fix the path by adding a trailing slash611 if fixTrailingSlash {612 for i, c := range []byte(n.indices) {613 if c == '/' {614 n = n.children[i]615 if (len(n.path) == 1 && n.handlers != nil) ||616 (n.nType == catchAll && n.children[0].handlers != nil) {617 return append(ciPath, '/')618 }619 return nil620 }622 } else {623 // Process a new rune624 var rv rune621 }622 }623 return nil624 }625626 // Find rune start.627 // Runes are up to 4 byte long,628 // -4 would definitely be another rune.629 var off int630 for max := min(npLen, 3); off < max; off++ {631 if i := npLen - off; utf8.RuneStart(oldPath[i]) {632 // read rune from cached path633 rv, _ = utf8.DecodeRuneInString(oldPath[i:])634 break635 }626 // If this node does not have a wildcard (param or catchAll) child,627 // we can just look up the next child node and continue to walk down628 // the tree629 if !n.wildChild {630 // Skip rune bytes already processed631 rb = shiftNRuneBytes(rb, npLen)632633 if rb[0] != 0 {634 // Old rune not finished635 idxc := rb[0]636 for i, c := range []byte(n.indices) {637 if c == idxc {638 // continue with child node639 n = n.children[i]640 npLen = len(n.path)641 continue walk642 }643 }644 } else {645 // Process a new rune646 var rv rune647638 // Calculate lowercase bytes of current rune639 lo := unicode.ToLower(rv)640 utf8.EncodeRune(rb[:], lo)648 // Find rune start.649 // Runes are up to 4 byte long,650 // -4 would definitely be another rune.651 var off int652 for max := min(npLen, 3); off < max; off++ {653 if i := npLen - off; utf8.RuneStart(oldPath[i]) {654 // read rune from cached path655 rv, _ = utf8.DecodeRuneInString(oldPath[i:])656 break657 }658 }659660 // Calculate lowercase bytes of current rune661 lo := unicode.ToLower(rv)662 utf8.EncodeRune(rb[:], lo)663642 // Skip already processed bytes664 // Skip already processed bytes665 rb = shiftNRuneBytes(rb, off)666667 idxc := rb[0]668 for i, c := range []byte(n.indices) {669 // Lowercase matches670 if c == idxc {671 // must use a recursive approach since both the672 // uppercase byte and the lowercase byte might exist673 // as an index674 if out := n.children[i].findCaseInsensitivePathRec(675 path, ciPath, rb, fixTrailingSlash,676 ); out != nil {677 return out678 }679 break680 }681 }682683 // If we found no match, the same for the uppercase rune,684 // if it differs685 if up := unicode.ToUpper(rv); up != lo {686 utf8.EncodeRune(rb[:], up)687 rb = shiftNRuneBytes(rb, off)688689 idxc := rb[0]690 for i, c := range []byte(n.indices) {647 // Lowercase matches691 // Uppercase matches692 if c == idxc {649 // must use a recursive approach since both the650 // uppercase byte and the lowercase byte might exist651 // as an index652 if out := n.children[i].findCaseInsensitivePathRec(653 path, ciPath, rb, fixTrailingSlash,654 ); out != nil {655 return out656 }657 break693 // Continue with child node694 n = n.children[i]695 npLen = len(n.path)696 continue walk697 }698 }660661 // If we found no match, the same for the uppercase rune,662 // if it differs663 if up := unicode.ToUpper(rv); up != lo {664 utf8.EncodeRune(rb[:], up)665 rb = shiftNRuneBytes(rb, off)666667 idxc := rb[0]668 for i, c := range []byte(n.indices) {669 // Uppercase matches670 if c == idxc {671 // Continue with child node672 n = n.children[i]673 npLen = len(n.path)674 continue walk675 }676 }677 }699 }700 }701680 // Nothing found. We can recommend to redirect to the same URL681 // without a trailing slash if a leaf exists for that path682 if fixTrailingSlash && path == "/" && n.handlers != nil {683 return ciPath684 }685 return nil702 // Nothing found. We can recommend to redirect to the same URL703 // without a trailing slash if a leaf exists for that path704 if fixTrailingSlash && path == "/" && n.handlers != nil {705 return ciPath706 }707 return nil708 }709688 n = n.children[0]689 switch n.nType {690 case param:691 // Find param end (either '/' or path end)692 end := 0693 for end < len(path) && path[end] != '/' {694 end++695 }710 n = n.children[0]711 switch n.nType {712 case param:713 // Find param end (either '/' or path end)714 end := 0715 for end < len(path) && path[end] != '/' {716 end++717 }718697 // Add param value to case insensitive path698 ciPath = append(ciPath, path[:end]...)719 // Add param value to case insensitive path720 ciPath = append(ciPath, path[:end]...)721700 // We need to go deeper!701 if end < len(path) {702 if len(n.children) > 0 {703 // Continue with child node704 n = n.children[0]705 npLen = len(n.path)706 path = path[end:]707 continue708 }709710 // ... but we can't711 if fixTrailingSlash && len(path) == end+1 {712 return ciPath713 }714 return nil722 // We need to go deeper!723 if end < len(path) {724 if len(n.children) > 0 {725 // Continue with child node726 n = n.children[0]727 npLen = len(n.path)728 path = path[end:]729 continue730 }731717 if n.handlers != nil {732 // ... but we can't733 if fixTrailingSlash && len(path) == end+1 {734 return ciPath735 }720721 if fixTrailingSlash && len(n.children) == 1 {722 // No handle found. Check if a handle for this path + a723 // trailing slash exists724 n = n.children[0]725 if n.path == "/" && n.handlers != nil {726 return append(ciPath, '/')727 }728 }729736 return nil731732 case catchAll:733 return append(ciPath, path...)734735 default:736 panic("invalid node type")737 }738 } else {739 // We should have reached the node containing the handle.740 // Check if this node has a handle registered.738739 if n.handlers != nil {740 return ciPath741 }742745 // No handle found.746 // Try to fix the path by adding a trailing slash747 if fixTrailingSlash {748 for i, c := range []byte(n.indices) {749 if c == '/' {750 n = n.children[i]751 if (len(n.path) == 1 && n.handlers != nil) ||752 (n.nType == catchAll && n.children[0].handlers != nil) {753 return append(ciPath, '/')754 }755 return nil756 }743 if fixTrailingSlash && len(n.children) == 1 {744 // No handle found. Check if a handle for this path + a745 // trailing slash exists746 n = n.children[0]747 if n.path == "/" && n.handlers != nil {748 return append(ciPath, '/')749 }750 }751752 return nil753754 case catchAll:755 return append(ciPath, path...)756757 default:758 panic("invalid node type")759 }760 }761762 // Nothing found.
12 unmodified lines