]> Cypherpunks repositories - gostls13.git/commitdiff
all: avoid string(i) where i has type int
authorIan Lance Taylor <iant@golang.org>
Tue, 25 Feb 2020 02:35:17 +0000 (18:35 -0800)
committerIan Lance Taylor <iant@golang.org>
Wed, 26 Feb 2020 04:38:19 +0000 (04:38 +0000)
Instead use string(r) where r has type rune.

This is in preparation for a vet warning for string(i).

Updates #32479

Change-Id: Ic205269bba1bd41723950219ecfb67ce17a7aa79
Reviewed-on: https://go-review.googlesource.com/c/go/+/220844
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Akhil Indurti <aindurti@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Toshihiro Shiino <shiino.toshihiro@gmail.com>
12 files changed:
src/bufio/bufio_test.go
src/encoding/xml/xml.go
src/fmt/fmt_test.go
src/fmt/scan.go
src/go/types/conversions.go
src/net/dnsclient_test.go
src/net/rpc/jsonrpc/all_test.go
src/reflect/value.go
src/runtime/pprof/internal/profile/proto.go
src/runtime/string_test.go
src/strconv/quote_test.go
src/strings/strings_test.go

index 9a9f102f151e78ddf4aae24e6cd1c694ffc213f3..4c4522c66042641167f01e2e82f2c5abce3c9036 100644 (file)
@@ -147,7 +147,7 @@ func TestReader(t *testing.T) {
        for i := 0; i < len(texts)-1; i++ {
                texts[i] = str + "\n"
                all += texts[i]
-               str += string(i%26 + 'a')
+               str += string(rune(i)%26 + 'a')
        }
        texts[len(texts)-1] = all
 
index 5e73dcf731660c3da429ec5164d3b97c7e35b59f..adaf4daf198b9364fe1fa8595635df9d06908607 100644 (file)
@@ -960,7 +960,7 @@ func (d *Decoder) ungetc(b byte) {
        d.offset--
 }
 
-var entity = map[string]int{
+var entity = map[string]rune{
        "lt":   '<',
        "gt":   '>',
        "amp":  '&',
@@ -1055,7 +1055,7 @@ Input:
                                        d.buf.WriteByte(';')
                                        n, err := strconv.ParseUint(s, base, 64)
                                        if err == nil && n <= unicode.MaxRune {
-                                               text = string(n)
+                                               text = string(rune(n))
                                                haveText = true
                                        }
                                }
index bbaf40a6199cb7c97604618a3966b2d3ebbaae79..072fc6bf3b4dd8292c667e119b676f0957cc2b84 100644 (file)
@@ -236,10 +236,10 @@ var fmtTests = []struct {
        {"%#q", "\U0010ffff", "`􏿿`"},
        {"%#+q", "\U0010ffff", "`􏿿`"},
        // Runes that are not valid.
-       {"%q", string(0x110000), `"�"`},
-       {"%+q", string(0x110000), `"\ufffd"`},
-       {"%#q", string(0x110000), "`�`"},
-       {"%#+q", string(0x110000), "`�`"},
+       {"%q", string(rune(0x110000)), `"�"`},
+       {"%+q", string(rune(0x110000)), `"\ufffd"`},
+       {"%#q", string(rune(0x110000)), "`�`"},
+       {"%#+q", string(rune(0x110000)), "`�`"},
 
        // characters
        {"%c", uint('x'), "x"},
@@ -1457,7 +1457,7 @@ func (flagPrinter) Format(f State, c rune) {
        s := "%"
        for i := 0; i < 128; i++ {
                if f.Flag(i) {
-                       s += string(i)
+                       s += string(rune(i))
                }
        }
        if w, ok := f.Width(); ok {
index 8cab0180ee85f71fdf437ba7e6b322dac5cdac0d..381577049c0fc9343eaf4b3b29798e6a763d04e3 100644 (file)
@@ -600,13 +600,13 @@ func (s *ss) scanNumber(digits string, haveDigits bool) string {
 // scanRune returns the next rune value in the input.
 func (s *ss) scanRune(bitSize int) int64 {
        s.notEOF()
-       r := int64(s.getRune())
+       r := s.getRune()
        n := uint(bitSize)
-       x := (r << (64 - n)) >> (64 - n)
-       if x != r {
+       x := (int64(r) << (64 - n)) >> (64 - n)
+       if x != int64(r) {
                s.errorString("overflow on character value " + string(r))
        }
-       return r
+       return int64(r)
 }
 
 // scanBasePrefix reports whether the integer begins with a base prefix
index 7ea8fd70aabb2621336836ad2b755bce4cfcbd07..4a6bddb24c1da0d8bfb751e89a6326d6796c8deb 100644 (file)
@@ -28,7 +28,7 @@ func (check *Checker) conversion(x *operand, T Type) {
                        // If codepoint < 0 the absolute value is too large (or unknown) for
                        // conversion. This is the same as converting any other out-of-range
                        // value - let string(codepoint) do the work.
-                       x.val = constant.MakeString(string(codepoint))
+                       x.val = constant.MakeString(string(rune(codepoint)))
                        ok = true
                }
        case x.convertibleTo(check, T):
index 3ab2b836ef60a3795c02b668704c80dfe73e8a0b..f3ed62db36a5c2907f16f9f83325227757130f07 100644 (file)
@@ -42,7 +42,7 @@ func testUniformity(t *testing.T, size int, margin float64) {
        rand.Seed(1)
        data := make([]*SRV, size)
        for i := 0; i < size; i++ {
-               data[i] = &SRV{Target: string('a' + i), Weight: 1}
+               data[i] = &SRV{Target: string('a' + rune(i)), Weight: 1}
        }
        checkDistribution(t, data, margin)
 }
index bbb8eb02918d68ff9049b4536c774ec58126150a..4e73edc70b11bd3745d12196e8155340300bb188 100644 (file)
@@ -127,8 +127,8 @@ func TestServer(t *testing.T) {
                if resp.Error != nil {
                        t.Fatalf("resp.Error: %s", resp.Error)
                }
-               if resp.Id.(string) != string(i) {
-                       t.Fatalf("resp: bad id %q want %q", resp.Id.(string), string(i))
+               if resp.Id.(string) != string(rune(i)) {
+                       t.Fatalf("resp: bad id %q want %q", resp.Id.(string), string(rune(i)))
                }
                if resp.Result.C != 2*i+1 {
                        t.Fatalf("resp: bad result: %d+%d=%d", i, i+1, resp.Result.C)
index 0f5e0836634633999221d71943ceeab8f3ec014a..51e7d195fe0b8d0f92f968c2ecdc4fc3d686bd1c 100644 (file)
@@ -2623,12 +2623,12 @@ func cvtComplex(v Value, t Type) Value {
 
 // convertOp: intXX -> string
 func cvtIntString(v Value, t Type) Value {
-       return makeString(v.flag.ro(), string(v.Int()), t)
+       return makeString(v.flag.ro(), string(rune(v.Int())), t)
 }
 
 // convertOp: uintXX -> string
 func cvtUintString(v Value, t Type) Value {
-       return makeString(v.flag.ro(), string(v.Uint()), t)
+       return makeString(v.flag.ro(), string(rune(v.Uint())), t)
 }
 
 // convertOp: []byte -> string
index 11d7f9ff9b38ed9ecb56c5af5675ba52b06eb5d6..294acfeb92f26ed4fb9009cd938facd39b9cb3bc 100644 (file)
@@ -232,7 +232,7 @@ func decodeField(b *buffer, data []byte) ([]byte, error) {
                b.u64 = uint64(le32(data[:4]))
                data = data[4:]
        default:
-               return nil, errors.New("unknown type: " + string(b.typ))
+               return nil, errors.New("unknown type: " + string(rune(b.typ)))
        }
 
        return data, nil
index 80c5fa6406124ba3ea652712384f86bffd03e20d..b9ac66753333bf20a6e146a1aa44a3d98165076f 100644 (file)
@@ -282,7 +282,7 @@ func TestStringOnStack(t *testing.T) {
 func TestIntString(t *testing.T) {
        // Non-escaping result of intstring.
        s := ""
-       for i := 0; i < 4; i++ {
+       for i := rune(0); i < 4; i++ {
                s += string(i+'0') + string(i+'0'+1)
        }
        if want := "01122334"; s != want {
@@ -291,7 +291,7 @@ func TestIntString(t *testing.T) {
 
        // Escaping result of intstring.
        var a [4]string
-       for i := 0; i < 4; i++ {
+       for i := rune(0); i < 4; i++ {
                a[i] = string(i + '0')
        }
        s = a[0] + a[1] + a[2] + a[3]
index cdc9aafd559e04e8da15762dfdcf02c5b2cecae2..f1faf137bdae4b2ded73983370a3e253f8342d02 100644 (file)
@@ -180,39 +180,39 @@ type canBackquoteTest struct {
 
 var canbackquotetests = []canBackquoteTest{
        {"`", false},
-       {string(0), false},
-       {string(1), false},
-       {string(2), false},
-       {string(3), false},
-       {string(4), false},
-       {string(5), false},
-       {string(6), false},
-       {string(7), false},
-       {string(8), false},
-       {string(9), true}, // \t
-       {string(10), false},
-       {string(11), false},
-       {string(12), false},
-       {string(13), false},
-       {string(14), false},
-       {string(15), false},
-       {string(16), false},
-       {string(17), false},
-       {string(18), false},
-       {string(19), false},
-       {string(20), false},
-       {string(21), false},
-       {string(22), false},
-       {string(23), false},
-       {string(24), false},
-       {string(25), false},
-       {string(26), false},
-       {string(27), false},
-       {string(28), false},
-       {string(29), false},
-       {string(30), false},
-       {string(31), false},
-       {string(0x7F), false},
+       {string(rune(0)), false},
+       {string(rune(1)), false},
+       {string(rune(2)), false},
+       {string(rune(3)), false},
+       {string(rune(4)), false},
+       {string(rune(5)), false},
+       {string(rune(6)), false},
+       {string(rune(7)), false},
+       {string(rune(8)), false},
+       {string(rune(9)), true}, // \t
+       {string(rune(10)), false},
+       {string(rune(11)), false},
+       {string(rune(12)), false},
+       {string(rune(13)), false},
+       {string(rune(14)), false},
+       {string(rune(15)), false},
+       {string(rune(16)), false},
+       {string(rune(17)), false},
+       {string(rune(18)), false},
+       {string(rune(19)), false},
+       {string(rune(20)), false},
+       {string(rune(21)), false},
+       {string(rune(22)), false},
+       {string(rune(23)), false},
+       {string(rune(24)), false},
+       {string(rune(25)), false},
+       {string(rune(26)), false},
+       {string(rune(27)), false},
+       {string(rune(28)), false},
+       {string(rune(29)), false},
+       {string(rune(30)), false},
+       {string(rune(31)), false},
+       {string(rune(0x7F)), false},
        {`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true},
        {`0123456789`, true},
        {`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
index ad14a0574ab2a6962eeb34c1d71120432d8d8d71..984fecfa8d4a382eedd0bf2eb67d23c685ed6c41 100644 (file)
@@ -678,8 +678,8 @@ func TestMap(t *testing.T) {
                }
                return r
        }
-       s := string(utf8.RuneSelf) + string(utf8.MaxRune)
-       r := string(utf8.MaxRune) + string(utf8.RuneSelf) // reverse of s
+       s := string(rune(utf8.RuneSelf)) + string(utf8.MaxRune)
+       r := string(utf8.MaxRune) + string(rune(utf8.RuneSelf)) // reverse of s
        m = Map(encode, s)
        if m != r {
                t.Errorf("encoding not handled correctly: expected %q got %q", r, m)