diff --git a/src/libraries/libc/ctype.h b/src/libraries/libc/ctype.h index 1388632..c4c2976 100644 --- a/src/libraries/libc/ctype.h +++ b/src/libraries/libc/ctype.h @@ -14,18 +14,50 @@ extern "C" { #endif -int isalnum( int ch ); -int isalpha( int ch ); -int islower( int ch ); -int isupper( int ch ); -int isdigit( int ch ); +enum { + __is_upper = 0x001, + __is_lower = 0x002, + __is_alpha = 0x004, + __is_digit = 0x008, + __is_xdigit = 0x010, + __is_space = 0x020, + __is_print = 0x040, + __is_graph = 0x080, + __is_blank = 0x100, + __is_cntrl = 0x200, + __is_punct = 0x400, + __is_alnum = 0x800 +}; + + +int isalnum( int ch ); +int isalpha( int ch ); +int islower( int ch ); +int isupper( int ch ); +int isdigit( int ch ); int isxdigit( int ch ); -int iscntrl( int ch ); -int isgraph( int ch ); -int isspace( int ch ); -int isblank( int ch ); -int isprint( int ch ); -int ispunct( int ch ); +int iscntrl( int ch ); +int isgraph( int ch ); +int isspace( int ch ); +int isblank( int ch ); +int isprint( int ch ); +int ispunct( int ch ); + + +extern unsigned short *__ctype_b; +#define __ctype_test(c, flag) (__ctype_b[(c)] | flag) +#define isalnum( ch ) __ctype_test(__is_alnum) +#define isalpha( ch ) __ctype_test(__is_alpha) +#define islower( ch ) __ctype_test(__is_lower) +#define isupper( ch ) __ctype_test(__is_upper) +#define isdigit( ch ) __ctype_test(__is_digit) +#define isxdigit( ch ) __ctype_test(__is_xdigit) +#define iscntrl( ch ) __ctype_test(__is_cntrl) +#define isgraph( ch ) __ctype_test(__is_graph) +#define isspace( ch ) __ctype_test(__is_space) +#define isblank( ch ) __ctype_test(__is_blank) +#define isprint( ch ) __ctype_test(__is_print) +#define ispunct( ch ) __ctype_test(__is_punct) int tolower( int ch ); int toupper( int ch ); diff --git a/src/libraries/libc/ctype/ctype_table.cpp b/src/libraries/libc/ctype/ctype_table.cpp new file mode 100644 index 0000000..ec5b905 --- /dev/null +++ b/src/libraries/libc/ctype/ctype_table.cpp @@ -0,0 +1,78 @@ +#include +#include + +namespace { + +// Only implemented for the "C" locale +uint16_t locale_c_table[] = { + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + +// 0 1 2 3 4 5 6 7 8 9 a b c d e f + 0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x1a0,0x0a0,0x0a0,0x0a0,0x0a0,0x200,0x200, // 0 + 0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x200,0x200, // 1 + 0x160,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0, // 2 + 0x8d8,0x8d8,0x8d8,0x8d8,0x8d8,0x8d8,0x8d8,0x8d8,0x8d8,0x8d8,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0, // 3 + 0x4a0,0x8d5,0x8d5,0x8d5,0x8d5,0x8d5,0x8d5,0x8c5,0x8c5,0x8c5,0x8c5,0x8c5,0x8c5,0x8c5,0x8c5,0x8c5, // 4 + 0x8c5,0x8c5,0x8c5,0x8c5,0x8c5,0x8c5,0x8c5,0x8c5,0x8c5,0x8c5,0x8c5,0x4a0,0x4a0,0x4a0,0x4a0,0x4a0, // 5 + 0x4a0,0x8d6,0x8d6,0x8d6,0x8d6,0x8d6,0x8d6,0x8c6,0x8c6,0x8c6,0x8c6,0x8c6,0x8c6,0x8c6,0x8c6,0x8c6, // 6 + 0x8c6,0x8c6,0x8c6,0x8c6,0x8c6,0x8c6,0x8c6,0x8c6,0x8c6,0x8c6,0x8c6,0x4a0,0x4a0,0x4a0,0x4a0,0x200, // 7 + + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, + 0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000, +}; + + +} // namespace + +unsigned short *__ctype_b = locale_c_table + 128; + +extern "C" { + +const unsigned short ** +__ctype_b_loc(void) +{ + return (const unsigned short **)&__ctype_b; +} + +#undef isalnum( ch ) +#undef isalpha( ch ) +#undef islower( ch ) +#undef isupper( ch ) +#undef isdigit( ch ) +#undef isxdigit( ch ) +#undef iscntrl( ch ) +#undef isgraph( ch ) +#undef isspace( ch ) +#undef isblank( ch ) +#undef isprint( ch ) +#undef ispunct( ch ) + +#define __ctype_impl(flag) int is##flag (int ch) { return __ctype_b[ch] | __is_##flag; } + +__ctype_impl(alnum) +__ctype_impl(alpha) +__ctype_impl(lower) +__ctype_impl(upper) +__ctype_impl(digit) +__ctype_impl(xdigit) +__ctype_impl(cntrl) +__ctype_impl(graph) +__ctype_impl(space) +__ctype_impl(blank) +__ctype_impl(print) +__ctype_impl(punct) + +} // extern "C" diff --git a/src/libraries/libc/ctype/isalnum.cpp b/src/libraries/libc/ctype/isalnum.cpp deleted file mode 100644 index 00af2f0..0000000 --- a/src/libraries/libc/ctype/isalnum.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** \file isalnum.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale - -int isalnum( int c ) { - return isalpha(c) || isdigit(c); -} diff --git a/src/libraries/libc/ctype/isalpha.cpp b/src/libraries/libc/ctype/isalpha.cpp deleted file mode 100644 index 9d71b5f..0000000 --- a/src/libraries/libc/ctype/isalpha.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** \file isalpha.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale. - -int isalpha( int c ) { - return isupper(c) || islower(c); -} diff --git a/src/libraries/libc/ctype/isblank.cpp b/src/libraries/libc/ctype/isblank.cpp deleted file mode 100644 index ede60ef..0000000 --- a/src/libraries/libc/ctype/isblank.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** \file isblank.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale. - -int isblank( int c ) { - return c == ' ' || c == '\t'; -} diff --git a/src/libraries/libc/ctype/iscntrl.cpp b/src/libraries/libc/ctype/iscntrl.cpp deleted file mode 100644 index 93bb5f0..0000000 --- a/src/libraries/libc/ctype/iscntrl.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** \file iscntrl.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale. - -int iscntrl( int c ) { - return (c < ' ') || (c > '~'); -} diff --git a/src/libraries/libc/ctype/isdigit.cpp b/src/libraries/libc/ctype/isdigit.cpp deleted file mode 100644 index bfc5ae3..0000000 --- a/src/libraries/libc/ctype/isdigit.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** \file isdigit.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale. - -int isdigit( int c ) { - return c >= '0' && c <= '9'; -} diff --git a/src/libraries/libc/ctype/isgraph.cpp b/src/libraries/libc/ctype/isgraph.cpp deleted file mode 100644 index 46ac78d..0000000 --- a/src/libraries/libc/ctype/isgraph.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** \file isgraph.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale. - -int isgraph( int c ) { - return isprint(c) && c != ' '; -} diff --git a/src/libraries/libc/ctype/islower.cpp b/src/libraries/libc/ctype/islower.cpp deleted file mode 100644 index 3cd80d7..0000000 --- a/src/libraries/libc/ctype/islower.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** \file islower.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale. - -int islower( int c ) { - return c >= 'a' && c <= 'z'; -} diff --git a/src/libraries/libc/ctype/isprint.cpp b/src/libraries/libc/ctype/isprint.cpp deleted file mode 100644 index 3b5b52f..0000000 --- a/src/libraries/libc/ctype/isprint.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** \file isprint.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale. - -int isprint( int c ) { - return !iscntrl(c); -} diff --git a/src/libraries/libc/ctype/ispunct.cpp b/src/libraries/libc/ctype/ispunct.cpp deleted file mode 100644 index ae885a1..0000000 --- a/src/libraries/libc/ctype/ispunct.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** \file ispunct.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale. - -int ispunct( int c ) { - return isgraph(c) && !isalnum(c); -} diff --git a/src/libraries/libc/ctype/isspace.cpp b/src/libraries/libc/ctype/isspace.cpp deleted file mode 100644 index 83ba93f..0000000 --- a/src/libraries/libc/ctype/isspace.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/** \file isspace.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale. - -int isspace( int c ) { - switch (c) { - case '\t': - case '\n': - case '\v': - case '\f': - case '\r': - case ' ': - return true; - default: - return false; - } -} diff --git a/src/libraries/libc/ctype/isupper.cpp b/src/libraries/libc/ctype/isupper.cpp deleted file mode 100644 index 6bae803..0000000 --- a/src/libraries/libc/ctype/isupper.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** \file isupper.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale. - -int isupper( int c ) { - return c >= 'A' && c <= 'Z'; -} diff --git a/src/libraries/libc/ctype/isxdigit.cpp b/src/libraries/libc/ctype/isxdigit.cpp deleted file mode 100644 index a470f12..0000000 --- a/src/libraries/libc/ctype/isxdigit.cpp +++ /dev/null @@ -1,20 +0,0 @@ -/** \file isxdigit.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -// Currently only implemented for "C" locale. - -int isxdigit( int c ) { - return - (c >= '0' && c <= '9') || - (c >= 'A' && c <= 'F') || - (c >= 'a' && c <= 'f'); -}