[Substrate-commits] SF.net SVN: substrate: [266] trunk
Brought to you by:
landonf
|
From: <la...@us...> - 2006-08-30 21:43:39
|
Revision: 266
http://svn.sourceforge.net/substrate/?rev=266&view=rev
Author: landonf
Date: 2006-08-30 14:43:26 -0700 (Wed, 30 Aug 2006)
Log Message:
-----------
r5679@zadder: landonf | 2006-08-30 14:37:37 -0700
Consolidate into two designated initializers, and implement a number of C string initialization methods.
Modified Paths:
--------------
trunk/Foundation/NSSimpleCString.m
trunk/Foundation/NSString.h
trunk/Foundation/NSString.m
trunk/Foundation/NSUnicodeString.m
trunk/Tests/Foundation.c
trunk/Tests/NSString.m
Property Changed:
----------------
trunk/
Property changes on: trunk
___________________________________________________________________
Name: svk:merge
- 11572a18-12fc-0310-9209-f8edcc8181a7:/local/substrate/trunk:5677
+ 11572a18-12fc-0310-9209-f8edcc8181a7:/local/substrate/trunk:5679
Modified: trunk/Foundation/NSSimpleCString.m
===================================================================
--- trunk/Foundation/NSSimpleCString.m 2006-08-30 18:51:24 UTC (rev 265)
+++ trunk/Foundation/NSSimpleCString.m 2006-08-30 21:43:26 UTC (rev 266)
@@ -54,10 +54,22 @@
*/
@implementation NSSimpleCString
-- initWithCString:(const char *) byteString {
- _numBytes = strlen(byteString);
- _bytes = (char *)malloc(_numBytes+1);
- bcopy(byteString, _bytes, _numBytes+1);
+- initWithBytes:(const void *)bytes length:(unsigned int)length encoding:(NSStringEncoding)encoding {
+ self = [super init];
+ if (!self)
+ return nil;
+
+ /* We only speak one encoding. */
+ if (encoding != NSASCIIStringEncoding) {
+ [self release];
+ [NSException raise: NSInvalidArgumentException format: @"Unknown NSStringEncoding"];
+ return nil;
+ }
+
+ _bytes = NSZoneMalloc(NULL, length);
+ _numBytes = length;
+ memcpy(_bytes, bytes, _numBytes);
+
return self;
}
@@ -71,6 +83,7 @@
}
- (unichar) characterAtIndex:(unsigned int) index {
+ /* ASCII can be directly mapped to UTF-16 */
return (unichar) _bytes[index];
}
@@ -79,6 +92,7 @@
}
- (unsigned int) cStringLength {
+ /* 1 byte per character */
return _numBytes;
}
Modified: trunk/Foundation/NSString.h
===================================================================
--- trunk/Foundation/NSString.h 2006-08-30 18:51:24 UTC (rev 265)
+++ trunk/Foundation/NSString.h 2006-08-30 21:43:26 UTC (rev 266)
@@ -117,14 +117,24 @@
- (const char *) cString;
/* Initialization */
++ string;
+- initWithCString:(const char *)cStr encoding:(NSStringEncoding)encoding;
- (id) initWithBytes:(const void *)bytes length:(unsigned int)len encoding:(NSStringEncoding)encoding;
- (id) initWithBytesNoCopy:(void *)bytes length:(unsigned int)len encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL) shouldFree;
+/* Encoding */
++ (NSStringEncoding) defaultCStringEncoding;
+
+/* Deprecated */
++ stringWithCString:(const char *)cStr;
+- initWithCString:(const char *)cStr;
+- initWithCString:(const char *)cStr length:(unsigned int)length;
+
@end
/*
* The following must be here to support compiler instantiation
- * of constant strings. Do not ever use the directly.
+ * of constant strings. Do not ever use them directly.
*/
extern struct objc_class _NSConstantStringClassReference;
Modified: trunk/Foundation/NSString.m
===================================================================
--- trunk/Foundation/NSString.m 2006-08-30 18:51:24 UTC (rev 265)
+++ trunk/Foundation/NSString.m 2006-08-30 21:43:26 UTC (rev 266)
@@ -80,6 +80,57 @@
@implementation NSString
/*!
+ * @name Encodings
+ * @{
+ */
+
+/*!
+ * Returns the default encoding used by C string handling methods.
+ * This value can not be changed programatically, and callers
+ * should avoid using implicit encoding.
+ */
++ (NSStringEncoding) defaultCStringEncoding {
+ /* Everything is a UTF-8 string. I swear. */
+ return NSUTF8StringEncoding;
+}
+
+/*! @} */
+
+
+/*!
+ * @name Allocation and Initialization
+ * @{
+ */
+
+/*!
+ * Allocate and initialize an NSString instance with the supplied C string.
+ * @param cStr NULL-terminted C string.
+ * @return Allocated and initialized NSString instance.
+ */
++ stringWithCString:(const char *)cStr {
+ return [[[self alloc] initWithCString:cStr] autorelease];
+}
+
+/*!
+ * Allocate and initialize an empty NSString instance.
+ * @return New empty NSString instance.
+ */
++ string {
+ [self release];
+ return [[[NSUnicodeString alloc] init] autorelease];
+}
+
+/*!
+ * Initialize the receiver with the supplied C string, encoded according to encoding.
+ * @param cStr NULL-terminated C string.
+ * @param encoding Character encoding for the C string.
+ * @return Newly initialized NSString instance.
+ */
+- initWithCString:(const char *)cStr encoding:(NSStringEncoding)encoding {
+ return [self initWithBytes: cStr length: strlen(cStr) encoding: encoding];
+}
+
+/*!
* Initializes the receiver with bytes, of length, of the specified encoding.
* @param bytes String data, not null-terminated.
* @param length Length of data.
@@ -87,7 +138,7 @@
* @return Newly initialized string.
*/
- (id) initWithBytes:(const void *)bytes length:(unsigned int)length encoding:(NSStringEncoding)encoding {
- self = [self init];
+ self = [super init];
if (!self)
return nil;
@@ -100,7 +151,10 @@
/* Otherwise, instantiate a concrete string */
[self release];
- self = [[NSUnicodeString alloc] initWithBytes: bytes length: length encoding: encoding];
+ if (encoding == NSASCIIStringEncoding)
+ self = [[NSSimpleCString alloc] initWithBytes: bytes length: length encoding: encoding];
+ else
+ self = [[NSUnicodeString alloc] initWithBytes: bytes length: length encoding: encoding];
return self;
}
@@ -119,7 +173,7 @@
* @return Newly initialized string.
*/
- (id) initWithBytesNoCopy:(void *)bytes length:(unsigned int)length encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)freeWhenDone {
- self = [self init];
+ self = [super init];
if (!self)
return nil;
@@ -136,11 +190,17 @@
return self;
}
+
/*!
+ * @}
+ * Allocation and Initialization.
+ */
+
+/*!
* Returns length.
*/
- (unsigned int) length {
- [NSException raise: NSGenericException format: @"-[NSString length] is abstract"];
+ [self _subclassResponsibility: _cmd];
/* Unreachable */
return 0;
}
@@ -151,7 +211,7 @@
* past the end of the receiver.
*/
- (unichar) characterAtIndex:(unsigned int) index {
- [NSException raise: NSGenericException format: @"-[NSString characterAtIndex:] is abstract"];
+ [self _subclassResponsibility: _cmd];
/* Unreachable */
return 0;
}
@@ -164,22 +224,6 @@
}
/*!
- * Returns representation of the receiver as a C string
- * in the default C encoding. 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.
- */
-- (char *) cString {
- /* TODO Unimplemented */
- return nil;
-}
-
-/*!
* Fills buffer with the characters contained within aRange
* of the string. An NSRangeException is raised if aRange
* is not fully contained within the receiver.
@@ -198,23 +242,55 @@
}
}
+/*!
+ * Returns representation of the receiver as a C string
+ * in the default C encoding. 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.
+ */
+- (char *) cString {
+ /* TODO Unimplemented */
+ return nil;
+}
+
- (NSString *) description {
return self;
}
-@end
+/*!
+ * @name Deprecated
+ * @{
+ */
-@implementation NSString(NSStringCreation)
-
-+ stringWithCString:(const char *)cStr {
- return [[[self alloc] initWithCString:cStr] autorelease];
+/*!
+ * Initialize the receiver with the supplied C string, using the default string encoding.
+ * @deprecated Caller's should make use of initWithCString:encoding: instead.
+ * @param cStr NULL-terminted C string.
+ * @return Allocated and initialized NSString instance.
+ */
+- initWithCString:(const char *)cStr {
+ return [self initWithCString: cStr encoding: [NSString defaultCStringEncoding]];
}
-- initWithCString:(const char *)cStr {
- [self release];
- return [[NSSimpleCString alloc] initWithCString:cStr];
+/*!
+ * Initialize the receiver with the supplied C string, of length, using the default string encoding.
+ * @deprecated Caller's should make use of initWithCString:encoding: or initWithBytes:length:encoding: instead.
+ * @param cStr C string, using the default system encoding.
+ * @param length Number of characters in string.
+ * @return Newly initialized NSString instance.
+ */
+- initWithCString:(const char *)cStr length:(unsigned int)length {
+ return [self initWithBytes: cStr length: length encoding: [NSString defaultCStringEncoding]];
}
+/*! @} */
+
@end
/*! @} */
Modified: trunk/Foundation/NSUnicodeString.m
===================================================================
--- trunk/Foundation/NSUnicodeString.m 2006-08-30 18:51:24 UTC (rev 265)
+++ trunk/Foundation/NSUnicodeString.m 2006-08-30 21:43:26 UTC (rev 266)
@@ -84,6 +84,12 @@
[super dealloc];
}
+/*!
+ * @name Initialization
+ * @internal
+ * @{
+ */
+
/*! Initialize the string.
* @todo Need better heuristics for determining destination buffer length, and
* for deciding when we should call realloc.
@@ -329,6 +335,20 @@
return self;
}
+/*! @} */
+
+/*!
+ * @name Accessing Data
+ * @internal
+ * @{
+ */
+
+- (unsigned int) length {
+ return _length;
+}
+
+/*! @} */
+
@end
/*! @} */
Modified: trunk/Tests/Foundation.c
===================================================================
--- trunk/Tests/Foundation.c 2006-08-30 18:51:24 UTC (rev 265)
+++ trunk/Tests/Foundation.c 2006-08-30 21:43:26 UTC (rev 266)
@@ -43,6 +43,8 @@
/* Standard Foundation Suites */
Suite *s = NSZone_suite();
SRunner *sr = srunner_create(s);
+ srunner_add_suite(sr, NSString_suite());
+ srunner_add_suite(sr, NSUnicodeString_suite());
srunner_add_suite(sr, NSAutoreleasePool_suite());
srunner_add_suite(sr, NSData_suite());
srunner_add_suite(sr, NSMutableData_suite());
@@ -54,8 +56,6 @@
srunner_add_suite(sr, NSRange_suite());
srunner_add_suite(sr, NSGeometry_suite());
srunner_add_suite(sr, NSByteOrder_suite());
- srunner_add_suite(sr, NSString_suite());
- srunner_add_suite(sr, NSUnicodeString_suite());
if (argc == 2)
srunner_set_xml(sr, argv[1]);
Modified: trunk/Tests/NSString.m
===================================================================
--- trunk/Tests/NSString.m 2006-08-30 18:51:24 UTC (rev 265)
+++ trunk/Tests/NSString.m 2006-08-30 21:43:26 UTC (rev 266)
@@ -90,6 +90,32 @@
}
END_TEST
+/* Encodings */
+
+START_TEST (test_cls_defaultCStringEncoding) {
+ /* We can't test for much here. Let's just call the method */
+ fail_unless([NSString defaultCStringEncoding]);
+}
+END_TEST
+
+
+/* Initialization */
+
+START_TEST (test_cls_string) {
+ NSString *empty;
+
+ empty = [NSString string];
+ fail_unless([empty length] == 0, "+[NSString string] returned a string with a non-zero length");
+}
+END_TEST
+
+START_TEST (test_cls_stringWithCString) {
+ NSString *string;
+ string = [NSString stringWithCString: TEST_STRING];
+ fail_unless([string length] == sizeof(TEST_STRING) - 1, "+[NSString stringWithCString] returned a string with incorrect length (Expected %d, got %d)", sizeof(TEST_STRING) - 1, [string length]);
+}
+END_TEST
+
START_TEST (test_initWithBytes) {
NSString *string;
@@ -98,6 +124,14 @@
}
END_TEST
+START_TEST (test_initWithCStringEncoding) {
+ NSString *string;
+
+ string = [[NSString alloc] initWithCString: TEST_STRING encoding: NSUTF8StringEncoding];
+ fail_if(string == nil, "-[NSString initWithCString: encoding:] returned nil");
+}
+END_TEST
+
START_TEST (test_initWithBytesNoCopyLengthEncodingFreeWhenDone) {
NSString *string;
char testData[] = "Hello, Universe!";
@@ -131,9 +165,17 @@
suite_add_tcase(s, tc_subclass);
tcase_add_test(tc_subclass, test_subclass_initWithBytes);
+ TCase *tc_encoding = tcase_create("Encodings");
+ tcase_add_checked_fixture(tc_encoding, setUp, tearDown);
+ suite_add_tcase(s, tc_encoding);
+ tcase_add_test(tc_subclass, test_cls_defaultCStringEncoding);
+
TCase *tc_init = tcase_create("Initialization");
tcase_add_checked_fixture(tc_init, setUp, tearDown);
suite_add_tcase(s, tc_init);
+ tcase_add_test(tc_init, test_cls_string);
+ tcase_add_test(tc_init, test_cls_stringWithCString);
+ tcase_add_test(tc_init, test_initWithCStringEncoding);
tcase_add_test(tc_init, test_initWithBytes);
tcase_add_test(tc_init, test_initWithBytesNoCopyLengthEncodingFreeWhenDone);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|