Skip to content

Commit

Permalink
fmt, strconv: document use of Unicode replacement character in %q
Browse files Browse the repository at this point in the history
Fixes golang#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]>
  • Loading branch information
rsc committed Mar 16, 2022
1 parent 33e752e commit 3efc721
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/fmt/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ For complex numbers, the width and precision apply to the two
components independently and the result is parenthesized, so %f applied
to 1.2+3.4i produces (1.200000+3.400000i).
When formatting a single integer code point or a rune string (type []rune)
with %q, invalid Unicode code points are changed to the Unicode replacement
character, U+FFFD, as in strconv.QuoteRune.
Other flags:
+ always print a sign for numeric values;
guarantee ASCII-only output for %q (%+q)
Expand Down
6 changes: 6 additions & 0 deletions src/strconv/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ func AppendQuoteToGraphic(dst []byte, s string) []byte {
// QuoteRune returns a single-quoted Go character literal representing the
// rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100)
// for control characters and non-printable characters as defined by IsPrint.
// If r is not a valid Unicode code point, it is interpreted as the Unicode
// replacement character U+FFFD.
func QuoteRune(r rune) string {
return quoteRuneWith(r, '\'', false, false)
}
Expand All @@ -179,6 +181,8 @@ func AppendQuoteRune(dst []byte, r rune) []byte {
// the rune. The returned string uses Go escape sequences (\t, \n, \xFF,
// \u0100) for non-ASCII characters and non-printable characters as defined
// by IsPrint.
// If r is not a valid Unicode code point, it is interpreted as the Unicode
// replacement character U+FFFD.
func QuoteRuneToASCII(r rune) string {
return quoteRuneWith(r, '\'', true, false)
}
Expand All @@ -193,6 +197,8 @@ func AppendQuoteRuneToASCII(dst []byte, r rune) []byte {
// the rune. If the rune is not a Unicode graphic character,
// as defined by IsGraphic, the returned string will use a Go escape sequence
// (\t, \n, \xFF, \u0100).
// If r is not a valid Unicode code point, it is interpreted as the Unicode
// replacement character U+FFFD.
func QuoteRuneToGraphic(r rune) string {
return quoteRuneWith(r, '\'', false, true)
}
Expand Down

0 comments on commit 3efc721

Please sign in to comment.