1
- // Copyright 2009 The Go Authors. All rights reserved.
1
+ // Copyright 2020 The Go Authors. All rights reserved.
2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
@@ -33,8 +33,9 @@ func parseComplexComponent(s, orig string, bitSize int) (float64, error) {
33
33
// When bitSize=64, the result still has type complex128, but it will be
34
34
// convertible to complex64 without changing its value.
35
35
//
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.
38
39
//
39
40
// ParseComplex accepts decimal and hexadecimal floating-point number syntax.
40
41
// 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) {
56
57
// ParseComplex recognizes the strings "NaN", "+Inf", and "-Inf" as their
57
58
// respective special floating point values for each component. It ignores case when matching.
58
59
func ParseComplex (s string , bitSize int ) (complex128 , error ) {
59
-
60
- orig := s
61
-
62
60
if len (s ) == 0 {
63
- return 0 , syntaxError (fnParseComplex , orig )
61
+ return 0 , syntaxError (fnParseComplex , s )
64
62
}
63
+ orig := s
65
64
66
- lastChar := s [len (s )- 1 : len ( s ) ]
65
+ endCh := s [len (s )- 1 ]
67
66
68
67
// Remove brackets
69
- if len (s ) > 1 && s [0 : 1 ] == "(" && lastChar == ")" {
68
+ if len (s ) > 1 && s [0 ] == '(' && endCh == ')' {
70
69
s = s [1 : len (s )- 1 ]
71
- lastChar = s [len (s )- 1 : len ( s ) ]
70
+ endCh = s [len (s )- 1 ]
72
71
}
73
72
74
73
// Is last character an i?
75
- if lastChar != "i" {
74
+ if endCh != 'i' {
76
75
// The last character is not an i so there is only a real component.
77
76
real , err := parseComplexComponent (s , orig , bitSize )
78
77
if err != nil {
@@ -85,17 +84,15 @@ func ParseComplex(s string, bitSize int) (complex128, error) {
85
84
s = s [0 : len (s )- 1 ]
86
85
87
86
// 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 )
93
91
}
94
92
}
95
93
96
- if len (pos ) == 0 {
94
+ if len (signPos ) == 0 {
97
95
// There is only an imaginary component
98
-
99
96
if s == "" {
100
97
return complex (0 , 1 ), nil
101
98
}
@@ -105,26 +102,26 @@ func ParseComplex(s string, bitSize int) (complex128, error) {
105
102
return 0 , err
106
103
}
107
104
return complex (0 , imag ), nil
108
-
109
- } else if len (pos ) > 4 {
105
+ } else if len (signPos ) > 4 {
110
106
// Too many ± exists for a valid complex number
111
107
return 0 , syntaxError (fnParseComplex , orig )
112
108
}
113
109
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.
115
112
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 ++ {
119
116
var idx int
120
117
if j % 2 == 0 {
121
118
idx = mid - j / 2
122
119
} else {
123
120
idx = mid + (j / 2 + 1 )
124
121
}
125
122
126
- left := s [0 :pos [idx ]]
127
- right := s [pos [idx ]:]
123
+ left := s [0 :signPos [idx ]]
124
+ right := s [signPos [idx ]:]
128
125
129
126
if left == "" {
130
127
left = left + "0"
@@ -144,7 +141,6 @@ func ParseComplex(s string, bitSize int) (complex128, error) {
144
141
if err != nil {
145
142
continue
146
143
}
147
-
148
144
return complex (real , imag ), nil
149
145
}
150
146
0 commit comments