summaryrefslogtreecommitdiff
path: root/src/util/casestr.c
blob: 2f694e93156dd61c320a58d45271634f716d587c (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
/* Copyright (c) 2012-2013 Yoran Heling

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <global.h>


/* XXX: Does the normalization form matter when performing case folding? I'm
 * assuming that the given strings are all NFC, but utf8proc_map() will first
 * decompose all characters (I assume that means NFD), then perform
 * casefolding, and then convert back to NFC. Will that give different results?
 * And so yes, should that approach be used here as well?
 */


static inline bool bitmask_isset(uint8_t *m, size_t i) {
	return ((m[i/8]) >> (i&7)) & 1;
}


static inline void bitmask_set(uint8_t *m, size_t i) {
	m[i/8] |= 1 << (i&7);
}


static inline void kputuc(int32_t uc, kstring_t *s) {
	char buf[6];
	kputsn(buf, utf8proc_encode_char(uc, (uint8_t *)buf), s);
}


void casestr_fold(const char *str, kstring_t *dest) {
	const char *s = str;
	while(*s) {
		int32_t uc;
		ssize_t l = utf8proc_iterate((uint8_t *)s, -1, &uc);
		assert(l > 0);

		const int32_t *folds = utf8proc_get_property(uc)->casefold_mapping;
		if(!folds)
			kputsn(s, l, dest);
		else
			while(*folds != -1)
				kputuc(*(folds++), dest);
		s += l;
	}
}


void casestr_create(const char *str, kstring_t *dest) {
	uint8_t bitmask[strlen(str)/8+1];
	bool usebitmask = true;
	int idx = 1;

	memset(bitmask, 0, sizeof(bitmask));

	const char *s = str;
	while(*s) {
		int32_t uc;
		ssize_t l = utf8proc_iterate((uint8_t *)s, -1, &uc);
		assert(l > 0);
		const utf8proc_property_t *p = utf8proc_get_property(uc);

		/* No casefolding for this character */
		if(!p->casefold_mapping)
			kputsn(s, l, dest);

		/* Folds to a single character */
		else if(p->casefold_mapping[1] == -1) {
			int32_t cf = *p->casefold_mapping;
			kputuc(cf, dest);

			/* Test whether the uppercase version of the folded character maps
			 * back to our original character */
			p = utf8proc_get_property(cf);
			if(p->uppercase_mapping == uc)
				bitmask_set(bitmask, idx);
			else
				usebitmask = false;
		}

		/* Folds to multiple characters */
		else {
			usebitmask = false;
			const int32_t *folds = p->casefold_mapping;
			while(*folds != -1)
				kputuc(*(folds++), dest);
		}

		s += l;
		idx++;
	}

	kputc(0, dest); /* null-terminate the casefolded string */

	if(usebitmask) {
		bitmask_set(bitmask, 0);
		kputsn((char *)bitmask, (idx+7)/8, dest);
	} else {
		kputc(0, dest); /* single-byte bitmask with no bits set */
		kputsn(str, s-str, dest);
		dest->l++; /* +1 to include the \0 added by kstring */
	}
}


void casestr_orig(const char *buf, kstring_t *dest) {
	const char *bitmask = buf + strlen(buf) + 1;
	if(!*bitmask) {
		kputs(bitmask+1, dest);
		return;
	}
	int idx = 1;
	while(*buf) {
		size_t l;
		if(bitmask_isset((uint8_t *)bitmask, idx)) {
			int32_t uc;
			l = utf8proc_iterate((uint8_t *)buf, -1, &uc);
			kputuc(utf8proc_get_property(uc)->uppercase_mapping, dest);
		} else {
			l = utf8proc_utf8class[*((uint8_t *)buf)]; /* faster than utf8proc_iterate() */
			kputsn(buf, l, dest);
		}

		buf += l;
		idx++;
	}
}


/* Calculates the size of the bitmask, in bytes. */
static size_t casestr_masklen(const char *buf) {
	size_t idx = 1;
	while(*buf) {
		buf += utf8proc_utf8class[*((uint8_t *)buf)];
		idx++;
	}
	return (idx+7)/8;
}


size_t casestr_len(const char *buf) {
	size_t maskoff = strlen(buf) + 1;
	return buf[maskoff]
		? maskoff + casestr_masklen(buf)
		: maskoff + strlen(buf+maskoff+1) + 2;
}


int casestr_cmp(const char *a, const char *b) {
	int i = strcmp(a, b);
	if(i)
		return i;
	size_t maskoff = strlen(a)+1; /* == strlen(b)+1, after the above check */
	return a[maskoff] != b[maskoff] ? a[maskoff] - b[maskoff] : a[maskoff]
		? memcmp(a+maskoff, b+maskoff, casestr_masklen(a))
		: strcmp(a+maskoff, b+maskoff);
}


/* vim: set noet sw=4 ts=4: */