📄 ctype.c
字号:
/* Copyright (C) 2003 Manuel Novoa III * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! * * Besides uClibc, I'm using this code in my libc for elks, which is * a 16-bit environment with a fairly limited compiler. It would make * things much easier for me if this file isn't modified unnecessarily. * In particular, please put any new or replacement functions somewhere * else, and modify the makefile to use your version instead. * Thanks. Manuel * * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */#define _GNU_SOURCE#define __NO_CTYPE#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <limits.h>#include <stdint.h>#include <assert.h>#include <locale.h>#ifdef __UCLIBC_HAS_XLOCALE__#include <xlocale.h>#endif /* __UCLIBC_HAS_XLOCALE__ *//**********************************************************************/#ifdef __UCLIBC_HAS_CTYPE_TABLES__#ifdef __UCLIBC_HAS_CTYPE_SIGNED__#if EOF >= CHAR_MIN#define CTYPE_DOMAIN_CHECK(C) \ (((unsigned int)((C) - CHAR_MIN)) <= (UCHAR_MAX - CHAR_MIN))#else#define CTYPE_DOMAIN_CHECK(C) \ ((((unsigned int)((C) - CHAR_MIN)) <= (UCHAR_MAX - CHAR_MIN)) || ((C) == EOF))#endif#else /* __UCLIBC_HAS_CTYPE_SIGNED__ */#if EOF == -1#define CTYPE_DOMAIN_CHECK(C) \ (((unsigned int)((C) - EOF)) <= (UCHAR_MAX - EOF))#else#define CTYPE_DOMAIN_CHECK(C) \ ((((unsigned int)(C)) <= UCHAR_MAX) || ((C) == EOF))#endif#endif /* __UCLIBC_HAS_CTYPE_SIGNED__ */#endif /* __UCLIBC_HAS_CTYPE_TABLES__ *//**********************************************************************/#ifdef __UCLIBC_MJN3_ONLY__#ifdef L_isspace/* emit only once */#warning CONSIDER: Should we assert when debugging and __UCLIBC_HAS_CTYPE_CHECKED?#warning TODO: Fix asserts in to{upper|lower}{_l}.#warning TODO: Optimize the isx*() funcs.#endif#endif /* __UCLIBC_MJN3_ONLY__ *//**********************************************************************/#undef PASTE2#define PASTE2(X,Y) X ## Y#ifdef __UCLIBC_HAS_CTYPE_TABLES__#undef CTYPE_NAME#undef ISCTYPE#undef CTYPE_ALIAS#ifdef __UCLIBC_DO_XLOCALE#define CTYPE_NAME(X) __is ## X ## _l#define ISCTYPE(C,F) __isctype_l( C, F, locale_arg)#define CTYPE_ALIAS(NAME) weak_alias( __is ## NAME ## _l , is ## NAME ## _l)#else#define CTYPE_NAME(X) is ## X#define ISCTYPE(C,F) __isctype( C, F )#define CTYPE_ALIAS(NAME)#endif#undef CTYPE_BODY#if defined(__UCLIBC_HAS_CTYPE_ENFORCED__)/* Make sure assert is active for to*() funcs below. */#undef NDEBUG#include <assert.h>extern void __isctype_assert(int c, int mask) __attribute__ ((__noreturn__));#define CTYPE_BODY(NAME,C,MASK) \ if (CTYPE_DOMAIN_CHECK(C)) { \ return ISCTYPE(C, MASK); \ } \ __isctype_assert(C, MASK);#elif defined(__UCLIBC_HAS_CTYPE_CHECKED__)#define CTYPE_BODY(NAME,C,MASK) \ return CTYPE_DOMAIN_CHECK(C) \ ? ISCTYPE(C, MASK) \ : 0;#elif defined(__UCLIBC_HAS_CTYPE_UNSAFE__)#define CTYPE_BODY(NAME,C,MASK) \ return ISCTYPE(C, MASK);#else /* No checking done. */#error Unknown type of ctype checking!#endif#define IS_FUNC_BODY(NAME) \int CTYPE_NAME(NAME) (int c __LOCALE_PARAM ) \{ \ CTYPE_BODY(NAME,c,PASTE2(_IS,NAME)) \} \CTYPE_ALIAS(NAME)#else /* __UCLIBC_HAS_CTYPE_TABLES__ */#define C_MACRO(X) PASTE2(__C_is,X)(c)#define CTYPE_NAME(X) is ## X#define IS_FUNC_BODY(NAME) \int CTYPE_NAME(NAME) (int c) \{ \ return C_MACRO(NAME); \}#endif /* __UCLIBC_HAS_CTYPE_TABLES__ *//**********************************************************************/#ifdef L___ctype_assert#ifdef __UCLIBC_HAS_CTYPE_ENFORCED__extern const char *__progname;void __isctype_assert(int c, int mask){ fprintf(stderr, "%s: __is*{_l}(%d,%#x {locale})\n", __progname, c, mask); abort();}#endif#endif/**********************************************************************/#if defined(L_isalnum) || defined(L_isalnum_l)IS_FUNC_BODY(alnum);#endif/**********************************************************************/#if defined(L_isalpha) || defined(L_isalpha_l)IS_FUNC_BODY(alpha);#endif/**********************************************************************/#if defined(L_isblank) || defined(L_isblank_l)IS_FUNC_BODY(blank);#endif/**********************************************************************/#if defined(L_iscntrl) || defined(L_iscntrl_l)IS_FUNC_BODY(cntrl);#endif/**********************************************************************/#if defined(L_isdigit) || defined(L_isdigit_l)#ifdef __UCLIBC_HAS_CTYPE_TABLES__/* The standards require EOF < 0. */#if EOF >= CHAR_MIN#define __isdigit_char_or_EOF(C) __isdigit_char((C))#else#define __isdigit_char_or_EOF(C) __isdigit_int((C))#endifint CTYPE_NAME(digit) (int C __LOCALE_PARAM){#if defined(__UCLIBC_HAS_CTYPE_ENFORCED__) if (CTYPE_DOMAIN_CHECK(C)) { return __isdigit_char_or_EOF(C); /* C is (unsigned) char or EOF. */ } __isctype_assert(C, _ISdigit);#else return __isdigit_int(C); /* C could be invalid. */#endif}CTYPE_ALIAS(digit)#else /* __UCLIBC_HAS_CTYPE_TABLES__ */IS_FUNC_BODY(digit);#endif /* __UCLIBC_HAS_CTYPE_TABLES__ */#endif/**********************************************************************/#if defined(L_isgraph) || defined(L_isgraph_l)IS_FUNC_BODY(graph);#endif/**********************************************************************/#if defined(L_islower) || defined(L_islower_l)IS_FUNC_BODY(lower);#endif/**********************************************************************/#if defined(L_isprint) || defined(L_isprint_l)IS_FUNC_BODY(print);#endif/**********************************************************************/#if defined(L_ispunct) || defined(L_ispunct_l)IS_FUNC_BODY(punct);#endif/**********************************************************************/#if defined(L_isspace) || defined(L_isspace_l)IS_FUNC_BODY(space);#endif/**********************************************************************/#if defined(L_isupper) || defined(L_isupper_l)IS_FUNC_BODY(upper);#endif/**********************************************************************/#if defined(L_isxdigit) || defined(L_isxdigit_l)IS_FUNC_BODY(xdigit);#endif/**********************************************************************/#ifdef L_tolower#ifdef __UCLIBC_HAS_CTYPE_TABLES__int tolower(int c){#if defined(__UCLIBC_HAS_CTYPE_ENFORCED__) assert(CTYPE_DOMAIN_CHECK(c));#endif return __UCLIBC_CTYPE_IN_TO_DOMAIN(c) ? (__UCLIBC_CTYPE_TOLOWER)[c] : c;}#else /* __UCLIBC_HAS_CTYPE_TABLES__ */int tolower(int c){ return __C_tolower(c);}#endif /* __UCLIBC_HAS_CTYPE_TABLES__ */#endif/**********************************************************************/#ifdef L_tolower_l#undef tolower_l#undef __tolower_lint __tolower_l(int c, __locale_t l){#if defined(__UCLIBC_HAS_CTYPE_ENFORCED__) assert(CTYPE_DOMAIN_CHECK(c));#endif return __UCLIBC_CTYPE_IN_TO_DOMAIN(c) ? l->__ctype_tolower[c] : c;}weak_alias(__tolower_l, tolower_l)#endif/**********************************************************************/#ifdef L_toupper#ifdef __UCLIBC_HAS_CTYPE_TABLES__int toupper(int c){#if defined(__UCLIBC_HAS_CTYPE_ENFORCED__) assert(CTYPE_DOMAIN_CHECK(c));#endif return __UCLIBC_CTYPE_IN_TO_DOMAIN(c) ? (__UCLIBC_CTYPE_TOUPPER)[c] : c;}#else /* __UCLIBC_HAS_CTYPE_TABLES__ */int toupper(int c){ return __C_toupper(c);}#endif /* __UCLIBC_HAS_CTYPE_TABLES__ */#endif/**********************************************************************/#ifdef L_toupper_l#undef toupper_l#undef __toupper_lint __toupper_l(int c, __locale_t l){#if defined(__UCLIBC_HAS_CTYPE_ENFORCED__) assert(CTYPE_DOMAIN_CHECK(c));#endif return __UCLIBC_CTYPE_IN_TO_DOMAIN(c) ? l->__ctype_toupper[c] : c;}weak_alias(__toupper_l, toupper_l)#endif/**********************************************************************/#if defined(L_isascii) || defined(L_isascii_l)#ifdef __UCLIBC_HAS_CTYPE_TABLES__int __XL(isascii)(int c){ return __isascii(c); /* locale-independent */}__XL_ALIAS(isascii)#else /* __UCLIBC_HAS_CTYPE_TABLES__ */int isascii(int c){ return __isascii(c); /* locale-independent */}#endif /* __UCLIBC_HAS_CTYPE_TABLES__ */#endif/**********************************************************************/#if defined(L_toascii) || defined(L_toascii_l)#ifdef __UCLIBC_HAS_CTYPE_TABLES__int __XL(toascii)(int c){ return __toascii(c); /* locale-independent */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -