Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"text/tabwriter"
)
Expand Down Expand Up @@ -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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, go compiler's scanner does not accept this type of literal whose number starts with '8' or '9' . So you don't have to care about '8' and '9' .

And current strconv.Quote()'s implementation returns not \000 but \x00 for both "\000" and "\x00" . Maybe L130-L131 are not used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😳 You're right. Sorry!

n = 3
}
p.colorPrint(quoted[pos:pos+n+1], "Magenta")
quoted = quoted[pos+n+1:]
}
p.colorPrint(`"`, "Red")
}

Expand Down
2 changes: 2 additions & 0 deletions printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -116,6 +117,7 @@ var (
FooPri{Public: "hello", private: "world"},
new(regexp.Regexp),
unsafe.Pointer(new(regexp.Regexp)),
"日本\t語\n\000\U00101234a",
}
)

Expand Down