MCPcopy
hub / github.com/go-chi/chi / patNextSegment

Function patNextSegment

tree.go:687–753  ·  view source on GitHub ↗

patNextSegment returns the next segment details from a pattern: node type, param key, regexp string, param tail byte, param starting index, param ending index

(pattern string)

Source from the content-addressed store, hash-verified

685// patNextSegment returns the next segment details from a pattern:
686// node type, param key, regexp string, param tail byte, param starting index, param ending index
687func patNextSegment(pattern string) (nodeTyp, string, string, byte, int, int) {
688 ps := strings.Index(pattern, "{")
689 ws := strings.Index(pattern, "*")
690
691 if ps < 0 && ws < 0 {
692 return ntStatic, "", "", 0, 0, len(pattern) // we return the entire thing
693 }
694
695 // Sanity check
696 if ps >= 0 && ws >= 0 && ws < ps {
697 panic("chi: wildcard '*' must be the last pattern in a route, otherwise use a '{param}'")
698 }
699
700 var tail byte = '/' // Default endpoint tail to / byte
701
702 if ps >= 0 {
703 // Param/Regexp pattern is next
704 nt := ntParam
705
706 // Read to closing } taking into account opens and closes in curl count (cc)
707 cc := 0
708 pe := ps
709 for i, c := range pattern[ps:] {
710 if c == '{' {
711 cc++
712 } else if c == '}' {
713 cc--
714 if cc == 0 {
715 pe = ps + i
716 break
717 }
718 }
719 }
720 if pe == ps {
721 panic("chi: route param closing delimiter '}' is missing")
722 }
723
724 key := pattern[ps+1 : pe]
725 pe++ // set end to next position
726
727 if pe < len(pattern) {
728 tail = pattern[pe]
729 }
730
731 key, rexpat, isRegexp := strings.Cut(key, ":")
732 if isRegexp {
733 nt = ntRegexp
734 }
735
736 if len(rexpat) > 0 {
737 if rexpat[0] != '^' {
738 rexpat = "^" + rexpat
739 }
740 if rexpat[len(rexpat)-1] != '$' {
741 rexpat += "$"
742 }
743 }
744

Callers 3

InsertRouteMethod · 0.85
addChildMethod · 0.85
patParamKeysFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected