[libc] Consolidate ctype

The ctype functions are now both macros and functions (as allowed by the
spec). They're now implemented in the ctype_b style of glibc, as
libunwind wants __ctype_b_loc to work.
This commit is contained in:
Justin C. Miller
2022-02-09 18:46:35 -08:00
parent 278876c19d
commit 4e3ba66b0c
14 changed files with 121 additions and 228 deletions

View File

@@ -14,6 +14,22 @@
extern "C" { extern "C" {
#endif #endif
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 isalnum( int ch );
int isalpha( int ch ); int isalpha( int ch );
int islower( int ch ); int islower( int ch );
@@ -27,6 +43,22 @@ int isblank( int ch );
int isprint( int ch ); int isprint( int ch );
int ispunct( 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 tolower( int ch );
int toupper( int ch ); int toupper( int ch );

View File

@@ -0,0 +1,78 @@
#include <ctype.h>
#include <stdint.h>
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"

View File

@@ -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 <ctype.h>
// Currently only implemented for "C" locale
int isalnum( int c ) {
return isalpha(c) || isdigit(c);
}

View File

@@ -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 <ctype.h>
// Currently only implemented for "C" locale.
int isalpha( int c ) {
return isupper(c) || islower(c);
}

View File

@@ -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 <ctype.h>
// Currently only implemented for "C" locale.
int isblank( int c ) {
return c == ' ' || c == '\t';
}

View File

@@ -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 <ctype.h>
// Currently only implemented for "C" locale.
int iscntrl( int c ) {
return (c < ' ') || (c > '~');
}

View File

@@ -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 <ctype.h>
// Currently only implemented for "C" locale.
int isdigit( int c ) {
return c >= '0' && c <= '9';
}

View File

@@ -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 <ctype.h>
// Currently only implemented for "C" locale.
int isgraph( int c ) {
return isprint(c) && c != ' ';
}

View File

@@ -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 <ctype.h>
// Currently only implemented for "C" locale.
int islower( int c ) {
return c >= 'a' && c <= 'z';
}

View File

@@ -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 <ctype.h>
// Currently only implemented for "C" locale.
int isprint( int c ) {
return !iscntrl(c);
}

View File

@@ -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 <ctype.h>
// Currently only implemented for "C" locale.
int ispunct( int c ) {
return isgraph(c) && !isalnum(c);
}

View File

@@ -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 <ctype.h>
// 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;
}
}

View File

@@ -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 <ctype.h>
// Currently only implemented for "C" locale.
int isupper( int c ) {
return c >= 'A' && c <= 'Z';
}

View File

@@ -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 <ctype.h>
// Currently only implemented for "C" locale.
int isxdigit( int c ) {
return
(c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'F') ||
(c >= 'a' && c <= 'f');
}