Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
029bbe18
Commit
029bbe18
authored
Feb 02, 2011
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fmt.Scan: scan binary-exponent floating format, 2.4p-3
R=rsc, rog, r2 CC=golang-dev
https://golang.org/cl/4128049
parent
bc874ec0
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
2 deletions
+28
-2
src/pkg/fmt/scan.go
src/pkg/fmt/scan.go
+24
-2
src/pkg/fmt/scan_test.go
src/pkg/fmt/scan_test.go
+4
-0
No files found.
src/pkg/fmt/scan.go
View file @
029bbe18
...
...
@@ -7,6 +7,7 @@ package fmt
import
(
"bytes"
"io"
"math"
"os"
"reflect"
"strconv"
...
...
@@ -459,7 +460,7 @@ const (
hexadecimalDigits
=
"0123456789aAbBcCdDeEfF"
sign
=
"+-"
period
=
"."
exponent
=
"eE"
exponent
=
"eE
p
"
)
// getBase returns the numeric base represented by the verb and its digit string.
...
...
@@ -617,6 +618,27 @@ func (s *ss) complexTokens() (real, imag string) {
// convertFloat converts the string to a float64value.
func
(
s
*
ss
)
convertFloat
(
str
string
,
n
int
)
float64
{
if
p
:=
strings
.
Index
(
str
,
"p"
);
p
>=
0
{
// Atof doesn't handle power-of-2 exponents,
// but they're easy to evaluate.
f
,
err
:=
strconv
.
AtofN
(
str
[
:
p
],
n
)
if
err
!=
nil
{
// Put full string into error.
if
e
,
ok
:=
err
.
(
*
strconv
.
NumError
);
ok
{
e
.
Num
=
str
}
s
.
error
(
err
)
}
n
,
err
:=
strconv
.
Atoi
(
str
[
p
+
1
:
])
if
err
!=
nil
{
// Put full string into error.
if
e
,
ok
:=
err
.
(
*
strconv
.
NumError
);
ok
{
e
.
Num
=
str
}
s
.
error
(
err
)
}
return
math
.
Ldexp
(
f
,
n
)
}
f
,
err
:=
strconv
.
AtofN
(
str
,
n
)
if
err
!=
nil
{
s
.
error
(
err
)
...
...
@@ -747,7 +769,7 @@ func (s *ss) hexString() string {
return
s
.
buf
.
String
()
}
const
floatVerbs
=
"eEfFgGv"
const
floatVerbs
=
"
b
eEfFgGv"
// scanOne scans a single value, deriving the scanner from the type of the argument.
func
(
s
*
ss
)
scanOne
(
verb
int
,
field
interface
{})
{
...
...
src/pkg/fmt/scan_test.go
View file @
029bbe18
...
...
@@ -160,6 +160,10 @@ var scanTests = []ScanTest{
{
"2.3
\n
"
,
&
float64Val
,
2.3
},
{
"2.3e1
\n
"
,
&
float32Val
,
float32
(
2.3e1
)},
{
"2.3e2
\n
"
,
&
float64Val
,
2.3e2
},
{
"2.3p2
\n
"
,
&
float64Val
,
2.3
*
4
},
{
"2.3p+2
\n
"
,
&
float64Val
,
2.3
*
4
},
{
"2.3p+66
\n
"
,
&
float64Val
,
2.3
*
(
1
<<
32
)
*
(
1
<<
32
)
*
4
},
{
"2.3p-66
\n
"
,
&
float64Val
,
2.3
/
((
1
<<
32
)
*
(
1
<<
32
)
*
4
)},
{
"2.35
\n
"
,
&
stringVal
,
"2.35"
},
{
"2345678
\n
"
,
&
bytesVal
,
[]
byte
(
"2345678"
)},
{
"(3.4e1-2i)
\n
"
,
&
complex128Val
,
3.4e1
-
2i
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment