forked from rsanders/nibwarelib
/
NibwareStringUtils.m
182 lines (148 loc) · 4.83 KB
/
NibwareStringUtils.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
//
// NibwareStringUtils.m
// pingle
//
// Created by Robert Sanders on 9/22/08.
// Copyright 2008 ViTrue, Inc.. All rights reserved.
//
#import "NibwareStringUtils.h"
#import "NibwareIO.h"
@implementation NibwareStringUtils
+ (BOOL) stringContains:(NSString *)string substring:(NSString *)substring {
NSRange range = [string rangeOfString:substring];
return (range.length != 0);
}
+ (NSString *) friendlyDataSize:(NSInteger)length decimalPlaces:(int)places
{
float num = length;
NSString *units;
if (places == -1) {
places = 1;
}
if (length >= 1024*1024*1024) {
num /= (1024*1024*1024);
units = @"GB";
} else if (length >= 1024*1024) {
num /= (1024*1024*1024);
units = @"MB";
} else if (length >= 1024) {
num /= 1024;
units = @"KB";
} else {
units = @"B";
places = 0;
}
NSString *fmtString;
if (places > 0) {
fmtString = [NSString stringWithFormat:@"%%.%df %%@", places];
} else {
fmtString = [NSString stringWithFormat:@"%%.0f %%@"];
num = round(num);
}
return [NSString stringWithFormat:fmtString, num, units];
}
+ (NSString *) friendlyDurationInSeconds:(NSInteger)seconds
{
NSMutableString *res = [[NSMutableString alloc] init];
float num = seconds / 3600.0;
if (num >= 1) {
[res appendFormat:@"%dh", (int)num];
}
num = (num - trunc(num)) * 60;
if (num >= 1) {
[res appendFormat:@"%dm", (int)num];
}
num = (num - trunc(num)) * 60;
if (num >= 1) {
[res appendFormat:@"%ds", (int)num];
}
return [res autorelease];
}
#pragma mark XML escaping
+ (NSString*) escapeForXML:(NSString *)string
{
NibwareDiskBackedBuffer *buf = [[NibwareDiskBackedBuffer alloc] initWithMaxSize:8192 capacity:string.length];
NSStringEncoding encoding = [string fastestEncoding];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (int i = 0; i < string.length; i++) {
unichar prochar = [string characterAtIndex:i];
NSString *replacement = nil;
switch (prochar) {
case '&':
replacement = @"&";
break;
case '<':
replacement = @"<";
break;
case '>':
replacement = @">";
break;
}
if (replacement) {
[buf appendString:replacement encoding:encoding];
} else {
[buf appendString:[NSString stringWithFormat:@"%C", prochar] encoding:encoding];
}
if (i % 50 == 0) [pool drain];
}
[pool release];
NSString *res = [buf inputString:encoding];
[buf release];
return res;
}
//////////////////////////////////////
#if 0
+ (void)detectURLs:(NSString*)string color:(NSColor*)linkColor
{
NSScanner* scanner;
NSRange scanRange;
NSString* scanString;
NSCharacterSet* whitespaceSet;
NSURL* foundURL;
NSDictionary* linkAttr;
// Create our scanner and supporting delimiting character set
scanner = [NSScanner scannerWithString:[string string]];
whitespaceSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
// Start Scan
while( ![scanner isAtEnd] )
{
// Pull out a token delimited by whitespace or new line
[scanner scanUpToCharactersFromSet:whitespaceSet intoString:&scanString];
scanRange.length = [scanString length];
scanRange.location = [scanner scanLocation] - scanRange.length;
// If we find a url modify the string attributes
if(( foundURL = findURL(scanString) ))
{
// Apply underline style and link color
linkAttr = [NSDictionary dictionaryWithObjectsAndKeys:
foundURL, NSLinkAttributeName,
[NSNumber numberWithInt:NSSingleUnderlineStyle], NSUnderlineStyleAttributeName,
linkColor, NSForegroundColorAttributeName, NULL ];
[self addAttributes:linkAttr range:scanRange];
}
}
}
NSURL* findURL(NSString* string)
{
NSRange theRange;
// Look for ://
theRange = [string rangeOfString:@"://"];
if( theRange.location != NSNotFound && theRange.length != 0 )
return [NSURL URLWithString:string];
// Look for www. at start
theRange = [string rangeOfString:@"www."];
if( theRange.location == 0 && theRange.length == 4 )
return [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", string]];
// Look for ftp. at start
theRange = [string rangeOfString:@"ftp."];
if( theRange.location == 0 && theRange.length == 4 )
return [NSURL URLWithString:[NSString stringWithFormat:@"ftp://%@", string]];
// Look for mailto: at start
theRange = [string rangeOfString:@"mailto:"];
if( theRange.location == 0 && theRange.length == 7 )
return [NSURL URLWithString:string];
return nil;
}
// \(?\bhttp://[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]
#endif
@end