qse/ase/lib/cmn/chr.c

146 lines
3.1 KiB
C
Raw Normal View History

2008-08-21 02:24:36 +00:00
/*
* $Id: ctype.c 132 2008-03-17 10:27:02Z baconevi $
*
* {License}
*/
#include <ase/cmn/chr.h>
#if defined(ASE_CHAR_IS_MCHAR)
#include <ctype.h>
2008-08-27 02:50:12 +00:00
static ase_bool_t is_upper (ase_cint_t c) { return isupper(c); }
static ase_bool_t is_lower (ase_cint_t c) { return islower(c); }
static ase_bool_t is_alpha (ase_cint_t c) { return isalpha(c); }
static ase_bool_t is_digit (ase_cint_t c) { return isdigit(c); }
static ase_bool_t is_xdigit (ase_cint_t c) { return isxdigit(c); }
static ase_bool_t is_alnum (ase_cint_t c) { return isalnum(c); }
static ase_bool_t is_space (ase_cint_t c) { return isspace(c); }
static ase_bool_t is_print (ase_cint_t c) { return isprint(c); }
static ase_bool_t is_graph (ase_cint_t c) { return isgraph(c); }
static ase_bool_t is_cntrl (ase_cint_t c) { return iscntrl(c); }
static ase_bool_t is_punct (ase_cint_t c) { return ispunct(c); }
2008-10-14 09:04:24 +00:00
ase_bool_t ase_ccls_is (ase_cint_t c, ase_ccls_type_t type)
2008-08-21 02:24:36 +00:00
{
/* TODO: use GetStringTypeW/A for WIN32 to implement these */
2008-08-27 02:50:12 +00:00
static ase_bool_t (*f[]) (ase_cint_t) =
{
is_upper,
is_lower,
is_alpha,
is_digit,
is_xdigit,
is_alnum,
is_space,
is_print,
is_graph,
is_cntrl,
is_punct
};
ASE_ASSERTX (type >= ASE_CCLS_UPPER && type <= ASE_CCLS_PUNCT,
"The character type should be one of ase_ccls_type_t values");
return f[type] (c);
2008-08-21 02:24:36 +00:00
}
2008-10-14 09:04:24 +00:00
ase_cint_t ase_ccls_to (ase_cint_t c, ase_ccls_type_t type)
2008-08-21 02:24:36 +00:00
{
ASE_ASSERTX (type >= ASE_CCLS_UPPER && type <= ASE_CCLS_LOWER,
"The character type should be one of ASE_CCLS_UPPER and ASE_CCLS_LOWER");
if (type == ASE_CCLS_UPPER) return toupper(c);
if (type == ASE_CCLS_LOWER) return tolower(c);
return c;
}
#elif defined(ASE_CHAR_IS_WCHAR)
#include <wctype.h>
2008-10-14 09:04:24 +00:00
ase_bool_t ase_ccls_is (ase_cint_t c, ase_ccls_type_t type)
2008-08-21 02:24:36 +00:00
{
2008-08-21 03:58:42 +00:00
static const char* name[] =
{
"upper",
"lower",
"alpha",
"digit",
"xdigit",
"alnum",
"space",
"print",
"graph",
"cntrl",
"punct"
};
2008-08-21 02:24:36 +00:00
static wctype_t desc[] =
{
2008-08-21 03:58:42 +00:00
(wctype_t)0,
(wctype_t)0,
(wctype_t)0,
(wctype_t)0,
(wctype_t)0,
(wctype_t)0,
(wctype_t)0,
(wctype_t)0,
(wctype_t)0,
(wctype_t)0,
(wctype_t)0
2008-08-21 02:24:36 +00:00
};
ASE_ASSERTX (type >= ASE_CCLS_UPPER && type <= ASE_CCLS_PUNCT,
"The character type should be one of ase_ccls_type_t values");
2008-08-21 03:58:42 +00:00
if (desc[type] == (wctype_t)0) desc[type] = wctype(name[type]);
2008-08-21 02:24:36 +00:00
return iswctype (c, desc[type]);
}
2008-10-14 09:04:24 +00:00
ase_cint_t ase_ccls_to (ase_cint_t c, ase_ccls_type_t type)
2008-08-21 02:24:36 +00:00
{
2008-08-21 03:58:42 +00:00
static const char* name[] =
{
2008-09-26 22:31:40 +00:00
"toupper",
"tolower"
2008-08-21 03:58:42 +00:00
};
static wctrans_t desc[] =
2008-08-21 02:24:36 +00:00
{
2008-08-21 03:58:42 +00:00
(wctrans_t)0,
(wctrans_t)0
2008-08-21 02:24:36 +00:00
};
ASE_ASSERTX (type >= ASE_CCLS_UPPER && type <= ASE_CCLS_LOWER,
"The character type should be one of ASE_CCLS_UPPER and ASE_CCLS_LOWER");
2008-08-21 03:58:42 +00:00
if (desc[type] == (wctrans_t)0) desc[type] = wctrans(name[type]);
2008-08-21 02:24:36 +00:00
return towctrans (c, desc[type]);
}
#else
#error unsupported character type
#endif
2008-10-14 09:04:24 +00:00
static ase_bool_t ccls_is (void* data, ase_cint_t c, ase_ccls_type_t type)
2008-08-21 04:58:19 +00:00
{
return ase_ccls_is (c, type);
}
2008-10-14 09:04:24 +00:00
static ase_cint_t ccls_to (void* data, ase_cint_t c, ase_ccls_type_t type)
2008-08-21 04:58:19 +00:00
{
return ase_ccls_to (c, type);
}
2008-08-21 02:24:36 +00:00
static ase_ccls_t ccls =
{
ccls_is,
ccls_to,
ASE_NULL
};
ase_ccls_t* ase_ccls = &ccls;
2008-10-14 05:32:58 +00:00