📄 wctype.c
字号:
/* Copyright (C) 2002 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 <wctype.h>#include <assert.h>#include <string.h>#include <errno.h>#include <locale.h>#include <ctype.h>/* We know wide char support is enabled. We wouldn't be here otherwise. *//* Define this if you want to unify the towupper and towlower code in the * towctrans function. *//* #define SMALL_UPLOW */#ifndef __LOCALE_C_ONLY#define __WCTYPE_WITH_LOCALE#endif/**********************************************************************/#ifndef __PASTE#define __PASTE(X,Y) X ## Y#endif#define C_MACRO(X) __PASTE(__C_,X)(wc)#define CT_MACRO(X) __PASTE(__ctype_,X)(wc)/**********************************************************************//* TODO: fix this! */#ifdef __WCTYPE_WITH_LOCALE#define WCctype (__global_locale.tblwctype)#define WCuplow (__global_locale.tblwuplow)#define WCcmob (__global_locale.tblwcomb)#define WCuplow_diff (__global_locale.tblwuplow_diff)#define ENCODING (__global_locale.encoding)#define ISW_FUNC_BODY(NAME) \int NAME (wint_t wc) \{ \ return iswctype(wc, __PASTE(_CTYPE_,NAME)); \}#else /* __WCTYPE_WITH_LOCALE */#define ISW_FUNC_BODY(NAME) \int NAME (wint_t wc) \{ \ return C_MACRO(NAME); \}#endif /* __WCTYPE_WITH_LOCALE *//**********************************************************************/#ifdef L_iswalnumISW_FUNC_BODY(iswalnum);#endif/**********************************************************************/#ifdef L_iswalphaISW_FUNC_BODY(iswalpha);#endif/**********************************************************************/#ifdef L_iswblankISW_FUNC_BODY(iswblank);#endif/**********************************************************************/#ifdef L_iswcntrlISW_FUNC_BODY(iswcntrl);#endif/**********************************************************************/#ifdef L_iswdigitint iswdigit(wint_t wc){ return __C_iswdigit(wc);}#endif/**********************************************************************/#ifdef L_iswgraphISW_FUNC_BODY(iswgraph);#endif/**********************************************************************/#ifdef L_iswlowerISW_FUNC_BODY(iswlower);#endif/**********************************************************************/#ifdef L_iswprintISW_FUNC_BODY(iswprint);#endif/**********************************************************************/#ifdef L_iswpunctISW_FUNC_BODY(iswpunct);#endif/**********************************************************************/#ifdef L_iswspaceISW_FUNC_BODY(iswspace);#endif/**********************************************************************/#ifdef L_iswupperISW_FUNC_BODY(iswupper);#endif/**********************************************************************/#ifdef L_iswxdigitint iswxdigit(wint_t wc){ return __C_iswxdigit(wc);}#endif/**********************************************************************/#ifdef L_towlower#ifdef __WCTYPE_WITH_LOCALE#ifdef SMALL_UPLOWwint_t towlower(wint_t wc){ return towctrans(wc, _CTYPE_tolower);}#elsewint_t towlower(wint_t wc){ unsigned int sc, n, i; __uwchar_t u = wc; if (ENCODING == __ctype_encoding_7_bit) { /* We're in the C/POSIX locale, so ignore the tables. */ return __C_towlower(wc); } if (u <= WC_TABLE_DOMAIN_MAX) { sc = u & ((1 << WCuplow_TI_SHIFT) - 1); u >>= WCuplow_TI_SHIFT; n = u & ((1 << WCuplow_II_SHIFT) - 1); u >>= WCuplow_II_SHIFT; i = ((unsigned int) WCuplow[u]) << WCuplow_II_SHIFT; i = ((unsigned int) WCuplow[WCuplow_II_LEN + i + n]) << WCuplow_TI_SHIFT; i = ((unsigned int) WCuplow[WCuplow_II_LEN + WCuplow_TI_LEN + i + sc]) << 1; wc += WCuplow_diff[i + 1]; } return wc;}#endif#else /* __WCTYPE_WITH_LOCALE */wint_t towlower(wint_t wc){ return __C_towlower(wc);}#endif /* __WCTYPE_WITH_LOCALE */#endif/**********************************************************************/#ifdef L_towupper#ifdef __WCTYPE_WITH_LOCALE#ifdef SMALL_UPLOWwint_t towupper(wint_t wc){ return towctrans(wc, _CTYPE_toupper);}#elsewint_t towupper(wint_t wc){ unsigned int sc, n, i; __uwchar_t u = wc; if (ENCODING == __ctype_encoding_7_bit) { /* We're in the C/POSIX locale, so ignore the tables. */ return __C_towupper(wc); } if (u <= WC_TABLE_DOMAIN_MAX) { sc = u & ((1 << WCuplow_TI_SHIFT) - 1); u >>= WCuplow_TI_SHIFT; n = u & ((1 << WCuplow_II_SHIFT) - 1); u >>= WCuplow_II_SHIFT; i = ((unsigned int) WCuplow[u]) << WCuplow_II_SHIFT; i = ((unsigned int) WCuplow[WCuplow_II_LEN + i + n]) << WCuplow_TI_SHIFT; i = ((unsigned int) WCuplow[WCuplow_II_LEN + WCuplow_TI_LEN + i + sc]) << 1; wc += WCuplow_diff[i]; } return wc;}#endif#else /* __WCTYPE_WITH_LOCALE */wint_t towupper(wint_t wc){ return __C_towupper(wc);}#endif /* __WCTYPE_WITH_LOCALE */#endif/**********************************************************************/#ifdef L_wctypestatic const unsigned char typestring[] = __CTYPE_TYPESTRING;/* extern const unsigned char typestring[]; */wctype_t wctype(const char *property){ const unsigned char *p; int i; p = typestring; i = 1; do { if (!strcmp(property, ++p)) { return i; } ++i; p += p[-1]; } while (*p); /* TODO - Add locale-specific classifications. */ return 0;}#endif/**********************************************************************/#ifdef L_iswctype#ifdef __UCLIBC_MJN3_ONLY__#warning duh... replace the range-based classification with table lookup!#endif#ifdef __WCTYPE_WITH_LOCALE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -