Home | History | Annotate | Download | only in strings
      1 // Character classification functions similar to standard <ctype.h>.
      2 // Some C++ implementations provide locale-sensitive implementations
      3 // of some <ctype.h> functions.  These ascii_* functions are
      4 // hard-wired for ASCII.  Hard-wired for ASCII is much faster.
      5 //
      6 // ascii_isalnum, ascii_isalpha, ascii_isascii, ascii_isblank,
      7 // ascii_iscntrl, ascii_isdigit, ascii_isgraph, ascii_islower,
      8 // ascii_isprint, ascii_ispunct, ascii_isspace, ascii_isupper,
      9 // ascii_isxdigit
     10 //   Similar to the <ctype.h> functions with similar names.
     11 //   Input parameter is an unsigned char.  Return value is a bool.
     12 //   If the input has a numerical value greater than 127
     13 //   then the output is "false".
     14 //
     15 // ascii_tolower, ascii_toupper
     16 //   Similar to the <ctype.h> functions with similar names.
     17 //   Input parameter is an unsigned char.  Return value is a char.
     18 //   If the input is not an ascii {lower,upper}-case letter
     19 //   (including numerical values greater than 127)
     20 //   then the output is the same as the input.
     21 
     22 #ifndef DYNAMIC_DEPTH_INTERNAL_STRINGS_ASCII_CTYPE_H_  // NOLINT
     23 #define DYNAMIC_DEPTH_INTERNAL_STRINGS_ASCII_CTYPE_H_  // NOLINT
     24 
     25 namespace dynamic_depth {
     26 
     27 // Array of character information.  This is an implementation detail.
     28 // The individual bits do not have names because the array definition is
     29 // already tightly coupled to these functions.  Names would just make it
     30 // harder to read and debug.
     31 
     32 extern const unsigned char kAsciiPropertyBits[256];
     33 
     34 // Public functions.
     35 
     36 static inline bool ascii_isalpha(unsigned char c) {
     37   return (kAsciiPropertyBits[c] & 0x01) != 0;
     38 }
     39 
     40 static inline bool ascii_isalnum(unsigned char c) {
     41   return (kAsciiPropertyBits[c] & 0x04) != 0;
     42 }
     43 
     44 static inline bool ascii_isspace(unsigned char c) {
     45   return (kAsciiPropertyBits[c] & 0x08) != 0;
     46 }
     47 
     48 static inline bool ascii_ispunct(unsigned char c) {
     49   return (kAsciiPropertyBits[c] & 0x10) != 0;
     50 }
     51 
     52 static inline bool ascii_isblank(unsigned char c) {
     53   return (kAsciiPropertyBits[c] & 0x20) != 0;
     54 }
     55 
     56 static inline bool ascii_iscntrl(unsigned char c) {
     57   return (kAsciiPropertyBits[c] & 0x40) != 0;
     58 }
     59 
     60 static inline bool ascii_isxdigit(unsigned char c) {
     61   return (kAsciiPropertyBits[c] & 0x80) != 0;
     62 }
     63 
     64 static inline bool ascii_isdigit(unsigned char c) {
     65   return c >= '0' && c <= '9';
     66 }
     67 
     68 static inline bool ascii_isprint(unsigned char c) { return c >= 32 && c < 127; }
     69 
     70 static inline bool ascii_isgraph(unsigned char c) { return c > 32 && c < 127; }
     71 
     72 static inline bool ascii_isupper(unsigned char c) {
     73   return c >= 'A' && c <= 'Z';
     74 }
     75 
     76 static inline bool ascii_islower(unsigned char c) {
     77   return c >= 'a' && c <= 'z';
     78 }
     79 
     80 static inline bool ascii_isascii(unsigned char c) { return c < 128; }
     81 
     82 extern const char kAsciiToLower[256];
     83 static inline char ascii_tolower(unsigned char c) { return kAsciiToLower[c]; }
     84 extern const char kAsciiToUpper[256];
     85 static inline char ascii_toupper(unsigned char c) { return kAsciiToUpper[c]; }
     86 
     87 }  // namespace dynamic_depth
     88 
     89 #endif  // DYNAMIC_DEPTH_INTERNAL_STRINGS_ASCII_CTYPE_H_  // NOLINT
     90