[Substrate-commits] SF.net SVN: substrate: [298] trunk
Brought to you by:
landonf
|
From: <la...@us...> - 2006-09-02 00:11:05
|
Revision: 298
http://svn.sourceforge.net/substrate/?rev=298&view=rev
Author: landonf
Date: 2006-09-01 17:11:00 -0700 (Fri, 01 Sep 2006)
Log Message:
-----------
r306@bluefish: landonf | 2006-09-01 16:09:39 -0700
Add initWithString, flesh out the initWithFormat implementation a tad
Modified Paths:
--------------
trunk/Foundation/NSString.h
trunk/Foundation/NSString.m
trunk/Tests/NSString.m
Property Changed:
----------------
trunk/
Property changes on: trunk
___________________________________________________________________
Name: svk:merge
- 11572a18-12fc-0310-9209-f8edcc8181a7:/local/substrate/trunk:5716
9c4e9a82-0035-45cc-8f88-5282b51d5481:/local/substrate/trunk:305
+ 11572a18-12fc-0310-9209-f8edcc8181a7:/local/substrate/trunk:5716
9c4e9a82-0035-45cc-8f88-5282b51d5481:/local/substrate/trunk:306
Modified: trunk/Foundation/NSString.h
===================================================================
--- trunk/Foundation/NSString.h 2006-09-02 00:10:54 UTC (rev 297)
+++ trunk/Foundation/NSString.h 2006-09-02 00:11:00 UTC (rev 298)
@@ -120,7 +120,11 @@
/* Initialization */
+ (id) string;
+ (id) stringWithCString:(const char *)cStr encoding:(NSStringEncoding)encoding;
++ (id) stringWithString:(NSString *)string;
+
+- (id) initWithString:(NSString *)string;
- (id) 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;
- (id) initWithFormat:(NSString *)format, ...;
Modified: trunk/Foundation/NSString.m
===================================================================
--- trunk/Foundation/NSString.m 2006-09-02 00:10:54 UTC (rev 297)
+++ trunk/Foundation/NSString.m 2006-09-02 00:11:00 UTC (rev 298)
@@ -156,12 +156,39 @@
}
/*!
+ * Allocate and initialize the receiver by copying the characters from the provided string.
+ *
+ * @param string Source string.
+ * @return Newly allocation and initialized NSString.
+ */
++ (id) stringWithString:(NSString *)string {
+ return [[[self alloc] initWithString: string] autorelease];
+}
+
+/*!
+ * Initialize the receiver by copying the characters from the provided string.
+ *
+ * @param string Source string.
+ * @return Initialized NSString.
+ */
+- (id) initWithString:(NSString *)string {
+ unsigned int length;
+ unichar *buffer;
+
+ length = [string length];
+ buffer = NSZoneMalloc(NULL, length * sizeof(unichar));
+ [string getCharacters: buffer];
+ /* Length required here is byte length, not character length. Ugh */
+ return [self initWithBytesNoCopy: buffer length: length * sizeof(unichar) encoding: NSUnicodeStringEncoding freeWhenDone: YES];
+}
+
+/*!
* 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 {
+- (id) initWithCString:(const char *)cStr encoding:(NSStringEncoding)encoding {
return [self initWithBytes: cStr length: strlen(cStr) encoding: encoding];
}
@@ -274,9 +301,14 @@
* @param locale Locale dictionary. If nil, [[NSUserDefaults standardUserDefaults] dictionaryRepresentation] will be used.
* @param args Format string arguments (va_list).
* @return Newly initialized string.
+ * @todo Avoid copying the newly created string when self is a concrete implementation subclass of NSString.
*/
- (id) initWithFormat:(NSString *)format locale:(NSDictionary *)locale arguments:(va_list)args {
- /* TODO Implement Me ... */
+ NSString *string;
+
+ /* All subclasses get the formatting implementation from NSUnicodeString. */
+ string = [[NSUnicodeString alloc] initWithFormat: format locale: locale arguments: args];
+ return [self initWithString: string];
}
/*!
Modified: trunk/Tests/NSString.m
===================================================================
--- trunk/Tests/NSString.m 2006-09-02 00:10:54 UTC (rev 297)
+++ trunk/Tests/NSString.m 2006-09-02 00:11:00 UTC (rev 298)
@@ -143,11 +143,23 @@
}
END_TEST
+START_TEST (test_cls_stringWithString) {
+ NSString *string = @"Hello, World!";
+ NSString *copy;
+
+ copy = [NSString stringWithString: @"Hello, World!"];
+ fail_if(copy == nil);
+
+ fail_unless(strcmp([copy UTF8String], [string UTF8String]) == 0, "+[NSString stringWithString] returned unexpected string (Expected: %s, got: %s).", [string UTF8String], [copy UTF8String]);
+}
+END_TEST
+
START_TEST (test_initWithBytes) {
NSString *string;
string = [[NSString alloc] initWithBytes: TEST_STRING length: sizeof(TEST_STRING) - 1 encoding: NSASCIIStringEncoding];
fail_if(string == nil, "-[NSString initWithBytes: length: encoding:] returned nil");
+ [string release];
}
END_TEST
@@ -156,6 +168,7 @@
string = [[NSString alloc] initWithCString: TEST_STRING encoding: NSUTF8StringEncoding];
fail_if(string == nil, "-[NSString initWithCString: encoding:] returned nil");
+ [string release];
}
END_TEST
@@ -178,12 +191,25 @@
}
END_TEST
+START_TEST (test_initWithString) {
+ NSString *string = @"Hello, World!";
+ NSString *copy;
+
+ copy = [[NSString alloc] initWithString: @"Hello, World!"];
+ fail_if(copy == nil);
+
+ fail_unless(strcmp([copy UTF8String], [string UTF8String]) == 0, "-[NSString initWithString] returned unexpected string (Expected: %s, got: %s).", [string UTF8String], [copy UTF8String]);
+ [copy release];
+}
+END_TEST
+
START_TEST (test_initWithFormatLocale) {
NSString *string;
string = [[NSString alloc] initWithFormat: @"%@ %s" locale: nil, @"Hello, ", "World!"];
fail_if(string == nil, "-[NSString initWithFormat: locale:] returned nil");
fail_unless(memcmp([string UTF8String], TEST_STRING, [string length]), "-[NSString initWithFormat: locale:] didn't handle our format correctly (Expected: %s, got: %s)", TEST_STRING, [string UTF8String]);
+ [string release];
}
END_TEST
@@ -250,9 +276,11 @@
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_cls_stringWithString);
tcase_add_test(tc_init, test_initWithCStringEncoding);
tcase_add_test(tc_init, test_initWithBytes);
tcase_add_test(tc_init, test_initWithBytesNoCopyLengthEncodingFreeWhenDone);
+ tcase_add_test(tc_init, test_initWithString);
tcase_add_test(tc_init, test_initWithFormatLocale);
TCase *tc_subclass = tcase_create("Subclassing");
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|