diff --git a/printer.go b/printer.go index cc5639c..abc88bd 100644 --- a/printer.go +++ b/printer.go @@ -5,6 +5,7 @@ import ( "fmt" "reflect" "regexp" + "strconv" "strings" "text/tabwriter" ) @@ -104,8 +105,34 @@ func (p *printer) colorPrint(text, color string) { } func (p *printer) printString() { + quoted := strconv.Quote(p.value.String()) + quoted = quoted[1 : len(quoted)-1] + p.colorPrint(`"`, "Red") - p.colorPrint(p.value.String(), "red") + for len(quoted) > 0 { + pos := strings.IndexByte(quoted, '\\') + if pos == -1 { + p.colorPrint(quoted, "red") + break + } + if pos != 0 { + p.colorPrint(quoted[0:pos], "red") + } + + n := 1 + switch quoted[pos+1] { + case 'x': // "\x00" + n = 3 + case 'u': // "\u0000" + n = 5 + case 'U': // "\U00000000" + n = 9 + case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': // "\000" + n = 3 + } + p.colorPrint(quoted[pos:pos+n+1], "Magenta") + quoted = quoted[pos+n+1:] + } p.colorPrint(`"`, "Red") } diff --git a/printer_test.go b/printer_test.go index 1816441..e42e5e1 100644 --- a/printer_test.go +++ b/printer_test.go @@ -97,6 +97,7 @@ var ( } `, }, + {"日本\t語\x00", `[red][bold]"[reset][red]日本[reset][magenta][bold]\t[reset][red]語[reset][magenta][bold]\x00[reset][red][bold]"`}, } arr [3]int @@ -116,6 +117,7 @@ var ( FooPri{Public: "hello", private: "world"}, new(regexp.Regexp), unsafe.Pointer(new(regexp.Regexp)), + "日本\t語\n\000\U00101234a", } )