// ReplaceAllStringSubmatchFunc is a helper function to replace regexp sub matches.
// based on https://gist.github.com/slimsag/14c66b88633bd52b7fa710349e4c6749 (MIT)
// Note: slice `in` is reused internally, make a copy if you need to keep it, ex:
// 	cp := append([]string(nil), in...)
// example:
//	re := regexp.MustCompile(`([:*].*?)(?:/|$)`)
//	ReplaceAllStringSubmatchFunc(re, "/path/:id/:name", func(in []string) []string {
//		for i, s := range in {
//			in[i] = "my-" + s[1:]
//		}
//		return in
//	}, -1) === "/path/my-id/my-name"
ReplaceAllStringSubmatchFunc is a helper function to replace regexp sub matches.
// ReplaceAllStringSubmatchFunc is a helper function to replace regexp sub matches . // based on https : // gist . github . com / slimsag / 14c66b88633bd52b7fa710349e4c6749 ( MIT ) // Note : slice ` in ` is reused internally make a copy if you need to keep it ex : // cp : = append ( [] string ( nil ) in ... ) // example : // re : = regexp . MustCompile ( ` ( [ : * ] . * ? ) ( ? : / |$ ) ` ) // ReplaceAllStringSubmatchFunc ( re " / path / : id / : name " func ( in [] string ) [] string { // for i s : = range in { // in [ i ] = " my - " + s [ 1 : ] // } // return in // } - 1 ) === " / path / my - id / my - name "
func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, src string, repl func([]string) []string, n int) string {
	var (
		matches = re.FindAllStringSubmatchIndex(src, n)
		res               strings.Builder
		groups            []string
		groupIndices      [][2]int
		gi                [2]int
		start, end, last  int
		lastGroup, gs, ge int
	)
	res.Grow(len(src))
	for _, match := range matches {
		start, end = match[0], match[1]
		res.WriteString(src[last:start])
		last = end
		groups, groupIndices = groups[:0], groupIndices[:0]
		for i := 2; i < len(match); i += 2 {
			start := match[i]
			end := match[i+1]
			if start < 0 || end < 0 {
				continue
			}
			groups = append(groups, src[start:end])
			groupIndices = append(groupIndices, [2]int{start, end})
		}
		groups = repl(groups)
		lastGroup = start
		for i := range groups {
			gi = groupIndices[i]
			gs, ge = gi[0], gi[1]
			res.WriteString(src[lastGroup:gs])
			lastGroup = ge
			res.WriteString(groups[i])
		}
		res.WriteString(src[lastGroup:end])
	}
	res.WriteString(src[last:])
	return res.String()
}
func ReplaceAllStringSubmatchFunc ( re * regexp . Regexp , src string , repl func ( [ ] string ) [ ] string , n int ) string { var ( matches = re . FindAllStringSubmatchIndex ( src , n ) res strings . Builder groups [ ] string groupIndices [ ] [ 2 ] int gi [ 2 ] int start , end , last int lastGroup , gs , ge int ) res . Grow ( len ( src ) ) for _ , match := range matches { start , end = match [ 0 ] , match [ 1 ] res . WriteString ( src [ last : start ] ) last = end groups , groupIndices = groups [ : 0 ] , groupIndices [ : 0 ] for i := 2 ; i < len ( match ) ; i += 2 { start := match [ i ] end := match [ i + 1 ] if start < 0 || end < 0 { continue } groups = append ( groups , src [ start : end ] ) groupIndices = append ( groupIndices , [ 2 ] int { start , end } ) } groups = repl ( groups ) lastGroup = start for i := range groups { gi = groupIndices [ i ] gs , ge = gi [ 0 ] , gi [ 1 ] res . WriteString ( src [ lastGroup : gs ] ) lastGroup = ge res . WriteString ( groups [ i ] ) } res . WriteString ( src [ lastGroup : end ] ) } res . WriteString ( src [ last : ] ) return res . String ( ) }
ReplaceAllStringSubmatchFunc is a helper function to replace regexp sub matches.
ReplaceAllStringSubmatchFunc is a helper function to replace regexp sub matches .
// Determine the groups / submatch bytes and indices. // Append match data. // Append the new group value.