You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
911 B
Go
48 lines
911 B
Go
1 year ago
|
package nstr
|
||
|
|
||
|
import "strings"
|
||
|
|
||
|
/*************************************************************
|
||
|
* String repeat operation
|
||
|
*************************************************************/
|
||
|
|
||
|
// Repeat a string
|
||
|
func Repeat(s string, times int) string {
|
||
|
if times <= 0 {
|
||
|
return ""
|
||
|
}
|
||
|
if times == 1 {
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
ss := make([]string, 0, times)
|
||
|
for i := 0; i < times; i++ {
|
||
|
ss = append(ss, s)
|
||
|
}
|
||
|
|
||
|
return strings.Join(ss, "")
|
||
|
}
|
||
|
|
||
|
// RepeatRune repeat a rune char.
|
||
|
func RepeatRune(char rune, times int) []rune {
|
||
|
return RepeatChars(char, times)
|
||
|
}
|
||
|
|
||
|
// RepeatBytes repeat a byte char.
|
||
|
func RepeatBytes(char byte, times int) []byte {
|
||
|
return RepeatChars(char, times)
|
||
|
}
|
||
|
|
||
|
// RepeatChars repeat a byte char.
|
||
|
func RepeatChars[T byte | rune](char T, times int) []T {
|
||
|
if times <= 0 {
|
||
|
return make([]T, 0)
|
||
|
}
|
||
|
|
||
|
chars := make([]T, 0, times)
|
||
|
for i := 0; i < times; i++ {
|
||
|
chars = append(chars, char)
|
||
|
}
|
||
|
return chars
|
||
|
}
|