From 15b296befc8fa20d93c34f6ca2ba934444d27749 Mon Sep 17 00:00:00 2001 From: Giovanni Bajo Date: Thu, 18 Dec 2014 15:09:01 +0100 Subject: [PATCH] Avoid using regexp --- text_formatter.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index ee34b50..658df9b 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -26,7 +26,6 @@ var ( func init() { baseTimestamp = time.Now() isTerminal = IsTerminal() - noQuoteNeeded, _ = regexp.Compile("^[a-zA-Z0-9.-]*$") } func miniTS() int { @@ -88,16 +87,28 @@ func printColored(b *bytes.Buffer, entry *Entry, keys []string) { } } +func needsQuoting(text string) bool { + for _, ch := range text { + if !((ch >= 'a' && ch <= 'z') || + (ch >= 'A' && ch <= 'Z') || + (ch >= '0' && ch < '9') || + ch == '-' || ch == '.') { + return false + } + } + return true +} + func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key, value interface{}) { switch value.(type) { case string: - if noQuoteNeeded.MatchString(value.(string)) { + if needsQuoting(value.(string)) { fmt.Fprintf(b, "%v=%s ", key, value) } else { fmt.Fprintf(b, "%v=%q ", key, value) } case error: - if noQuoteNeeded.MatchString(value.(error).Error()) { + if needsQuoting(value.(error).Error()) { fmt.Fprintf(b, "%v=%s ", key, value) } else { fmt.Fprintf(b, "%v=%q ", key, value)