-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathNSDataExtensions.m
152 lines (124 loc) · 3.9 KB
/
NSDataExtensions.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
//
// NSDataExtensions.m
// LineReader
//
// Created by Tobias Preuss on 08.10.10.
// Copyright 2010 Tobias Preuss. All rights reserved.
//
#import "NSDataExtensions.h"
// -----------------------------------------------------------------------------
// NSData additions.
// -----------------------------------------------------------------------------
/**
Extension of the NSData class.
Data can be found forwards or backwards. Further the extension supplies a function
to convert the contents to string for debugging purposes.
@param Additions Category labeled Additions.
@returns An initialized NSData object or nil if the object could not be created.
*/
@implementation NSData (Additions)
/**
Returns a range of data.
@param dataToFind Data object specifying the delimiter and encoding.
@returns A range.
*/
- (NSRange)rangeOfData:(NSData*)dataToFind {
const void* bytes = [self bytes];
NSUInteger length = [self length];
const void* searchBytes = [dataToFind bytes];
NSUInteger searchLength = [dataToFind length];
NSUInteger searchIndex = 0;
NSRange foundRange = {NSNotFound, searchLength};
for (NSUInteger index = 0; index < length; index++) {
// The current character matches.
if (((char*)bytes)[index] == ((char*)searchBytes)[searchIndex]) {
// Store found location if not done earlier.
if (foundRange.location == NSNotFound) {
foundRange.location = index;
}
// Increment search character index to check for match.
searchIndex++;
// All search character match.
// Break search routine and return found position.
if (searchIndex >= searchLength) {
return foundRange;
}
}
// Match does not continue.
// Return to the first search character.
// Discard former found location.
else {
searchIndex = 0;
foundRange.location = NSNotFound;
}
}
return foundRange;
}
- (NSRange)rangeOfDataBackwardsSearch:(NSData*)dataToFind {
const void* bytes = [self bytes];
NSUInteger length = [self length];
const void* searchBytes = [dataToFind bytes];
NSUInteger searchLength = [dataToFind length];
NSUInteger searchIndex = 0;
NSRange foundRange = {NSNotFound, searchLength};
if (length < searchLength) {
return foundRange;
}
for (NSInteger index = length - searchLength; index >= 0;) {
// NSLog(@"%c == %c", ((char*)bytes)[index], ((char*)searchBytes)[searchIndex]); /* DEBUG LOG */
if (((char*)bytes)[index] == ((char*)searchBytes)[searchIndex]) {
// The current character matches.
if (foundRange.location == NSNotFound) {
foundRange.location = index;
}
index++;
searchIndex++;
if (searchIndex >= searchLength) {
return foundRange;
}
}
else {
// Decrement to search backwards.
if (foundRange.location == NSNotFound) {
// Skip if first byte has been reached.
if (index == 0) {
foundRange.location = NSNotFound;
return foundRange;
}
index--;
}
// Jump over the former found location
// to avoid endless loop.
else {
index = index - 2;
}
searchIndex = 0;
foundRange.location = NSNotFound;
}
}
return foundRange;
}
- (NSString*)stringValueWithEncoding:(NSStringEncoding)encoding {
return [[NSString alloc] initWithData:self encoding:encoding];
}
@end
// -----------------------------------------------------------------------------
// NSMutableData additions.
// -----------------------------------------------------------------------------
/**
Extension of the NSMutableData class.
Data can be prepended in addition to the append function of the framework.
@param Additions Category labeled Additions.
@returns An initialized NSMutableData object or nil if the object could not be created.
*/
@implementation NSMutableData (Additions)
/**
Inserts the data before the data of the object.
@param data Data to be prepended.
*/
- (void)prepend:(NSData*)data {
NSMutableData* concat = [NSMutableData dataWithData:data];
[concat appendData:self];
[self setData:concat];
}
@end