github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/pad.go (about)

     1  package util
     2  
     3  import "bytes"
     4  
     5  // Pad 填充字符串到指定长度, 同 Python3: 'str'.center()
     6  func Pad(s, pad string, n int) string {
     7  	need := n - len(s)
     8  	if need <= 0 {
     9  		return s
    10  	}
    11  
    12  	bs := make([]byte, n)
    13  	half := need - need>>1
    14  
    15  	// 一半填充到左边
    16  	// 奇数时左边比右边多: 1
    17  	copyPad(bs, pad, half)
    18  
    19  	m := copy(bs[half:], s)
    20  	m += half
    21  
    22  	// 一半填充到右边
    23  	copyPad(bs[m:], pad, n-m)
    24  
    25  	return string(bs)
    26  }
    27  
    28  // LeftPad 从左填充字符串到指定长度
    29  func LeftPad(s, pad string, n int) string {
    30  	need := n - len(s)
    31  	if need <= 0 {
    32  		return s
    33  	}
    34  
    35  	bs := make([]byte, n)
    36  	copyPad(bs, pad, need)
    37  	copy(bs[need:], s)
    38  
    39  	return string(bs)
    40  }
    41  
    42  // RightPad 从右填充字符串到指定长度
    43  func RightPad(s, pad string, n int) string {
    44  	need := n - len(s)
    45  	if need <= 0 {
    46  		return s
    47  	}
    48  
    49  	bs := make([]byte, n)
    50  	l := copy(bs, s)
    51  	copyPad(bs[l:], pad, need)
    52  
    53  	return string(bs)
    54  }
    55  
    56  // PadBytes 填充到指定长度
    57  func PadBytes(s, pad []byte, n int) []byte {
    58  	need := n - len(s)
    59  	if need <= 0 {
    60  		return s
    61  	}
    62  
    63  	bs := make([]byte, n)
    64  	half := need - need>>1
    65  
    66  	// 一半填充到左边
    67  	// 奇数时左边比右边多: 1
    68  	copyPadBytes(bs, pad, half)
    69  
    70  	m := copy(bs[half:], s)
    71  	m += half
    72  
    73  	// 一半填充到右边
    74  	copyPadBytes(bs[m:], pad, n-m)
    75  
    76  	return bs
    77  }
    78  
    79  // LeftPadBytes 从左填充到指定长度
    80  func LeftPadBytes(b, pad []byte, n int) []byte {
    81  	need := n - len(b)
    82  	if need <= 0 {
    83  		return b
    84  	}
    85  
    86  	bs := make([]byte, n)
    87  	copyPadBytes(bs, pad, need)
    88  	copy(bs[need:], b)
    89  
    90  	return bs
    91  }
    92  
    93  // RightPadBytes 从右填充到指定长度
    94  func RightPadBytes(b, pad []byte, n int) []byte {
    95  	need := n - len(b)
    96  	if need <= 0 {
    97  		return b
    98  	}
    99  
   100  	bs := make([]byte, n)
   101  	l := copy(bs, b)
   102  	copyPadBytes(bs[l:], pad, need)
   103  
   104  	return bs
   105  }
   106  
   107  // JoinBytes 拼接 []byte
   108  func JoinBytes(b ...[]byte) []byte {
   109  	return bytes.Join(b, nil)
   110  }
   111  
   112  // copyPad 重复填充到指定长度
   113  func copyPad(bs []byte, pad string, need int) {
   114  	if need <= 0 {
   115  		return
   116  	}
   117  	n := copy(bs[:need], pad)
   118  	for n < need {
   119  		copy(bs[n:need], bs[:n])
   120  		n = n << 1
   121  	}
   122  }
   123  
   124  func copyPadBytes(bs, pad []byte, need int) {
   125  	if need <= 0 {
   126  		return
   127  	}
   128  	n := copy(bs[:need], pad)
   129  	for n < need {
   130  		copy(bs[n:need], bs[:n])
   131  		n = n << 1
   132  	}
   133  }