+ RemoveLeadingStrings
This commit is contained in:
parent
f5c734e417
commit
da8c963b7a
16
strings.go
16
strings.go
@ -52,6 +52,22 @@ func RemoveDoubledStrings(s string, target string) string {
|
||||
)
|
||||
}
|
||||
|
||||
// RemoveLeadingStrings removes all leading occurrences of target in s
|
||||
func RemoveLeadingStrings(s string, target string) string {
|
||||
lenS := len(s)
|
||||
lenTarget := len(target)
|
||||
|
||||
if lenS < lenTarget {
|
||||
return s
|
||||
}
|
||||
|
||||
if s[:lenTarget] != target {
|
||||
return s
|
||||
}
|
||||
|
||||
return RemoveLeadingStrings(s[lenTarget:], target)
|
||||
}
|
||||
|
||||
// SanitizeStringWithWhitelist cleans s by all substrings which do not occur in whitelist
|
||||
func SanitizeStringWithWhitelist(s string, whitelist map[string]string) string {
|
||||
sanitizedString := ""
|
||||
|
@ -1,6 +1,8 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFirstCharacterToUppercase(t *testing.T) {
|
||||
data := map[string]string{
|
||||
@ -56,6 +58,36 @@ func TestRemoveDoubledStrings(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveLeadingStrings(t *testing.T) {
|
||||
data := map[string]string{
|
||||
"": "",
|
||||
".": "",
|
||||
"...Jahr 2022... die überleben wollen...": "Jahr 2022... die überleben wollen...",
|
||||
}
|
||||
|
||||
for d, expectedResult := range data {
|
||||
result := RemoveLeadingStrings(d, ".")
|
||||
if result != expectedResult {
|
||||
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
|
||||
}
|
||||
}
|
||||
|
||||
data = map[string]string{
|
||||
"": "",
|
||||
".": ".",
|
||||
"..": "..",
|
||||
"...": "",
|
||||
"....": ".",
|
||||
}
|
||||
|
||||
for d, expectedResult := range data {
|
||||
result := RemoveLeadingStrings(d, "...")
|
||||
if result != expectedResult {
|
||||
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeStringWithWhitelist(t *testing.T) {
|
||||
whitelistedChars := []string{
|
||||
".", "-",
|
||||
|
Loading…
Reference in New Issue
Block a user