Commit 74879f0f authored by Sabin Mihai Rapan's avatar Sabin Mihai Rapan Committed by Brad Fitzpatrick

strconv: update Unquote example to be more concise

Changed the example to convey the intent of the Unquote function
in a more succint way.

Fixes #23693

Change-Id: I49465641d730e70b5af0d47057335af39882bcec
Reviewed-on: https://go-review.googlesource.com/92015Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 8e9386db
...@@ -281,27 +281,23 @@ func ExampleQuoteToASCII() { ...@@ -281,27 +281,23 @@ func ExampleQuoteToASCII() {
} }
func ExampleUnquote() { func ExampleUnquote() {
test := func(s string) { s, err := strconv.Unquote("You can't unquote a string without quotes")
t, err := strconv.Unquote(s) fmt.Printf("%q, %v\n", s, err)
if err != nil { s, err = strconv.Unquote("\"The string must be either double-quoted\"")
fmt.Printf("Unquote(%#v): %v\n", s, err) fmt.Printf("%q, %v\n", s, err)
} else { s, err = strconv.Unquote("`or backquoted.`")
fmt.Printf("Unquote(%#v) = %v\n", s, t) fmt.Printf("%q, %v\n", s, err)
} s, err = strconv.Unquote("'\u263a'") // single character only allowed in single quotes
} fmt.Printf("%q, %v\n", s, err)
s, err = strconv.Unquote("'\u2639\u2639'")
s := `\"Fran & Freddie's Diner\t\u263a\"\"` fmt.Printf("%q, %v\n", s, err)
// If the string doesn't have quotes, it can't be unquoted.
test(s) // invalid syntax
test("`" + s + "`")
test(`"` + s + `"`)
test(`'\u263a'`)
// Output: // Output:
// Unquote("\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\""): invalid syntax // "", invalid syntax
// Unquote("`\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\"`") = \"Fran & Freddie's Diner\t\u263a\"\" // "The string must be either double-quoted", <nil>
// Unquote("\"\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\"\"") = "Fran & Freddie's Diner ☺"" // "or backquoted.", <nil>
// Unquote("'\\u263a'") = ☺ // "☺", <nil>
// "", invalid syntax
} }
func ExampleUnquoteChar() { func ExampleUnquoteChar() {
......
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