package strutilimport ()// PosFlag typetypePosFlag = comdef.Position// Position for padding/resize stringconst (PosLeftPosFlag = iotaPosMiddlePosRightPosAuto)/************************************************************* * String padding operation *************************************************************/// Padding Fill the string to the specified length.//// params:// - s: 原始字符串// - pad: 用于填充的字符或字符串// - length: 目标长度// - padPos: 填充位置标志(左填充或右填充)func (, string, int, PosFlag) string {returnpadding(, , len(), , )}// Utf8Padding Fill the string to the specified length. use utf8 width.func (, string, int, PosFlag) string {returnpadding(, , Utf8Width(), , )}func padding(, string, , int, PosFlag) string { := - if >= 0 { // do not need padding.return }// pad space.iflen() == && ( == "" || == " ") {// Sprintf: 是按字数来填充的,不管中英文都是一个字符 - 有问题if == PosRight { // to rightreturn + strings.Repeat(" ", -) }returnstrings.Repeat(" ", -) + }// other character.if == PosRight { // to rightreturn + Repeat(, -) }returnRepeat(, -) + }// PadLeft a string.func (, string, int) string {returnPadding(, , , PosLeft)}// PadRight a string.func (, string, int) string {returnPadding(, , , PosRight)}// PadChars padding a rune/byte to want length and with position flagfunc [ byte | rune]( [], , int, PosFlag) [] { := len()if >= { := make([], )copy(, [:])return } := - := make([], )if == PosRight {copy(, )for := ; < ; ++ { [] = }return }// to leftfor := 0; < ; ++ { [] = }copy([:], )return}// PadBytes padding a byte to want length and with position flagfunc ( []byte, byte, int, PosFlag) []byte {returnPadChars(, , , )}// PadBytesLeft a byte to want lengthfunc ( []byte, byte, int) []byte {returnPadChars(, , , PosLeft)}// PadBytesRight a byte to want lengthfunc ( []byte, byte, int) []byte {returnPadChars(, , , PosRight)}// PadRunes padding a rune to want length and with position flagfunc ( []rune, rune, int, PosFlag) []rune {returnPadChars(, , , )}// PadRunesLeft a rune to want lengthfunc ( []rune, rune, int) []rune {returnPadChars(, , , PosLeft)}// PadRunesRight a rune to want lengthfunc ( []rune, rune, int) []rune {returnPadChars(, , , PosRight)}// Align a string by given length and align settings. alias of Resizefunc ( string, int, comdef.Align) string {returnresize(, len(), , , false)}// Utf8Align a string by given length and align settings. alias of Utf8Resizefunc ( string, int, comdef.Align) string {returnresize(, Utf8Width(), , , false)}// Resize a string by given length and align settings. use padding space.// If len(s) > wantLen, will truncate it.func ( string, int, comdef.Align) string {returnresize(, len(), , , true)}// Utf8Resize a string by given length and align settings. use padding space.// If width(s) > wantLen, will truncate it.func ( string, int, comdef.Align) string {returnresize(, Utf8Width(), , , true)}// resize a string by given length and align settings. use padding space.func resize( string, , int, comdef.Align, bool) string { := - if >= 0 { // do not need padding.// cutOverflow: truncate on sLen > wantLenif && > {iflen() == {return [:] }returnutf8Truncate(, , , "") }return }if == comdef.Center { := ( - ) / 2if = - *2; > 0 { += " " } := string(RepeatBytes(' ', ))return + + } := string(RepeatBytes(' ', -))// tip: 左对齐 - 使用空白填充右边if == comdef.Left || == comdef.PosAuto {return + }return + }/************************************************************* * String repeat operation *************************************************************/// Repeat a string by given times.func ( string, int) string {if < 1 {return"" }if == 1 {return }varstrings.Builder .Grow(len() * )for := 0; < ; ++ { .WriteString() }return .String()}// RepeatRune repeat a rune char.func ( rune, int) []rune { returnRepeatChars(, ) }// RepeatBytes repeat a byte char.func ( byte, int) []byte { returnRepeatChars(, ) }// RepeatChars repeat a byte char.func [ byte | rune]( , int) [] {if <= 0 {returnmake([], 0) } := make([], )for := 0; < ; ++ { [] = }return}
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.