aboutsummaryrefslogtreecommitdiff
path: root/src/system/utf8.c
blob: d38b292ca82693136117061e72f4afa1e9f96301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "../../include/mgl/system/utf8.h"

static inline bool utf8_get_codepoint_length(unsigned char b, size_t *codepoint_length) {
    if((b & 0x80) == 0) {
        *codepoint_length = 1;
        return true;
    } else if((b & 0xE0) == 0xC0) {
        *codepoint_length = 2;
        return true;
    } else if((b & 0xF0) == 0xE0) {
        *codepoint_length = 3;
        return true;
    } else if((b & 0xF8) == 0xF0) {
        *codepoint_length = 4;
        return true;
    } else {
        return false;
    }
}

/* TODO: Optimize (remove branching, etc) */
bool mgl_utf8_decode(const unsigned char *str, size_t size, uint32_t *decoded_codepoint, size_t *codepoint_length) {
    if(size == 0) {
        *decoded_codepoint = 0;
        *codepoint_length = 0;
        return false;
    }

    size_t clen;
    if(!utf8_get_codepoint_length(str[0], &clen)) {
        *decoded_codepoint = str[0];
        *codepoint_length = 1;
        return false;
    }

    if(size < clen) {
        *decoded_codepoint = str[0];
        *codepoint_length = 1;
        return false;
    }

    for(size_t i = 1; i < clen; ++i) {
        if((str[i] & 0xC0) != 0x80) {
            *decoded_codepoint = str[0];
            *codepoint_length = 1;
            return false;
        }
    }

    uint32_t codepoint;
    switch(clen) {
        case 1:
            codepoint =  (uint32_t)(str[0] & 0x7F);
            break;
        case 2:
            codepoint = ((uint32_t)(str[0] & 0x1F) << 6);
            codepoint |= (uint32_t)(str[1] & 0x3F);
            break;
        case 3:
            codepoint =  ((uint32_t)(str[0] & 0x0F) << 12);
            codepoint |= ((uint32_t)(str[1] & 0x3F) << 6);
            codepoint |=  (uint32_t)(str[2] & 0x3F);
            break;
        case 4:
            codepoint =  ((uint32_t)(str[0] & 0x07) << 18);
            codepoint |= ((uint32_t)(str[1] & 0x3F) << 12);
            codepoint |= ((uint32_t)(str[2] & 0x3F) << 6);
            codepoint |=  (uint32_t)(str[3] & 0x3F);
            break;
    }

    *codepoint_length = clen;
    *decoded_codepoint = codepoint;
    return true;
}

/* TODO: Optimize (remove branching, etc) */
size_t mgl_utf8_get_start_of_codepoint(const unsigned char *str, size_t size, size_t offset) {
    if(size == 0)
        return 0;

    if(offset > size - 1)
        offset = size - 1;

    /* i <= offset is an overflow (underflow?) check */
    for(size_t i = offset; i <= offset; --i) {
        if((str[i] & 0xC0) != 0x80)
            return i;
    }

    return 0;
}

/* TODO: Optimize (remove branching, etc) */
size_t mgl_utf8_index_to_offset(const unsigned char *str, size_t size, size_t index) {
    size_t codepoint_index = 0;
    for(size_t i = 0; i < size;) {
        const unsigned char *cp = &str[i];
        uint32_t codepoint;
        size_t clen;
        if(!mgl_utf8_decode(cp, size - i, &codepoint, &clen)) {
            codepoint = *cp;
            clen = 1;
        }

        if(codepoint_index >= index)
            return i;

        i += clen;
        ++codepoint_index;
    }
    return size;
}