Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix parsing bug affected by fmt (#1398)
* fix parsing bug affected by fmt
- Loading branch information
Showing
7 changed files
with
185 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package swag | ||
|
||
import "unicode" | ||
|
||
// FieldsFunc split a string s by a func splitter into max n parts | ||
func FieldsFunc(s string, f func(rune2 rune) bool, n int) []string { | ||
// A span is used to record a slice of s of the form s[start:end]. | ||
// The start index is inclusive and the end index is exclusive. | ||
type span struct { | ||
start int | ||
end int | ||
} | ||
spans := make([]span, 0, 32) | ||
|
||
// Find the field start and end indices. | ||
// Doing this in a separate pass (rather than slicing the string s | ||
// and collecting the result substrings right away) is significantly | ||
// more efficient, possibly due to cache effects. | ||
start := -1 // valid span start if >= 0 | ||
for end, rune := range s { | ||
if f(rune) { | ||
if start >= 0 { | ||
spans = append(spans, span{start, end}) | ||
// Set start to a negative value. | ||
// Note: using -1 here consistently and reproducibly | ||
// slows down this code by a several percent on amd64. | ||
start = ^start | ||
} | ||
} else { | ||
if start < 0 { | ||
start = end | ||
if n > 0 && len(spans)+1 >= n { | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Last field might end at EOF. | ||
if start >= 0 { | ||
spans = append(spans, span{start, len(s)}) | ||
} | ||
|
||
// Create strings from recorded field indices. | ||
a := make([]string, len(spans)) | ||
for i, span := range spans { | ||
a[i] = s[span.start:span.end] | ||
} | ||
return a | ||
} | ||
|
||
// FieldsByAnySpace split a string s by any space character into max n parts | ||
func FieldsByAnySpace(s string, n int) []string { | ||
return FieldsFunc(s, unicode.IsSpace, n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package swag | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestFieldsByAnySpace(t *testing.T) { | ||
type args struct { | ||
s string | ||
n int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []string | ||
}{ | ||
{"test1", | ||
args{ | ||
" aa bb cc dd ff", | ||
2, | ||
}, | ||
[]string{"aa", "bb\tcc dd \t\tff"}, | ||
}, | ||
{"test2", | ||
args{ | ||
` aa "bb cc dd ff"`, | ||
2, | ||
}, | ||
[]string{"aa", `"bb cc dd ff"`}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.want, FieldsByAnySpace(tt.args.s, tt.args.n), "FieldsByAnySpace(%v, %v)", tt.args.s, tt.args.n) | ||
}) | ||
} | ||
} |