Skip to content

Commit 57a6959

Browse files
committed
fix an error for 'bufio.Scanner: token too long'
Signed-off-by: lifubang <[email protected]>
1 parent 35d9e92 commit 57a6959

File tree

4 files changed

+60
-39
lines changed

4 files changed

+60
-39
lines changed

testdata/step.judger/datav7.out

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

testdata/step.judger/userv7.out

Whitespace-only changes.

tool/differ.go

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tool
22

33
import (
44
"bufio"
5+
"io"
56
"os"
67
"strings"
78
)
@@ -15,54 +16,65 @@ func clearString(str string) string {
1516
return trimStr
1617
}
1718

18-
// DiffOut is to diff the output between user and testcase
19-
func DiffOut(userOut, dataOut string, diffIgnoreHead bool, strictMode bool) (bool, error) {
20-
src, err := os.Open(userOut)
19+
// clearEnter replace all '\r' chars
20+
func clearEnter(str string) string {
21+
trimStr := strings.ReplaceAll(str, "\r", "")
22+
return trimStr
23+
}
24+
25+
func getFileContent(fileName string, diffIgnoreHead bool, strictMode bool) (string, error) {
26+
src, err := os.Open(fileName)
2127
if err != nil {
22-
return false, err
28+
return "", err
2329
}
2430
defer src.Close()
25-
dst, err := os.Open(dataOut)
26-
if err != nil {
27-
return false, err
28-
}
29-
defer dst.Close()
3031

31-
var strSrc, strDest strings.Builder
32+
if stat, err := src.Stat(); err == nil {
33+
if stat.Size() == 0 {
34+
return "", err
35+
}
36+
} else {
37+
return "", err
38+
}
3239

33-
rd := bufio.NewScanner(src)
40+
var strBuilder strings.Builder
41+
rd := bufio.NewReader(src)
3442
srcIgnoreHead := diffIgnoreHead
35-
for rd.Scan() {
36-
str := rd.Text()
37-
if srcIgnoreHead {
38-
srcIgnoreHead = false
39-
} else {
40-
trimStr := str
41-
if !strictMode {
42-
trimStr = clearString(trimStr)
43-
}
44-
if len(trimStr) > 0 {
45-
strSrc.WriteString(trimStr)
43+
for {
44+
str, err := rd.ReadString('\n')
45+
if err == nil || err == io.EOF {
46+
if srcIgnoreHead {
47+
srcIgnoreHead = false
48+
} else {
49+
trimStr := str
50+
if !strictMode {
51+
trimStr = clearString(trimStr)
52+
} else {
53+
trimStr = clearEnter(trimStr)
54+
}
55+
if len(trimStr) > 0 {
56+
strBuilder.WriteString(trimStr)
57+
}
4658
}
4759
}
60+
if err == io.EOF {
61+
break
62+
}
4863
}
64+
return strBuilder.String(), nil
65+
}
4966

50-
rd = bufio.NewScanner(dst)
51-
dstIgnoreHead := diffIgnoreHead
52-
for rd.Scan() {
53-
str := rd.Text()
54-
if dstIgnoreHead {
55-
dstIgnoreHead = false
56-
} else {
57-
trimStr := str
58-
if !strictMode {
59-
trimStr = clearString(trimStr)
60-
}
61-
if len(trimStr) > 0 {
62-
strDest.WriteString(trimStr)
63-
}
64-
}
67+
// DiffOut is to diff the output between user and testcase
68+
func DiffOut(userOut, dataOut string, diffIgnoreHead bool, strictMode bool) (bool, error) {
69+
strSrc, err := getFileContent(userOut, diffIgnoreHead, strictMode)
70+
if err != nil {
71+
return false, err
72+
}
73+
74+
strDst, err := getFileContent(dataOut, diffIgnoreHead, strictMode)
75+
if err != nil {
76+
return false, err
6577
}
6678

67-
return strings.Trim(strSrc.String(), " ") == strings.Trim(strDest.String(), " "), nil
79+
return strings.Trim(strSrc, " ") == strings.Trim(strDst, " "), nil
6880
}

tool/differ_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestDiffOutFunc(t *testing.T) {
7373
dataOut: "datav3.out",
7474
ignoreHead: false,
7575
strictMode: true,
76-
shouldSame: true,
76+
shouldSame: false,
7777
},
7878
{
7979
desc: "(L)Ignore the first line",
@@ -107,6 +107,14 @@ func TestDiffOutFunc(t *testing.T) {
107107
strictMode: false,
108108
shouldSame: true,
109109
},
110+
{
111+
desc: "(L)The size of user.out is zero",
112+
userOut: "userv7.out",
113+
dataOut: "datav7.out",
114+
ignoreHead: false,
115+
strictMode: false,
116+
shouldSame: false,
117+
},
110118
}
111119
for idx, tc := range tests {
112120
t.Logf("%d. %s:\n", idx+1, tc.desc)

0 commit comments

Comments
 (0)