aboutsummaryrefslogtreecommitdiff
path: root/sys/libunicode/encode.c
blob: fa7c93e201bb69f3cea15d9bf46d37ec35777cb7 (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
#include "internal.h"

int
utf8·encode(rune *r, byte *s)
{
    rune c;

    c = *r;
    if(c < Rune1Byte){ // 7 bits
        s[0] = (uint8)c;
        return 1;
    }

    if(c < Rune2Byte){ // 11 bits
        s[0] = TByte1 | (c >> 6);
        s[1] = Tx     | (c & TMask);
        return 2;
    }

    if(c < Rune3Byte){ // 16 bits
        s[0] = TByte2 | ((c >> 12));
        s[1] = Tx     | ((c >> 6) & TMask);
        s[2] = Tx     | ((c)      & TMask);
        return 3;
    }

    // 22 bits
    if(c > RuneMax || (RuneSurrogateMin <= c && c <= RuneSurrogateMax))
        c = RuneErr;

    s[0] = TByte3 | ((c >> 18));
    s[1] = Tx     | ((c >> 12) & TMask);
    s[2] = Tx     | ((c >> 6)  & TMask);
    s[3] = Tx     | ((c)       & TMask);

    return 4;
}

#if 0
int
utf8·encode(rune* r, byte* s)
{
    int i, j;
    rune c;

    c = *r;
    if(c <= Rune1) {
        s[0] = c;
        return 1;
    }

    for(i = 2; i < UTFmax + 1; i++){
        if(i == 3){
            if(c > RuneMax)
                c = RuneErr;
            if(SurrogateMin <= c && c <= SurrogateMax)
                c = RuneErr;
        }
        if(c <= RuneX(i) || i == UTFmax) {
            s[0] = Tbyte(i) |  (c >> (i - 1)*Bitx);
            for(j = 1; j < i; j++)
                s[j] = Tx | ((c >> (i - j - 1)*Bitx) & Maskx);
            return i;
        }
    }

    return UTFmax;
}
#endif