Oolite
Loading...
Searching...
No Matches
NSStringOOExtensions.m
Go to the documentation of this file.
1/*
2
3NSStringOOExtensions.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
27#import "OOFoundation.h"
28
29@implementation NSString (OOExtensions)
30
31+ (instancetype)stringWithContentsOfUnicodeFile:(NSString*)path
32{
33 id result = nil;
34 BOOL OK = YES;
35 NSData* data = nil;
36 const uint8_t* bytes = NULL;
37 size_t length = 0;
38 const uint8_t* effectiveBytes = NULL;
39 size_t effectiveLength = 0;
40
41 data = [[NSData oo_dataWithOXZFile:path] retain];
42 if (data == nil)
43 OK = NO;
44
45 if (OK) {
46 length = [data length];
47 bytes = [data bytes];
48 }
49
50 if (OK && 2 <= length && (length % sizeof(unichar)) == 0) {
51 // Could be UTF-16
52 unichar firstChar = bytes[0];
53 firstChar = (firstChar << 8) | bytes[1]; // Endianism doesn't matter, because we test both orders of BOM.
54 if (firstChar == 0xFFFE || firstChar == 0xFEFF) {
55 // Consider it to be UTF-16.
56 result = [NSString stringWithCharacters:(unichar*)(bytes + sizeof(unichar)) length:(length / sizeof(unichar)) - 1];
57 if (result == nil)
58 OK = NO;
59 }
60 }
61
62 if (OK && result == nil) {
63 // Not UTF-16. Try UTF-8.
64 if (3 <= length && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
65 // File starts with UTF-8 BOM; skip it.
66 effectiveBytes = bytes + 3;
67 effectiveLength = length + 3;
68 } else {
69 effectiveBytes = bytes;
70 effectiveLength = length;
71 }
72
73 // Attempt to interpret as UTF-8
74 result = [[[NSString alloc] initWithBytes:effectiveBytes length:effectiveLength encoding:NSUTF8StringEncoding] autorelease];
75 }
76
77 if (OK && result == nil) {
78 // Not UTF-16 or UTF-8. Use ISO-Latin-1 (which should work for any byte sequence).
79 result = [[[NSString alloc] initWithBytes:effectiveBytes length:effectiveLength encoding:NSISOLatin1StringEncoding] autorelease];
80 }
81
82 [data release];
83 return result;
84}
85
86+ (instancetype)stringWithUTF16String:(const unichar*)chars
87{
88 size_t length;
89 const unichar* end;
90
91 if (chars == NULL)
92 return nil;
93
94 // Find length of string.
95 end = chars;
96 while (*end++) { }
97 length = end - chars - 1;
98
99 return [NSString stringWithCharacters:chars length:length];
100}
101
102- (NSData*)utf16DataWithBOM:(BOOL)includeByteOrderMark
103{
104 size_t lengthInChars;
105 size_t lengthInBytes;
106 unichar* buffer = NULL;
107 unichar* characters = NULL;
108
109 // Calculate sizes
110 lengthInChars = [self length];
111 lengthInBytes = lengthInChars * sizeof(unichar);
112 if (includeByteOrderMark)
113 lengthInBytes += sizeof(unichar);
114
115 // Allocate buffer
116 buffer = malloc(lengthInBytes);
117 if (buffer == NULL)
118 return nil;
119
120 // write BOM (native-endian) if desired
121 characters = buffer;
122 if (includeByteOrderMark) {
123 *characters++ = 0xFEFF;
124 }
125
126 // Get the contents
127 [self getCharacters:characters];
128
129 // NSData takes ownership of the buffer.
130 return [NSData dataWithBytesNoCopy:buffer length:lengthInBytes freeWhenDone:YES];
131}
132
133- (uint32_t)oo_hash
134{
135 NSUInteger i, length = [self length];
136 uint32_t hash = 5381;
137 for (i = 0; i < length; i++) {
138 hash = ((hash << 5) + hash) /* 33 * hash */ ^ [self characterAtIndex:i];
139 }
140 return hash;
141}
142
143- (NSString*)stringByTrimmingLeadingCharactersInSet:(NSCharacterSet*)characterSet
144{
145 NSRange rangeOfFirstWantedCharacter = [self rangeOfCharacterFromSet:[characterSet invertedSet]];
146 if (rangeOfFirstWantedCharacter.location == NSNotFound)
147 return @"";
148
149 return [self substringFromIndex:rangeOfFirstWantedCharacter.location];
150}
151
153{
154 return [self stringByTrimmingLeadingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
155}
156
157- (NSString*)stringByTrimmingTrailingCharactersInSet:(NSCharacterSet*)characterSet
158{
159 NSRange rangeOfLastWantedCharacter = [self rangeOfCharacterFromSet:[characterSet invertedSet] options:NSBackwardsSearch];
160 if (rangeOfLastWantedCharacter.location == NSNotFound)
161 return @"";
162
163 return [self substringToIndex:rangeOfLastWantedCharacter.location + 1]; // non-inclusive
164}
165
167{
168 return [self stringByTrimmingTrailingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
169}
170
171@end
172
173@implementation NSMutableString (OOExtensions)
174
175- (void)appendLine:(NSString*)line
176{
177 [self appendString:line ? [line stringByAppendingString:@"\n"] : (NSString*)@"\n"];
178}
179
180- (void)appendFormatLine:(NSString*)fmt, ...
181{
182 va_list args;
183 va_start(args, fmt);
184 [self appendFormatLine:fmt arguments:args];
185 va_end(args);
186}
187
188- (void)appendFormatLine:(NSString*)fmt arguments:(va_list)args
189{
190 NSString* formatted = [[NSString alloc] initWithFormat:fmt arguments:args];
191 [self appendLine:formatted];
192 [formatted release];
193}
194
195- (void)deleteCharacterAtIndex:(unsigned long)index
196{
197 [self deleteCharactersInRange:NSMakeRange(index, 1)];
198}
199
200@end
201
202NSString* OOTabString(NSUInteger count)
203{
204 NSString* const staticTabs[] = {
205 @"",
206 @"\t",
207 @"\t\t",
208 @"\t\t\t",
209 @"\t\t\t\t",
210 @"\t\t\t\t\t", // 5
211 @"\t\t\t\t\t\t",
212 @"\t\t\t\t\t\t\t",
213 @"\t\t\t\t\t\t\t\t",
214 @"\t\t\t\t\t\t\t\t\t",
215 @"\t\t\t\t\t\t\t\t\t\t" // 10
216 };
217 enum { kStaticTabCount = sizeof staticTabs / sizeof *staticTabs };
218
219 if (count < kStaticTabCount) {
220 return staticTabs[count];
221 } else {
222 return [staticTabs[kStaticTabCount - 1] stringByAppendingString:OOTabString(count - (kStaticTabCount - 1))];
223 }
224}
NSString * OOTabString(NSUInteger count)
unsigned count
return nil
void appendFormatLine:arguments:(NSString *fmt,[arguments] va_list args)
NSString * stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *characterSet)
NSString * stringByTrimmingTrailingWhitespaceAndNewlineCharacters()
NSString * stringByTrimmingLeadingCharactersInSet:(NSCharacterSet *characterSet)
NSString * stringByTrimmingLeadingWhitespaceAndNewlineCharacters()