package strutilimport ()// RuneIsWord char: a-zA-Zfunc ( rune) bool {returnRuneIsLower() || RuneIsUpper()}// RuneIsLower charfunc ( rune) bool {return'a' <= && <= 'z'}// RuneIsUpper charfunc ( rune) bool {return'A' <= && <= 'Z'}// RunePos alias of the strings.IndexRunefunc ( string, rune) int { returnstrings.IndexRune(, ) }// IsSpaceRune returns true if the given rune is a space, otherwise false.func ( rune) bool {return <= 256 && IsSpace(byte()) || unicode.IsSpace()}// Utf8Len count rune of the string.//// Examples://// str := "hi,你好"//// len(str) // 9// strutil.RunesWidth(str) // 7 一个中文字占两个字符// RuneCount(str) = Utf8Len(s) // 5 按字算func ( string) int { returnutf8.RuneCountInString() }// Utf8len count rune of the string.func ( string) int { returnutf8.RuneCountInString() }// RuneCount of the string.//// Examples://// str := "hi,你好"//// len(str) // 9// strutil.RunesWidth(str) // 7 一个中文字占两个字符// RuneCount(str) = utf8.RuneCountInString(s) // 5 按字算func ( string) int { returnlen([]rune()) }// RuneWidth of the rune.//// Example://// RuneWidth('你') // 2// RuneWidth('a') // 1// RuneWidth('\n') // 0func ( rune) int { := width.LookupRune() := .Kind()// eg: "\n"if == width.Neutral {return0 }if == width.EastAsianFullwidth || == width.EastAsianWide || == width.EastAsianAmbiguous {return2 }return1}// TextWidth utf8 string width. alias of RunesWidth()func ( string) int { returnUtf8Width() }// Utf8Width utf8 string width. alias of RunesWidthfunc ( string) int { returnRunesWidth([]rune()) }// RunesWidth utf8 runes string width.//// Examples://// str := "hi,你好"//// len(str) // 9// strutil.RunesWidth(str) // 7 一个中文字占两个字符// len([]rune(str)) = utf8.RuneCountInString(s) // 5 按字算func ( []rune) ( int) {iflen() == 0 {return }for , := range { += RuneWidth() }return}// Truncate alias of the Utf8Truncate()func ( string, int, string) string { returnUtf8Truncate(, , ) }// TextTruncate alias of the Utf8Truncate()func ( string, int, string) string { returnUtf8Truncate(, , ) }// Utf8Truncate a string with given width.func ( string, int, string) string { returnutf8Truncate(, Utf8Width(), , ) }// utf8Truncate a string with given width.func utf8Truncate( string, , int, string) string {if <= {return } := 0 := []rune() -= TextWidth() := 0for ; < len(); ++ { := RuneWidth([])if + > {break } += }returnstring([0:]) + }// Chunk split string to chunks by size.// func Chunk[T ~string](s T, size int) []T {// }// TextSplit alias of the Utf8Split()func ( string, int) []string { returnUtf8Split(, ) }// Utf8Split split a string by width.func ( string, int) []string { := Utf8Width()if <= {return []string{} } := 0 := "" := make([]string, 0, /+1)for , := range { := RuneWidth()if + == { += string() = append(, ) , = 0, ""// resetcontinue }if + > { = append(, )// append to next line. , = , string()continue } += += string() }if > 0 { = append(, ) }return}// TextWrap a string by "\n". alias of the WidthWrap()func ( string, int) string { returnWidthWrap(, ) }// WidthWrap a string by "\n"//// Example:// s := "hello 你好, world 世界"// s1 := strutil.TextWrap(s, 6) // "hello \n你好, \nworld \n世界"func ( string, int) string { := 0 := ""for , := range { := RuneWidth()if == '\n' { += string() = 0continue }if + > { += "\n" = 0 += string() += continue } += string() += }return}// WordWrap text string and limit width.func ( string, int) string { := 0 := ""for , := rangestrings.Split(, " ") { := TextWidth()if + > {if != 0 { += "\n" } = 0 += += continue } += += }return}// Runes data slicetypeRunes []rune// Padding a rune to want length and with positionfunc ( Runes) ( rune, int, PosFlag) []rune {returnPadChars(, , , )}// PadLeft a rune to want lengthfunc ( Runes) ( rune, int) []rune {return .Padding(, , PosLeft)}// PadRight a rune to want lengthfunc ( Runes) ( rune, int) []rune {return .Padding(, , PosRight)}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.