kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

ctype.c (2154B)


      1 //===-- ctype.c - kit freestanding ctype primitives ----------------------===//
      2 //
      3 // SPDX-License-Identifier: 0BSD
      4 //===----------------------------------------------------------------------===//
      5 
      6 static unsigned int kit_ctype_byte(int c) {
      7   return (unsigned int)(unsigned char)c;
      8 }
      9 
     10 static int kit_isupper(unsigned int c) { return c >= 'A' && c <= 'Z'; }
     11 
     12 static int kit_islower(unsigned int c) { return c >= 'a' && c <= 'z'; }
     13 
     14 __attribute__((weak)) int isdigit(int c) {
     15   unsigned int ch = kit_ctype_byte(c);
     16   return ch >= '0' && ch <= '9';
     17 }
     18 
     19 __attribute__((weak)) int isupper(int c) {
     20   return kit_isupper(kit_ctype_byte(c));
     21 }
     22 
     23 __attribute__((weak)) int islower(int c) {
     24   return kit_islower(kit_ctype_byte(c));
     25 }
     26 
     27 __attribute__((weak)) int isalpha(int c) {
     28   unsigned int ch = kit_ctype_byte(c);
     29   return kit_isupper(ch) || kit_islower(ch);
     30 }
     31 
     32 __attribute__((weak)) int isalnum(int c) { return isalpha(c) || isdigit(c); }
     33 
     34 __attribute__((weak)) int isblank(int c) {
     35   unsigned int ch = kit_ctype_byte(c);
     36   return ch == ' ' || ch == '\t';
     37 }
     38 
     39 __attribute__((weak)) int iscntrl(int c) {
     40   unsigned int ch = kit_ctype_byte(c);
     41   return ch <= 0x1f || ch == 0x7f;
     42 }
     43 
     44 __attribute__((weak)) int isgraph(int c) {
     45   unsigned int ch = kit_ctype_byte(c);
     46   return ch >= 0x21 && ch <= 0x7e;
     47 }
     48 
     49 __attribute__((weak)) int isprint(int c) {
     50   unsigned int ch = kit_ctype_byte(c);
     51   return ch >= 0x20 && ch <= 0x7e;
     52 }
     53 
     54 __attribute__((weak)) int ispunct(int c) { return isgraph(c) && !isalnum(c); }
     55 
     56 __attribute__((weak)) int isspace(int c) {
     57   unsigned int ch = kit_ctype_byte(c);
     58   return ch == ' ' || (ch >= '\t' && ch <= '\r');
     59 }
     60 
     61 __attribute__((weak)) int isxdigit(int c) {
     62   unsigned int ch = kit_ctype_byte(c);
     63   return isdigit((int)ch) || (ch >= 'A' && ch <= 'F') ||
     64          (ch >= 'a' && ch <= 'f');
     65 }
     66 
     67 __attribute__((weak)) int tolower(int c) {
     68   if (c == -1) return c;
     69   unsigned int ch = kit_ctype_byte(c);
     70   if (kit_isupper(ch)) return (int)(ch - 'A' + 'a');
     71   return c;
     72 }
     73 
     74 __attribute__((weak)) int toupper(int c) {
     75   if (c == -1) return c;
     76   unsigned int ch = kit_ctype_byte(c);
     77   if (kit_islower(ch)) return (int)(ch - 'a' + 'A');
     78   return c;
     79 }