Extract quoting into separate method

This commit is contained in:
Paul Seiffert 2017-07-10 14:17:59 +02:00
parent 0383f49850
commit 0025402362
No known key found for this signature in database
GPG Key ID: C9F76DC911D6EA29
1 changed files with 9 additions and 8 deletions

View File

@ -174,22 +174,23 @@ func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) {
if !f.needsQuoting(value) {
b.WriteString(value)
} else {
escapedQuote := fmt.Sprintf("\\%s", f.QuoteCharacter)
escapedValue := strings.Replace(value, f.QuoteCharacter, escapedQuote, -1)
fmt.Fprintf(b, "%s%v%s", f.QuoteCharacter, escapedValue, f.QuoteCharacter)
b.WriteString(f.quoteString(value))
}
case error:
errmsg := value.Error()
if !f.needsQuoting(errmsg) {
b.WriteString(errmsg)
} else {
escapedQuote := fmt.Sprintf("\\%s", f.QuoteCharacter)
escapedErrmsg := strings.Replace(errmsg, f.QuoteCharacter, escapedQuote, -1)
fmt.Fprintf(b, "%s%v%s", f.QuoteCharacter, escapedErrmsg, f.QuoteCharacter)
b.WriteString(f.quoteString(errmsg))
}
default:
fmt.Fprint(b, value)
}
}
func (f *TextFormatter) quoteString(v string) string {
escapedQuote := fmt.Sprintf("\\%s", f.QuoteCharacter)
escapedValue := strings.Replace(v, f.QuoteCharacter, escapedQuote, -1)
return fmt.Sprintf("%s%v%s", f.QuoteCharacter, escapedValue, f.QuoteCharacter)
}