gitee.com/quant1x/gox@v1.21.2/api/string.go (about)

     1  // Copyright (c) 2015, Emir Pasic. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package utils provides common utility functions.
     6  //
     7  // Provided functionalities:
     8  // - sorting
     9  // - comparators
    10  
    11  package api
    12  
    13  import (
    14  	"fmt"
    15  	"strconv"
    16  	"strings"
    17  )
    18  
    19  // ToString converts a value to string.
    20  func ToString(value interface{}) string {
    21  	switch value := value.(type) {
    22  	case string:
    23  		return value
    24  	case int8:
    25  		return strconv.FormatInt(int64(value), 10)
    26  	case int16:
    27  		return strconv.FormatInt(int64(value), 10)
    28  	case int32:
    29  		return strconv.FormatInt(int64(value), 10)
    30  	case int64:
    31  		return strconv.FormatInt(int64(value), 10)
    32  	case uint8:
    33  		return strconv.FormatUint(uint64(value), 10)
    34  	case uint16:
    35  		return strconv.FormatUint(uint64(value), 10)
    36  	case uint32:
    37  		return strconv.FormatUint(uint64(value), 10)
    38  	case uint64:
    39  		return strconv.FormatUint(uint64(value), 10)
    40  	case float32:
    41  		return strconv.FormatFloat(float64(value), 'g', -1, 64)
    42  	case float64:
    43  		return strconv.FormatFloat(float64(value), 'g', -1, 64)
    44  	case bool:
    45  		return strconv.FormatBool(value)
    46  	default:
    47  		return fmt.Sprintf("%+v", value)
    48  	}
    49  }
    50  
    51  // ToCamelCase 转驼峰
    52  func ToCamelCase(kebab string) (camelCase string) {
    53  	isToUpper := false
    54  	for _, runeValue := range kebab {
    55  		if isToUpper {
    56  			camelCase += strings.ToUpper(string(runeValue))
    57  			isToUpper = false
    58  		} else {
    59  			if runeValue == '-' || runeValue == '_' {
    60  				isToUpper = true
    61  			} else {
    62  				camelCase += string(runeValue)
    63  			}
    64  		}
    65  	}
    66  	return
    67  }
    68  
    69  // StartsWith 字符串前缀判断
    70  func StartsWith(str string, prefixes []string) bool {
    71  	if len(str) == 0 || len(prefixes) == 0 {
    72  		return false
    73  	}
    74  	for _, prefix := range prefixes {
    75  		if strings.HasPrefix(str, prefix) {
    76  			return true
    77  		}
    78  	}
    79  	return false
    80  }
    81  
    82  // EndsWith 字符串前缀判断
    83  func EndsWith(str string, suffixes []string) bool {
    84  	if len(str) == 0 || len(suffixes) == 0 {
    85  		return false
    86  	}
    87  	for _, prefix := range suffixes {
    88  		if strings.HasSuffix(str, prefix) {
    89  			return true
    90  		}
    91  	}
    92  	return false
    93  }
    94  
    95  // IsEmpty Code to test if string is empty
    96  func IsEmpty(s string) bool {
    97  	if strings.TrimSpace(s) == "" {
    98  		return true
    99  	} else {
   100  		return false
   101  	}
   102  }