Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
18063d46
Commit
18063d46
authored
Aug 07, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fmt.Scan: empty strings are errors
Fixes #1002. R=rsc CC=golang-dev
https://golang.org/cl/1882046
parent
df88fc61
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
4 deletions
+50
-4
scan.go
src/pkg/fmt/scan.go
+10
-4
scan_test.go
src/pkg/fmt/scan_test.go
+40
-0
No files found.
src/pkg/fmt/scan.go
View file @
18063d46
...
...
@@ -598,18 +598,24 @@ func (s *ss) scanComplex(verb int, n int) complex128 {
// convertString returns the string represented by the next input characters.
// The format of the input is determined by the verb.
func
(
s
*
ss
)
convertString
(
verb
int
)
string
{
func
(
s
*
ss
)
convertString
(
verb
int
)
(
str
string
)
{
if
!
s
.
okVerb
(
verb
,
"svqx"
,
"string"
)
{
return
""
}
s
.
skipSpace
(
false
)
switch
verb
{
case
'q'
:
return
s
.
quotedString
()
str
=
s
.
quotedString
()
case
'x'
:
return
s
.
hexString
()
str
=
s
.
hexString
()
default
:
str
=
s
.
token
()
// %s and %v just return the next word
}
// Empty strings other than with %q are not OK.
if
len
(
str
)
==
0
&&
verb
!=
'q'
&&
s
.
maxWid
>
0
{
s
.
errorString
(
"Scan: no data for string"
)
}
return
s
.
token
()
// %s and %v just return the next word
return
}
// quotedString returns the double- or back-quoted string represented by the next input characters.
...
...
src/pkg/fmt/scan_test.go
View file @
18063d46
...
...
@@ -464,6 +464,46 @@ func TestScanMultiple(t *testing.T) {
if
a
!=
123
||
s
!=
"abc"
{
t
.
Errorf
(
"Sscan wrong values: got (%d %q) expected (123
\"
abc
\"
)"
,
a
,
s
)
}
n
,
err
=
Sscan
(
"asdf"
,
&
s
,
&
a
)
if
n
!=
1
{
t
.
Errorf
(
"Sscan count error: expected 1: got %d"
,
n
)
}
if
err
==
nil
{
t
.
Errorf
(
"Sscan expected error; got none"
,
err
)
}
if
s
!=
"asdf"
{
t
.
Errorf
(
"Sscan wrong values: got %q expected
\"
asdf
\"
"
,
s
)
}
}
// Empty strings are not valid input when scanning a string.
func
TestScanEmpty
(
t
*
testing
.
T
)
{
var
s1
,
s2
string
n
,
err
:=
Sscan
(
"abc"
,
&
s1
,
&
s2
)
if
n
!=
1
{
t
.
Errorf
(
"Sscan count error: expected 1: got %d"
,
n
)
}
if
err
==
nil
{
t
.
Errorf
(
"Sscan <one item> expected error; got none"
)
}
if
s1
!=
"abc"
{
t
.
Errorf
(
"Sscan wrong values: got %q expected
\"
abc
\"
"
,
s1
)
}
n
,
err
=
Sscan
(
""
,
&
s1
,
&
s2
)
if
n
!=
0
{
t
.
Errorf
(
"Sscan count error: expected 0: got %d"
,
n
)
}
if
err
==
nil
{
t
.
Errorf
(
"Sscan <empty> expected error; got none"
)
}
// Quoted empty string is OK.
n
,
err
=
Sscanf
(
`""`
,
"%q"
,
&
s1
)
if
n
!=
1
{
t
.
Errorf
(
"Sscanf count error: expected 1: got %d"
,
n
)
}
if
err
!=
nil
{
t
.
Errorf
(
"Sscanf <empty> expected no error with quoted string; got %s"
,
err
)
}
}
func
TestScanNotPointer
(
t
*
testing
.
T
)
{
...
...
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