@@ -2,6 +2,7 @@ package tool
22
33import (
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}
0 commit comments