Commit 4692711d authored by Shenghou Ma's avatar Shenghou Ma

strconv, fmt: clarify behavior of CanBackquote and "%#q".

Fixes #4858.

R=golang-dev, bradfitz, r, rsc
CC=golang-dev
https://golang.org/cl/7387044
parent 89cf67eb
...@@ -74,7 +74,8 @@ ...@@ -74,7 +74,8 @@
- pad with spaces on the right rather than the left (left-justify the field) - pad with spaces on the right rather than the left (left-justify the field)
# alternate format: add leading 0 for octal (%#o), 0x for hex (%#x); # alternate format: add leading 0 for octal (%#o), 0x for hex (%#x);
0X for hex (%#X); suppress 0x for %p (%#p); 0X for hex (%#X); suppress 0x for %p (%#p);
print a raw (backquoted) string if possible for %q (%#q); for %q, print a raw (backquoted) string if strconv.CanBackquote
returns true;
write e.g. U+0078 'x' if the character is printable for %U (%#U). write e.g. U+0078 'x' if the character is printable for %U (%#U).
' ' (space) leave a space for elided sign in numbers (% d); ' ' (space) leave a space for elided sign in numbers (% d);
put spaces between bytes printing strings or slices in hex (% x, % X) put spaces between bytes printing strings or slices in hex (% x, % X)
......
...@@ -139,8 +139,9 @@ func AppendQuoteRuneToASCII(dst []byte, r rune) []byte { ...@@ -139,8 +139,9 @@ func AppendQuoteRuneToASCII(dst []byte, r rune) []byte {
return append(dst, QuoteRuneToASCII(r)...) return append(dst, QuoteRuneToASCII(r)...)
} }
// CanBackquote returns whether the string s would be // CanBackquote reports whether the string s can be represented
// a valid Go string literal if enclosed in backquotes. // unchanged as a single-line backquoted string without control
// characters other than space and tab.
func CanBackquote(s string) bool { func CanBackquote(s string) bool {
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
if (s[i] < ' ' && s[i] != '\t') || s[i] == '`' { if (s[i] < ' ' && s[i] != '\t') || s[i] == '`' {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment