Skip to content

Commit 3efc721

Browse files
committed
fmt, strconv: document use of Unicode replacement character in %q
Fixes #51526. Change-Id: I365a763454bd201f804df29f800416b1731b8ebc Reviewed-on: https://go-review.googlesource.com/c/go/+/390436 Trust: Russ Cox <[email protected]> Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Rob Pike <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 33e752e commit 3efc721

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/fmt/doc.go

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ For complex numbers, the width and precision apply to the two
110110
components independently and the result is parenthesized, so %f applied
111111
to 1.2+3.4i produces (1.200000+3.400000i).
112112
113+
When formatting a single integer code point or a rune string (type []rune)
114+
with %q, invalid Unicode code points are changed to the Unicode replacement
115+
character, U+FFFD, as in strconv.QuoteRune.
116+
113117
Other flags:
114118
+ always print a sign for numeric values;
115119
guarantee ASCII-only output for %q (%+q)

src/strconv/quote.go

+6
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ func AppendQuoteToGraphic(dst []byte, s string) []byte {
165165
// QuoteRune returns a single-quoted Go character literal representing the
166166
// rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100)
167167
// for control characters and non-printable characters as defined by IsPrint.
168+
// If r is not a valid Unicode code point, it is interpreted as the Unicode
169+
// replacement character U+FFFD.
168170
func QuoteRune(r rune) string {
169171
return quoteRuneWith(r, '\'', false, false)
170172
}
@@ -179,6 +181,8 @@ func AppendQuoteRune(dst []byte, r rune) []byte {
179181
// the rune. The returned string uses Go escape sequences (\t, \n, \xFF,
180182
// \u0100) for non-ASCII characters and non-printable characters as defined
181183
// by IsPrint.
184+
// If r is not a valid Unicode code point, it is interpreted as the Unicode
185+
// replacement character U+FFFD.
182186
func QuoteRuneToASCII(r rune) string {
183187
return quoteRuneWith(r, '\'', true, false)
184188
}
@@ -193,6 +197,8 @@ func AppendQuoteRuneToASCII(dst []byte, r rune) []byte {
193197
// the rune. If the rune is not a Unicode graphic character,
194198
// as defined by IsGraphic, the returned string will use a Go escape sequence
195199
// (\t, \n, \xFF, \u0100).
200+
// If r is not a valid Unicode code point, it is interpreted as the Unicode
201+
// replacement character U+FFFD.
196202
func QuoteRuneToGraphic(r rune) string {
197203
return quoteRuneWith(r, '\'', false, true)
198204
}

0 commit comments

Comments
 (0)