]> mj.ucw.cz Git - libucw.git/blob - ucw/chartype.h
Logging: Following streams by stderr is now configurable.
[libucw.git] / ucw / chartype.h
1 /*
2  *      UCW Library -- Character Types
3  *
4  *      (c) 1997--2004 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_CHARTYPE_H
11 #define _UCW_CHARTYPE_H
12
13 /***
14  * We define our own routines to classify 8-bit characters (based on US-ASCII charset).
15  * This way we bypass most possible problems with different compilation environments.
16  *
17  * All functions and macros accept any numbers and if it is necessary, they simply ignore higher bits.
18  * It does not matter whether a parameter is signed or unsigned. Parameters are evaluated exactly once,
19  * so they can have side-effects.
20  ***/
21
22 #define _C_UPPER 1                      /* Upper-case letters */
23 #define _C_LOWER 2                      /* Lower-case letters */
24 #define _C_PRINT 4                      /* Printable */
25 #define _C_DIGIT 8                      /* Digits */
26 #define _C_CTRL 16                      /* Control characters */
27 #define _C_XDIGIT 32                    /* Hexadecimal digits */
28 #define _C_BLANK 64                     /* White spaces (spaces, tabs, newlines) */
29 #define _C_INNER 128                    /* `inner punctuation' -- underscore etc. */
30
31 #define _C_ALPHA (_C_UPPER | _C_LOWER)
32 #define _C_ALNUM (_C_ALPHA | _C_DIGIT)
33 #define _C_WORD (_C_ALNUM | _C_INNER)
34 #define _C_WSTART (_C_ALPHA | _C_INNER)
35
36 extern const byte _c_cat[256], _c_upper[256], _c_lower[256];
37
38 #define Category(x) (_c_cat[(byte)(x)])
39 #define Ccat(x,y) (Category(x) & y)
40
41 #define Cupper(x) Ccat(x, _C_UPPER)     /** Checks for an upper-case character (`A-Z`). **/
42 #define Clower(x) Ccat(x, _C_LOWER)     /** Checks for a lower-case character (`a-z`). **/
43 #define Calpha(x) Ccat(x, _C_ALPHA)     /** Checks for an alphabetic character (`a-z`, `A-Z`). **/
44 #define Calnum(x) Ccat(x, _C_ALNUM)     /** Checks for an alpha-numeric character (`a-z`, `A-Z`, `0-9`). */
45 #define Cprint(x) Ccat(x, _C_PRINT)     /** Checks for printable characters, including 8-bit values (`\t`, `0x20-0x7E`, `0x80-0xFF`). **/
46 #define Cdigit(x) Ccat(x, _C_DIGIT)     /** Checks for a digit (`0-9`). **/
47 #define Cxdigit(x) Ccat(x, _C_XDIGIT)   /** Checks for a hexadecimal digit (`0-9`, `a-f`, `A-F`). **/
48 #define Cword(x) Ccat(x, _C_WORD)       /** Checks for an alpha-numeric character or an inner punctation (`a-z`, `A-Z`, `0-9`, `_`). **/
49 #define Cblank(x) Ccat(x, _C_BLANK)     /** Checks for a white space (`0x20`, `\t`, `\n`, `\r`, `0x8`, `0xC`). **/
50 #define Cctrl(x) Ccat(x, _C_CTRL)       /** Checks for control characters (`0x0-0x1F`, `0x7F`). **/
51 #define Cspace(x) Cblank(x)
52
53 #define Cupcase(x) (_c_upper[(byte)(x)]) /** Convert a letter to upper case, leave non-letter characters unchanged. **/
54 #define Clocase(x) (_c_lower[(byte)(x)]) /** Convert a letter to lower case, leave non-letter characters unchanged. **/
55
56 /**
57  * Compute the value of a valid hexadecimal character (ie. passed the @Cxdigit() check).
58  **/
59 static inline uns Cxvalue(byte x)
60 {
61   return (x < (uns)'A') ? x - '0' : (x & 0xdf) - 'A' + 10;
62 }
63
64 #endif