From 0025402362434acb5a02999b920080debe8bba4f Mon Sep 17 00:00:00 2001 From: Paul Seiffert Date: Mon, 10 Jul 2017 14:17:59 +0200 Subject: [PATCH] Extract quoting into separate method --- text_formatter.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index 32adbf8..26dcc15 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -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) +}