From 84cd68de7f225e795f390bfacd9fd1427601bb04 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sun, 19 Feb 2023 12:34:39 +0800 Subject: [PATCH] fix: fix bugs of PadEnd and PadStart --- strutil/string.go | 40 +++++++------------------------------- strutil/string_internal.go | 37 +++++++++++++++++++++++++++++++++++ strutil/string_test.go | 1 + 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/strutil/string.go b/strutil/string.go index b527b09..80bcfa9 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -67,44 +67,18 @@ func LowerFirst(s string) string { return string(r) + s[size:] } -// PadEnd pads string on the right side if it's shorter than size. -// Padding characters are truncated if they exceed size. -// Play: https://go.dev/play/p/9xP8rN0vz-- -func PadEnd(source string, size int, padStr string) string { - len1 := len(source) - len2 := len(padStr) - - if len1 >= size { - return source - } - - fill := "" - if len2 >= size-len1 { - fill = padStr[0 : size-len1] - } else { - fill = strings.Repeat(padStr, size-len1) - } - return source + fill[0:size-len1] -} - // PadStart pads string on the left side if it's shorter than size. // Padding characters are truncated if they exceed size. // Play: https://go.dev/play/p/xpTfzArDfvT func PadStart(source string, size int, padStr string) string { - len1 := len(source) - len2 := len(padStr) + return padAtPosition(source, size, padStr, 1) +} - if len1 >= size { - return source - } - - fill := "" - if len2 >= size-len1 { - fill = padStr[0 : size-len1] - } else { - fill = strings.Repeat(padStr, size-len1) - } - return fill[0:size-len1] + source +// PadEnd pads string on the right side if it's shorter than size. +// Padding characters are truncated if they exceed size. +// Play: https://go.dev/play/p/9xP8rN0vz-- +func PadEnd(source string, size int, padStr string) string { + return padAtPosition(source, size, padStr, 2) } // KebabCase coverts string to kebab-case, non letters and numbers will be ignored. diff --git a/strutil/string_internal.go b/strutil/string_internal.go index 2d4b54c..9ae2920 100644 --- a/strutil/string_internal.go +++ b/strutil/string_internal.go @@ -98,3 +98,40 @@ func toUpperAll(rs []rune) []rune { } return rs } + +// padWithPosition pads string +func padAtPosition(str string, length int, padStr string, position int) string { + if len(str) >= length { + return str + } + + if padStr == "" { + padStr = " " + } + + length = length - len(str) + startPadLen := 0 + if position == 0 { + startPadLen = length / 2 + } else if position == 1 { + startPadLen = length + } + endPadLen := length - startPadLen + + charLen := len(padStr) + leftPad := "" + cur := 0 + for cur < startPadLen { + leftPad += string(padStr[cur%charLen]) + cur++ + } + + cur = 0 + rightPad := "" + for cur < endPadLen { + rightPad += string(padStr[cur%charLen]) + cur++ + } + + return leftPad + str + rightPad +} diff --git a/strutil/string_test.go b/strutil/string_test.go index 26ef3fd..6682ebc 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -171,6 +171,7 @@ func TestLowerFirst(t *testing.T) { func TestPadEnd(t *testing.T) { assert := internal.NewAssert(t, "TestPadEnd") + assert.Equal("a ", PadEnd("a", 2, " ")) assert.Equal("a", PadEnd("a", 1, "b")) assert.Equal("ab", PadEnd("a", 2, "b")) assert.Equal("abcdmn", PadEnd("abcd", 6, "mno"))