My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//
// NSString_CompletionExtensions.m
// Quicksilver
//
// Created by Alcor on Mon Mar 03 2003.

//

#import "NSString_BLTRExtensions.h"
#import "NSArray_BLTRExtensions.h"
//#import "QSense.h"


NSComparisonResult prefixCompare(NSString *aString, NSString *bString) {
int length = MIN([aString length] , [bString length]);
if (!length) return NSOrderedSame;
return [aString compare:bString options:NSCaseInsensitiveSearch range:NSMakeRange(0, MIN([aString length] , [bString length]) )];
}

@implementation NSString (Abbreviation)
- (float) scoreForString:(NSString *)testString {
float score = 1;
int i;
// NSString *remainingString;
NSString *characterString;
NSRange currentRange = NSMakeRange(0, [testString length]);
int index;
for (i = 0; i<(int) [self length]; i++) {

characterString = [self substringWithRange:NSMakeRange(i, 1)];

index = [testString rangeOfString:characterString options:NSCaseInsensitiveSearch range:currentRange] .location;
if (index == NSNotFound) return 0;
score -= (float) (index-currentRange.location)/[testString length];
currentRange.location = index+1;
currentRange.length = [testString length] -index-1;
// QSLog(@"Character at:%i", index);
}
score -= currentRange.length/[testString length];
//QSLog(@"Score:%f", score*100);
return score;
}


- (float) oldScoreForAbbreviation:(NSString *)abbreviation hitMask:(NSMutableIndexSet *)mask {
return [self scoreForAbbreviation:abbreviation inRange:NSMakeRange(0, [self length]) fromRange:NSMakeRange(0, [abbreviation length]) hitMask:mask];
}
- (float) oldScoreForAbbreviation:(NSString *)abbreviation {
return [self oldScoreForAbbreviation:abbreviation hitMask:nil];
}
- (float) scoreForAbbreviation:(NSString *)abbreviation {
return [self scoreForAbbreviation:abbreviation hitMask:nil];
}
- (float) scoreForAbbreviation:(NSString *)abbreviation hitMask:(NSMutableIndexSet *)mask {

//return QSScoreForAbbreviation((CFStringRef) self, (CFStringRef)abbreviation, mask);

return [self scoreForAbbreviation:abbreviation inRange:NSMakeRange(0, [self length]) fromRange:NSMakeRange(0, [abbreviation length]) hitMask:mask];
}

- (float) scoreForAbbreviation:(NSString *)abbreviation inRange:(NSRange)searchRange fromRange:(NSRange)abbreviationRange hitMask:(NSMutableIndexSet *)mask {
float score, remainingScore;
int i, j;
NSRange matchedRange, remainingSearchRange;
if (!abbreviationRange.length) return 0.9; //deduct some points for all remaining letters
if (abbreviationRange.length>searchRange.length) return 0.0;
/*
{
matchedRange = [self rangeOfString:[abbreviation substringWithRange:NSMakeRange(abbreviationRange.location, 1)]
options:NSCaseInsensitiveSearch
range:searchRange];

if (matchedRange.location == NSNotFound) return 0.9;
searchRange.length -= matchedRange.location-searchRange.location;
searchRange.location = matchedRange.location;
}
*/
for (i = abbreviationRange.length; i>0; i--) { //Search for steadily smaller portions of the abbreviation
matchedRange = [self rangeOfString:[abbreviation substringWithRange:NSMakeRange(abbreviationRange.location, i)]
options:NSCaseInsensitiveSearch
range:searchRange];

if (matchedRange.location == NSNotFound) continue;
if (matchedRange.location+abbreviationRange.length>NSMaxRange(searchRange) ) continue;

if (mask) [mask addIndexesInRange:matchedRange];

remainingSearchRange.location = NSMaxRange(matchedRange);
remainingSearchRange.length = NSMaxRange(searchRange) -remainingSearchRange.location;

// Search what is left of the string with the rest of the abbreviation
remainingScore = [self scoreForAbbreviation:abbreviation
inRange:remainingSearchRange
fromRange:NSMakeRange(abbreviationRange.location+i, abbreviationRange.length-i)
hitMask:mask];
if (remainingScore) {
score = remainingSearchRange.location-searchRange.location;
// ignore skipped characters if is first letter of a word
if (matchedRange.location>searchRange.location) {//if some letters were skipped
j = 0;
if ([[NSCharacterSet whitespaceCharacterSet] characterIsMember:[self characterAtIndex:matchedRange.location-1]]) {
for (j = matchedRange.location-2; j >= (int) searchRange.location; j--) {
if ([[NSCharacterSet whitespaceCharacterSet] characterIsMember:[self characterAtIndex:j]]) score--;
else score -= 0.15;
}

} else if ([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[self characterAtIndex:matchedRange.location]]) {
for (j = matchedRange.location-1; j >= (int) searchRange.location; j--) {
if ([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[self characterAtIndex:j]])
score--;
else
score -= 0.15;
}
} else {
score -= matchedRange.location-searchRange.location;
}

}

score += remainingScore*remainingSearchRange.length;
score /= searchRange.length;
return score;
}
}
return 0;

}



- (NSArray *)hitsForString:(NSString *)testString {
NSMutableArray *hitsArray = [NSMutableArray arrayWithCapacity:[self length]];
int i;
NSString *characterString;
NSRange currentRange = NSMakeRange(0, [testString length]);
int index;
for (i = 0; i<(int) [self length]; i++) {
characterString = [self substringWithRange:NSMakeRange(i, 1)];
index = [testString rangeOfString:characterString options:NSCaseInsensitiveSearch range:currentRange] .location;
if (index == NSNotFound) return hitsArray;
[hitsArray addObject:[NSNumber numberWithInt:index]];
currentRange.location = index+1;
currentRange.length = [testString length] -index-1;
}
return hitsArray;
}



@end



@implementation NSAttributedString (Sizing)
- (NSSize) sizeForWidth:(float)width {
NSSize size;

NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:self];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *container = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(width, MAXFLOAT)];

[textStorage addLayoutManager:layoutManager];
[layoutManager addTextContainer:container];

unsigned numberOfLines, index, numberOfGlyphs = [layoutManager numberOfGlyphs];
NSRange lineRange;
//float height;
for (numberOfLines = 0, index = 0; index < numberOfGlyphs; numberOfLines++) {
NSRect rect = [layoutManager lineFragmentRectForGlyphAtIndex:index
effectiveRange:&lineRange];
size.height += NSHeight(rect);
size.width = MAX(size.width, NSWidth(rect) );
index = NSMaxRange(lineRange);
}

[container release];
[layoutManager release];
[textStorage release];

//return (numberOfLines+0.1) *[layoutManager defaultLineHeightForFont:[NSFont boldSystemFontOfSize:size]];

return size;

}
@end

@implementation NSString (URLEncoding)

- (NSString *)URLEncoding {// escape embedded %-signs that don't appear to actually be escape sequences,
// and pre-decode the result to avoid double-encoding
return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef) self, NULL, NULL, kCFStringEncodingUTF8);
}

- (NSString *)URLEncodingWithEncoding:(CFStringEncoding) encoding {// escape embedded %-signs that don't appear to actually be escape sequences,
// and pre-decode the result to avoid double-encoding
return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef) self, NULL, NULL, encoding);
}

- (NSString *)URLDecoding {
// escape embedded %-signs that don't appear to actually be escape sequences
//NSString * preppedString = escapePercentsInString(self);
return (NSString *)
CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault, (CFStringRef) self, (CFStringRef) @"");
}
@end

@implementation NSString (Truncation)

- (NSString *)stringTruncatedToWidth:(float) width withAttributes:(NSDictionary *)attributes {


if ([self sizeWithAttributes:attributes] .width <= width) return self;

NSString *ellipsisString = @"...";
NSSize ellipsisSize = [ellipsisString sizeWithAttributes:attributes];

if (width<ellipsisSize.width) return @"";
//QSLog(@"Size of %@: \r%f trunc to %f", self, [self sizeWithAttributes:attributes] .width, width);

NSMutableString *truncString = [[self mutableCopy] autorelease];

float naturalWidth = [truncString sizeWithAttributes:attributes] .width;
int extra = (float) [self length] * (naturalWidth-width)/naturalWidth;
//QSLog(@"slicing:%d", extra);
[truncString deleteCharactersInRange:NSMakeRange(([self length] -extra) /2, extra)];

while ([truncString sizeWithAttributes:attributes] .width+ellipsisSize.width > width) {
[truncString deleteCharactersInRange:NSMakeRange([truncString length] /2, 1)];
}

//QSLog(Blacktree, Inc. %d", [truncString length] /2+1);
if ([truncString length])
[truncString insertString:ellipsisString atIndex:([truncString length] +1) /2];
//QSLog(@"Trunc:%@", truncString);
return truncString;
}

@end
@implementation NSString(uuid)

+ (NSString *)uniqueString
{
CFUUIDRef uuid = CFUUIDCreate(NULL);
CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
[(NSString *)uuidStr autorelease];
return (NSString *)uuidStr;
}

@end

@implementation NSString (Hex)


- (NSString *)decodedPasteboardType {
NSString *coreString = @"CorePasteboardFlavorType 0x";
if (![self hasPrefix:coreString]) return self;
return [NSString stringWithFormat:@"'%@'", [[self substringFromIndex:[coreString length]]decodedHexString]];
}

- (NSString *)encodedPasteboardType {
if (![self hasPrefix:@"'"]) return self;
if (![self hasSuffix:@"'"]) return self;
return [NSString stringWithFormat:@"CorePasteboardFlavorType 0x%@", [[[self substringWithRange:NSMakeRange(1, [self length] -2)] encodedHexString] uppercaseString]];
}

- (NSString *)decodedHexString {
char s[4];
unsigned x;
int i;
for (i = 0; i<((int) [self length] /2); i++) {
[[NSScanner scannerWithString:[self substringWithRange:NSMakeRange(i*2, 2)]]scanHexInt:&x];
s[i] = (char) x;
}
return [NSString stringWithCString:s length:4];
}
- (unsigned) hexIntValue {
unsigned x;
[[NSScanner scannerWithString:self] scanHexInt:&x];
return x;
}

- (NSString *)encodedHexString {
NSMutableString *myHexString = [NSMutableString string];
char aChar;
short index = 0;

for (; index < [self lengthOfBytesUsingEncoding:NSASCIIStringEncoding]; index++) {
aChar = [self characterAtIndex:index];
[myHexString appendFormat:@"%x", aChar];
}
return myHexString;
}

- (NSComparisonResult) versionCompare:(NSString *)other {

return [self hexIntValue] -[other hexIntValue];
}


@end

@implementation NSString (Replacement)
- (NSArray *)lines {
NSMutableString *mut = [NSMutableString stringWithString:self];
[mut replaceOccurrencesOfString:@"\r\n" withString:@"\n" options:0 range:NSMakeRange(0, [mut length])];
[mut replaceOccurrencesOfString:@"\r" withString:@"\n" options:0 range:NSMakeRange(0, [mut length])];
return [mut componentsSeparatedByString:@"\n"];
}
- (NSString *)stringByReplacing:(NSString *)search with:(NSString *)replacement {
NSMutableString *result = [NSMutableString stringWithCapacity:[self length]];
[result setString:self];
[result replaceOccurrencesOfString:search withString:replacement options:NSLiteralSearch range:NSMakeRange(0, [self length])];
return result;
}

@end


@implementation NSString (Fit)

- (NSDictionary *)attributesToFitNumbersInRect:(NSRect) rect withAttributes:(NSDictionary *)attributes {
NSMutableDictionary *newAttributes = [[attributes mutableCopy] autorelease];

if (!newAttributes) newAttributes = [NSMutableDictionary dictionaryWithCapacity:1];
NSFont *font = [newAttributes objectForKey:NSFontAttributeName];

font = [NSFont fontWithName:[font fontName] size:12];
[newAttributes setObject:font forKey:NSFontAttributeName];

NSSize baseSize = [self sizeWithAttributes:newAttributes];
float xScale = NSWidth(rect) /baseSize.width;
float yScale = NSHeight(rect) /([font ascender] -[font descender]);
//QSLog(@"m x %f y %f", NSHeight(rect), [font defaultLineHeightForFont]);
//QSLog(@"scale x %f y %f", xScale, yScale);
float newFontSize = 12*MIN(xScale, yScale);
font = [NSFont fontWithName:[font fontName] size:newFontSize];
[newAttributes setObject:font forKey:NSFontAttributeName];

return newAttributes;
}

- (NSDictionary *)attributesToFitRect:(NSRect) rect withAttributes:(NSDictionary *)attributes {
NSMutableDictionary *newAttributes = [[attributes mutableCopy] autorelease];

if (!newAttributes) newAttributes = [NSMutableDictionary dictionaryWithCapacity:1];
NSFont *font = [newAttributes objectForKey:NSFontAttributeName];
//if (!font);
float fontSize = [font pointSize];

NSSize textSize;
for (; fontSize>6; fontSize--) {
[newAttributes setObject:[NSFont systemFontOfSize:fontSize] forKey:NSFontAttributeName];
textSize = [self sizeWithAttributes:newAttributes];
if (textSize.width <= NSWidth(rect) && textSize.height <= NSHeight(rect) )
return newAttributes;
}

return newAttributes;

}

@end


@implementation NSString (Blacktree)
- (NSArray *)componentsSeparatedByStrings:(NSArray *)strings {
//QSLog(@"%@ - >> %@", self, strings);
NSArray *array;
if ([strings count] >0)
array = [self componentsSeparatedByString:[strings head]];

if ([strings count] >1)
array = [array arrayByPerformingSelector:@selector(componentsSeparatedByStrings:)
withObject:[strings tail]];
return array;
}

- (NSString *)stringByResolvingWildcardsInPath {
NSMutableArray *components = [[[[self stringByStandardizingPath] pathComponents] mutableCopy] autorelease];
NSFileManager *manager = [NSFileManager defaultManager];

int index = [components indexOfObject:@"*"];
if (index == NSNotFound) return [self stringByStandardizingPath];
NSString *basePath = nil;
NSArray *contents = nil;
while((index = [components indexOfObject:@"*"]) != NSNotFound) {
basePath = [NSString pathWithComponents:[components subarrayWithRange:NSMakeRange(0, index)]];
contents = [manager directoryContentsAtPath:basePath];
if (![contents count]) return self;
[components replaceObjectAtIndex:index withObject:[contents lastObject]];

// QSLog([NSString pathWithComponents:components]);
}

//QSLog([NSString pathWithComponents:components]);
return [NSString pathWithComponents:components];


}

- (NSString *)firstUnusedFilePath {
NSString *basePath = [self stringByDeletingPathExtension];
NSString *extension = [self pathExtension];
NSString *alternatePath = self;
int i;
for (i = 1; [[NSFileManager defaultManager] fileExistsAtPath:alternatePath]; i++)
alternatePath = [NSString stringWithFormat:@"%@ %d.%@", basePath, i, extension];
return alternatePath;
}

@end

Change log

r87 by tiennou7 on Jan 3, 2008   Diff
Added QSApp & QSController informal
protocols. Fixed some bugs. Indentation.
Go to: 
Project members, sign in to write a code review

Older revisions

r18 by jnj on Dec 8, 2007   Diff
Reorganize source
r10 by jnj on Nov 10, 2007   Diff
Added copying file, fixed elements
reference
r5 by jnj on Oct 30, 2007   Diff
Initial import to GC
All revisions of this file

File info

Size: 14747 bytes, 412 lines
Powered by Google Project Hosting