aboutsummaryrefslogtreecommitdiff
path: root/sys/libunicode/vendor/common.c
blob: 6b5d1b38562b4629c76cb9ba8286e2c596b4a0bb (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "common.h"

// -----------------------------------------------------------------------
// input functions

int
parse(io·Stream *io, int nfield, char **field, int len, char *line)
{
    int n;
    if((n=io·readln(io, len, line)) <= 0)
        return ParseEOF;

    if(n == len)
        panicf("line too long");

    if(line[n-1] != '\n')
        panicf("invalid line: expected '\n', found '%c'", line[n]);

    line[n-1] = 0;

    if(line[0] == '#' || line[0] == 0)
        return ParseSkip;

    /* tokenize line into fields */
    n = 0;
    field[n] = line;
    while(*line){
        if(*line == ';'){
            *line = 0;
            field[++n] = line+1;
        }
        line++;
    }

    if(n != nfield-1)
        panicf("expected %d number of fields, got %d: %s", nfield, n, line);

    return ParseOK;
}

int
codepoint(char *s)
{
    int c, b;

    c = 0;
    while((b=*s++)){
        c <<= 4;
        if(b >= '0' && b <= '9')
            c += b - '0';
        else if(b >= 'A' && b <= 'F')
            c += b - 'A' + 10;
        else
            panicf("bad codepoint char '%c'", b);
    }

    return c;
}

void
codepointrange(io·Stream *utf8, char *field[NumFields], int *start,  int *stop)
{
    int e, c;
    char *other[NumFields], line[1024];

    // XXX: the stop variable passes in the previous stopping character
    e = *stop;
    c = codepoint(field[Fcode]);

    if(c >= NumRunes)
        panicf("unexpected large codepoint %x", c);
    if(c <= e)
        panicf("bad code sequence: %x then %x", e, c);
    e = c;

    if(strstr(field[Fname], ", First>") != nil){
        if(!parse(utf8, arrlen(other), other, arrlen(line), line))
            panicf("range start at end of file");
        if(strstr(other[Fname], ", Last>") == nil)
            panicf("range start not followed by range end");

        e = codepoint(other[Fcode]);

        if(e <= c)
            panicf("bad code sequence: %x then %x", c, e);
        if(strcmp(field[Fcategory], other[Fcategory]) != 0)
            panicf("range with mismatched category");
    }

    *start = c;
    *stop  = e;
}

// -----------------------------------------------------------------------
// output functions

void
putsearch(void)
{
    puts(
        "#include <u.h>\n"
        "#include <libunicode.h>\n"
        "\n"
        "static\n"
        "rune*\n"
        "rangesearch(rune c, rune *t, int n, int ne)\n"
        "{\n"
        "   rune *p;\n"
        "   int m;\n"
        "   while(n > 1) {\n"
        "   m = n >> 1;\n"
        "   p = t + m*ne;\n"
        "   if(c >= p[0]){\n"
        "       t = p;\n"
        "       n = n-m;\n"
        "   }else\n"
        "       n = m;\n"
        "   }\n"
        "   if(n && c >= t[0])\n"
        "       return t;\n"
        "   return 0;\n"
        "}\n"
    );

}

int
putrange(char *ident, char *prop, int force)
{
    int l, r, start;

    start = 0;
    for(l = 0; l < NumRunes;) {
        if(!prop[l]){
            l++;
            continue;
        }

        for(r = l+1; r < NumRunes; r++){
            if(!prop[r])
                break;
            prop[r] = 0;
        }

        if(force || r > l + 1){
            if(!start){
                printf("static rune %s[] = {\n", ident);
                start = 1;
            }
            prop[l] = 0;
            printf("\t0x%.4x, 0x%.4x,\n", l, r-1);
        }

        l = r;
    }

    if(start)
        printf("};\n\n");

    return start;
}

int
putpair(char *ident, char *prop)
{
    int l, r, start;

    start = 0;
    for(l=0; l+2 < NumRunes; ){
        if(!prop[l]){
            l++;
            continue;
        }

        for(r = l + 2; r < NumRunes; r += 2){
            if(!prop[r])
                break;
            prop[r] = 0;
        }

        if(r != l + 2){
            if(!start){
                printf("static rune %s[] = {\n", ident);
                start = 1;
            }
            prop[l] = 0;
            printf("\t0x%.4x, 0x%.4x,\n", l, r - 2);
        }

        l = r;
    }

    if(start)
        printf("};\n\n");
    return start;
}

int
putsingle(char *ident, char *prop)
{
    int i, start;

    start = 0;
    for(i = 0; i < NumRunes; i++) {
        if(!prop[i])
            continue;

        if(!start){
            printf("static rune %s[] = {\n", ident);
            start = 1;
        }
        prop[i] = 0;
        printf("\t0x%.4x,\n", i);
    }

    if(start)
        printf("};\n\n");

    return start;
}