Fix lint issues in util

Reduce execution time of ConvertComponentIdIntoFriendlyPathSearch by
50-60% and add benchmark

Signed-off-by: Nicholas Jackson <nickajacks1@gmail.com>
This commit is contained in:
Nicholas Jackson
2023-10-09 19:14:35 -07:00
committed by quobix
parent 771baafa71
commit def8e997b2
2 changed files with 20 additions and 16 deletions

View File

@@ -565,7 +565,8 @@ func IsHttpVerb(verb string) bool {
}
// define bracket name expression
var bracketNameExp = regexp.MustCompile("^(\\w+)\\[(\\w+)\\]$")
var bracketNameExp = regexp.MustCompile(`^(\w+)\[(\w+)\]$`)
var pathCharExp = regexp.MustCompile(`[%=;~.]`)
func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
segs := strings.Split(id, "/")
@@ -574,8 +575,7 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
// check for strange spaces, chars and if found, wrap them up, clean them and create a new cleaned path.
for i := range segs {
pathCharExp, _ := regexp.MatchString("[%=;~.]", segs[i])
if pathCharExp {
if pathCharExp.Match([]byte(segs[i])) {
segs[i], _ = url.QueryUnescape(strings.ReplaceAll(segs[i], "~1", "/"))
segs[i] = fmt.Sprintf("['%s']", segs[i])
if len(cleaned) > 0 {
@@ -613,11 +613,9 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
_, err := strconv.ParseInt(name, 10, 32)
var replaced string
if err != nil {
replaced = strings.ReplaceAll(fmt.Sprintf("%s",
strings.Join(cleaned, ".")), "#", "$")
replaced = strings.ReplaceAll(strings.Join(cleaned, "."), "#", "$")
} else {
replaced = strings.ReplaceAll(fmt.Sprintf("%s",
strings.Join(cleaned, ".")), "#", "$")
replaced = strings.ReplaceAll(strings.Join(cleaned, "."), "#", "$")
}
if len(replaced) > 0 {