Merge pull request #20 from car409/master

Fix nil panic in toSubstr
pull/31/head
TP Honey 3 years ago committed by GitHub
commit 7bf45dbf53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -102,9 +102,14 @@ func toSubstr(s string, args ...string) string {
}
if pos+length >= len(s) {
if pos < len(s) {
// if the position exceeds the length of the
// string just return the rest of it like bash
return s[pos:]
}
// if the position exceeds the length of the
// string just return the rest of it like bash
return s[pos:]
// string an empty string is returned
return ""
}
return s[pos : pos+length]

@ -96,4 +96,9 @@ func Test_substr(t *testing.T) {
if got != want {
t.Errorf("Expect substr function to cut from the beginning to length for negative offsets exceeding string length")
}
got, want = toSubstr("12345678", "9", "1"), ""
if got != want {
t.Errorf("Expect substr function to cut entire string if pos is itself out of bound")
}
}

Loading…
Cancel
Save