瀏覽代碼

Added +[dataFromCString:] and +[dataFromCString:]; much cleanup

master
achmizs 4 年之前
父節點
當前提交
eada9d3cd1
共有 3 個檔案被更改,包括 48 行新增32 行删除
  1. 11
    1
      NSData+SA_NSDataExtensions.h
  2. 36
    30
      NSData+SA_NSDataExtensions.m
  3. 1
    1
      README.md

+ 11
- 1
NSData+SA_NSDataExtensions.h 查看文件

@@ -90,6 +90,16 @@
/** Returns an NSData object containing a blank C string (i.e. a byte sequence
of length 1, containing the null character '\0').
*/
+(NSData *)dataWithBlankCString;
+(NSData *) dataWithBlankCString;

/** Returns an NSData object containing bytes copied from the given C string
(sans the null terminator).
*/
+(NSData *) dataFromCString:(const char *)cString;

/** Returns an NSData object containing the bytes of the given C string
(sans the null terminator).
*/
+(NSData *) dataWithCString:(char *)cString;

@end

+ 36
- 30
NSData+SA_NSDataExtensions.m 查看文件

@@ -25,59 +25,65 @@

@implementation NSData (SA_NSDataExtensions)

-(BOOL)isNullTerminated
{
-(BOOL) isNullTerminated {
if (self.length == 0)
return NO;

return (((char*) self.bytes)[self.length - 1] == '\0');
}

-(const char *)SA_terminatedCString
{
-(const char *) SA_terminatedCString {
return self.SA_dataWithTerminatedCString.bytes;
}

-(NSData *)SA_dataWithTerminatedCString
{
if(self.length == 0)
{
return [NSData dataWithBytes:"\0" length:1];
}
else if(self.isNullTerminated)
{
-(NSData *) SA_dataWithTerminatedCString {
if (self.length == 0) {
return [NSData dataWithBytes:"\0"
length:1];
} else if (self.isNullTerminated) {
return self;
}
else
{
} else {
char* terminated_string_buffer = malloc(self.length + 1);
[self getBytes:terminated_string_buffer length:self.length];
[self getBytes:terminated_string_buffer
length:self.length];
terminated_string_buffer[self.length] = '\0';
return [NSData dataWithBytesNoCopy:terminated_string_buffer length:(self.length + 1) freeWhenDone:YES];
return [NSData dataWithBytesNoCopy:terminated_string_buffer
length:(self.length + 1)
freeWhenDone:YES];
}
}

-(const char *)SA_unterminatedByteString
{
-(const char *) SA_unterminatedByteString {
return self.SA_dataWithUnterminatedByteString.bytes;
}

-(NSData *)SA_dataWithUnterminatedByteString
{
if(self.length == 0 || self.isNullTerminated == NO)
{
-(NSData *) SA_dataWithUnterminatedByteString {
if (self.length == 0 || self.isNullTerminated == NO) {
return self;
}
else
{
} else {
char* unterminated_string_buffer = malloc(self.length - 1);
[self getBytes:unterminated_string_buffer length:self.length - 1];
return [NSData dataWithBytesNoCopy:unterminated_string_buffer length:(self.length - 1) freeWhenDone:YES];
return [NSData dataWithBytesNoCopy:unterminated_string_buffer
length:(self.length - 1)
freeWhenDone:YES];
}
}

+(NSData *)dataWithBlankCString
{
return [NSData dataWithBytes:"\0" length:1];
+(NSData *) dataWithBlankCString {
return [NSData dataWithBytes:"\0"
length:1];
}

+(NSData *) dataFromCString:(const char *)cString {
return [NSData dataWithBytes:cString
length:strlen(cString)];
}

+(NSData *) dataWithCString:(char *)cString {
return [NSData dataWithBytesNoCopy:cString
length:strlen(cString)];
}

@end

+ 1
- 1
README.md 查看文件

@@ -2,6 +2,6 @@ NSData+SA_NSDataExtensions

Adds utility functions to NSData, that help deal with null termination of C strings.

This category on NSData adds properties that allow you to get the null-terminated or non-null-terminated versions of byte arrays stored as NSData objects, and to easily check whether an NSData's byte array is, or is not, null-terminated (that is, whether its last byte is a null).
This category on NSData adds properties that allow you to get the null-terminated or non-null-terminated versions of byte arrays stored as NSData objects, and to easily check whether an NSDatas byte array is, or is not, null-terminated (that is, whether its last byte is a null).

Copyright (c) 2015 Said Achmiz.

Loading…
取消
儲存