forked from tupil/hyphenate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSString+Hyphenate.m
228 lines (179 loc) · 7.51 KB
/
NSString+Hyphenate.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// NSString+Hyphenate.m
//
// Created by Eelco Lempsink on 09-06-10.
// Copyright 2010 Tupil. All rights reserved.
//
#import "NSString+Hyphenate.h"
#include "hyphen.h"
@implementation NSString (Hyphenate)
- (NSString*)stringByHyphenatingWithLocale:(NSLocale*)locale usingSharedDictionaries:(id)sharedDictionaries
{
HyphenDict* dict = NULL;
NSString* localeIdentifier = nil;
static NSBundle* bundle = nil;
////////////////////////////////////////////////////////////////////////////
// Setup.
//
// Establish that we got all the information we need: the bundle with
// dictionaries, the locale and the loaded dictionary. Cache dictionary and
// save the language code used to retrieve it.
//
// Try to guess the locale from the string, if not given.
CFStringRef language;
if (locale == nil
&& (language = CFStringTokenizerCopyBestStringLanguage(
(CFStringRef)self, CFRangeMake(0, MIN(200, [self length])))))
{
locale = [[NSLocale alloc]
initWithLocaleIdentifier:(__bridge NSString*)language];
CFRelease(language);
}
if (locale == nil) {
return self;
} // else
if (![localeIdentifier isEqualToString:[locale localeIdentifier]]
&& dict != NULL)
{
hnj_hyphen_free(dict);
dict = NULL;
}
if (bundle == nil) {
NSString* bundlePath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:
@"Hyphenate.bundle"];
bundle = [NSBundle bundleWithPath:bundlePath];
}
localeIdentifier = [locale localeIdentifier];
NSArray * localeComponents = [localeIdentifier componentsSeparatedByString:@"_"];
@synchronized (sharedDictionaries) {
BOOL needToStore = NO;
if (sharedDictionaries && [sharedDictionaries objectForKey:localeIdentifier]) {
dict = [[sharedDictionaries objectForKey:localeIdentifier] pointerValue];
}
if (dict == NULL && localeComponents.count > 1) {
dict = [[sharedDictionaries objectForKey:localeComponents[0]] pointerValue];
}
if (dict == NULL) {
NSString * dictionaryPath = [bundle pathForResource:
[NSString stringWithFormat:@"hyph_%@",
localeIdentifier]
ofType:@"dic"];
if (dictionaryPath != nil) {
dict = hnj_hyphen_load([dictionaryPath UTF8String]);
needToStore = YES;
}
}
if (dict == NULL) {
if (localeComponents.count > 1) {
localeIdentifier = localeComponents[0];
dict = hnj_hyphen_load([[bundle pathForResource:
[NSString stringWithFormat:@"hyph_%@",
localeComponents[0]]
ofType:@"dic"]
UTF8String]);
needToStore = YES;
}
}
if (needToStore) {
[sharedDictionaries setObject:[NSValue valueWithPointer:dict] forKey:localeIdentifier];
}
}
if (dict == NULL) {
return self;
} // else
////////////////////////////////////////////////////////////////////////////
// The works.
//
// No turning back now. We traverse the string using a tokenizer and pass
// every word we find into the hyphenation function. Non-used tokens and
// hyphenated words will be appended to the result string.
//
NSMutableString* result = [NSMutableString stringWithCapacity:
[self length] * 1.2];
// Varibles used for tokenizing
CFStringTokenizerRef tokenizer;
CFStringTokenizerTokenType tokenType;
CFRange tokenRange;
NSString* token;
// Varibles used for hyphenation
char* hyphens;
char** rep;
int* pos;
int* cut;
int wordLength;
int i;
tokenizer = CFStringTokenizerCreate(kCFAllocatorDefault,
(CFStringRef)self,
CFRangeMake(0, [self length]),
kCFStringTokenizerUnitWordBoundary,
(CFLocaleRef)locale);
BOOL skippingTag = NO;
while ((tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer))
!= kCFStringTokenizerTokenNone)
{
tokenRange = CFStringTokenizerGetCurrentTokenRange(tokenizer);
token = [self substringWithRange:
NSMakeRange(tokenRange.location, tokenRange.length)];
if ([token hasPrefix:@"<"]) {
skippingTag = YES;
}
if ([token hasSuffix:@">"]) {
skippingTag = NO;
}
if (tokenType & kCFStringTokenizerTokenHasNonLettersMask) {
[result appendString:token];
} else {
char const* tokenChars = [token UTF8String];
wordLength = (int)strlen(tokenChars);
// This is the buffer size the algorithm needs.
hyphens = (char*)malloc(wordLength + 5); // +5, see hypen.h
rep = NULL; // Will be allocated by the algorithm
pos = NULL; // Idem
cut = NULL; // Idem
// rep, pos and cut are not currently used, but the simpler
// hyphenation function is deprecated.
hnj_hyphen_hyphenate2(dict, tokenChars, wordLength, hyphens,
NULL, &rep, &pos, &cut);
NSUInteger loc = 0;
NSUInteger len = 0;
@autoreleasepool {
for (i = 0; i < wordLength; i++) {
if (hyphens[i] & 1 && !skippingTag) {
NSString *tokenized = [[NSString alloc] initWithBytesNoCopy:(void *)tokenChars + loc length:i - loc + 1 encoding:NSUTF8StringEncoding freeWhenDone:NO];
if (tokenized.length < 2) {
continue;
}
len = i - loc + 1;
[result appendString:tokenized];
[result appendString:@"\u00AD"];
loc = loc + len;
}
}
if (loc < wordLength) {
NSString * tokenized = [[NSString alloc] initWithBytesNoCopy:(void *)tokenChars + loc length:wordLength - loc encoding:NSUTF8StringEncoding freeWhenDone:NO];
[result appendString:tokenized];
}
}
// Clean up
free(hyphens);
if (rep) {
for (i = 0; i < wordLength; i++) {
if (rep[i]) free(rep[i]);
}
free(rep);
free(pos);
free(cut);
}
}
}
CFRelease(tokenizer);
if (!sharedDictionaries) {
hnj_hyphen_free(dict);
}
return result;
}
- (NSString*)stringByHyphenatingWithLocale:(NSLocale*)locale {
return [self stringByHyphenatingWithLocale:locale usingSharedDictionaries:nil];
}
@end