ciabatta/code/ctype.c

76 lines
1.2 KiB
C
Raw Normal View History

2022-06-02 23:41:48 +00:00
// TODO: table-driven?
// TODO: other locales
#include <ctype.h>
#include <locale.h>
#define in_range(low, v, high) ((low <= v) && (v <= high))
2022-06-02 23:57:14 +00:00
int isalnum(int c) {
2022-06-02 23:41:48 +00:00
return isalpha(c) || isdigit(c);
}
2022-06-02 23:57:14 +00:00
int isalpha(int c) {
2022-06-02 23:41:48 +00:00
return islower(c) || isupper(c);
}
2022-06-02 23:57:14 +00:00
int isblank(int c) {
2022-06-02 23:41:48 +00:00
return c == ' ' || c == '\t';
}
2022-06-02 23:57:14 +00:00
int iscntrl(int c) {
2022-06-02 23:41:48 +00:00
return in_range('\x00', c, '\x1f') || c == '\x7f';
}
2022-06-02 23:57:14 +00:00
int isdigit(int c) {
2022-06-02 23:41:48 +00:00
return in_range('0', c, '9');
}
2022-06-02 23:57:14 +00:00
int isgraph(int c) {
2022-06-02 23:41:48 +00:00
return isprint(c) && (c != ' ');
}
2022-06-02 23:57:14 +00:00
int islower(int c) {
2022-06-02 23:41:48 +00:00
return in_range('a', c, 'z');
}
2022-06-02 23:57:14 +00:00
int isprint(int c) {
2022-06-02 23:41:48 +00:00
return in_range(' ', c, '\x7e');
}
2022-06-02 23:57:14 +00:00
int ispunct(int c) {
2022-06-02 23:41:48 +00:00
return in_range('\x21', c, '\x2f')
|| in_range('\x3a', c, '\x40')
|| in_range('\x5b', c, '\x60')
|| in_range('\x7b', c, '\x7e');
}
2022-06-02 23:57:14 +00:00
int isspace(int c) {
return in_range('\x09', c, '\x0d') || c == ' ';
2022-06-02 23:41:48 +00:00
}
2022-06-02 23:57:14 +00:00
int isupper(int c) {
2022-06-02 23:41:48 +00:00
return in_range('A', c, 'Z');
}
2022-06-02 23:57:14 +00:00
int isxdigit(int c) {
2022-06-02 23:41:48 +00:00
return in_range('0', c, '9')
|| in_range('a', c, 'f')
|| in_range('A', c, 'F');
}
2022-06-02 23:57:14 +00:00
int tolower(int c) {
2022-06-02 23:41:48 +00:00
if(isupper(c)) {
return c-'A'+'a';
}
2022-06-02 23:57:14 +00:00
return c;
2022-06-02 23:41:48 +00:00
}
2022-06-02 23:57:14 +00:00
int toupper(int c) {
2022-06-02 23:41:48 +00:00
if(islower(c)) {
return c-'a'+'A';
}
2022-06-02 23:57:14 +00:00
return c;
2022-06-02 23:41:48 +00:00
}