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) { if !f.needsQuoting(value) {
b.WriteString(value) b.WriteString(value)
} else { } else {
escapedQuote := fmt.Sprintf("\\%s", f.QuoteCharacter) b.WriteString(f.quoteString(value))
escapedValue := strings.Replace(value, f.QuoteCharacter, escapedQuote, -1)
fmt.Fprintf(b, "%s%v%s", f.QuoteCharacter, escapedValue, f.QuoteCharacter)
} }
case error: case error:
errmsg := value.Error() errmsg := value.Error()
if !f.needsQuoting(errmsg) { if !f.needsQuoting(errmsg) {
b.WriteString(errmsg) b.WriteString(errmsg)
} else { } else {
escapedQuote := fmt.Sprintf("\\%s", f.QuoteCharacter) b.WriteString(f.quoteString(errmsg))
escapedErrmsg := strings.Replace(errmsg, f.QuoteCharacter, escapedQuote, -1)
fmt.Fprintf(b, "%s%v%s", f.QuoteCharacter, escapedErrmsg, f.QuoteCharacter)
} }
default: default:
fmt.Fprint(b, value) 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)
}