unicode.c 2.23 KB
Newer Older
Damien's avatar
Damien committed
1
2
3
#include <stdint.h>

#include "misc.h"
4
#include "mpconfig.h"
Damien's avatar
Damien committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

// attribute flags
#define FL_PRINT (0x01)
#define FL_SPACE (0x02)
#define FL_DIGIT (0x04)
#define FL_ALPHA (0x08)
#define FL_UPPER (0x10)
#define FL_LOWER (0x20)

// shorthand character attributes
#define AT_PR (FL_PRINT)
#define AT_SP (FL_SPACE | FL_PRINT)
#define AT_DI (FL_DIGIT | FL_PRINT)
#define AT_AL (FL_ALPHA | FL_PRINT)
#define AT_UP (FL_UPPER | FL_ALPHA | FL_PRINT)
#define AT_LO (FL_LOWER | FL_ALPHA | FL_PRINT)

// table of attributes for ascii characters
23
STATIC const uint8_t attr[] = {
Damien's avatar
Damien committed
24
    0, 0, 0, 0, 0, 0, 0, 0,
25
    0, AT_SP, AT_SP, AT_SP, AT_SP, AT_SP, 0, 0,
Damien's avatar
Damien committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    AT_SP, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
    AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
    AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI,
    AT_DI, AT_DI, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
    AT_PR, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP,
    AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP,
    AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP,
    AT_UP, AT_UP, AT_UP, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
    AT_PR, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO,
    AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO,
    AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO,
    AT_LO, AT_LO, AT_LO, AT_PR, AT_PR, AT_PR, AT_PR, 0
};

42
unichar utf8_get_char(const char *s) {
Damien's avatar
Damien committed
43
44
45
    return *s;
}

46
char *utf8_next_char(const char *s) {
Damien's avatar
Damien committed
47
48
49
    return (char*)(s + 1);
}

50
bool unichar_isspace(unichar c) {
Damien's avatar
Damien committed
51
52
53
    return c < 128 && (attr[c] & FL_SPACE) != 0;
}

54
bool unichar_isalpha(unichar c) {
Damien's avatar
Damien committed
55
56
57
    return c < 128 && (attr[c] & FL_ALPHA) != 0;
}

58
bool unichar_isprint(unichar c) {
Damien's avatar
Damien committed
59
60
61
    return c < 128 && (attr[c] & FL_PRINT) != 0;
}

62
bool unichar_isdigit(unichar c) {
Damien's avatar
Damien committed
63
64
65
    return c < 128 && (attr[c] & FL_DIGIT) != 0;
}

66
67
68
69
bool unichar_isxdigit(unichar c) {
    return unichar_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}

Damien's avatar
Damien committed
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
bool char_is_alpha_or_digit(unichar c) {
    return c < 128 && (attr[c] & (FL_ALPHA | FL_DIGIT)) != 0;
}

bool char_is_upper(unichar c) {
    return c < 128 && (attr[c] & FL_UPPER) != 0;
}

bool char_is_lower(unichar c) {
    return c < 128 && (attr[c] & FL_LOWER) != 0;
}
*/