File: fold.c

package info (click to toggle)
wine 4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 209,096 kB
  • sloc: ansic: 2,906,412; perl: 18,817; yacc: 15,629; makefile: 9,134; objc: 6,543; lex: 4,315; python: 1,786; cpp: 1,042; sh: 771; java: 742; xml: 557; awk: 69; cs: 17
file content (198 lines) | stat: -rw-r--r-- 5,289 bytes parent folder | download | duplicates (5)
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
/*
 * String folding
 *
 * Copyright 2003 Jon Griffiths
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "wine/unicode.h"

static inline WCHAR to_unicode_digit( WCHAR ch )
{
    extern const WCHAR wine_digitmap[] DECLSPEC_HIDDEN;
    return ch + wine_digitmap[wine_digitmap[ch >> 8] + (ch & 0xff)];
}

static inline WCHAR to_unicode_native( WCHAR ch )
{
    extern const WCHAR wine_compatmap[] DECLSPEC_HIDDEN;
    return ch + wine_compatmap[wine_compatmap[ch >> 8] + (ch & 0xff)];
}

static const WCHAR wine_ligatures[] =
{
    0x00c6, 0x00de, 0x00df, 0x00e6, 0x00fe, 0x0132, 0x0133, 0x0152,
    0x0153, 0x01c4, 0x01c5, 0x01c6, 0x01c7, 0x01c8, 0x01c9, 0x01ca,
    0x01cb, 0x01cc, 0x01e2, 0x01e3, 0x01f1, 0x01f2, 0x01f3, 0x01fc,
    0x01fd, 0x05f0, 0x05f1, 0x05f2, 0xfb00, 0xfb01, 0xfb02, 0xfb03,
    0xfb04, 0xfb05, 0xfb06
};

/* Unicode expanded ligatures */
static const WCHAR wine_expanded_ligatures[][4] =
{
    { 'A','E','\0',1 },
    { 'T','H','\0',1 },
    { 's','s','\0',1 },
    { 'a','e','\0',1 },
    { 't','h','\0',1 },
    { 'I','J','\0',1 },
    { 'i','j','\0',1 },
    { 'O','E','\0',1 },
    { 'o','e','\0',1 },
    { 'D',0x017d,'\0',1 },
    { 'D',0x017e,'\0',1 },
    { 'd',0x017e,'\0',1 },
    { 'L','J','\0',1 },
    { 'L','j','\0',1 },
    { 'l','j','\0',1 },
    { 'N','J','\0',1 },
    { 'N','j','\0',1 },
    { 'n','j','\0',1 },
    { 0x0100,0x0112,'\0',1 },
    { 0x0101,0x0113,'\0',1 },
    { 'D','Z','\0',1 },
    { 'D','z','\0',1 },
    { 'd','z','\0',1 },
    { 0x00c1,0x00c9,'\0',1 },
    { 0x00e1,0x00e9,'\0',1 },
    { 0x05d5,0x05d5,'\0',1 },
    { 0x05d5,0x05d9,'\0',1 },
    { 0x05d9,0x05d9,'\0',1 },
    { 'f','f','\0',1 },
    { 'f','i','\0',1 },
    { 'f','l','\0',1 },
    { 'f','f','i',2 },
    { 'f','f','l',2 },
    { 0x017f,'t','\0',1 },
    { 's','t','\0',1 }
};

static inline int get_ligature_len( WCHAR wc )
{
    int low = 0, high = sizeof(wine_ligatures)/sizeof(WCHAR) -1;
    while (low <= high)
    {
        int pos = (low + high) / 2;
        if (wine_ligatures[pos] < wc)
            low = pos + 1;
        else if (wine_ligatures[pos] > wc)
            high = pos - 1;
        else
            return wine_expanded_ligatures[pos][3];
    }
    return 0;
}

static inline const WCHAR* get_ligature( WCHAR wc )
{
    static const WCHAR empty_ligature[] = { '\0','\0','\0', 0 };
    int low = 0, high = sizeof(wine_ligatures)/sizeof(WCHAR) -1;
    while (low <= high)
    {
        int pos = (low + high) / 2;
        if (wine_ligatures[pos] < wc)
            low = pos + 1;
        else if (wine_ligatures[pos] > wc)
            high = pos - 1;
        else
            return wine_expanded_ligatures[pos];
    }
    return empty_ligature;
}

/* fold a unicode string */
int wine_fold_string( int flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen )
{
    WCHAR *dstbase = dst;
    const WCHAR *expand;
    int i;

    if (srclen == -1)
        srclen = strlenW(src) + 1; /* Include terminating NUL in count */

    if (!dstlen)
    {
        /* Calculate the required size for dst */
        dstlen = srclen;

        if (flags & MAP_EXPAND_LIGATURES)
        {
            while (srclen--)
            {
                dstlen += get_ligature_len(*src);
                src++;
            }
        }
        else if (flags & MAP_COMPOSITE)
        {
            /* FIXME */
        }
        else if (flags & MAP_PRECOMPOSED)
        {
            /* FIXME */
        }
        return dstlen;
    }

    if (srclen > dstlen)
        return 0;

    dstlen -= srclen;

    /* Actually perform the mapping(s) specified */
    for (i = 0; i < srclen; i++)
    {
        WCHAR ch = *src;

        if (flags & MAP_EXPAND_LIGATURES)
        {
            expand = get_ligature(ch);
            if (expand[0])
            {
                if (!dstlen--)
                    return 0;
                dst[0] = expand[0];
                if (expand[2])
                {
                    if (!dstlen--)
                        return 0;
                    *++dst = expand[1];
                    ch = expand[2];
                }
                else
                    ch = expand[1];
                dst++;
            }
        }
        else if (flags & MAP_COMPOSITE)
        {
            /* FIXME */
        }
        else if (flags & MAP_PRECOMPOSED)
        {
            /* FIXME */
        }
        if (flags & MAP_FOLDDIGITS)
            ch = to_unicode_digit(ch);
        if (flags & MAP_FOLDCZONE)
            ch = to_unicode_native(ch);

        *dst++ = ch;
        src++;
    }
    return dst - dstbase;
}