+ RemoveLeadingStrings

This commit is contained in:
Andreas Schulte 2023-10-25 22:48:39 +02:00
parent f5c734e417
commit da8c963b7a
Signed by: andreas
GPG Key ID: DCD1B6A247B69DB6
2 changed files with 49 additions and 1 deletions

View File

@ -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 := ""

View File

@ -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{
".", "-",