Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
NSString(OOExtensions) Category Reference

#include <NSStringOOExtensions.h>

Instance Methods

(NSData *) - utf16DataWithBOM:
 
(uint32_t) - oo_hash
 
(NSString *) - stringByTrimmingLeadingCharactersInSet:
 
(NSString *) - stringByTrimmingLeadingWhitespaceAndNewlineCharacters
 
(NSString *) - stringByTrimmingTrailingCharactersInSet:
 
(NSString *) - stringByTrimmingTrailingWhitespaceAndNewlineCharacters
 

Class Methods

(instancetype) + stringWithContentsOfUnicodeFile:
 
(instancetype) + stringWithUTF16String:
 

Detailed Description

Definition at line 30 of file NSStringOOExtensions.h.

Method Documentation

◆ oo_hash

- (uint32_t) oo_hash

Definition at line 218 of file NSStringOOExtensions.m.

142{
143 NSUInteger i, length = [self length];
144 uint32_t hash = 5381;
145 for (i = 0; i < length; i++)
146 {
147 hash = ((hash << 5) + hash) /* 33 * hash */ ^ [self characterAtIndex:i];
148 }
149 return hash;
150}
return self

◆ stringByTrimmingLeadingCharactersInSet:

- (NSString *) stringByTrimmingLeadingCharactersInSet: (NSCharacterSet *) characterSet

Definition at line 218 of file NSStringOOExtensions.m.

153 :(NSCharacterSet *)characterSet
154{
155 NSRange rangeOfFirstWantedCharacter = [self rangeOfCharacterFromSet:[characterSet invertedSet]];
156 if (rangeOfFirstWantedCharacter.location == NSNotFound) return @"";
157
158 return [self substringFromIndex:rangeOfFirstWantedCharacter.location];
159}

◆ stringByTrimmingLeadingWhitespaceAndNewlineCharacters

- (NSString *) stringByTrimmingLeadingWhitespaceAndNewlineCharacters

Definition at line 218 of file NSStringOOExtensions.m.

163{
164 return [self stringByTrimmingLeadingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
165}

◆ stringByTrimmingTrailingCharactersInSet:

- (NSString *) stringByTrimmingTrailingCharactersInSet: (NSCharacterSet *) characterSet

Definition at line 218 of file NSStringOOExtensions.m.

168 :(NSCharacterSet *)characterSet
169{
170 NSRange rangeOfLastWantedCharacter = [self rangeOfCharacterFromSet:[characterSet invertedSet] options:NSBackwardsSearch];
171 if (rangeOfLastWantedCharacter.location == NSNotFound) return @"";
172
173 return [self substringToIndex:rangeOfLastWantedCharacter.location+1]; // non-inclusive
174}

◆ stringByTrimmingTrailingWhitespaceAndNewlineCharacters

- (NSString *) stringByTrimmingTrailingWhitespaceAndNewlineCharacters

Definition at line 218 of file NSStringOOExtensions.m.

178{
179 return [self stringByTrimmingTrailingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
180}

◆ stringWithContentsOfUnicodeFile:

+ (instancetype) stringWithContentsOfUnicodeFile: (NSString *) path

Definition at line 218 of file NSStringOOExtensions.m.

32 :(NSString *)path
33{
34 id result = nil;
35 BOOL OK = YES;
36 NSData *data = nil;
37 const uint8_t *bytes = NULL;
38 size_t length = 0;
39 const uint8_t *effectiveBytes = NULL;
40 size_t effectiveLength = 0;
41
42 data = [[NSData oo_dataWithOXZFile:path] retain];
43 if (data == nil) OK = NO;
44
45 if (OK)
46 {
47 length = [data length];
48 bytes = [data bytes];
49 }
50
51 if (OK && 2 <= length && (length % sizeof(unichar)) == 0)
52 {
53 // Could be UTF-16
54 unichar firstChar = bytes[0];
55 firstChar = (firstChar << 8) | bytes[1]; // Endianism doesn't matter, because we test both orders of BOM.
56 if (firstChar == 0xFFFE || firstChar == 0xFEFF)
57 {
58 // Consider it to be UTF-16.
59 result = [NSString stringWithCharacters:(unichar *)(bytes + sizeof(unichar)) length:(length / sizeof(unichar)) - 1];
60 if (result == nil) OK = NO;
61 }
62 }
63
64 if (OK && result == nil)
65 {
66 // Not UTF-16. Try UTF-8.
67 if (3 <= length && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
68 {
69 // File starts with UTF-8 BOM; skip it.
70 effectiveBytes = bytes + 3;
71 effectiveLength = length + 3;
72 }
73 else
74 {
75 effectiveBytes = bytes;
76 effectiveLength = length;
77 }
78
79 // Attempt to interpret as UTF-8
80 result = [[[NSString alloc] initWithBytes:effectiveBytes length:effectiveLength encoding:NSUTF8StringEncoding] autorelease];
81 }
82
83 if (OK && result == nil)
84 {
85 // Not UTF-16 or UTF-8. Use ISO-Latin-1 (which should work for any byte sequence).
86 result = [[[NSString alloc] initWithBytes:effectiveBytes length:effectiveLength encoding:NSISOLatin1StringEncoding] autorelease];
87 }
88
89 [data release];
90 return result;
91}
return nil

◆ stringWithUTF16String:

+ (instancetype) stringWithUTF16String: (const unichar *) chars

Definition at line 218 of file NSStringOOExtensions.m.

94 :(const unichar *)chars
95{
96 size_t length;
97 const unichar *end;
98
99 if (chars == NULL) return nil;
100
101 // Find length of string.
102 end = chars;
103 while (*end++) {}
104 length = end - chars - 1;
105
106 return [NSString stringWithCharacters:chars length:length];
107}

◆ utf16DataWithBOM:

- (NSData *) utf16DataWithBOM: (BOOL) includeByteOrderMark

Definition at line 218 of file NSStringOOExtensions.m.

110 :(BOOL)includeByteOrderMark
111{
112 size_t lengthInChars;
113 size_t lengthInBytes;
114 unichar *buffer = NULL;
115 unichar *characters = NULL;
116
117 // Calculate sizes
118 lengthInChars = [self length];
119 lengthInBytes = lengthInChars * sizeof(unichar);
120 if (includeByteOrderMark) lengthInBytes += sizeof(unichar);
121
122 // Allocate buffer
123 buffer = malloc(lengthInBytes);
124 if (buffer == NULL) return nil;
125
126 // write BOM (native-endian) if desired
127 characters = buffer;
128 if (includeByteOrderMark)
129 {
130 *characters++ = 0xFEFF;
131 }
132
133 // Get the contents
134 [self getCharacters:characters];
135
136 // NSData takes ownership of the buffer.
137 return [NSData dataWithBytesNoCopy:buffer length:lengthInBytes freeWhenDone:YES];
138}

The documentation for this category was generated from the following files: