github.com/jxskiss/gopkg@v0.17.3/strutil/table.go (about)

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