Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate and extend printf truncation #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion internal/rfmt/fmtsort/sort.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 25 additions & 3 deletions internal/rfmt/format.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 75 additions & 6 deletions internal/rfmt/format.go.diff
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
--- format.go.orig 2021-06-18 16:45:51.065116000 +0200
+++ format.go 2021-06-18 17:30:58.628179000 +0200
@@ -1,3 +1,6 @@
--- format.go.orig 2021-09-03 11:01:24.164634000 +0200
+++ format.go 2021-09-03 11:06:51.632979000 +0200
@@ -1,8 +1,11 @@
+// Code generated from the Go standard library. DO NOT EDIT
+// GENERATED FILE DO NOT EDIT
+//
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
@@ -65,25 +68,17 @@

-package fmt
+package rfmt

import (
"strconv"
@@ -12,6 +15,8 @@
const (
ldigits = "0123456789abcdefx"
udigits = "0123456789ABCDEFX"
+
+ ellipsisS = "…"
)

const (
@@ -29,6 +34,9 @@
space bool
zero bool

+ // ellipsis tells printf to insert an ellipsis after a truncated value.
+ ellipsis bool
+
// For the formats %+v %#v, we set the plusV/sharpV flags
// and clear the plus/sharp flags since %+v and %#v are in effect
// different, flagless formats set at the top level.
@@ -65,25 +73,17 @@
if n <= 0 { // No padding bytes needed.
return
}
Expand Down Expand Up @@ -36,7 +61,46 @@
}

// pad appends b to f.buf, padded on left (!f.minus) or right (f.minus).
@@ -401,19 +396,20 @@
@@ -327,7 +327,11 @@
for i := range s {
n--
if n < 0 {
- return s[:i]
+ res := s[:i]
+ if f.ellipsis {
+ res += ellipsisS
+ }
+ return res
}
}
}
@@ -341,7 +345,11 @@
for i := 0; i < len(b); {
n--
if n < 0 {
- return b[:i]
+ res := b[:i]
+ if f.ellipsis {
+ res = append(res, ellipsisS...)
+ }
+ return res
}
wid := 1
if b[i] >= utf8.RuneSelf {
@@ -373,8 +381,12 @@
length = len(s)
}
// Set length to not process more bytes than the precision demands.
+ addEllipsis := false
if f.precPresent && f.prec < length {
length = f.prec
+ if f.ellipsis {
+ addEllipsis = true
+ }
}
// Compute width of the encoding taking into account the f.sharp and f.space flag.
width := 2 * length
@@ -401,19 +413,20 @@
f.writePadding(f.wid - width)
}
// Write the encoding directly into the output buffer.
Expand All @@ -61,7 +125,7 @@
}
}
if b != nil {
@@ -422,9 +418,9 @@
@@ -422,9 +435,14 @@
c = s[i] // Take a byte from the input string.
}
// Encode each byte as two hexadecimal digits.
Expand All @@ -70,6 +134,11 @@
+ f.buf.WriteByte(digits[c&0xF])
}
- *f.buf = buf
+ // If an ellipsis was requested, add it.
+ if addEllipsis {
+ f.buf.writeString(ellipsisS)
+ }
+
// Handle padding to the right.
if f.widPresent && f.wid > width && f.minus {
f.writePadding(f.wid - width)
2 changes: 2 additions & 0 deletions internal/rfmt/print.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions internal/rfmt/print.go.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- print.go.orig 2021-06-18 16:45:56.623967000 +0200
+++ print.go 2021-06-19 16:26:32.751061000 +0200
--- print.go.orig 2021-09-03 11:01:24.167095000 +0200
+++ print.go 2021-09-03 11:06:51.631705000 +0200
@@ -1,16 +1,27 @@
+// Code generated from print.go.orig. DO NOT EDIT
+// GENERATED FILE DO NOT EDIT
Expand Down Expand Up @@ -471,15 +471,24 @@
end := len(format)
argNum := 0 // we process one argument per non-trivial format
afterIndex := false // previous item in format was an index like [3].
@@ -1147,6 +1265,7 @@
@@ -1011,6 +1129,8 @@
p.fmt.zero = false // Do not pad with zeros to the right.
case ' ':
p.fmt.space = true
+ case '!':
+ p.fmt.ellipsis = true
default:
// Fast path for common case of ascii lower case simple verbs
// without precision or width or argument indices.
@@ -1147,6 +1267,7 @@
}

func (p *pp) doPrint(a []interface{}) {
+ p.buf.SetMode(b.SafeEscaped)
prevString := false
for argNum, arg := range a {
isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String
@@ -1162,6 +1281,7 @@
@@ -1162,6 +1283,7 @@
// doPrintln is like doPrint but always adds a space between arguments
// and a newline after the last argument.
func (p *pp) doPrintln(a []interface{}) {
Expand Down
42 changes: 42 additions & 0 deletions markers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,48 @@ func TestPrinter(t *testing.T) {
{func(w p) { w.Print(buf) }, "safe ‹unsafe›"},
{func(w p) { w.Printf("%v", &buf) }, "safe ‹unsafe›"},
{func(w p) { w.Print(&buf) }, "safe ‹unsafe›"},

// Redaction supports truncation for simple strings like printf.
{func(w p) { w.Printf("tr %.3s %.3s", "hello", Safe("world")) }, `tr ‹hel› wor`},
{func(w p) { w.Printf("tr %.3q %.3q", "hello", Safe("world")) }, `tr ‹"hel"› "wor"`},
{func(w p) { w.Printf("tr %.3v %.3v", "hello", Safe("world")) }, `tr ‹hel› wor`},
{func(w p) { w.Printf("tr %#.3q %#.3q", "hello", Safe("world")) }, "tr ‹`hel`› `wor`"},
{func(w p) { w.Printf("tr %.1T", 123) }, `tr i`},
{func(w p) { w.Printf("tr %.3v", map[string]string{"hello": "world"}) }, `tr map[‹hel›:‹wor›]`},
// Note that the precision affects the amount of input represented, not the size of the output.
// For example, with hexadecimal output.
{func(w p) { w.Printf("tr %.3x", "hello") }, `tr ‹68656c›`},
// Respect UTF-8 boundaries.
{func(w p) { w.Printf("tr %.1v", "☃☀") }, `tr ‹☃›`},
// Byte arrays are either represented as byte values or strings.
{func(w p) { w.Printf("tr %.1v %.1v", []byte("hello"), Safe([]byte("world"))) },
`tr [‹104› ‹101› ‹108› ‹108› ‹111›] [119 111 114 108 100]`},
{func(w p) { w.Printf("tr %.3s %.3s", []byte("hello"), Safe([]byte("world"))) }, `tr ‹hel› wor`},
// Special values do not get truncated.
{func(w p) { w.Printf("tr %.1v", nil) }, `tr <nil>`},
// Simple objects like booleans, integers etc do not get truncated.
{func(w p) { w.Printf("tr %.1v %.1v %.1v", 123, true, 11i) }, `tr ‹123› ‹true› (‹0›‹+1e+01›i)`},
{func(w p) { w.Printf("tr %.1v %.1v %.1v", Safe(123), Safe(true), Safe(11i)) }, `tr 123 true (0+1e+01i)`},
// Redactable strings do not get truncated by precision, because they have internal structure
// that could be broken by truncation.
{func(w p) { w.Printf("tr %.3v", RedactableString("‹hello› world")) }, `tr ‹hello› world`},
// By default, complex objects with a SafeFormat function do not get truncated by precision.
// This is because SafeFormat is itself responsible for implementing truncation by recognizing
// the precision field.
{func(w p) { w.Printf("tr %.3v", &safeNil{}) }, `tr hello ‹world›`},
{func(w p) { w.Printf("tr %.3v", buf) }, "tr safe ‹unsafe›"},

// Additionally, it can report when truncation has occurred.
{func(w p) { w.Printf("tre %!.3v %!.3v", "hello", Safe("world")) }, `tre ‹hel…› wor…`},
{func(w p) { w.Printf("tre %!.3s %!.3s", "hello", Safe("world")) }, `tre ‹hel…› wor…`},
{func(w p) { w.Printf("tre %!.3q %!.3q", "hello", Safe("world")) }, `tre ‹"hel…"› "wor…"`},
{func(w p) { w.Printf("tre %!#.3q %!#.3q", "hello", Safe("world")) }, "tre ‹`hel…`› `wor…`"},
{func(w p) { w.Printf("tre %!.3v", map[string]string{"hello": "world"}) }, `tre map[‹hel…›:‹wor…›]`},
{func(w p) { w.Printf("tre %!.1T", 123) }, `tre i…`},
{func(w p) { w.Printf("tre %!.3s %!.3s", []byte("hello"), Safe([]byte("world"))) }, `tre ‹hel…› wor…`},
{func(w p) { w.Printf("tre %!.3x", "hello") }, `tre ‹68656c…›`},
{func(w p) { w.Printf("tre %!.1v", "☃☀") }, `tre ‹☃…›`},

}

var methods = []struct {
Expand Down