// Copyright ©2020 The Gonum Authors. All rights reserved. // Use of this code is governed by a BSD-style // license that can be found in the LICENSE file package cscalar import ( "fmt" "math/cmplx" "strconv" "strings" ) // parse converts the string s to a complex128. The string may be parenthesized and // has the format [±]N±Ni. The order of the components is not strict. func parse(s string) (complex128, error) { if len(s) == 0 { return 0, parseError{state: -1} } orig := s wantClose := s[0] == '(' if wantClose { if s[len(s)-1] != ')' { return 0, parseError{string: orig, state: -1} } s = s[1 : len(s)-1] } if len(s) == 0 { return 0, parseError{string: orig, state: -1} } switch s[0] { case 'n', 'N': if strings.ToLower(s) == "nan" { return cmplx.NaN(), nil } case 'i', 'I': if strings.ToLower(s) == "inf" { return cmplx.Inf(), nil } } var q complex128 var parts byte for i := 0; i < 4; i++ { beg, end, p, err := floatPart(s) if err != nil { return q, parseError{string: orig, state: -1} } if parts&(1<