forked from JanX2/NSString-DamerauLevenshtein
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JXLDStringDistanceUtilities.m
58 lines (45 loc) · 1.73 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
//
// JXLDStringDistanceUtilities.m
// Damerau-Levenshtein
//
// Created by Jan on 18.01.11.
// Copyright 2011-2012 geheimwerk.de. All rights reserved.
//
#import "JXLDStringDistanceUtilities.h"
void jxld_CFStringRemoveWhitespace(CFMutableStringRef string) {
CFIndex string_length = CFStringGetLength(string);
CFCharacterSetRef characterSetWhitespaceAndNewline = CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline);
CFRange result;
CFRange range_to_search = CFRangeMake(0, string_length);
while ( CFStringFindCharacterFromSet(string,
characterSetWhitespaceAndNewline,
range_to_search,
kCFCompareBackwards,
&result) ) {
CFStringDelete(string, result);
range_to_search.length = result.location; // We can do this safely, because we use kCFCompareBackwards
}
}
void jxld_CFStringPreprocessWithOptions(CFMutableStringRef string, JXLDStringDistanceOptions options) {
if (!(options & JXLDLiteralComparison)) {
CFOptionFlags foldingOptions = 0;
if (options & JXLDCaseInsensitiveComparison) {
foldingOptions |= kCFCompareCaseInsensitive;
}
if (options & JXLDDiacriticInsensitiveComparison) {
foldingOptions |= kCFCompareDiacriticInsensitive;
}
if (options & JXLDWidthInsensitiveComparison) {
foldingOptions |= kCFCompareWidthInsensitive;
}
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);
}
}