aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/ast/format.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-02-10 14:45:20 +0100
committerDmitry Vyukov <dvyukov@google.com>2020-02-10 14:45:20 +0100
commit18847f55bb3fe9db41e46a2e9e49a9f7c28143af (patch)
treee181c33c75f5851a5042290ff59dd44cce353c66 /pkg/ast/format.go
parentd0da558cb16f3fb7cf26f20d24ee89eeb49a6f30 (diff)
pkg/ast: introduce hex-encoded string literals
The stringnozescapes does not make sense with filename, also we may need similar escaping for string flags. Handle escaped strings on ast level instead. This avoids introducing new type and works seamleassly with flags. As alternative I've also tried using strconv.Quote/Unquote but it leads to ugly half-escaped strings: "\xb0\x80s\xe8\xd4N\x91\xe3ڒ,\"C\x82D\xbb\x88\\i\xe2i\xc8\xe9\xd85\xb1\x14):M\xdcn" Make hex-encoded strings a separate string format instead.
Diffstat (limited to 'pkg/ast/format.go')
-rw-r--r--pkg/ast/format.go15
1 files changed, 13 insertions, 2 deletions
diff --git a/pkg/ast/format.go b/pkg/ast/format.go
index c3d931706..a2ead06f2 100644
--- a/pkg/ast/format.go
+++ b/pkg/ast/format.go
@@ -50,6 +50,17 @@ func FormatInt(v uint64, format IntFmt) string {
}
}
+func FormatStr(v string, format StrFmt) string {
+ switch format {
+ case StrFmtRaw:
+ return fmt.Sprintf(`"%v"`, v)
+ case StrFmtHex:
+ return fmt.Sprintf("`%x`", v)
+ default:
+ panic(fmt.Sprintf("unknown str format %v", format))
+ }
+}
+
type serializer interface {
serialize(w io.Writer)
}
@@ -153,7 +164,7 @@ func (flags *IntFlags) serialize(w io.Writer) {
func (flags *StrFlags) serialize(w io.Writer) {
fmt.Fprintf(w, "%v = ", flags.Name.Name)
for i, v := range flags.Values {
- fmt.Fprintf(w, "%v\"%v\"", comma(i, ""), v.Value)
+ fmt.Fprintf(w, "%v%v", comma(i, ""), FormatStr(v.Value, v.Fmt))
}
fmt.Fprintf(w, "\n")
}
@@ -172,7 +183,7 @@ func fmtType(t *Type) string {
case t.Ident != "":
v = t.Ident
case t.HasString:
- v = fmt.Sprintf("\"%v\"", t.String)
+ v = FormatStr(t.String, t.StringFmt)
default:
v = FormatInt(t.Value, t.ValueFmt)
}