[Substrate-commits] SF.net SVN: substrate: [292] trunk
Brought to you by:
landonf
|
From: <la...@us...> - 2006-09-01 19:05:43
|
Revision: 292
http://svn.sourceforge.net/substrate/?rev=292&view=rev
Author: landonf
Date: 2006-09-01 12:05:27 -0700 (Fri, 01 Sep 2006)
Log Message:
-----------
Implement cString accessor
Modified Paths:
--------------
trunk/Foundation/NSString.m
trunk/Tests/NSString.m
Modified: trunk/Foundation/NSString.m
===================================================================
--- trunk/Foundation/NSString.m 2006-09-01 05:09:57 UTC (rev 291)
+++ trunk/Foundation/NSString.m 2006-09-01 19:05:27 UTC (rev 292)
@@ -34,6 +34,7 @@
#include <Foundation/NSString.h>
#include <Foundation/NSUnicodeString.h>
+#include <Foundation/NSData.h>
#include <Foundation/NSException.h>
#include <Foundation/NSObjCRuntime.h>
#include <Foundation/NSObjectPrivate.h>
@@ -94,6 +95,7 @@
return NSUTF8StringEncoding;
}
+
/*!
* Convert the receiving string to the specified encoding, returning
* the result as an instance of NSData.
@@ -277,19 +279,33 @@
/*!
* Returns representation of the receiver as a C string
- * in the default C encoding. This method has been deprecated in
- * favor of UTF8String.
+ * in the default C encoding.
+ * @deprecated This method has been deprecated in favor of
+ * UTF8String.
*
* If the string can not be converted with the default encoding,
* an NSCharacterConversionException will be raised.
*
- * The string returned by this method will be freed when the
- * receiver is deallocated.
- * @todo Unimplemented.
+ * The string returned by this method is added to the autorelease
+ * pool.
*/
- (const char *) cString {
- /* TODO Unimplemented */
- return NULL;
+ NSData *data;
+ NSMutableData *mutable;
+
+ /* A rather ineffecient implementation */
+ data = [self dataUsingEncoding: [NSString defaultCStringEncoding] allowLossyConversion: NO];
+ if (!data)
+ [NSException raise: NSCharacterConversionException format:
+ @"Unable to convert string to the default C string encoding."];
+
+ mutable = [data mutableCopy];
+ /* Append a NULL terminator */
+ [mutable appendBytes: "" length: sizeof("")];
+
+ /* Return a to-be-autoreleased C string*/
+ [mutable autorelease];
+ return [mutable bytes];
}
- (NSString *) description {
Modified: trunk/Tests/NSString.m
===================================================================
--- trunk/Tests/NSString.m 2006-09-01 05:09:57 UTC (rev 291)
+++ trunk/Tests/NSString.m 2006-09-01 19:05:27 UTC (rev 292)
@@ -51,6 +51,7 @@
@implementation NSDumbString
- (id) initWithBytes:(const void *)bytes length:(unsigned int) length encoding:(NSStringEncoding)encoding {
+ self = [super init];
return self;
}
@@ -177,7 +178,20 @@
}
END_TEST
+START_TEST (test_cString) {
+ NSString *string;
+ const char *cStr;
+ string = [[NSDumbString alloc] init];
+ fail_if(string == nil);
+
+ cStr = [string cString];
+ fail_if(cStr == NULL, "-[NSString cString] returned nil.");
+
+ fail_unless(strcmp(cStr, TEST_STRING) == 0, "-[NSString cString] returned unexpected string (Expected: %s, got: %s).", TEST_STRING, cStr);
+}
+END_TEST
+
Suite *NSString_suite(void) {
Suite *s = suite_create("NSString");
@@ -191,6 +205,7 @@
suite_add_tcase(s, tc_encoding);
tcase_add_test(tc_encoding, test_cls_defaultCStringEncoding);
tcase_add_test(tc_encoding, test_dataUsingEncodingAllowLossyConversion);
+ tcase_add_test(tc_encoding, test_cString);
TCase *tc_init = tcase_create("Initialization");
tcase_add_checked_fixture(tc_init, setUp, tearDown);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|