github.com/gogf/gf/v2@v2.7.4/internal/utils/utils_str.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package utils
     8  
     9  import (
    10  	"bytes"
    11  	"strings"
    12  )
    13  
    14  var (
    15  	// DefaultTrimChars are the characters which are stripped by Trim* functions in default.
    16  	DefaultTrimChars = string([]byte{
    17  		'\t', // Tab.
    18  		'\v', // Vertical tab.
    19  		'\n', // New line (line feed).
    20  		'\r', // Carriage return.
    21  		'\f', // New page.
    22  		' ',  // Ordinary space.
    23  		0x00, // NUL-byte.
    24  		0x85, // Delete.
    25  		0xA0, // Non-breaking space.
    26  	})
    27  )
    28  
    29  // IsLetterUpper checks whether the given byte b is in upper case.
    30  func IsLetterUpper(b byte) bool {
    31  	if b >= byte('A') && b <= byte('Z') {
    32  		return true
    33  	}
    34  	return false
    35  }
    36  
    37  // IsLetterLower checks whether the given byte b is in lower case.
    38  func IsLetterLower(b byte) bool {
    39  	if b >= byte('a') && b <= byte('z') {
    40  		return true
    41  	}
    42  	return false
    43  }
    44  
    45  // IsLetter checks whether the given byte b is a letter.
    46  func IsLetter(b byte) bool {
    47  	return IsLetterUpper(b) || IsLetterLower(b)
    48  }
    49  
    50  // IsNumeric checks whether the given string s is numeric.
    51  // Note that float string like "123.456" is also numeric.
    52  func IsNumeric(s string) bool {
    53  	var (
    54  		dotCount = 0
    55  		length   = len(s)
    56  	)
    57  	if length == 0 {
    58  		return false
    59  	}
    60  	for i := 0; i < length; i++ {
    61  		if (s[i] == '-' || s[i] == '+') && i == 0 {
    62  			if length == 1 {
    63  				return false
    64  			}
    65  			continue
    66  		}
    67  		if s[i] == '.' {
    68  			dotCount++
    69  			if i > 0 && i < length-1 {
    70  				continue
    71  			} else {
    72  				return false
    73  			}
    74  		}
    75  		if s[i] < '0' || s[i] > '9' {
    76  			return false
    77  		}
    78  	}
    79  	return dotCount <= 1
    80  }
    81  
    82  // UcFirst returns a copy of the string s with the first letter mapped to its upper case.
    83  func UcFirst(s string) string {
    84  	if len(s) == 0 {
    85  		return s
    86  	}
    87  	if IsLetterLower(s[0]) {
    88  		return string(s[0]-32) + s[1:]
    89  	}
    90  	return s
    91  }
    92  
    93  // ReplaceByMap returns a copy of `origin`,
    94  // which is replaced by a map in unordered way, case-sensitively.
    95  func ReplaceByMap(origin string, replaces map[string]string) string {
    96  	for k, v := range replaces {
    97  		origin = strings.ReplaceAll(origin, k, v)
    98  	}
    99  	return origin
   100  }
   101  
   102  // RemoveSymbols removes all symbols from string and lefts only numbers and letters.
   103  func RemoveSymbols(s string) string {
   104  	var b = make([]rune, 0, len(s))
   105  	for _, c := range s {
   106  		if c > 127 {
   107  			b = append(b, c)
   108  		} else if (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') {
   109  			b = append(b, c)
   110  		}
   111  	}
   112  	return string(b)
   113  }
   114  
   115  // EqualFoldWithoutChars checks string `s1` and `s2` equal case-insensitively,
   116  // with/without chars '-'/'_'/'.'/' '.
   117  func EqualFoldWithoutChars(s1, s2 string) bool {
   118  	return strings.EqualFold(RemoveSymbols(s1), RemoveSymbols(s2))
   119  }
   120  
   121  // SplitAndTrim splits string `str` by a string `delimiter` to an array,
   122  // and calls Trim to every element of this array. It ignores the elements
   123  // which are empty after Trim.
   124  func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
   125  	array := make([]string, 0)
   126  	for _, v := range strings.Split(str, delimiter) {
   127  		v = Trim(v, characterMask...)
   128  		if v != "" {
   129  			array = append(array, v)
   130  		}
   131  	}
   132  	return array
   133  }
   134  
   135  // Trim strips whitespace (or other characters) from the beginning and end of a string.
   136  // The optional parameter `characterMask` specifies the additional stripped characters.
   137  func Trim(str string, characterMask ...string) string {
   138  	trimChars := DefaultTrimChars
   139  	if len(characterMask) > 0 {
   140  		trimChars += characterMask[0]
   141  	}
   142  	return strings.Trim(str, trimChars)
   143  }
   144  
   145  // FormatCmdKey formats string `s` as command key using uniformed format.
   146  func FormatCmdKey(s string) string {
   147  	return strings.ToLower(strings.ReplaceAll(s, "_", "."))
   148  }
   149  
   150  // FormatEnvKey formats string `s` as environment key using uniformed format.
   151  func FormatEnvKey(s string) string {
   152  	return strings.ToUpper(strings.ReplaceAll(s, ".", "_"))
   153  }
   154  
   155  // StripSlashes un-quotes a quoted string by AddSlashes.
   156  func StripSlashes(str string) string {
   157  	var buf bytes.Buffer
   158  	l, skip := len(str), false
   159  	for i, char := range str {
   160  		if skip {
   161  			skip = false
   162  		} else if char == '\\' {
   163  			if i+1 < l && str[i+1] == '\\' {
   164  				skip = true
   165  			}
   166  			continue
   167  		}
   168  		buf.WriteRune(char)
   169  	}
   170  	return buf.String()
   171  }