[go] fmt: document use of Unicode replacement character in %q

148 views
Skip to first unread message

Russ Cox (Gerrit)

unread,
Mar 16, 2022, 12:28:42 PM3/16/22
to Russ Cox, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Gopher Robot, Rob Pike, golang-co...@googlegroups.com

Russ Cox submitted this change.

View Change



2 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:

```
The name of the file: src/strconv/quote.go
Insertions: 6, Deletions: 6.

@@ -165,8 +165,8 @@
// 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.
+// 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)
}
@@ -181,8 +181,8 @@
// 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.
+// 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)
}
@@ -197,8 +197,8 @@
// 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.
+// 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)
}
```

Approvals: Rob Pike: Looks good to me, approved Russ Cox: Trusted; Run TryBots Gopher Robot: TryBots succeeded
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 <r...@golang.org>
Run-TryBot: Russ Cox <r...@golang.org>
Reviewed-by: Rob Pike <r...@golang.org>
TryBot-Result: Gopher Robot <go...@golang.org>
---
M src/fmt/doc.go
M src/strconv/quote.go
2 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/src/fmt/doc.go b/src/fmt/doc.go
index f14a7a7..6b49ded 100644
--- a/src/fmt/doc.go
+++ b/src/fmt/doc.go
@@ -110,6 +110,10 @@
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)
diff --git a/src/strconv/quote.go b/src/strconv/quote.go
index d2814b9..9d20b75 100644
--- a/src/strconv/quote.go
+++ b/src/strconv/quote.go
@@ -165,6 +165,8 @@
// 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)
}
@@ -179,6 +181,8 @@
// 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)
}
@@ -193,6 +197,8 @@
// 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)
}

To view, visit change 390436. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I365a763454bd201f804df29f800416b1731b8ebc
Gerrit-Change-Number: 390436
Gerrit-PatchSet: 4
Gerrit-Owner: Russ Cox <r...@golang.org>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Rob Pike <r...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-MessageType: merged
Reply all
Reply to author
Forward
0 new messages