Merge "Fix crash in MakeString.EndsWith()"
This commit is contained in:
@@ -279,7 +279,7 @@ func (ms *MakeString) TrimRightOne() {
|
|||||||
|
|
||||||
func (ms *MakeString) EndsWith(ch rune) bool {
|
func (ms *MakeString) EndsWith(ch rune) bool {
|
||||||
s := ms.Strings[len(ms.Strings)-1]
|
s := ms.Strings[len(ms.Strings)-1]
|
||||||
return s[len(s)-1] == uint8(ch)
|
return len(s) > 0 && s[len(s)-1] == uint8(ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ms *MakeString) ReplaceLiteral(input string, output string) {
|
func (ms *MakeString) ReplaceLiteral(input string, output string) {
|
||||||
|
@@ -217,6 +217,36 @@ func TestMakeStringWords(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var endsWithTestCases = []struct {
|
||||||
|
in *MakeString
|
||||||
|
endsWith rune
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
in: genMakeString("foo", "X", "bar ="),
|
||||||
|
endsWith: '=',
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: genMakeString("foo", "X", "bar ="),
|
||||||
|
endsWith: ':',
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: genMakeString("foo", "X", ""),
|
||||||
|
endsWith: '=',
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMakeStringEndsWith(t *testing.T) {
|
||||||
|
for _, test := range endsWithTestCases {
|
||||||
|
if test.in.EndsWith(test.endsWith) != test.expected {
|
||||||
|
t.Errorf("with:\n%q\nexpected:\n%t\ngot:\n%t", test.in.Dump(), test.expected, !test.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func dumpArray(a []*MakeString) string {
|
func dumpArray(a []*MakeString) string {
|
||||||
ret := make([]string, len(a))
|
ret := make([]string, len(a))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user