forked from cmus/cmus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu_collate.c
138 lines (110 loc) · 2.86 KB
/
u_collate.c
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
/*
* Copyright 2010-2013 Various Authors
* Copyright 2010 Johannes Weißl
*
* based on gunicollate.c from glib
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "u_collate.h"
#include "uchar.h"
#include "xmalloc.h"
#include "ui_curses.h" /* using_utf8, charset */
#include "convert.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int u_strcoll(const char *str1, const char *str2)
{
int result;
if (using_utf8) {
result = strcoll(str1, str2);
} else {
char *str1_locale = NULL, *str2_locale = NULL;
convert(str1, -1, &str1_locale, -1, charset, "UTF-8");
convert(str2, -1, &str2_locale, -1, charset, "UTF-8");
if (str1_locale && str2_locale)
result = strcoll(str1_locale, str2_locale);
else
result = strcmp(str1, str2);
if (str2_locale)
free(str2_locale);
if (str1_locale)
free(str1_locale);
}
return result;
}
int u_strcasecoll(const char *str1, const char *str2)
{
char *cf_a, *cf_b;
int res;
cf_a = u_casefold(str1);
cf_b = u_casefold(str2);
res = u_strcoll(cf_a, cf_b);
free(cf_b);
free(cf_a);
return res;
}
int u_strcasecoll0(const char *str1, const char *str2)
{
if (!str1)
return str2 ? -1 : 0;
if (!str2)
return 1;
return u_strcasecoll(str1, str2);
}
char *u_strcoll_key(const char *str)
{
char *result = NULL;
if (using_utf8) {
size_t xfrm_len = strxfrm(NULL, str, 0);
if ((ssize_t) xfrm_len >= 0 && xfrm_len < INT_MAX - 2) {
result = xnew(char, xfrm_len + 1);
strxfrm(result, str, xfrm_len + 1);
}
}
if (!result) {
char *str_locale = NULL;
convert(str, -1, &str_locale, -1, charset, "UTF-8");
if (str_locale) {
size_t xfrm_len = strxfrm(NULL, str_locale, 0);
if ((ssize_t) xfrm_len >= 0 && xfrm_len < INT_MAX - 2) {
result = xnew(char, xfrm_len + 2);
result[0] = 'A';
strxfrm(result + 1, str_locale, xfrm_len + 1);
}
free(str_locale);
}
}
if (!result) {
size_t xfrm_len = strlen(str);
result = xmalloc(xfrm_len + 2);
result[0] = 'B';
memcpy(result + 1, str, xfrm_len);
result[xfrm_len+1] = '\0';
}
return result;
}
char *u_strcasecoll_key(const char *str)
{
char *key, *cf_str;
cf_str = u_casefold(str);
key = u_strcoll_key(cf_str);
free(cf_str);
return key;
}
char *u_strcasecoll_key0(const char *str)
{
return str ? u_strcasecoll_key(str) : NULL;
}