12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- // Copyright 2019 gf Author(https://github.com/gogf/gf). 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 (
- "strings"
- )
- // 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 {
- length := len(s)
- if length == 0 {
- return false
- }
- for i := 0; i < len(s); i++ {
- if s[i] == '-' && i == 0 {
- continue
- }
- if s[i] == '.' {
- if i > 0 && i < len(s)-1 {
- continue
- } else {
- return false
- }
- }
- if s[i] < '0' || s[i] > '9' {
- return false
- }
- }
- return true
- }
- // 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.Replace(origin, k, v, -1)
- }
- return origin
- }
- // RemoveSymbols removes all symbols from string and lefts only numbers and letters.
- func RemoveSymbols(s string) string {
- var b []byte
- for _, c := range s {
- if (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') {
- b = append(b, byte(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))
- }
|