]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: Add support for capital '%X' format verb for scanning
authorThorben Krueger <thorben.krueger@gmail.com>
Wed, 14 Oct 2015 19:18:59 +0000 (19:18 +0000)
committerAndrew Gerrand <adg@golang.org>
Tue, 20 Oct 2015 01:08:54 +0000 (01:08 +0000)
For printing, the format verb '%X' results in a capitalized
hex-representation of the formatted value. Conversely, using
'%X' in a Scanf function should scan a hex-representation
into the given interface{}. The existing implementation
however only supports '%X' for scanning hex values into
integers; strings or byte slices remain empty. On the other
hand, lower-case '%x' supports strings and byte slices just
fine. This is merely an oversight, which this commit fixes.
(Additional tests also included.)

    Fixes #12940

Change-Id: I178a7f615bae950dfc014ca8c0a038448cf0452a
Reviewed-on: https://go-review.googlesource.com/15689
Reviewed-by: Andrew Gerrand <adg@golang.org>
src/fmt/scan.go
src/fmt/scan_test.go

index e3e0fd0b5858d79d84a8888159aa567da7c594ee..6bd8fd52e08c6b3338587690ccafb46f5ff3b8da 100644 (file)
@@ -813,7 +813,7 @@ func (s *ss) scanComplex(verb rune, n int) complex128 {
 // convertString returns the string represented by the next input characters.
 // The format of the input is determined by the verb.
 func (s *ss) convertString(verb rune) (str string) {
-       if !s.okVerb(verb, "svqx", "string") {
+       if !s.okVerb(verb, "svqxX", "string") {
                return ""
        }
        s.skipSpace(false)
@@ -821,7 +821,7 @@ func (s *ss) convertString(verb rune) (str string) {
        switch verb {
        case 'q':
                str = s.quotedString()
-       case 'x':
+       case 'x', 'X':
                str = s.hexString()
        default:
                str = string(s.token(true, notSpace)) // %s and %v just return the next word
index 334c4a6b2428b944f9c71b9904067862f0a72b89..1924c02ac69a873c4dcf0bc2e47ae35ceb5487ad 100644 (file)
@@ -255,12 +255,14 @@ var scanfTests = []ScanfTest{
        // Strings
        {"%s", "using-%s\n", &stringVal, "using-%s"},
        {"%x", "7573696e672d2578\n", &stringVal, "using-%x"},
+       {"%X", "7573696E672D2558\n", &stringVal, "using-%X"},
        {"%q", `"quoted\twith\\do\u0075bl\x65s"` + "\n", &stringVal, "quoted\twith\\doubles"},
        {"%q", "`quoted with backs`\n", &stringVal, "quoted with backs"},
 
        // Byte slices
        {"%s", "bytes-%s\n", &bytesVal, []byte("bytes-%s")},
        {"%x", "62797465732d2578\n", &bytesVal, []byte("bytes-%x")},
+       {"%X", "62797465732D2558\n", &bytesVal, []byte("bytes-%X")},
        {"%q", `"bytes\rwith\vdo\u0075bl\x65s"` + "\n", &bytesVal, []byte("bytes\rwith\vdoubles")},
        {"%q", "`bytes with backs`\n", &bytesVal, []byte("bytes with backs")},