gorilla.go
func gorillaPattern(tpl string, matchHost, prefixMatch, strictSlash bool) (string, error) {
	// Check if it is well-formed.
	idxs, err := braceIndices(tpl)
	if err != nil {
		return "", err
	}
	// Now let's parse it.
	defaultPattern := "[^/]+"
	if matchHost {
		defaultPattern = "[^.]+"
		prefixMatch, strictSlash = false, false
	} else {
		if prefixMatch {
			strictSlash = false
		}
		if strictSlash && strings.HasSuffix(tpl, "/") {
			tpl = tpl[:len(tpl)-1]
		}
	}
	pattern := bytes.NewBufferString("^")
	var end int
	for i := 0; i < len(idxs); i += 2 {
		// Set all values we are interested in.
		raw := tpl[end:idxs[i]]
		end = idxs[i+1]
		parts := strings.SplitN(tpl[idxs[i]+1:end-1], ":", 2)
		name := parts[0]
		patt := defaultPattern
		if len(parts) == 2 {
			patt = parts[1]
		}
		// Name or pattern can't be empty.
		if name == "" || patt == "" {
			return "", fmt.Errorf("missing name or pattern in %q",
				tpl[idxs[i]:end])
		}
		// Build the regexp pattern.
		fmt.Fprintf(pattern, "%s(?P<%s>%s)", regexp.QuoteMeta(raw), name, patt)
	}
	// Add the remaining.
	raw := tpl[end:]
	pattern.WriteString(regexp.QuoteMeta(raw))
	if strictSlash {
		pattern.WriteString("[/]?")
	}
	if !prefixMatch {
		pattern.WriteByte('$')
	}
	return pattern.String(), nil
}
func gorillaPattern(tpl string, matchHost, prefixMatch, strictSlash bool) (string, error) {
	// Check if it is well-formed.
	idxs, err := braceIndices(tpl)
	if err != nil {
		return "", err
	}
	// Now let's parse it.
	defaultPattern := "[^/]+"
	if matchHost {
		defaultPattern = "[^.]+"
		prefixMatch, strictSlash = false, false
	} else {
		if prefixMatch {
			strictSlash = false
		}
		if strictSlash && strings.HasSuffix(tpl, "/") {
			tpl = tpl[:len(tpl)-1]
		}
	}
	pattern := bytes.NewBufferString("^")
	var end int
	for i := 0; i < len(idxs); i += 2 {
		// Set all values we are interested in.
		raw := tpl[end:idxs[i]]
		end = idxs[i+1]
		parts := strings.SplitN(tpl[idxs[i]+1:end-1], ":", 2)
		name := parts[0]
		patt := defaultPattern
		if len(parts) == 2 {
			patt = parts[1]
		}
		// Name or pattern can't be empty.
		if name == "" || patt == "" {
			return "", fmt.Errorf("missing name or pattern in %q",
				tpl[idxs[i]:end])
		}
		// Build the regexp pattern.
		fmt.Fprintf(pattern, "%s(?P<%s>%s)", regexp.QuoteMeta(raw), name, patt)
	}
	// Add the remaining.
	raw := tpl[end:]
	pattern.WriteString(regexp.QuoteMeta(raw))
	if strictSlash {
		pattern.WriteString("[/]?")
	}
	if !prefixMatch {
		pattern.WriteByte('$')
	}
	return pattern.String(), nil
}
func gorillaPattern ( tpl string , matchHost , prefixMatch , strictSlash bool ) ( string , error ) { // Check if it is well-formed. idxs , err := braceIndices ( tpl ) if err != nil { return " " , err } // Now let's parse it. defaultPattern := " " if matchHost { defaultPattern = " " prefixMatch , strictSlash = false , false } else { if prefixMatch { strictSlash = false } if strictSlash && strings . HasSuffix ( tpl , " " ) { tpl = tpl [ : len ( tpl ) - 1 ] } } pattern := bytes . NewBufferString ( " " ) var end int for i := 0 ; i < len ( idxs ) ; i += 2 { // Set all values we are interested in. raw := tpl [ end : idxs [ i ] ] end = idxs [ i + 1 ] parts := strings . SplitN ( tpl [ idxs [ i ] + 1 : end - 1 ] , " " , 2 ) name := parts [ 0 ] patt := defaultPattern if len ( parts ) == 2 { patt = parts [ 1 ] } // Name or pattern can't be empty. if name == " " || patt == " " { return " " , fmt . Errorf ( " " , tpl [ idxs [ i ] : end ] ) } // Build the regexp pattern. fmt . Fprintf ( pattern , " " , regexp . QuoteMeta ( raw ) , name , patt ) } // Add the remaining. raw := tpl [ end : ] pattern . WriteString ( regexp . QuoteMeta ( raw ) ) if strictSlash { pattern . WriteString ( " " ) } if ! prefixMatch { pattern . WriteByte ( '$' ) } return pattern . String ( ) , nil }
// Helpers --------------------------------------------------------------------
// gorillaPattern transforms a gorilla pattern into a regexp pattern.
Helpers -------------------------------------------------------------------- gorillaPattern transforms a gorilla pattern into a regexp pattern .