-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathJXLDStringDistanceUtilities.m
180 lines (144 loc) · 6.35 KB
/
JXLDStringDistanceUtilities.m
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
//
// JXLDStringDistanceUtilities.m
// Damerau-Levenshtein
//
// Created by Jan on 18.01.11.
// Copyright 2011-2015 geheimwerk.de. All rights reserved.
//
#import "JXLDStringDistanceUtilities.h"
#define USE_BACKWARD_SEARCH 0 // Backward search has been used for a long time, because of its simpler implementation.
CF_INLINE void updateSearchRangeAfterModification(CFRange * const search_range_p, const CFRange result_range, const CFIndex replacement_length) {
const CFIndex old_location = search_range_p->location;
const CFIndex new_location = result_range.location + replacement_length;
const CFIndex location_change = old_location - new_location; // usually negative
const CFIndex length_change = replacement_length - result_range.length; // usually negative
search_range_p->location = new_location;
search_range_p->length += location_change + length_change;
}
CF_INLINE void jxld_CFStringDeleteCharactersFromSet(CFMutableStringRef string, CFCharacterSetRef characterSet) {
CFIndex string_length = CFStringGetLength(string);
CFRange result_range;
CFRange search_range = CFRangeMake(0, string_length);
#if USE_BACKWARD_SEARCH
while ( CFStringFindCharacterFromSet(string,
characterSet,
search_range,
kCFCompareBackwards,
&result_range) ) {
CFStringDelete(string, result_range);
search_range.length = result_range.location; // We can do this safely, because we use kCFCompareBackwards
}
#else
const CFIndex replacement_length = 0;
while ( CFStringFindCharacterFromSet(string,
characterSet,
search_range,
0,
&result_range) ) {
CFStringDelete(string, result_range);
updateSearchRangeAfterModification(&search_range, result_range, replacement_length);
}
#endif
}
CF_INLINE void jxld_CFStringReplaceCharactersFromSet(CFMutableStringRef string, CFCharacterSetRef characterSet, CFStringRef replacement) {
// We may be able to optimize this function by directly manipulating a UniChar buffer.
CFIndex string_length = CFStringGetLength(string);
CFRange result_range;
CFRange search_range = CFRangeMake(0, string_length);
#if USE_BACKWARD_SEARCH
while ( CFStringFindCharacterFromSet(string,
characterSet,
search_range,
kCFCompareBackwards,
&result_range) ) {
CFStringReplace(string, result_range, replacement);
search_range.length = result_range.location; // We can do this safely, because we use kCFCompareBackwards
}
#else
CFIndex replacement_length = CFStringGetLength(replacement);
while ( CFStringFindCharacterFromSet(string,
characterSet,
search_range,
0,
&result_range) ) {
CFStringReplace(string, result_range, replacement);
updateSearchRangeAfterModification(&search_range, result_range, replacement_length);
}
#endif
}
void jxld_CFStringRemoveWhitespace(CFMutableStringRef string) {
CFCharacterSetRef characterSetWhitespaceAndNewline = CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline);
jxld_CFStringDeleteCharactersFromSet(string, characterSetWhitespaceAndNewline);
}
void jxld_CFStringReplaceDelimitersWithSpace(CFMutableStringRef string) {
static CFCharacterSetRef delimitersCharacterSet = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
delimitersCharacterSet = CFCharacterSetCreateWithCharactersInString(kCFAllocatorDefault, CFSTR("_-"));
});
CFStringRef replacement = CFSTR(" ");
jxld_CFStringReplaceCharactersInSet(string, delimitersCharacterSet, replacement);
}
void jxld_CFStringStraightenQuotes(CFMutableStringRef string) {
CFStringRef replacement;
static CFCharacterSetRef singleQuotesCharacterSet = nil;
static CFCharacterSetRef doubleQuotesCharacterSet = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
singleQuotesCharacterSet = CFCharacterSetCreateWithCharactersInString(kCFAllocatorDefault, CFSTR("‘’❯❮❛❜›‹‚‛❟"));
doubleQuotesCharacterSet = CFCharacterSetCreateWithCharactersInString(kCFAllocatorDefault, CFSTR("“”„‟"〟〞〝❝❞»«❠"));
});
replacement = CFSTR("\"");
jxld_CFStringReplaceCharactersInSet(string, doubleQuotesCharacterSet, replacement);
replacement = CFSTR("\'");
jxld_CFStringReplaceCharactersInSet(string, singleQuotesCharacterSet, replacement);
}
void jxld_CFStringReplaceCharactersInSet(CFMutableStringRef string, CFCharacterSetRef delimitersCharacterSet, CFStringRef replacement) {
jxld_CFStringReplaceCharactersFromSet(string, delimitersCharacterSet, replacement);
}
void jxld_CFStringPreprocessWithOptions(CFMutableStringRef string, JXLDStringDistanceOptions options) {
if (!(options & JXLDLiteralComparison)) {
CFStringCompareFlags foldingOptions = 0;
if (options & JXLDCaseInsensitiveComparison) {
foldingOptions |= kCFCompareCaseInsensitive;
}
if (options & JXLDDiacriticInsensitiveComparison) {
foldingOptions |= kCFCompareDiacriticInsensitive;
}
if (options & JXLDWidthInsensitiveComparison) {
foldingOptions |= kCFCompareWidthInsensitive;
}
if (options & JXLDDelimiterInsensitiveComparison) {
jxld_CFStringReplaceDelimitersWithSpace(string);
}
if (options & JXLDQuoteTypeInsensitiveComparison) {
jxld_CFStringStraightenQuotes(string);
}
if (options & JXLDWhitespaceInsensitiveComparison) {
//CFStringTransform(string, NULL, CFSTR("[:WhiteSpace:] any-remove;"), false); // This works, but is very slow.
jxld_CFStringRemoveWhitespace(string);
}
if (options & JXLDWhitespaceTrimmingComparison) {
CFStringTrimWhitespace(string);
}
CFStringFold(string, foldingOptions, NULL);
CFStringNormalize(string, kCFStringNormalizationFormC);
}
}
float jxld_normalizeDistance(NSUInteger length1, NSUInteger length2, float maxDistance, NSUInteger (^levensteinDistanceBlock)(void)) {
float normalizedDistance = 0.0f;
NSUInteger longStringLength = MAX(length1, length2);
if (maxDistance <= 1.0f) {
NSUInteger shortStringLength = MIN(length1, length2);
NSUInteger minPossibleDistance = longStringLength - shortStringLength;
float minPossibleNormalizedDistance = (float)minPossibleDistance/longStringLength;
if (minPossibleNormalizedDistance >= maxDistance) {
return minPossibleNormalizedDistance;
}
}
if (longStringLength > 0) {
NSUInteger levensteinDistance = levensteinDistanceBlock();
normalizedDistance = (float)levensteinDistance/longStringLength;
}
return normalizedDistance;
}