Created
Some checks failed
Go / build (push) Failing after 7s

This commit is contained in:
scheibling
2025-04-08 19:16:39 +02:00
commit b4eb50ab55
63 changed files with 7333 additions and 0 deletions

90
bytex/byte.go Normal file
View File

@@ -0,0 +1,90 @@
package bytex
import (
"bytes"
"unsafe"
)
// IsEmpty Check byte is empty
func IsEmpty(b []byte) bool {
return len(b) == 0
}
func IsBlank(b []byte) bool {
return len(b) == 0 || len(bytes.TrimSpace(b)) == 0
}
func ToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func StartsWith(s []byte, ss [][]byte, caseSensitive bool) bool {
if ss == nil || len(ss) == 0 {
return true
}
has := false
if !caseSensitive {
s = bytes.ToLower(s)
}
for _, prefix := range ss {
if len(prefix) == 0 {
has = true
} else {
if !caseSensitive {
prefix = bytes.ToLower(prefix)
}
has = bytes.HasPrefix(s, prefix)
}
if has {
break
}
}
return has
}
func EndsWith(s []byte, ss [][]byte, caseSensitive bool) bool {
if ss == nil || len(ss) == 0 {
return true
}
has := false
if !caseSensitive {
s = bytes.ToLower(s)
}
for _, suffix := range ss {
if len(suffix) == 0 {
has = true
} else {
if !caseSensitive {
suffix = bytes.ToLower(suffix)
}
has = bytes.HasSuffix(s, suffix)
}
if has {
break
}
}
return has
}
func Contains(s []byte, ss [][]byte, caseSensitive bool) bool {
in := false
if !caseSensitive {
s = bytes.ToLower(s)
}
for _, substr := range ss {
if len(substr) == 0 {
in = true
} else {
if !caseSensitive {
substr = bytes.ToLower(substr)
}
in = bytes.Contains(s, substr)
}
if in {
break
}
}
return in
}

126
bytex/byte_test.go Normal file
View File

@@ -0,0 +1,126 @@
package bytex
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestIsEmpty(t *testing.T) {
testCases := []struct {
Number int
Byte []byte
Except bool
}{
{1, []byte("a"), false},
{2, []byte(""), true},
{3, []byte(" "), false},
}
for _, testCase := range testCases {
isEmpty := IsEmpty(testCase.Byte)
if isEmpty != testCase.Except {
t.Errorf("%d except: %#v, actual: %#v", testCase.Number, testCase.Except, isEmpty)
}
}
}
func TestIsBlank(t *testing.T) {
testCases := []struct {
Number int
Byte []byte
Except bool
}{
{1, []byte("a"), false},
{2, []byte(""), true},
{3, []byte(" "), true},
}
for _, testCase := range testCases {
isBlank := IsBlank(testCase.Byte)
if isBlank != testCase.Except {
t.Errorf("%d except: %#v, actual: %#v", testCase.Number, testCase.Except, isBlank)
}
}
}
func TestToString(t *testing.T) {
tests := []struct {
tag string
bytesValue []byte
string string
}{
{"t1", []byte{'a'}, "a"},
{"t2", []byte("abc"), "abc"},
{"t3", []byte("a b c "), "a b c "},
}
for _, test := range tests {
s := ToString(test.bytesValue)
assert.Equal(t, test.string, s, test.tag)
}
}
func TestStartsWith(t *testing.T) {
tests := []struct {
tag string
string []byte
words [][]byte
caseSensitive bool
except bool
}{
{"t1", []byte("Hello world!"), [][]byte{[]byte("he"), []byte("He")}, false, true},
{"t2", []byte("Hello world!"), [][]byte{[]byte("he"), []byte("He")}, true, true},
{"t3", []byte("Hello world!"), [][]byte{[]byte("he")}, true, false},
{"t4", []byte(""), [][]byte{[]byte("")}, true, true},
{"t5", []byte(""), nil, true, true},
{"t6", []byte(""), [][]byte{}, true, true},
{"t7", []byte("Hello world!"), [][]byte{[]byte("")}, true, true},
}
for _, test := range tests {
b := StartsWith(test.string, test.words, test.caseSensitive)
assert.Equal(t, test.except, b, test.tag)
}
}
func TestEndsWith(t *testing.T) {
tests := []struct {
tag string
string []byte
words [][]byte
caseSensitive bool
except bool
}{
{"t1", []byte("Hello world!"), [][]byte{[]byte("he"), []byte("He")}, false, false},
{"t2", []byte("Hello world!"), [][]byte{[]byte("he"), []byte("He")}, true, false},
{"t3", []byte("Hello world!"), [][]byte{[]byte("d!"), []byte("!")}, true, true},
{"t4", []byte("Hello world!"), [][]byte{[]byte("WORLD!")}, false, true},
{"t5", []byte(""), [][]byte{[]byte("")}, true, true},
{"t6", []byte(""), nil, true, true},
{"t7", []byte(""), [][]byte{}, true, true},
{"t8", []byte("Hello world!"), [][]byte{[]byte("")}, true, true},
}
for _, test := range tests {
b := EndsWith(test.string, test.words, test.caseSensitive)
assert.Equal(t, test.except, b, test.tag)
}
}
func TestContains(t *testing.T) {
tests := []struct {
tag string
string []byte
words [][]byte
caseSensitive bool
except bool
}{
{"t1", []byte("Hello world!"), [][]byte{[]byte("ol"), []byte("LL")}, false, true},
{"t2", []byte("Hello world!"), [][]byte{[]byte("ol"), []byte("LL")}, true, false},
{"t3", []byte("Hello world!"), [][]byte{[]byte("notfound"), []byte("world")}, false, true},
{"t4", []byte("Hello world!"), [][]byte{[]byte("notfound"), []byte("world")}, true, true},
{"t5", []byte(""), [][]byte{[]byte("")}, true, true},
{"t6", []byte("Hello world!"), [][]byte{[]byte("")}, true, true},
}
for _, test := range tests {
b := Contains(test.string, test.words, test.caseSensitive)
assert.Equal(t, test.except, b, test.tag)
}
}