Skip to content

Commit

Permalink
Use regex to extract label
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-at-luther committed Jan 31, 2024
1 parent 510199b commit 8a8fc4d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lisp/x/profiler/funlabeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"regexp"
"strings"
"unicode"

"github.com/luthersystems/elps/lisp"
"github.com/luthersystems/elps/lisp/lisplib/libhelp"
Expand Down Expand Up @@ -41,20 +40,17 @@ func defaultFunName(runtime *lisp.Runtime, fun *lisp.LVal) string {
const ELPSDocLabel = `@trace{([^}]+)}`

var elpsDocLabelRegExp = regexp.MustCompile(ELPSDocLabel)

var sanitizeRegExp = regexp.MustCompile(`[\W_]+`)
var validLabelRegExp = regexp.MustCompile(`[a-zA-Z_][\w_]*`)

func sanitizeLabel(userLabel string) string {
userLabel = strings.TrimSpace(userLabel)
if userLabel == "" {
return ""
}

// Replace spaces with underscores
userLabel = sanitizeRegExp.ReplaceAllString(userLabel, "_")
// Ensure the label doesn't start with a digit or special character
for len(userLabel) > 0 && !unicode.IsLetter(rune(userLabel[0])) {
userLabel = userLabel[1:]
}

// Eliminate duplicate underscores
parts := strings.Split(userLabel, "_")
Expand All @@ -64,7 +60,16 @@ func sanitizeLabel(userLabel string) string {
nonEmptyParts = append(nonEmptyParts, part)
}
}
return strings.Join(nonEmptyParts, "_")
userLabel = strings.Join(nonEmptyParts, "_")

// Find the first valid label match
matches := validLabelRegExp.FindStringSubmatch(userLabel)
if len(matches) > 0 {
return matches[0]
}

return ""

}

func elpsDocFunLabeler(runtime *lisp.Runtime, fun *lisp.LVal) string {
Expand Down

0 comments on commit 8a8fc4d

Please sign in to comment.