sortkey.c 9.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Unicode sort key generation
 *
 * Copyright 2003 Dmitry Timoshkov
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21
 */
#include "wine/unicode.h"

22
extern unsigned int wine_decompose( int flags, WCHAR ch, WCHAR *dst, unsigned int dstlen );
23
extern const unsigned int collation_table[];
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

/*
 * flags - normalization NORM_* flags
 *
 * FIXME: 'variable' flag not handled
 */
int wine_get_sortkey(int flags, const WCHAR *src, int srclen, char *dst, int dstlen)
{
    WCHAR dummy[4]; /* no decomposition is larger than 4 chars */
    int key_len[4];
    char *key_ptr[4];
    const WCHAR *src_save = src;
    int srclen_save = srclen;

    key_len[0] = key_len[1] = key_len[2] = key_len[3] = 0;
    for (; srclen; srclen--, src++)
    {
41
        unsigned int i, decomposed_len = 1;/*wine_decompose(*src, dummy, 4);*/
42
        dummy[0] = *src;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        if (decomposed_len)
        {
            for (i = 0; i < decomposed_len; i++)
            {
                WCHAR wch = dummy[i];
                unsigned int ce;

                /* tests show that win2k just ignores NORM_IGNORENONSPACE,
                 * and skips white space and punctuation characters for
                 * NORM_IGNORESYMBOLS.
                 */
                if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
                    continue;

                if (flags & NORM_IGNORECASE) wch = tolowerW(wch);

                ce = collation_table[collation_table[wch >> 8] + (wch & 0xff)];
                if (ce != (unsigned int)-1)
                {
                    if (ce >> 16) key_len[0] += 2;
                    if ((ce >> 8) & 0xff) key_len[1]++;
                    if ((ce >> 4) & 0x0f) key_len[2]++;
65
                    if (ce & 1)
66 67 68
                    {
                        if (wch >> 8) key_len[3]++;
                        key_len[3]++;
69
                    }
70
                }
71
                else
72 73 74 75
                {
                    key_len[0] += 2;
                    if (wch >> 8) key_len[0]++;
                    if (wch & 0xff) key_len[0]++;
76
		}
77 78 79 80 81
            }
        }
    }

    if (!dstlen) /* compute length */
82 83
        /* 4 * '\1' + key length */
        return key_len[0] + key_len[1] + key_len[2] + key_len[3] + 4;
84 85 86 87 88 89 90 91 92 93 94 95 96 97

    if (dstlen < key_len[0] + key_len[1] + key_len[2] + key_len[3] + 4 + 1)
        return 0; /* overflow */

    src = src_save;
    srclen = srclen_save;

    key_ptr[0] = dst;
    key_ptr[1] = key_ptr[0] + key_len[0] + 1;
    key_ptr[2] = key_ptr[1] + key_len[1] + 1;
    key_ptr[3] = key_ptr[2] + key_len[2] + 1;

    for (; srclen; srclen--, src++)
    {
98
        unsigned int i, decomposed_len = 1;/*wine_decompose(*src, dummy, 4);*/
99
        dummy[0] = *src;
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
        if (decomposed_len)
        {
            for (i = 0; i < decomposed_len; i++)
            {
                WCHAR wch = dummy[i];
                unsigned int ce;

                /* tests show that win2k just ignores NORM_IGNORENONSPACE,
                 * and skips white space and punctuation characters for
                 * NORM_IGNORESYMBOLS.
                 */
                if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
                    continue;

                if (flags & NORM_IGNORECASE) wch = tolowerW(wch);

                ce = collation_table[collation_table[wch >> 8] + (wch & 0xff)];
                if (ce != (unsigned int)-1)
                {
                    WCHAR key;
                    if ((key = ce >> 16))
                    {
                        *key_ptr[0]++ = key >> 8;
                        *key_ptr[0]++ = key & 0xff;
                    }
                    /* make key 1 start from 2 */
                    if ((key = (ce >> 8) & 0xff)) *key_ptr[1]++ = key + 1;
                    /* make key 2 start from 2 */
                    if ((key = (ce >> 4) & 0x0f)) *key_ptr[2]++ = key + 1;
                    /* key 3 is always a character code */
130
                    if (ce & 1)
131 132 133
                    {
                        if (wch >> 8) *key_ptr[3]++ = wch >> 8;
                        if (wch & 0xff) *key_ptr[3]++ = wch & 0xff;
134
                    }
135
                }
136
                else
137 138 139 140 141
                {
                    *key_ptr[0]++ = 0xff;
                    *key_ptr[0]++ = 0xfe;
                    if (wch >> 8) *key_ptr[0]++ = wch >> 8;
                    if (wch & 0xff) *key_ptr[0]++ = wch & 0xff;
142
                }
143 144 145 146 147 148 149 150 151 152 153 154
            }
        }
    }

    *key_ptr[0] = '\1';
    *key_ptr[1] = '\1';
    *key_ptr[2] = '\1';
    *key_ptr[3]++ = '\1';
    *key_ptr[3] = 0;

    return key_ptr[3] - dst;
}
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
enum weight
{
    UNICODE_WEIGHT,
    DIACRITIC_WEIGHT,
    CASE_WEIGHT
};

static unsigned int get_weight(WCHAR ch, enum weight type)
{
    unsigned int ret;

    ret = collation_table[collation_table[ch >> 8] + (ch & 0xff)];
    if (ret == (unsigned int)-1)
        return ch;

    switch(type)
    {
    case UNICODE_WEIGHT:
        return ret >> 16;
    case DIACRITIC_WEIGHT:
        return (ret >> 8) & 0xff;
    case CASE_WEIGHT:
    default:
        return (ret >> 4) & 0x0f;
    }
}

183 184 185 186 187 188 189 190 191 192 193
static void inc_str_pos(const WCHAR **str, int *len, int *dpos, int *dlen)
{
    (*dpos)++;
    if (*dpos == *dlen)
    {
        *dpos = *dlen = 0;
        (*str)++;
        (*len)--;
    }
}

194 195
static inline int compare_weights(int flags, const WCHAR *str1, int len1,
                                  const WCHAR *str2, int len2, enum weight type)
196
{
197 198
    int dpos1 = 0, dpos2 = 0, dlen1 = 0, dlen2 = 0;
    WCHAR dstr1[4], dstr2[4];
199 200 201 202 203 204 205 206
    unsigned int ce1, ce2;

    /* 32-bit collation element table format:
     * unicode weight - high 16 bit, diacritic weight - high 8 bit of low 16 bit,
     * case weight - high 4 bit of low 8 bit.
     */
    while (len1 > 0 && len2 > 0)
    {
207 208 209
        if (!dlen1) dlen1 = wine_decompose(0, *str1, dstr1, 4);
        if (!dlen2) dlen2 = wine_decompose(0, *str2, dstr2, 4);

210 211 212 213
        if (flags & NORM_IGNORESYMBOLS)
        {
            int skip = 0;
            /* FIXME: not tested */
214
            if (get_char_typeW(dstr1[dpos1]) & (C1_PUNCT | C1_SPACE))
215
            {
216
                inc_str_pos(&str1, &len1, &dpos1, &dlen1);
217 218
                skip = 1;
            }
219
            if (get_char_typeW(dstr2[dpos2]) & (C1_PUNCT | C1_SPACE))
220
            {
221
                inc_str_pos(&str2, &len2, &dpos2, &dlen2);
222 223 224 225 226 227 228 229
                skip = 1;
            }
            if (skip) continue;
        }

       /* hyphen and apostrophe are treated differently depending on
        * whether SORT_STRINGSORT specified or not
        */
230
        if (type == UNICODE_WEIGHT && !(flags & SORT_STRINGSORT))
231
        {
232
            if (dstr1[dpos1] == '-' || dstr1[dpos1] == '\'')
233
            {
234
                if (dstr2[dpos2] != '-' && dstr2[dpos2] != '\'')
235
                {
236
                    inc_str_pos(&str1, &len1, &dpos1, &dlen1);
237 238 239
                    continue;
                }
            }
240
            else if (dstr2[dpos2] == '-' || dstr2[dpos2] == '\'')
241
            {
242
                inc_str_pos(&str2, &len2, &dpos2, &dlen2);
243 244 245 246
                continue;
            }
        }

247
        ce1 = get_weight(dstr1[dpos1], type);
248 249 250 251 252
        if (!ce1)
        {
            inc_str_pos(&str1, &len1, &dpos1, &dlen1);
            continue;
        }
253
        ce2 = get_weight(dstr2[dpos2], type);
254 255 256 257 258
        if (!ce2)
        {
            inc_str_pos(&str2, &len2, &dpos2, &dlen2);
            continue;
        }
259

260
        if (ce1 - ce2) return ce1 - ce2;
261

262 263
        inc_str_pos(&str1, &len1, &dpos1, &dlen1);
        inc_str_pos(&str2, &len2, &dpos2, &dlen2);
264
    }
265
    while (len1)
266
    {
267 268 269 270 271
        if (!dlen1) dlen1 = wine_decompose(0, *str1, dstr1, 4);

        ce1 = get_weight(dstr1[dpos1], type);
        if (ce1) break;
        inc_str_pos(&str1, &len1, &dpos1, &dlen1);
272
    }
273
    while (len2)
274
    {
275 276 277 278 279
        if (!dlen2) dlen2 = wine_decompose(0, *str2, dstr2, 4);

        ce2 = get_weight(dstr2[dpos2], type);
        if (ce2) break;
        inc_str_pos(&str2, &len2, &dpos2, &dlen2);
280
    }
281 282 283 284 285 286 287 288
    return len1 - len2;
}

int wine_compare_string(int flags, const WCHAR *str1, int len1,
                        const WCHAR *str2, int len2)
{
    int ret;

289
    ret = compare_weights(flags, str1, len1, str2, len2, UNICODE_WEIGHT);
290 291 292
    if (!ret)
    {
        if (!(flags & NORM_IGNORENONSPACE))
293
            ret = compare_weights(flags, str1, len1, str2, len2, DIACRITIC_WEIGHT);
294
        if (!ret && !(flags & NORM_IGNORECASE))
295
            ret = compare_weights(flags, str1, len1, str2, len2, CASE_WEIGHT);
296 297 298
    }
    return ret;
}