30@implementation NSString (OOExtensions)
32+ (instancetype) stringWithContentsOfUnicodeFile:(NSString *)path
37 const uint8_t *bytes = NULL;
39 const uint8_t *effectiveBytes = NULL;
40 size_t effectiveLength = 0;
42 data = [[NSData oo_dataWithOXZFile:path] retain];
43 if (data ==
nil) OK = NO;
47 length = [data length];
51 if (OK && 2 <= length && (length %
sizeof(unichar)) == 0)
54 unichar firstChar = bytes[0];
55 firstChar = (firstChar << 8) | bytes[1];
56 if (firstChar == 0xFFFE || firstChar == 0xFEFF)
59 result = [NSString stringWithCharacters:(unichar *)(bytes + sizeof(unichar)) length:(length / sizeof(unichar)) - 1];
60 if (result ==
nil) OK = NO;
64 if (OK && result ==
nil)
67 if (3 <= length && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
70 effectiveBytes = bytes + 3;
71 effectiveLength = length + 3;
75 effectiveBytes = bytes;
76 effectiveLength = length;
80 result = [[[NSString alloc] initWithBytes:effectiveBytes length:effectiveLength encoding:NSUTF8StringEncoding] autorelease];
83 if (OK && result ==
nil)
86 result = [[[NSString alloc] initWithBytes:effectiveBytes length:effectiveLength encoding:NSISOLatin1StringEncoding] autorelease];
94+ (instancetype) stringWithUTF16String:(const unichar *)chars
99 if (chars == NULL)
return nil;
104 length = end - chars - 1;
106 return [NSString stringWithCharacters:chars length:length];
110- (NSData *) utf16DataWithBOM:(BOOL)includeByteOrderMark
112 size_t lengthInChars;
113 size_t lengthInBytes;
114 unichar *buffer = NULL;
115 unichar *characters = NULL;
118 lengthInChars = [
self length];
119 lengthInBytes = lengthInChars *
sizeof(unichar);
120 if (includeByteOrderMark) lengthInBytes += sizeof(unichar);
123 buffer = malloc(lengthInBytes);
124 if (buffer == NULL)
return nil;
128 if (includeByteOrderMark)
130 *characters++ = 0xFEFF;
134 [
self getCharacters:characters];
137 return [NSData dataWithBytesNoCopy:buffer length:lengthInBytes freeWhenDone:YES];
143 NSUInteger i, length = [
self length];
144 uint32_t hash = 5381;
145 for (i = 0; i < length; i++)
147 hash = ((hash << 5) + hash) ^ [
self characterAtIndex:i];
153- (NSString *)stringByTrimmingLeadingCharactersInSet:(NSCharacterSet *)characterSet
155 NSRange rangeOfFirstWantedCharacter = [
self rangeOfCharacterFromSet:[characterSet invertedSet]];
156 if (rangeOfFirstWantedCharacter.location == NSNotFound)
return @"";
158 return [
self substringFromIndex:rangeOfFirstWantedCharacter.location];
164 return [
self stringByTrimmingLeadingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
168- (NSString *)stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *)characterSet
170 NSRange rangeOfLastWantedCharacter = [
self rangeOfCharacterFromSet:[characterSet invertedSet] options:NSBackwardsSearch];
171 if (rangeOfLastWantedCharacter.location == NSNotFound)
return @"";
173 return [
self substringToIndex:rangeOfLastWantedCharacter.location+1];
179 return [
self stringByTrimmingTrailingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
185@implementation NSMutableString (OOExtensions)
187- (void) appendLine:(NSString *)line
189 [
self appendString:line ? [line stringByAppendingString:@"\n"] : (NSString *)@"\n"];
193- (void) appendFormatLine:(NSString *)fmt, ...
197 [
self appendFormatLine:fmt arguments:args];
202- (void) appendFormatLine:(NSString *)fmt arguments:(va_list)args
204 NSString *formatted = [[NSString alloc] initWithFormat:fmt arguments:args];
205 [
self appendLine:formatted];
210- (void) deleteCharacterAtIndex:(
unsigned long)index
212 [
self deleteCharactersInRange:NSMakeRange(index, 1)];
220 NSString *
const staticTabs[] =
231 @"\t\t\t\t\t\t\t\t\t",
232 @"\t\t\t\t\t\t\t\t\t\t"
234 enum { kStaticTabCount =
sizeof staticTabs /
sizeof *staticTabs };
236 if (
count < kStaticTabCount)
238 return staticTabs[count];
242 return [staticTabs[kStaticTabCount - 1] stringByAppendingString:OOTabString(count - (kStaticTabCount - 1))];
NSString * OOTabString(NSUInteger count)
NSString * stringByTrimmingTrailingWhitespaceAndNewlineCharacters()
NSString * stringByTrimmingLeadingWhitespaceAndNewlineCharacters()