/
NSString-data.m
33 lines (28 loc) · 1.12 KB
/
NSString-data.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
// Copyright (c) 2010-2011, Rasmus Andersson. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
#import "NSString-data.h"
#import "common.h"
@implementation NSString (data)
- (NSMutableData*)mutableDataUsingEncoding:(NSStringEncoding)encoding {
return [self mutableDataUsingEncoding:encoding
range:NSMakeRange(0, self.length)];
}
- (NSMutableData*)mutableDataUsingEncoding:(NSStringEncoding)encoding
range:(NSRange)range {
NSUInteger estimatedSize = [self maximumLengthOfBytesUsingEncoding:encoding];
NSUInteger actualSize = 0;
char *bytes = (char*)CFAllocatorAllocate(NULL, estimatedSize, 0);
[self getBytes:bytes
maxLength:estimatedSize
usedLength:&actualSize
encoding:encoding
options:0
range:range
remainingRange:NULL];
NSMutableData *data = [NSMutableData dataWithBytesNoCopy:bytes
length:actualSize
freeWhenDone:YES];
return data;
}
@end