libs/strings: move to internal (#8890) (#8891)

This commit is contained in:
mergify[bot]
2022-06-27 18:27:46 -04:00
committed by GitHub
parent f63c9b0d28
commit ed7f51460b
7 changed files with 5 additions and 5 deletions
-62
View File
@@ -1,62 +0,0 @@
package strings
import (
"fmt"
"strings"
)
// SplitAndTrimEmpty slices s into all subslices separated by sep and returns a
// slice of the string s with all leading and trailing Unicode code points
// contained in cutset removed. If sep is empty, SplitAndTrim splits after each
// UTF-8 sequence. First part is equivalent to strings.SplitN with a count of
// -1. also filter out empty strings, only return non-empty strings.
func SplitAndTrimEmpty(s, sep, cutset string) []string {
if s == "" {
return []string{}
}
spl := strings.Split(s, sep)
nonEmptyStrings := make([]string, 0, len(spl))
for i := 0; i < len(spl); i++ {
element := strings.Trim(spl[i], cutset)
if element != "" {
nonEmptyStrings = append(nonEmptyStrings, element)
}
}
return nonEmptyStrings
}
// ASCIITrim removes spaces from an a ASCII string, erroring if the
// sequence is not an ASCII string.
func ASCIITrim(s string) (string, error) {
if len(s) == 0 {
return "", nil
}
r := make([]byte, 0, len(s))
for _, b := range []byte(s) {
switch {
case b == 32:
continue // skip space
case 32 < b && b <= 126:
r = append(r, b)
default:
return "", fmt.Errorf("non-ASCII (non-tab) char 0x%X", b)
}
}
return string(r), nil
}
// StringSliceEqual checks if string slices a and b are equal
func StringSliceEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
-89
View File
@@ -1,89 +0,0 @@
package strings
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestSplitAndTrimEmpty(t *testing.T) {
testCases := []struct {
s string
sep string
cutset string
expected []string
}{
{"a,b,c", ",", " ", []string{"a", "b", "c"}},
{" a , b , c ", ",", " ", []string{"a", "b", "c"}},
{" a, b, c ", ",", " ", []string{"a", "b", "c"}},
{" a, ", ",", " ", []string{"a"}},
{" ", ",", " ", []string{}},
}
for _, tc := range testCases {
require.Equal(t, tc.expected, SplitAndTrimEmpty(tc.s, tc.sep, tc.cutset), "%s", tc.s)
}
}
func assertCorrectTrim(t *testing.T, input, expected string) {
t.Helper()
output, err := ASCIITrim(input)
require.NoError(t, err)
require.Equal(t, expected, output)
}
func TestASCIITrim(t *testing.T) {
t.Run("Validation", func(t *testing.T) {
t.Run("NonASCII", func(t *testing.T) {
notASCIIText := []string{
"\xC2", "\xC2\xA2", "\xFF", "\x80", "\xF0", "\n", "\t",
}
for _, v := range notASCIIText {
_, err := ASCIITrim(v)
require.Error(t, err, "%q is not ascii-text", v)
}
})
t.Run("EmptyString", func(t *testing.T) {
out, err := ASCIITrim("")
require.NoError(t, err)
require.Zero(t, out)
})
t.Run("ASCIIText", func(t *testing.T) {
asciiText := []string{
" ", ".", "x", "$", "_", "abcdefg;", "-", "0x00", "0", "123",
}
for _, v := range asciiText {
_, err := ASCIITrim(v)
require.NoError(t, err, "%q is ascii-text", v)
}
})
_, err := ASCIITrim("\xC2\xA2")
require.Error(t, err)
})
t.Run("Trimming", func(t *testing.T) {
assertCorrectTrim(t, " ", "")
assertCorrectTrim(t, " a", "a")
assertCorrectTrim(t, "a ", "a")
assertCorrectTrim(t, " a ", "a")
})
}
func TestStringSliceEqual(t *testing.T) {
tests := []struct {
a []string
b []string
want bool
}{
{[]string{"hello", "world"}, []string{"hello", "world"}, true},
{[]string{"test"}, []string{"test"}, true},
{[]string{"test1"}, []string{"test2"}, false},
{[]string{"hello", "world."}, []string{"hello", "world!"}, false},
{[]string{"only 1 word"}, []string{"two", "words!"}, false},
{[]string{"two", "words!"}, []string{"only 1 word"}, false},
}
for i, tt := range tests {
require.Equal(t, tt.want, StringSliceEqual(tt.a, tt.b),
"StringSliceEqual failed on test %d", i)
}
}