Skip to content

Commit d0e2ade

Browse files
committed
- amend based on review comments
1 parent 4ec856a commit d0e2ade

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

src/strconv/atoc.go

+23-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2009 The Go Authors. All rights reserved.
1+
// Copyright 2020 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

@@ -33,8 +33,9 @@ func parseComplexComponent(s, orig string, bitSize int) (float64, error) {
3333
// When bitSize=64, the result still has type complex128, but it will be
3434
// convertible to complex64 without changing its value.
3535
//
36-
// The number represented by s may or may not be parenthesized and have the format (N+Ni) where N is
37-
// a floating-point number. There must not be spaces between the real and imaginary components.
36+
// The number represented by s may or may not be parenthesized and have the format
37+
// (N+Ni) where N is a floating-point number. There must not be spaces between the real
38+
// and imaginary components.
3839
//
3940
// ParseComplex accepts decimal and hexadecimal floating-point number syntax.
4041
// If s is well-formed and near a valid floating-point number,
@@ -56,23 +57,21 @@ func parseComplexComponent(s, orig string, bitSize int) (float64, error) {
5657
// ParseComplex recognizes the strings "NaN", "+Inf", and "-Inf" as their
5758
// respective special floating point values for each component. It ignores case when matching.
5859
func ParseComplex(s string, bitSize int) (complex128, error) {
59-
60-
orig := s
61-
6260
if len(s) == 0 {
63-
return 0, syntaxError(fnParseComplex, orig)
61+
return 0, syntaxError(fnParseComplex, s)
6462
}
63+
orig := s
6564

66-
lastChar := s[len(s)-1 : len(s)]
65+
endCh := s[len(s)-1]
6766

6867
// Remove brackets
69-
if len(s) > 1 && s[0:1] == "(" && lastChar == ")" {
68+
if len(s) > 1 && s[0] == '(' && endCh == ')' {
7069
s = s[1 : len(s)-1]
71-
lastChar = s[len(s)-1 : len(s)]
70+
endCh = s[len(s)-1]
7271
}
7372

7473
// Is last character an i?
75-
if lastChar != "i" {
74+
if endCh != 'i' {
7675
// The last character is not an i so there is only a real component.
7776
real, err := parseComplexComponent(s, orig, bitSize)
7877
if err != nil {
@@ -85,17 +84,15 @@ func ParseComplex(s string, bitSize int) (complex128, error) {
8584
s = s[0 : len(s)-1]
8685

8786
// Count how many ± exist.
88-
pos := []int{}
89-
90-
for idx, rune := range s {
91-
if rune == '+' || rune == '-' {
92-
pos = append(pos, idx)
87+
signPos := []int{}
88+
for i, ch := range s {
89+
if ch == '+' || ch == '-' {
90+
signPos = append(signPos, i)
9391
}
9492
}
9593

96-
if len(pos) == 0 {
94+
if len(signPos) == 0 {
9795
// There is only an imaginary component
98-
9996
if s == "" {
10097
return complex(0, 1), nil
10198
}
@@ -105,26 +102,26 @@ func ParseComplex(s string, bitSize int) (complex128, error) {
105102
return 0, err
106103
}
107104
return complex(0, imag), nil
108-
109-
} else if len(pos) > 4 {
105+
} else if len(signPos) > 4 {
110106
// Too many ± exists for a valid complex number
111107
return 0, syntaxError(fnParseComplex, orig)
112108
}
113109

114-
/* From here onwards, it is either a complex number with both a real and imaginary component OR a pure imaginary number in exponential form. */
110+
// From here onwards, it is either a complex number with both a real and imaginary component
111+
// XOR a pure imaginary number in exponential form.
115112

116-
// Loop through pos from middle of slice, outwards
117-
mid := (len(pos) - 1) >> 1
118-
for j := 0; j < len(pos); j++ {
113+
// Loop through signPos from middle of slice, outwards
114+
mid := (len(signPos) - 1) >> 1
115+
for j := 0; j < len(signPos); j++ {
119116
var idx int
120117
if j%2 == 0 {
121118
idx = mid - j/2
122119
} else {
123120
idx = mid + (j/2 + 1)
124121
}
125122

126-
left := s[0:pos[idx]]
127-
right := s[pos[idx]:]
123+
left := s[0:signPos[idx]]
124+
right := s[signPos[idx]:]
128125

129126
if left == "" {
130127
left = left + "0"
@@ -144,7 +141,6 @@ func ParseComplex(s string, bitSize int) (complex128, error) {
144141
if err != nil {
145142
continue
146143
}
147-
148144
return complex(real, imag), nil
149145
}
150146

0 commit comments

Comments
 (0)