github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/util/string.go (about)

     1  package util
     2  
     3  import (
     4  	"encoding/hex"
     5  	"strconv"
     6  	"strings"
     7  	"unsafe"
     8  )
     9  
    10  func SplitUint16(sep, str string) ([]uint16, *Err) {
    11  	items := strings.Split(str, sep)
    12  	vals := make([]uint16, len(items))
    13  	for i, item := range items {
    14  		v, e := strconv.Atoi(item)
    15  		if e != nil {
    16  			return nil, WrapErr(EcParseErr, e)
    17  		}
    18  		vals[i] = uint16(v)
    19  	}
    20  	return vals, nil
    21  }
    22  
    23  func SplitInt64(sep, str string) ([]int64, *Err) {
    24  	items := strings.Split(str, sep)
    25  	vals := make([]int64, len(items))
    26  	for i, item := range items {
    27  		v, e := strconv.ParseInt(item, 10, 64)
    28  		if e != nil {
    29  			return nil, WrapErr(EcParseErr, e)
    30  		}
    31  		vals[i] = v
    32  	}
    33  	return vals, nil
    34  }
    35  
    36  func BytesToStr(b []byte) string {
    37  	return *(*string)(unsafe.Pointer(&b))
    38  }
    39  
    40  func StrToBytes(s string) []byte {
    41  	x := (*[2]uintptr)(unsafe.Pointer(&s))
    42  	h := [3]uintptr{x[0], x[1], x[1]}
    43  	return *(*[]byte)(unsafe.Pointer(&h))
    44  }
    45  
    46  const (
    47  	_UpToLowerOffset = byte('a' - 'A')
    48  )
    49  
    50  func ToBigHump(str string) string {
    51  	bytes := []byte(str)
    52  	var sb strings.Builder
    53  	l := len(bytes)
    54  	sb.Grow(l)
    55  	h := false
    56  	first := true
    57  	for i := 0; i < l; i++ {
    58  		s := bytes[i]
    59  		if s == '_' {
    60  			h = true
    61  			continue
    62  		}
    63  		if s >= 'a' && s <= 'z' && (h || first) {
    64  			h = false
    65  			first = false
    66  			sb.WriteByte(s - _UpToLowerOffset)
    67  			continue
    68  		}
    69  		sb.WriteByte(s)
    70  	}
    71  	return sb.String()
    72  }
    73  
    74  func ToUnderline(str string) string {
    75  	bytes := []byte(str)
    76  	var sb strings.Builder
    77  	sb.Grow(len(bytes))
    78  	for i, s := range bytes {
    79  		if s < 'A' || s > 'Z' {
    80  			sb.WriteByte(s)
    81  		} else {
    82  			if i > 0 {
    83  				sb.WriteByte('_')
    84  			}
    85  			sb.WriteByte(s + _UpToLowerOffset)
    86  		}
    87  	}
    88  	return sb.String()
    89  }
    90  
    91  func Hex(bytes []byte) string {
    92  	str := hex.EncodeToString(bytes)
    93  	l := len(str)
    94  	s := ""
    95  	i := 0
    96  	for i < l {
    97  		s += str[i : i+2]
    98  		s += " "
    99  		i += 2
   100  	}
   101  	return s
   102  }
   103  
   104  func SplitWords(str string, words *[]string) {
   105  	var (
   106  		start = 0
   107  		end   = 0
   108  	)
   109  	bytes := []byte(str)
   110  	l := len(bytes)
   111  	for ; end < l; end++ {
   112  		s := bytes[end]
   113  		if end == 0 || s < 'A' || s > 'Z' {
   114  			continue
   115  		}
   116  
   117  		*words = append(*words, BytesToStr(bytes[start:end]))
   118  		start = end
   119  	}
   120  	if start < l {
   121  		*words = append(*words, BytesToStr(bytes[start:l]))
   122  	}
   123  }
   124  
   125  func ParseInt[T Num](s string) (v T, err *Err) {
   126  	i, e := strconv.ParseInt(s, 10, 64)
   127  	if e != nil {
   128  		return 0, NewErr(EcParseErr, M{
   129  			"string": s,
   130  		})
   131  	}
   132  	v = T(i)
   133  	return
   134  }
   135  
   136  func StringsJoin(sep string, slc ...string) string {
   137  	return strings.Join(slc, sep)
   138  }