Implemented BeOS compatible ctype functions.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@8332 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2004-07-06 00:30:37 +00:00
parent 13fde991bd
commit ead2658b25

View File

@ -1,126 +1,299 @@
/* /*
** Copyright 2001, Travis Geiselbrecht. All rights reserved. ** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the NewOS License. ** Distributed under the terms of the Haiku License.
*/ */
#include <ctype.h> #include <ctype.h>
#define _U 0x01 /* upper */
#define _L 0x02 /* lower */
#define _D 0x04 /* digit */
#define _C 0x08 /* cntrl */
#define _P 0x10 /* punct */
#define _S 0x20 /* white space (space/lf/tab) */
#define _X 0x40 /* hex digit */
#define _SP 0x80 /* hard space (0x20) */
#define ismask(x) (_ctype[(int)(unsigned char)(x)]) // disable macros defined in ctype.h
#undef isalnum
#undef isalpha
#undef isascii
#undef isblank
#undef iscntrl
#undef isdigit
#undef islower
#undef isgraph
#undef isprint
#undef ispunct
#undef isspace
#undef isupper
#undef isxdigit
#undef toascii
#undef tolower
#undef toupper
static unsigned char _ctype[] = {
_C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */
_C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */
_C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */
_C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */
_S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */
_P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */
_D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */
_D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */
_P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */
_U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */
_U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */
_U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */
_P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */
_L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */
_L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */
_L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
_S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 160-175 */
_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 176-191 */
_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U, /* 192-207 */
_U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L, /* 208-223 */
_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L, /* 224-239 */
_L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */
int tolower(int c) // standard ASCII tables
// ToDo: These should probably be revised for ISO-8859-1
// ToDo: change the tables depending on the current locale
static unsigned short int __ctype_b_internal[] = {
/* 0 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 8 */ _IScntrl, _ISblank|_IScntrl|_ISspace, _IScntrl|_ISspace, _IScntrl|_ISspace, _IScntrl|_ISspace, _IScntrl|_ISspace, _IScntrl, _IScntrl,
/* 16 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 24 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 32 */ _ISblank|_ISspace|_ISprint, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph,
/* 40 */ _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph,
/* 48 */ _ISalnum|_ISdigit|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISdigit|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISdigit|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISdigit|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISdigit|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISdigit|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISdigit|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISdigit|_ISxdigit|_ISprint|_ISgraph,
/* 56 */ _ISalnum|_ISdigit|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISdigit|_ISxdigit|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph,
/* 64 */ _ISpunct|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph,
/* 72 */ _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph,
/* 80 */ _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph,
/* 88 */ _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISupper|_ISalpha|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph,
/* 96 */ _ISpunct|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISxdigit|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph,
/* 104 */ _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph,
/* 112 */ _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph,
/* 120 */ _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISalnum|_ISlower|_ISalpha|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _ISpunct|_ISprint|_ISgraph, _IScntrl,
/* 128 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 136 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 144 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 152 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 160 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 168 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 176 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 184 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 192 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 200 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 208 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 216 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 224 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 232 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 240 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
/* 248 */ _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl, _IScntrl,
};
const int __ctype_tolower_internal[] = {
/* 0 */ 0, 1, 2, 3, 4, 5, 6, 7,
/* 8 */ 8, 9, 10, 11, 12, 13, 14, 15,
/* 16 */ 16, 17, 18, 19, 20, 21, 22, 23,
/* 24 */ 24, 25, 26, 27, 28, 29, 30, 31,
/* 32 */ 32, 33, 34, 35, 36, 37, 38, 39,
/* 40 */ 40, 41, 42, 43, 44, 45, 46, 47,
/* 48 */ '0', '1', '2', '3', '4', '5', '6', '7',
/* 56 */ '8', '9', 58, 59, 60, 61, 62, 63,
/* 64 */ 64, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
/* 72 */ 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
/* 80 */ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
/* 88 */ 'x', 'y', 'z', 91, 92, 93, 94, 95,
/* 96 */ 96, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
/* 104 */ 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
/* 112 */ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
/* 120 */ 'x', 'y', 'z', 123, 124, 125, 126, 127,
/* 128 */ 128, 129, 130, 131, 132, 133, 134, 135,
/* 136 */ 136, 137, 138, 139, 140, 141, 142, 143,
/* 144 */ 144, 145, 146, 147, 148, 149, 150, 151,
/* 152 */ 152, 153, 154, 155, 156, 157, 158, 159,
/* 160 */ 160, 161, 162, 163, 164, 165, 166, 167,
/* 168 */ 168, 169, 170, 171, 172, 173, 174, 175,
/* 176 */ 176, 177, 178, 179, 180, 181, 182, 183,
/* 184 */ 184, 185, 186, 187, 188, 189, 190, 191,
/* 192 */ 192, 193, 194, 195, 196, 197, 198, 199,
/* 200 */ 200, 201, 202, 203, 204, 205, 206, 207,
/* 208 */ 208, 209, 210, 211, 212, 213, 214, 215,
/* 216 */ 216, 217, 218, 219, 220, 221, 222, 223,
/* 224 */ 224, 225, 226, 227, 228, 229, 230, 231,
/* 232 */ 232, 233, 234, 235, 236, 237, 238, 239,
/* 240 */ 240, 241, 242, 243, 244, 245, 246, 247,
/* 248 */ 248, 249, 250, 251, 252, 253, 254, 255,
};
const int __ctype_toupper_internal[] = {
/* 0 */ 0, 1, 2, 3, 4, 5, 6, 7,
/* 8 */ 8, 9, 10, 11, 12, 13, 14, 15,
/* 16 */ 16, 17, 18, 19, 20, 21, 22, 23,
/* 24 */ 24, 25, 26, 27, 28, 29, 30, 31,
/* 32 */ 32, 33, 34, 35, 36, 37, 38, 39,
/* 40 */ 40, 41, 42, 43, 44, 45, 46, 47,
/* 48 */ '0', '1', '2', '3', '4', '5', '6', '7',
/* 56 */ '8', '9', 58, 59, 60, 61, 62, 63,
/* 64 */ 64, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
/* 72 */ 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
/* 80 */ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
/* 88 */ 'X', 'Y', 'Z', 91, 92, 93, 94, 95,
/* 96 */ 96, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
/* 104 */ 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
/* 112 */ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
/* 120 */ 'X', 'Y', 'Z', 123, 124, 125, 126, 127,
/* 128 */ 128, 129, 130, 131, 132, 133, 134, 135,
/* 136 */ 136, 137, 138, 139, 140, 141, 142, 143,
/* 144 */ 144, 145, 146, 147, 148, 149, 150, 151,
/* 152 */ 152, 153, 154, 155, 156, 157, 158, 159,
/* 160 */ 160, 161, 162, 163, 164, 165, 166, 167,
/* 168 */ 168, 169, 170, 171, 172, 173, 174, 175,
/* 176 */ 176, 177, 178, 179, 180, 181, 182, 183,
/* 184 */ 184, 185, 186, 187, 188, 189, 190, 191,
/* 192 */ 192, 193, 194, 195, 196, 197, 198, 199,
/* 200 */ 200, 201, 202, 203, 204, 205, 206, 207,
/* 208 */ 208, 209, 210, 211, 212, 213, 214, 215,
/* 216 */ 216, 217, 218, 219, 220, 221, 222, 223,
/* 224 */ 224, 225, 226, 227, 228, 229, 230, 231,
/* 232 */ 232, 233, 234, 235, 236, 237, 238, 239,
/* 240 */ 240, 241, 242, 243, 244, 245, 246, 247,
/* 248 */ 248, 249, 250, 251, 252, 253, 254, 255,
};
const unsigned short int *__ctype_b = __ctype_b_internal;
const int *__ctype_tolower = __ctype_tolower_internal;
const int *__ctype_toupper = __ctype_toupper_internal;
int
isalnum(int c)
{ {
if(isupper(c)) if (c >= 0 && c < 256)
c -= 'A'-'a'; return __ctype_b[c] & (_ISupper | _ISlower | _ISdigit);
return 0;
}
int
isalpha(int c)
{
if (c >= 0 && c < 256)
return __ctype_b[c] & (_ISupper | _ISlower);
return 0;
}
int
isascii(int c)
{
/* ASCII characters have bit 8 cleared */
return (c & ~0x7f) == 0;
}
int
isblank(int c)
{
if (c >= 0 && c < 256)
return __ctype_b[c] & _ISblank;
return 0;
}
int
iscntrl(int c)
{
if (c >= 0 && c < 256)
return __ctype_b[c] & _IScntrl;
return 0;
}
int
isdigit(int c)
{
if (c >= 0 && c < 256)
return __ctype_b[c] & _ISdigit;
return 0;
}
int
isgraph(int c)
{
if (c >= 0 && c < 256)
return __ctype_b[c] & _ISgraph;
return 0;
}
int
islower(int c)
{
if (c >= 0 && c < 256)
return __ctype_b[c] & _ISlower;
return 0;
}
int
isprint(int c)
{
if (c >= 0 && c < 256)
return __ctype_b[c] & _ISprint;
return 0;
}
int
ispunct(int c)
{
if (c >= 0 && c < 256)
return __ctype_b[c] & _ISpunct;
return 0;
}
int
isspace(int c)
{
if (c >= 0 && c < 256)
return __ctype_b[c] & _ISspace;
return 0;
}
int
isupper(int c)
{
if (c >= 0 && c < 256)
return __ctype_b[c] & _ISupper;
return 0;
}
int
isxdigit(int c)
{
if (c >= 0 && c < 256)
return __ctype_b[c] & _ISxdigit;
return 0;
}
int
toascii(int c)
{
/* Clear higher bits */
return c & 0x7f;
}
int
tolower(int c)
{
if (c >= 0 && c < 256)
return __ctype_tolower[c];
return c; return c;
} }
int toupper(int c)
int
toupper(int c)
{ {
if(islower(c)) if (c >= 0 && c < 256)
c -= 'a'-'A'; return __ctype_toupper[c];
return c; return c;
} }
int isalnum(int c)
{
return ((ismask(c)&(_U|_L|_D)) != 0);
}
int isalpha(int c)
{
return ((ismask(c)&(_U|_L)) != 0);
}
int iscntrl(int c)
{
return ((ismask(c)&(_C)) != 0);
}
int isdigit(int c)
{
return ((ismask(c)&(_D)) != 0);
}
int isgraph(int c)
{
return ((ismask(c)&(_P|_U|_L|_D)) != 0);
}
int islower(int c)
{
return ((ismask(c)&(_L)) != 0);
}
int isprint(int c)
{
return ((ismask(c)&(_P|_U|_L|_D|_SP)) != 0);
}
int ispunct(int c)
{
return ((ismask(c)&(_P)) != 0);
}
int isspace(int c)
{
return ((ismask(c)&(_S)) != 0);
}
int isupper(int c)
{
return ((ismask(c)&(_U)) != 0);
}
int isxdigit(int c)
{
return ((ismask(c)&(_D|_X)) != 0);
}
int isascii(int c)
{
return ((unsigned char)c <= 0x7f);
}
int toascii(int c)
{
return ((unsigned char)c & 0x7f);
}
int isblank(int c)
{
return (((unsigned char)c == ' ') || ((unsigned char)c == '\t'));
}