diff --git a/goextVersion.go b/goextVersion.go index c1acb57..3672632 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ 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" diff --git a/langext/string.go b/langext/string.go index d119c56..71e8072 100644 --- a/langext/string.go +++ b/langext/string.go @@ -88,12 +88,15 @@ func StrRunePadRight(str string, pad string, padlen int) string { func Indent(str string, pad string) string { eonl := strings.HasSuffix(str, "\n") + if eonl { + str = str[0 : len(str)-1] + } r := "" for _, v := range strings.Split(str, "\n") { r += pad + v + "\n" } - if eonl { + if !eonl { r = r[0 : len(r)-1] } @@ -115,3 +118,21 @@ func StrRepeat(val string, count int) string { } 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 +} diff --git a/langext/string_test.go b/langext/string_test.go new file mode 100644 index 0000000..9d6170a --- /dev/null +++ b/langext/string_test.go @@ -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) + } +}