github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekastr/char_report.go (about)

     1  // Copyright © 2020. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekastr
     7  
     8  //goland:noinspection GoSnakeCaseUsage
     9  const (
    10  
    11  	/*
    12  		There are constants for CharReport function.
    13  		CharReport may return any of these variables.
    14  	*/
    15  
    16  	CR_NUMBER            = int8(1)
    17  	CR_UPPER_CASE_LETTER = int8(2)
    18  	CR_LOWER_CASE_LETTER = int8(3)
    19  	CR_WHITESPACE        = int8(4)
    20  )
    21  
    22  /*
    23  CharReport reports whether passed byte is. A letter? A number? A whitespace?
    24  Use predefined constants with "CR_" prefix to compare with return value,
    25  or specified methods.
    26  Returns -1 for those bytes that are not covers by "CR_" constants.
    27  */
    28  func CharReport(b byte) int8 {
    29  	switch {
    30  
    31  	case b >= '0' && b <= '9':
    32  		return CR_NUMBER
    33  
    34  	case b >= 'A' && b <= 'Z':
    35  		return CR_UPPER_CASE_LETTER
    36  
    37  	case b >= 'a' && b <= 'z':
    38  		return CR_LOWER_CASE_LETTER
    39  
    40  	case b <= 0x32:
    41  		return CR_WHITESPACE
    42  
    43  	default:
    44  		return -1
    45  	}
    46  }
    47  
    48  /*
    49  CharIsLetter reports whether b is the range [A..Z] + [a..z] of ASCII table.
    50  */
    51  func CharIsLetter(b byte) bool {
    52  	return CharIsUpperCaseLetter(b) || CharIsLowerCaseLetter(b)
    53  }
    54  
    55  /*
    56  CharIsUpperCaseLetter reports whether b is in the range [A..Z] of ASCII table.
    57  */
    58  func CharIsUpperCaseLetter(b byte) bool {
    59  	return CharReport(b) == CR_UPPER_CASE_LETTER
    60  }
    61  
    62  /*
    63  CharIsLowerCaseLetter reports whether b is in the range [a..z] of ASCII table.
    64  */
    65  func CharIsLowerCaseLetter(b byte) bool {
    66  	return CharReport(b) == CR_LOWER_CASE_LETTER
    67  }
    68  
    69  /*
    70  CharIsNumber reports whether b is in the range [0..9] of ASCII table.
    71  */
    72  func CharIsNumber(b byte) bool {
    73  	return CharReport(b) == CR_NUMBER
    74  }
    75  
    76  /*
    77  CharIsWhitespace reports whether b is in the range [0x00..0x20] of ASCII table.
    78  */
    79  func CharIsWhitespace(b byte) bool {
    80  	return CharReport(b) == CR_WHITESPACE
    81  }