summaryrefslogtreecommitdiff
path: root/yarp/enc/yp_big5.c
blob: a7c879cd0abb1685b1254e9bc6f79f0c11ee0e17 (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
#include "yarp/enc/yp_encoding.h"

static size_t
yp_encoding_big5_char_width(const uint8_t *b, ptrdiff_t n) {
    // These are the single byte characters.
    if (*b < 0x80) {
        return 1;
    }

    // These are the double byte characters.
    if ((n > 1) && (b[0] >= 0xA1 && b[0] <= 0xFE) && (b[1] >= 0x40 && b[1] <= 0xFE)) {
        return 2;
    }

    return 0;
}

static size_t
yp_encoding_big5_alpha_char(const uint8_t *b, ptrdiff_t n) {
    if (yp_encoding_big5_char_width(b, n) == 1) {
        return yp_encoding_ascii_alpha_char(b, n);
    } else {
        return 0;
    }
}

static size_t
yp_encoding_big5_alnum_char(const uint8_t *b, ptrdiff_t n) {
    if (yp_encoding_big5_char_width(b, n) == 1) {
        return yp_encoding_ascii_alnum_char(b, n);
    } else {
        return 0;
    }
}

static bool
yp_encoding_big5_isupper_char(const uint8_t *b, ptrdiff_t n) {
    if (yp_encoding_big5_char_width(b, n) == 1) {
        return yp_encoding_ascii_isupper_char(b, n);
    } else {
        return false;
    }
}

yp_encoding_t yp_encoding_big5 = {
    .name = "big5",
    .char_width = yp_encoding_big5_char_width,
    .alnum_char = yp_encoding_big5_alnum_char,
    .alpha_char = yp_encoding_big5_alpha_char,
    .isupper_char = yp_encoding_big5_isupper_char,
    .multibyte = true
};