diff --git a/context.go b/context.go index 525ac3a..3ebf18f 100644 --- a/context.go +++ b/context.go @@ -3,11 +3,8 @@ package pongo2 import ( "errors" "fmt" - "regexp" ) -var reIdentifiers = regexp.MustCompile("^[a-zA-Z0-9_]+$") - var autoescape = true func SetAutoescape(newValue bool) { @@ -30,7 +27,7 @@ type Context map[string]any func (c Context) checkForValidIdentifiers() *Error { for k, v := range c { - if !reIdentifiers.MatchString(k) { + if !isValidIdentifier(k) { return &Error{ Sender: "checkForValidIdentifiers", OrigError: fmt.Errorf("context-key '%s' (value: '%+v') is not a valid identifier", k, v), @@ -40,6 +37,22 @@ func (c Context) checkForValidIdentifiers() *Error { return nil } +func isValidIdentifier(s string) bool { + for i := range s { + if !isValidIdentifierChar(s[i]) { + return false + } + } + return true +} + +func isValidIdentifierChar(c byte) bool { + return (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || + c == '_' +} + // Update updates this context with the key/value-pairs from another context. func (c Context) Update(other Context) Context { for k, v := range other {