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
3748d226
Commit
3748d226
authored
Jun 18, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fmt.Scanf: improve error message when input does not match format
R=rsc CC=golang-dev
https://golang.org/cl/1693043
parent
4fd7880d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
7 additions
and
4 deletions
+7
-4
src/pkg/fmt/scan.go
src/pkg/fmt/scan.go
+6
-4
src/pkg/fmt/scan_test.go
src/pkg/fmt/scan_test.go
+1
-0
No files found.
src/pkg/fmt/scan.go
View file @
3748d226
...
...
@@ -885,6 +885,7 @@ func (s *ss) doScan(a []interface{}) (numProcessed int, err os.Error) {
// either input or format behave as a single space. This routine also
// handles the %% case. If the return value is zero, either format
// starts with a % (with no following %) or the input is empty.
// If it is negative, the input did not match the string.
func
(
s
*
ss
)
advance
(
format
string
)
(
i
int
)
{
for
i
<
len
(
format
)
{
fmtc
,
w
:=
utf8
.
DecodeRuneInString
(
format
[
i
:
])
...
...
@@ -919,7 +920,7 @@ func (s *ss) advance(format string) (i int) {
inputc
:=
s
.
mustGetRune
()
if
fmtc
!=
inputc
{
s
.
UngetRune
(
inputc
)
return
return
-
1
}
i
+=
w
}
...
...
@@ -940,10 +941,11 @@ func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err os.E
}
// Either we failed to advance, we have a percent character, or we ran out of input.
if
format
[
i
]
!=
'%'
{
// Can't advance format.
Do we have arguments still to process
?
if
i
<
len
(
a
)
{
s
.
errorString
(
"
too many arguments for
format"
)
// Can't advance format.
Why not
?
if
w
<
0
{
s
.
errorString
(
"
input does not match
format"
)
}
// Otherwise at EOF; "too many operands" error handled below
break
}
i
++
// % is one byte
...
...
src/pkg/fmt/scan_test.go
View file @
3748d226
...
...
@@ -303,6 +303,7 @@ var multiTests = []ScanfMultiTest{
ScanfMultiTest
{
"%d %d %d"
,
"23 18"
,
args
(
&
i
,
&
j
),
args
(
23
,
18
),
"too few operands"
},
ScanfMultiTest
{
"%d %d"
,
"23 18 27"
,
args
(
&
i
,
&
j
,
&
k
),
args
(
23
,
18
),
"too many operands"
},
ScanfMultiTest
{
"%c"
,
"
\u0100
"
,
args
(
&
int8Val
),
nil
,
"overflow"
},
ScanfMultiTest
{
"X%d"
,
"10X"
,
args
(
&
intVal
),
nil
,
"input does not match format"
},
// Bad UTF-8: should see every byte.
ScanfMultiTest
{
"%c%c%c"
,
"
\xc2
X
\xc2
"
,
args
(
&
i
,
&
j
,
&
k
),
args
(
utf8
.
RuneError
,
'X'
,
utf8
.
RuneError
),
""
},
...
...
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