diff --git a/bindings/objc/mpack/mpack-objc.h b/bindings/objc/mpack/mpack-objc.h index 8fe5b49..60ce1bb 100644 --- a/bindings/objc/mpack/mpack-objc.h +++ b/bindings/objc/mpack/mpack-objc.h @@ -60,7 +60,7 @@ static NSString* const mpack_nserror_domain = @"com.ludocode.mpack"; * This does not accept any UTF-8 variant such as Modified UTF-8, CESU-8 or * WTF-8. Only pure UTF-8 is allowed. * - * If an error occurs, nil is returned. + * If an error occurs, an empty NSString is returned. * * @param reader The MPack reader * @param length The length of the string in bytes @@ -77,7 +77,7 @@ NSString* mpack_read_nsstring(mpack_reader_t* reader, size_t length); * which yielded one of these types, or by a call to an expect function * such as mpack_expect_str() or mpack_expect_bin(). * - * If an error occurs, nil is returned. + * If an error occurs, an empty NSData is returned. * * This can be called multiple times for a single str, bin or ext * to read the data in chunks. The total data read must add up @@ -96,10 +96,10 @@ NSData* mpack_read_nsdata(mpack_reader_t* reader, size_t count); * Reads a UTF-8 string with the given maximum byte size and returns * it as an NSString. * + * If an error occurs, an empty NSString is returned. + * * @throws mpack_error_too_big If the string is larger than the given maxsize. * @throws mpack_error_type If the value is not a string. - * - * @return An NSString, or nil if an error occurs. */ NSString* mpack_expect_nsstring(mpack_reader_t* reader, size_t maxsize); @@ -107,10 +107,10 @@ NSString* mpack_expect_nsstring(mpack_reader_t* reader, size_t maxsize); * Reads a binary blob with the given maximum byte size and returns it * as an NSData. * + * If an error occurs, an empty NSData is returned. + * * @throws mpack_error_too_big If the data is larger than the given maxsize. * @throws mpack_error_type If the value is not a binary blob. - * - * @return An NSData, or nil if an error occurs. */ NSData* mpack_expect_bin_nsdata(mpack_reader_t* reader, size_t maxsize); #endif @@ -122,10 +122,10 @@ NSData* mpack_expect_bin_nsdata(mpack_reader_t* reader, size_t maxsize); * Returns an NSString representing the contents of this string node with * the given maximum byte size, or nil if an error occurs. * + * If an error occurs, an empty NSString is returned. + * * @throws mpack_error_too_big If the string is larger than the given maxsize. * @throws mpack_error_type If the value is not a string. - * - * @return An NSString, or nil if an error occurs. */ NSString* mpack_node_nsstring(mpack_node_t node, size_t maxsize); @@ -133,11 +133,11 @@ NSString* mpack_node_nsstring(mpack_node_t node, size_t maxsize); * Returns an NSData representing the contents of this node with the * given maximum byte size, or nil if an error occurs. * + * If an error occurs, an empty NSData is returned. + * * @throws mpack_error_too_big If the data is larger than the given maxsize. * @throws mpack_error_type If the value is not a string, binary blob or * ext type. - * - * @return An NSData, or nil if an error occurs. */ NSData* mpack_node_bin_nsdata(mpack_node_t node, size_t maxsize); #endif diff --git a/bindings/objc/mpack/mpack-objc.m b/bindings/objc/mpack/mpack-objc.m index 36a6eb5..cc65a7d 100644 --- a/bindings/objc/mpack/mpack-objc.m +++ b/bindings/objc/mpack/mpack-objc.m @@ -76,13 +76,13 @@ - (NSString*)localizedDescription { NSString* mpack_read_nsstring(mpack_reader_t* reader, size_t length) { if (mpack_reader_error(reader) != mpack_ok) - return nil; + return @""; // small string if (mpack_should_read_bytes_inplace(reader, length)) { const char* bytes = mpack_read_utf8_inplace(reader, length); if (mpack_reader_error(reader) != mpack_ok) - return nil; + return @""; return MPACK_OBJC_AUTORELEASE([[NSString alloc] initWithBytes:(const void*)bytes length:length encoding:NSUTF8StringEncoding]); } @@ -90,12 +90,12 @@ - (NSString*)localizedDescription { char* bytes = (char*)mpack_stdlib_malloc(length); if (bytes == NULL) { mpack_reader_flag_error(reader, mpack_error_memory); - return nil; + return @""; } mpack_read_utf8(reader, bytes, length); if (mpack_reader_error(reader) != mpack_ok) - return nil; + return @""; return MPACK_OBJC_AUTORELEASE([[NSString alloc] initWithBytesNoCopy:(void*)bytes length:length encoding:NSUTF8StringEncoding freeWhenDone:YES]); @@ -107,7 +107,7 @@ - (NSString*)localizedDescription { if (mpack_should_read_bytes_inplace(reader, count)) { const char* bytes = mpack_read_bytes_inplace(reader, count); if (mpack_reader_error(reader) != mpack_ok) - return nil; + return [NSData data]; return [NSData dataWithBytes:(const void*)bytes length:count]; } @@ -115,7 +115,7 @@ - (NSString*)localizedDescription { NSMutableData* mdata = [NSMutableData dataWithLength:count]; mpack_read_bytes(reader, (char*)[mdata mutableBytes], count); if (mpack_reader_error(reader) != mpack_ok) - return nil; + return [NSData data]; return mdata; } @@ -148,12 +148,12 @@ - (NSString*)localizedDescription { NSString* mpack_node_nsstring(mpack_node_t node, size_t maxsize) { if (mpack_node_strlen(node) > maxsize) { mpack_node_flag_error(node, mpack_error_too_big); - return nil; + return @""; } mpack_node_check_utf8(node); if (mpack_node_error(node) != mpack_ok) - return nil; + return @""; size_t count = (size_t)node.data->len; const char* bytes = node.data->value.bytes; @@ -164,11 +164,11 @@ - (NSString*)localizedDescription { NSData* mpack_node_bin_nsdata(mpack_node_t node, size_t maxsize) { if (mpack_node_error(node) != mpack_ok) - return nil; + return [NSData data]; if (node.data->type != mpack_type_bin) { mpack_node_flag_error(node, mpack_error_type); - return nil; + return [NSData data]; } size_t count = (size_t)node.data->len; @@ -176,7 +176,7 @@ - (NSString*)localizedDescription { if (count > maxsize) { mpack_node_flag_error(node, mpack_error_too_big); - return nil; + return [NSData data]; } return [NSData dataWithBytes:(const void*)bytes length:count];