github.heygears.com/openimsdk/tools@v0.0.49/utils/stringutil/strings.go (about)

     1  // Copyright © 2023 OpenIM. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package stringutil
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"github.com/pkg/errors"
    21  	"hash/crc32"
    22  	"runtime"
    23  	"strconv"
    24  	"strings"
    25  	"unicode"
    26  )
    27  
    28  func IntToString(i int) string {
    29  	return strconv.FormatInt(int64(i), 10)
    30  }
    31  
    32  func StringToInt(i string) int {
    33  	j, _ := strconv.Atoi(i)
    34  	return j
    35  }
    36  func StringToInt64(i string) int64 {
    37  	j, _ := strconv.ParseInt(i, 10, 64)
    38  	return j
    39  }
    40  func StringToInt32(i string) int32 {
    41  	j, _ := strconv.ParseInt(i, 10, 64)
    42  	return int32(j)
    43  }
    44  func Int32ToString(i int32) string {
    45  	return strconv.FormatInt(int64(i), 10)
    46  }
    47  
    48  func Uint32ToString(i uint32) string {
    49  	return strconv.FormatInt(int64(i), 10)
    50  }
    51  
    52  // judge a string whether in the  string list
    53  func IsContain(target string, List []string) bool {
    54  	for _, element := range List {
    55  
    56  		if target == element {
    57  			return true
    58  		}
    59  	}
    60  	return false
    61  }
    62  func IsContainInt32(target int32, List []int32) bool {
    63  	for _, element := range List {
    64  		if target == element {
    65  			return true
    66  		}
    67  	}
    68  	return false
    69  }
    70  func IsContainInt(target int, List []int) bool {
    71  	for _, element := range List {
    72  		if target == element {
    73  			return true
    74  		}
    75  	}
    76  	return false
    77  }
    78  func InterfaceArrayToStringArray(data []any) (i []string) {
    79  	for _, param := range data {
    80  		i = append(i, param.(string))
    81  	}
    82  	return i
    83  }
    84  
    85  func StructToJsonBytes(param any) []byte {
    86  	dataType, _ := json.Marshal(param)
    87  	return dataType
    88  }
    89  
    90  func Int64ToString(i int64) string {
    91  	return strconv.FormatInt(i, 10)
    92  }
    93  
    94  func RemoveDuplicateElement(idList []string) []string {
    95  	result := make([]string, 0, len(idList))
    96  	temp := map[string]struct{}{}
    97  	for _, item := range idList {
    98  		if _, ok := temp[item]; !ok {
    99  			temp[item] = struct{}{}
   100  			result = append(result, item)
   101  		}
   102  	}
   103  	return result
   104  }
   105  
   106  func RemoveDuplicate[T comparable](arr []T) []T {
   107  	result := make([]T, 0, len(arr))
   108  	temp := map[T]struct{}{}
   109  	for _, item := range arr {
   110  		if _, ok := temp[item]; !ok {
   111  			temp[item] = struct{}{}
   112  			result = append(result, item)
   113  		}
   114  	}
   115  	return result
   116  }
   117  
   118  func IsDuplicateStringSlice(arr []string) bool {
   119  	t := make(map[string]struct{})
   120  	for _, s := range arr {
   121  		if _, ok := t[s]; ok {
   122  			return true
   123  		}
   124  		t[s] = struct{}{}
   125  	}
   126  	return false
   127  }
   128  
   129  func WithMessage(err error, message string) error {
   130  	return errors.WithMessage(err, "==> "+printCallerNameAndLine()+message)
   131  }
   132  
   133  func printCallerNameAndLine() string {
   134  	pc, _, line, _ := runtime.Caller(2)
   135  	return runtime.FuncForPC(pc).Name() + "()@" + strconv.Itoa(line) + ": "
   136  }
   137  
   138  func GetSelfFuncName() string {
   139  	pc, _, _, _ := runtime.Caller(1)
   140  	return cleanUpFuncName(runtime.FuncForPC(pc).Name())
   141  }
   142  
   143  func GetFuncName(skips ...int) string {
   144  	skip := 1
   145  	if len(skips) > 0 {
   146  		skip = skips[0] + 1
   147  	}
   148  	pc, _, _, _ := runtime.Caller(skip)
   149  	return cleanUpFuncName(runtime.FuncForPC(pc).Name())
   150  }
   151  
   152  func cleanUpFuncName(funcName string) string {
   153  	end := strings.LastIndex(funcName, ".")
   154  	if end == -1 {
   155  		return ""
   156  	}
   157  	return funcName[end+1:]
   158  }
   159  
   160  // Get the intersection of two slices
   161  func IntersectString(slice1, slice2 []string) []string {
   162  	m := make(map[string]bool)
   163  	n := make([]string, 0)
   164  	for _, v := range slice1 {
   165  		m[v] = true
   166  	}
   167  	for _, v := range slice2 {
   168  		flag, _ := m[v]
   169  		if flag {
   170  			n = append(n, v)
   171  		}
   172  	}
   173  	return n
   174  }
   175  
   176  // Get the diff of two slices
   177  func DifferenceString(slice1, slice2 []string) []string {
   178  	m := make(map[string]bool)
   179  	n := make([]string, 0)
   180  	inter := IntersectString(slice1, slice2)
   181  	for _, v := range inter {
   182  		m[v] = true
   183  	}
   184  	for _, v := range slice1 {
   185  		if !m[v] {
   186  			n = append(n, v)
   187  		}
   188  	}
   189  
   190  	for _, v := range slice2 {
   191  		if !m[v] {
   192  			n = append(n, v)
   193  		}
   194  	}
   195  	return n
   196  }
   197  
   198  // Get the intersection of two slices
   199  func Intersect(slice1, slice2 []int64) []int64 {
   200  	m := make(map[int64]bool)
   201  	n := make([]int64, 0)
   202  	for _, v := range slice1 {
   203  		m[v] = true
   204  	}
   205  	for _, v := range slice2 {
   206  		flag, _ := m[v]
   207  		if flag {
   208  			n = append(n, v)
   209  		}
   210  	}
   211  	return n
   212  }
   213  
   214  // Get the diff of two slices
   215  func Difference(slice1, slice2 []int64) []int64 {
   216  	m := make(map[int64]bool)
   217  	n := make([]int64, 0)
   218  	inter := Intersect(slice1, slice2)
   219  	for _, v := range inter {
   220  		m[v] = true
   221  	}
   222  	for _, v := range slice1 {
   223  		if !m[v] {
   224  			n = append(n, v)
   225  		}
   226  	}
   227  
   228  	for _, v := range slice2 {
   229  		if !m[v] {
   230  			n = append(n, v)
   231  		}
   232  	}
   233  	return n
   234  }
   235  
   236  func GetHashCode(s string) uint32 {
   237  	return crc32.ChecksumIEEE([]byte(s))
   238  }
   239  
   240  // FormatString formats a string with a specified length and alignment.
   241  // `text` is the input string to format.
   242  // `length` is the desired length of the output string.
   243  // `alignLeft` specifies whether the string should be left-aligned (true) or right-aligned (false).
   244  func FormatString(text string, length int, alignLeft bool) string {
   245  	if len(text) > length {
   246  		// Truncate the string if it's longer than the desired length
   247  		return text[:length]
   248  	}
   249  
   250  	// Create a format string based on alignment preference
   251  	var formatStr string
   252  	if alignLeft {
   253  		formatStr = fmt.Sprintf("%%-%ds", length) // Left align
   254  	} else {
   255  		formatStr = fmt.Sprintf("%%%ds", length) // Right align
   256  	}
   257  
   258  	// Use the format string to format the text
   259  	return fmt.Sprintf(formatStr, text)
   260  }
   261  
   262  // CamelCaseToSpaceSeparated converts a camelCase string to a space-separated format
   263  func CamelCaseToSpaceSeparated(input string) string {
   264  	var result []rune
   265  	for i, r := range input {
   266  		if unicode.IsUpper(r) && i > 0 {
   267  			result = append(result, ' ')
   268  		}
   269  		result = append(result, unicode.ToLower(r))
   270  	}
   271  	return string(result)
   272  }
   273  
   274  // UpperFirst upper the first letter of the input string
   275  func UpperFirst(input string) string {
   276  	if len(input) == 0 {
   277  		return input
   278  	}
   279  	runes := []rune(input)
   280  	runes[0] = unicode.ToUpper(runes[0])
   281  	return string(runes)
   282  }
   283  
   284  // LowerFirst lower the first letter of the input string
   285  func LowerFirst(input string) string {
   286  	if len(input) == 0 {
   287  		return input
   288  	}
   289  	runes := []rune(input)
   290  	runes[0] = unicode.ToLower(runes[0])
   291  	return string(runes)
   292  }