123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
- //
- // This Source Code Form is subject to the terms of the MIT License.
- // If a copy of the MIT was not distributed with this file,
- // You can obtain one at https://github.com/gogf/gf.
- package utils
- import (
- "bytes"
- "strings"
- )
- var (
- // DefaultTrimChars are the characters which are stripped by Trim* functions in default.
- DefaultTrimChars = string([]byte{
- '\t', // Tab.
- '\v', // Vertical tab.
- '\n', // New line (line feed).
- '\r', // Carriage return.
- '\f', // New page.
- ' ', // Ordinary space.
- 0x00, // NUL-byte.
- 0x85, // Delete.
- 0xA0, // Non-breaking space.
- })
- )
- // IsLetterUpper checks whether the given byte b is in upper case.
- func IsLetterUpper(b byte) bool {
- if b >= byte('A') && b <= byte('Z') {
- return true
- }
- return false
- }
- // IsLetterLower checks whether the given byte b is in lower case.
- func IsLetterLower(b byte) bool {
- if b >= byte('a') && b <= byte('z') {
- return true
- }
- return false
- }
- // IsLetter checks whether the given byte b is a letter.
- func IsLetter(b byte) bool {
- return IsLetterUpper(b) || IsLetterLower(b)
- }
- // IsNumeric checks whether the given string s is numeric.
- // Note that float string like "123.456" is also numeric.
- func IsNumeric(s string) bool {
- var (
- dotCount = 0
- length = len(s)
- )
- if length == 0 {
- return false
- }
- for i := 0; i < length; i++ {
- if s[i] == '-' && i == 0 {
- continue
- }
- if s[i] == '.' {
- dotCount++
- if i > 0 && i < length-1 {
- continue
- } else {
- return false
- }
- }
- if s[i] < '0' || s[i] > '9' {
- return false
- }
- }
- return dotCount <= 1
- }
- // UcFirst returns a copy of the string s with the first letter mapped to its upper case.
- func UcFirst(s string) string {
- if len(s) == 0 {
- return s
- }
- if IsLetterLower(s[0]) {
- return string(s[0]-32) + s[1:]
- }
- return s
- }
- // ReplaceByMap returns a copy of `origin`,
- // which is replaced by a map in unordered way, case-sensitively.
- func ReplaceByMap(origin string, replaces map[string]string) string {
- for k, v := range replaces {
- origin = strings.ReplaceAll(origin, k, v)
- }
- return origin
- }
- // RemoveSymbols removes all symbols from string and lefts only numbers and letters.
- func RemoveSymbols(s string) string {
- var b = make([]rune, 0, len(s))
- for _, c := range s {
- if c > 127 {
- b = append(b, c)
- } else if (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') {
- b = append(b, c)
- }
- }
- return string(b)
- }
- // EqualFoldWithoutChars checks string `s1` and `s2` equal case-insensitively,
- // with/without chars '-'/'_'/'.'/' '.
- func EqualFoldWithoutChars(s1, s2 string) bool {
- return strings.EqualFold(RemoveSymbols(s1), RemoveSymbols(s2))
- }
- // SplitAndTrim splits string `str` by a string `delimiter` to an array,
- // and calls Trim to every element of this array. It ignores the elements
- // which are empty after Trim.
- func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
- array := make([]string, 0)
- for _, v := range strings.Split(str, delimiter) {
- v = Trim(v, characterMask...)
- if v != "" {
- array = append(array, v)
- }
- }
- return array
- }
- // Trim strips whitespace (or other characters) from the beginning and end of a string.
- // The optional parameter `characterMask` specifies the additional stripped characters.
- func Trim(str string, characterMask ...string) string {
- trimChars := DefaultTrimChars
- if len(characterMask) > 0 {
- trimChars += characterMask[0]
- }
- return strings.Trim(str, trimChars)
- }
- // FormatCmdKey formats string `s` as command key using uniformed format.
- func FormatCmdKey(s string) string {
- return strings.ToLower(strings.ReplaceAll(s, "_", "."))
- }
- // FormatEnvKey formats string `s` as environment key using uniformed format.
- func FormatEnvKey(s string) string {
- return strings.ToUpper(strings.ReplaceAll(s, ".", "_"))
- }
- // StripSlashes un-quotes a quoted string by AddSlashes.
- func StripSlashes(str string) string {
- var buf bytes.Buffer
- l, skip := len(str), false
- for i, char := range str {
- if skip {
- skip = false
- } else if char == '\\' {
- if i+1 < l && str[i+1] == '\\' {
- skip = true
- }
- continue
- }
- buf.WriteRune(char)
- }
- return buf.String()
- }
|