Oolite
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 29 of file NSStringOOExtensions.h.

Method Documentation

◆ oo_hash

- (uint32_t) oo_hash

Definition at line 133 of file NSStringOOExtensions.m.

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}
return self

References oo_hash.

Referenced by oo_hash.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stringByTrimmingLeadingCharactersInSet:

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

Definition at line 143 of file NSStringOOExtensions.m.

143 :(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}

Referenced by stringByTrimmingLeadingWhitespaceAndNewlineCharacters.

Here is the caller graph for this function:

◆ stringByTrimmingLeadingWhitespaceAndNewlineCharacters

- (NSString *) stringByTrimmingLeadingWhitespaceAndNewlineCharacters

Definition at line 152 of file NSStringOOExtensions.m.

153{
154 return [self stringByTrimmingLeadingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
155}

References stringByTrimmingLeadingCharactersInSet:, and stringByTrimmingLeadingWhitespaceAndNewlineCharacters.

Referenced by stringByTrimmingLeadingWhitespaceAndNewlineCharacters.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stringByTrimmingTrailingCharactersInSet:

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

Definition at line 157 of file NSStringOOExtensions.m.

157 :(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}

Referenced by stringByTrimmingTrailingWhitespaceAndNewlineCharacters.

Here is the caller graph for this function:

◆ stringByTrimmingTrailingWhitespaceAndNewlineCharacters

- (NSString *) stringByTrimmingTrailingWhitespaceAndNewlineCharacters

Definition at line 166 of file NSStringOOExtensions.m.

167{
168 return [self stringByTrimmingTrailingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
169}

References stringByTrimmingTrailingCharactersInSet:, and stringByTrimmingTrailingWhitespaceAndNewlineCharacters.

Referenced by stringByTrimmingTrailingWhitespaceAndNewlineCharacters.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stringWithContentsOfUnicodeFile:

+ (instancetype) stringWithContentsOfUnicodeFile: (NSString*) path

Definition at line 31 of file NSStringOOExtensions.m.

31 :(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}
return nil

References nil.

◆ stringWithUTF16String:

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

Definition at line 86 of file NSStringOOExtensions.m.

86 :(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}

References nil.

◆ utf16DataWithBOM:

- (NSData *) utf16DataWithBOM: (BOOL) includeByteOrderMark

Definition at line 102 of file NSStringOOExtensions.m.

102 :(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}

References nil.


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