github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/utils/strutil/table.go (about)

     1  package strutil
     2  
     3  import "unicode"
     4  
     5  const (
     6  	Digits         = "0123456789"
     7  	HexDigits      = "0123456789abcdefABCDEF"
     8  	OctDigits      = "01234567"
     9  	UpperLetters   = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    10  	UpperAndDigits = UpperLetters + Digits
    11  	LowerLetters   = "abcdefghijklmnopqrstuvwxyz"
    12  	LowerAndDigits = LowerLetters + Digits
    13  	Letters        = UpperLetters + LowerLetters
    14  	AlphaDigits    = Letters + Digits
    15  
    16  	PasswordSpecial = "!#$%&()*+-./:;=?@[]^_~"
    17  )
    18  
    19  // IsASCII returns true if the string is empty or all characters in the
    20  // string are ASCII, false otherwise.
    21  func IsASCII(str string) bool {
    22  	for _, x := range str {
    23  		if x > unicode.MaxASCII {
    24  			return false
    25  		}
    26  	}
    27  	return true
    28  }
    29  
    30  // IsASCIIDigit returns true if all characters in the string are in range 0-9
    31  // and there is at least one character, false otherwise.
    32  func IsASCIIDigit(str string) bool {
    33  	if len(str) == 0 {
    34  		return false
    35  	}
    36  	for _, x := range str {
    37  		if !('0' <= x && x <= '9') {
    38  			return false
    39  		}
    40  	}
    41  	return true
    42  }
    43  
    44  // IsDigit returns true if all characters in the string are digits and
    45  // there is at least one character, false otherwise
    46  func IsDigit(str string) bool {
    47  	if len(str) == 0 {
    48  		return false
    49  	}
    50  	for _, x := range str {
    51  		if !unicode.IsDigit(x) {
    52  			return false
    53  		}
    54  	}
    55  	return true
    56  }
    57  
    58  // IsLower returns true if all cased characters in the string are lowercase
    59  // and there is at least one cased character, false otherwise.
    60  func IsLower(str string) bool {
    61  	hasCased := false
    62  	for _, x := range str {
    63  		isUpper, isLower := unicode.IsUpper(x), unicode.IsLower(x)
    64  		if isUpper || isLower {
    65  			hasCased = true
    66  			if isUpper {
    67  				return false
    68  			}
    69  		}
    70  	}
    71  	return hasCased
    72  }
    73  
    74  // IsUpper returns true if all cased characters in the string are uppercase
    75  // and there is at least one cased character, false otherwise.
    76  func IsUpper(str string) bool {
    77  	hasCased := false
    78  	for _, x := range str {
    79  		isUpper, isLower := unicode.IsUpper(x), unicode.IsLower(x)
    80  		if isUpper || isLower {
    81  			hasCased = true
    82  			if isLower {
    83  				return false
    84  			}
    85  		}
    86  	}
    87  	return hasCased
    88  }
    89  
    90  // IsPrintable returns true if all characters in the string are printable
    91  // or the string is empty, false otherwise.
    92  func IsPrintable(str string) bool {
    93  	for _, x := range str {
    94  		if !unicode.IsPrint(x) {
    95  			return false
    96  		}
    97  	}
    98  	return true
    99  }