v0.0.477 add langext.StrWrap
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m4s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m4s
This commit is contained in:
parent
dff8941bd3
commit
cb95bb561c
@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.476"
|
const GoextVersion = "0.0.477"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-06-28T18:37:02+0200"
|
const GoextVersionTimestamp = "2024-06-29T15:36:39+0200"
|
||||||
|
@ -88,12 +88,15 @@ func StrRunePadRight(str string, pad string, padlen int) string {
|
|||||||
|
|
||||||
func Indent(str string, pad string) string {
|
func Indent(str string, pad string) string {
|
||||||
eonl := strings.HasSuffix(str, "\n")
|
eonl := strings.HasSuffix(str, "\n")
|
||||||
|
if eonl {
|
||||||
|
str = str[0 : len(str)-1]
|
||||||
|
}
|
||||||
r := ""
|
r := ""
|
||||||
for _, v := range strings.Split(str, "\n") {
|
for _, v := range strings.Split(str, "\n") {
|
||||||
r += pad + v + "\n"
|
r += pad + v + "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
if eonl {
|
if !eonl {
|
||||||
r = r[0 : len(r)-1]
|
r = r[0 : len(r)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,3 +118,21 @@ func StrRepeat(val string, count int) string {
|
|||||||
}
|
}
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func StrWrap(val string, linelen int, seperator string) string {
|
||||||
|
res := ""
|
||||||
|
|
||||||
|
for iPos := 0; ; {
|
||||||
|
next := min(iPos+linelen, len(val))
|
||||||
|
res += val[iPos:next]
|
||||||
|
|
||||||
|
iPos = next
|
||||||
|
if iPos >= len(val) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
res += seperator
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
152
langext/string_test.go
Normal file
152
langext/string_test.go
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package langext
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestStrLimitBehaviour(t *testing.T) {
|
||||||
|
val := "Hello, World!"
|
||||||
|
maxlen := 5
|
||||||
|
suffix := "..."
|
||||||
|
expected := "He..."
|
||||||
|
result := StrLimit(val, maxlen, suffix)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStrSplitBehaviour1(t *testing.T) {
|
||||||
|
val := "Hello,World,,"
|
||||||
|
sep := ","
|
||||||
|
expected := []string{"Hello", "World"}
|
||||||
|
result := StrSplit(val, sep, false)
|
||||||
|
if len(result) != len(expected) {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStrSplitBehaviour2(t *testing.T) {
|
||||||
|
val := "Hello,World,,"
|
||||||
|
sep := ","
|
||||||
|
expected := []string{"Hello", "World", "", ""}
|
||||||
|
result := StrSplit(val, sep, true)
|
||||||
|
if len(result) != len(expected) {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStrPadRightBehaviour(t *testing.T) {
|
||||||
|
str := "Hello"
|
||||||
|
pad := "*"
|
||||||
|
padlen := 10
|
||||||
|
expected := "Hello*****"
|
||||||
|
result := StrPadRight(str, pad, padlen)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStrPadLeftBehaviour(t *testing.T) {
|
||||||
|
str := "Hello"
|
||||||
|
pad := "*"
|
||||||
|
padlen := 10
|
||||||
|
expected := "*****Hello"
|
||||||
|
result := StrPadLeft(str, pad, padlen)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStrRunePadLeftBehaviour(t *testing.T) {
|
||||||
|
str := "Hello"
|
||||||
|
pad := "*"
|
||||||
|
padlen := 10
|
||||||
|
expected := "*****Hello"
|
||||||
|
result := StrRunePadLeft(str, pad, padlen)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStrRunePadRightBehaviour(t *testing.T) {
|
||||||
|
str := "Hello"
|
||||||
|
pad := "*"
|
||||||
|
padlen := 10
|
||||||
|
expected := "Hello*****"
|
||||||
|
result := StrRunePadRight(str, pad, padlen)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndentBehaviour1(t *testing.T) {
|
||||||
|
str := "Hello\nWorld"
|
||||||
|
pad := ".."
|
||||||
|
expected := "..Hello\n..World"
|
||||||
|
result := Indent(str, pad)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndentBehaviour2(t *testing.T) {
|
||||||
|
str := "Hello\nWorld\n"
|
||||||
|
pad := ".."
|
||||||
|
expected := "..Hello\n..World\n"
|
||||||
|
result := Indent(str, pad)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStrRepeatBehaviour(t *testing.T) {
|
||||||
|
val := "Hello"
|
||||||
|
count := 3
|
||||||
|
expected := "HelloHelloHello"
|
||||||
|
result := StrRepeat(val, count)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStrWrapBehaviour1(t *testing.T) {
|
||||||
|
val := "123456789"
|
||||||
|
linelen := 5
|
||||||
|
seperator := "\n"
|
||||||
|
expected := "12345\n6789"
|
||||||
|
result := StrWrap(val, linelen, seperator)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStrWrapBehaviour2(t *testing.T) {
|
||||||
|
val := "1234567890"
|
||||||
|
linelen := 5
|
||||||
|
seperator := "\n"
|
||||||
|
expected := "12345\n67890"
|
||||||
|
result := StrWrap(val, linelen, seperator)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStrWrapBehaviour3(t *testing.T) {
|
||||||
|
val := "****************"
|
||||||
|
linelen := 4
|
||||||
|
seperator := "\n"
|
||||||
|
expected := "****\n****\n****\n****"
|
||||||
|
result := StrWrap(val, linelen, seperator)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStrWrapBehaviour4(t *testing.T) {
|
||||||
|
val := "*****************"
|
||||||
|
linelen := 4
|
||||||
|
seperator := "\n"
|
||||||
|
expected := "****\n****\n****\n****\n*"
|
||||||
|
result := StrWrap(val, linelen, seperator)
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %v but got %v", expected, result)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user