[Substrate-commits] SF.net SVN: substrate: [294] trunk
Brought to you by:
landonf
|
From: <la...@us...> - 2006-09-02 00:10:39
|
Revision: 294
http://svn.sourceforge.net/substrate/?rev=294&view=rev
Author: landonf
Date: 2006-09-01 17:10:31 -0700 (Fri, 01 Sep 2006)
Log Message:
-----------
r302@bluefish: landonf | 2006-09-01 14:51:02 -0700
Implement cStringUsingEncoding:(NSStringEncoding)encoding, and use it for cString and UTF8String
Modified Paths:
--------------
trunk/Foundation/NSSimpleCString.m
trunk/Foundation/NSString.h
trunk/Foundation/NSString.m
trunk/Tests/Foundation.c
trunk/Tests/Foundation.h
trunk/Tests/Makefile.in
trunk/Tests/NSException.m
trunk/Tests/NSString.m
trunk/Tests/NSZone.m
Added Paths:
-----------
trunk/Tests/NSSimpleString.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:301
+ 11572a18-12fc-0310-9209-f8edcc8181a7:/local/substrate/trunk:5716
9c4e9a82-0035-45cc-8f88-5282b51d5481:/local/substrate/trunk:302
Modified: trunk/Foundation/NSSimpleCString.m
===================================================================
--- trunk/Foundation/NSSimpleCString.m 2006-09-02 00:10:20 UTC (rev 293)
+++ trunk/Foundation/NSSimpleCString.m 2006-09-02 00:10:31 UTC (rev 294)
@@ -31,6 +31,7 @@
#include <Foundation/NSException.h>
#include <Foundation/NSString.h>
+#include <Foundation/NSData.h>
#include <string.h>
/*!
@@ -87,8 +88,18 @@
return (unichar) _bytes[index];
}
-- (const char *) cString {
- return _bytes;
+- (const char *) cStringUsingEncoding:(NSStringEncoding)encoding {
+ /* We can handle UTF-8 and ASCII with no conversion overhead. */
+ if (encoding == NSUTF8StringEncoding || encoding == NSASCIIStringEncoding) {
+ /* Get an autoreleased byte array */
+ NSMutableData *data = [NSMutableData dataWithBytes: _bytes length: _numBytes];
+
+ /* Append a NULL terminator */
+ [data appendBytes: "" length: sizeof("")];
+ return [data bytes];
+ } else {
+ return [super cStringUsingEncoding:encoding];
+ }
}
- (unsigned int) cStringLength {
Modified: trunk/Foundation/NSString.h
===================================================================
--- trunk/Foundation/NSString.h 2006-09-02 00:10:20 UTC (rev 293)
+++ trunk/Foundation/NSString.h 2006-09-02 00:10:31 UTC (rev 294)
@@ -116,7 +116,6 @@
- (void) getCharacters:(unichar *)buffer;
- (void) getCharacters:(unichar *)buffer range:(NSRange)aRange;
-- (const char *) cString;
/* Initialization */
+ (id) string;
@@ -128,11 +127,14 @@
/* Encoding */
+ (NSStringEncoding) defaultCStringEncoding;
- (NSData *) dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy;
+- (const char *) cStringUsingEncoding:(NSStringEncoding)encoding;
+- (const char *) UTF8String;
/* Deprecated */
+ stringWithCString:(const char *)cStr;
- initWithCString:(const char *)cStr;
- initWithCString:(const char *)cStr length:(unsigned int)length;
+- (const char *) cString;
@end
Modified: trunk/Foundation/NSString.m
===================================================================
--- trunk/Foundation/NSString.m 2006-09-02 00:10:20 UTC (rev 293)
+++ trunk/Foundation/NSString.m 2006-09-02 00:10:31 UTC (rev 294)
@@ -278,23 +278,22 @@
}
/*!
- * Returns representation of the receiver as a C string
- * in the default C encoding.
- * @deprecated This method has been deprecated in favor of
- * UTF8String.
+ * Returns representation of the receiver as a NULL-terminated C string,
+ * using the supplied encoding.
*
* If the string can not be converted with the default encoding,
* an NSCharacterConversionException will be raised.
*
- * The string returned by this method is added to the autorelease
- * pool.
+ * The string returned by this method is owned by the receiver and
+ * will be autoreleased -- you must copy the result if you wish to
+ * retain it ouside of the current autorelease context.
*/
-- (const char *) cString {
+- (const char *) cStringUsingEncoding:(NSStringEncoding)encoding {
NSData *data;
NSMutableData *mutable;
/* A rather ineffecient implementation */
- data = [self dataUsingEncoding: [NSString defaultCStringEncoding] allowLossyConversion: NO];
+ data = [self dataUsingEncoding: encoding allowLossyConversion: NO];
if (!data)
[NSException raise: NSCharacterConversionException format:
@"Unable to convert string to the default C string encoding."];
@@ -303,11 +302,40 @@
/* Append a NULL terminator */
[mutable appendBytes: "" length: sizeof("")];
- /* Return a to-be-autoreleased C string*/
+ /* Return a to-be-autoreleased C string */
[mutable autorelease];
return [mutable bytes];
}
+/*!
+ * Returns representation of the receiver as a UTF-8 encoding C string.
+ *
+ * The string returned by this method is owned by the receiver and
+ * will be autoreleased -- you must copy the result if you wish to
+ * retain it ouside of the current autorelease context.
+ */
+- (const char *) UTF8String {
+ return [self cStringUsingEncoding: NSUTF8StringEncoding];
+}
+
+
+/*!
+ * Returns representation of the receiver as a C string
+ * 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 is owned by the receiver and
+ * will be autoreleased -- you must copy the result if you wish to
+ * retain it ouside of the current autorelease context.
+ */
+- (const char *) cString {
+ return [self cStringUsingEncoding: [NSString defaultCStringEncoding]];
+}
+
- (NSString *) description {
return self;
}
Modified: trunk/Tests/Foundation.c
===================================================================
--- trunk/Tests/Foundation.c 2006-09-02 00:10:20 UTC (rev 293)
+++ trunk/Tests/Foundation.c 2006-09-02 00:10:31 UTC (rev 294)
@@ -44,6 +44,7 @@
Suite *s = NSZone_suite();
SRunner *sr = srunner_create(s);
srunner_add_suite(sr, NSString_suite());
+ srunner_add_suite(sr, NSSimpleString_suite());
srunner_add_suite(sr, NSUnicodeString_suite());
srunner_add_suite(sr, NSAutoreleasePool_suite());
srunner_add_suite(sr, NSData_suite());
Modified: trunk/Tests/Foundation.h
===================================================================
--- trunk/Tests/Foundation.h 2006-09-02 00:10:20 UTC (rev 293)
+++ trunk/Tests/Foundation.h 2006-09-02 00:10:31 UTC (rev 294)
@@ -33,6 +33,7 @@
Suite *NSObject_suite(void);
Suite *NSProcessInfo_suite(void);
Suite *NSString_suite(void);
+Suite *NSSimpleString_suite(void);
Suite *NSUnicodeString_suite(void);
Suite *NSRange_suite(void);
Suite *NSGeometry_suite(void);
Modified: trunk/Tests/Makefile.in
===================================================================
--- trunk/Tests/Makefile.in 2006-09-02 00:10:20 UTC (rev 293)
+++ trunk/Tests/Makefile.in 2006-09-02 00:10:31 UTC (rev 294)
@@ -14,8 +14,9 @@
FOUNDATION_OBJS= Foundation.o NSAutoreleasePool.o NSException.o NSZone.o \
NSObjectAllocation.o NSObject.o NSProcessInfo.o \
- NSString.o NSUnicodeString.o NSRange.o NSGeometry.o \
- NSData.o NSMutableData.o NSConcreteData.o NSByteOrder.o
+ NSString.o NSSimpleString.o NSUnicodeString.o NSRange.o \
+ NSGeometry.o NSData.o NSMutableData.o NSConcreteData.o \
+ NSByteOrder.o
SPINLOCK_OBJS= s_lock.o
all: Foundation Spinlocks
Modified: trunk/Tests/NSException.m
===================================================================
--- trunk/Tests/NSException.m 2006-09-02 00:10:20 UTC (rev 293)
+++ trunk/Tests/NSException.m 2006-09-02 00:10:31 UTC (rev 294)
@@ -33,6 +33,17 @@
/* For strcmp */
#include <string.h>
+static NSAutoreleasePool *releasePool;
+
+static void setUp (void) {
+ releasePool = [[NSAutoreleasePool alloc] init];
+}
+
+static void tearDown (void) {
+ [releasePool release];
+}
+
+
START_TEST (test_initWithName) {
NSException *e;
#ifdef TODO_NSDictionary
@@ -45,19 +56,14 @@
e = [[NSException alloc] initWithName: @"Test" reason: @"Failure" userInfo: userInfo];
fail_if(e == nil, "-[[NSException alloc] initWithName: reason: userInfo:] returned nil");
-#ifndef TODO_NSString
name = [e name];
- fail_unless(strcmp([name cString], "Test") == 0, "-[NSException name] returned invalid name. (Expected %s, got %s)", "Test", [name cString]);
-#else
-# error Implement once NSString is functional
-#endif
+ fail_unless(strcmp([name UTF8String], "Test") == 0, "-[NSException name] returned invalid name. (Expected %s, got %s)", "Test", [name UTF8String]);
[e release];
}
END_TEST
START_TEST (test_exceptionWithName) {
- NSAutoreleasePool *pool;
NSException *e;
#ifdef TODO_NSDictionary
NSDictionary *userInfo;
@@ -65,22 +71,14 @@
NSDictionary *userInfo = NULL;
#endif
- pool = [[NSAutoreleasePool alloc] init];
-
e = [NSException exceptionWithName: @"Test" reason: @"Failure" userInfo: userInfo];
fail_if(e == nil, "+[NSException exceptionWithName: reason: userInfo:] returned nil");
-
- [pool release];
}
END_TEST
START_TEST (test_raiseformat) {
- NSAutoreleasePool *pool;
BOOL wasRaised = NO;
- pool = [[NSAutoreleasePool alloc] init];
-
-
NS_DURING
[NSException raise: NSGenericException format: @"Fred %@", NSOldStyleException];
NS_HANDLER
@@ -88,8 +86,6 @@
NS_ENDHANDLER
fail_unless(wasRaised, "+[NSException raise: format:] did not throw an exception.");
-
- [pool release];
}
END_TEST
@@ -113,23 +109,14 @@
}
START_TEST (test_raiseformatarguments) {
- NSAutoreleasePool *pool;
-
- pool = [[NSAutoreleasePool alloc] init];
-
fail_unless(call_raiseFormatArguments(@"Fred %@", @"Jones"), "+[NSException raise: format: arguments:] did not throw an exception.");
-
- [pool release];
}
END_TEST
START_TEST (test_raise) {
- NSAutoreleasePool *pool;
NSException *e;
BOOL wasRaised = NO;
- pool = [[NSAutoreleasePool alloc] init];
-
e = [NSException exceptionWithName: @"Test" reason: @"Failure" userInfo: nil];
fail_if(e == nil, "+[NSException exceptionWithName: reason: userInfo:] returned nil");
@@ -141,13 +128,10 @@
NS_ENDHANDLER
fail_unless(wasRaised, "-[NSException raise] did not throw an exception.");
-
- [pool release];
}
END_TEST
START_TEST (test_copy) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSException *e = [NSException exceptionWithName: @"Test" reason: @"Failure" userInfo: nil];
NSException *copy = [e copy];
Class class = LFGetClass("NSException");
@@ -156,7 +140,6 @@
fail_unless([copy class] == class, "-[NSException copy] did not return an object of class NSException.");
[copy release];
- [pool release];
}
END_TEST
@@ -170,7 +153,6 @@
START_TEST (test_NSSetUncaughtExceptionHandler) {
NSUncaughtExceptionHandler *handler;
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
/* Set the handler */
NSSetUncaughtExceptionHandler(ueh_handler);
@@ -181,30 +163,19 @@
/* Now let's trigger its use */
[NSException raise: @"TestException" format: @"TestException"];
-
- /* This is never reached if the above was successful */
- [pool release];
}
END_TEST
START_TEST (test_default_NSUncaughtExceptionHandler) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
/* Trigger the default handler */
[NSException raise: @"TestException" format: @"TestException"];
-
- /* This is never reached if the above was successful */
- [pool release];
}
END_TEST
START_TEST (test_LFObjCExceptionHandling) {
- NSAutoreleasePool *pool;
NSException *e;
- pool = [[NSAutoreleasePool alloc] init];
-
e = [NSException exceptionWithName: @"Test" reason: @"Failure" userInfo: nil];
fail_if(e == nil, "+[NSException exceptionWithName: reason: userInfo:] returned nil");
@@ -214,19 +185,13 @@
NS_HANDLER
fail_unless(localException == e, "Exception support code did not throw correct exception. (Expected %p, got %p)", e, localException);
NS_ENDHANDLER
-
- [pool release];
-
}
END_TEST
START_TEST (test_NSExceptionLegacyExceptions) {
- NSAutoreleasePool *pool;
NSException *e;
BOOL wasRaised = NO;
- pool = [[NSAutoreleasePool alloc] init];
-
e = [NSException exceptionWithName: @"Test" reason: @"Failure" userInfo: nil];
fail_if(e == nil, "+[NSException exceptionWithName: reason: userInfo:] returned nil");
@@ -239,9 +204,6 @@
NS_ENDHANDLER
fail_unless(wasRaised, "-[NSException raise] did not throw an exception.");
-
- [pool release];
-
}
END_TEST
@@ -277,6 +239,7 @@
Suite *s = suite_create("NSException");
TCase *tc = tcase_create("Default");
+ tcase_add_checked_fixture(tc, setUp, tearDown);
suite_add_tcase(s, tc);
tcase_add_test(tc, test_exceptionWithName);
tcase_add_test(tc, test_initWithName);
@@ -286,6 +249,7 @@
tcase_add_test(tc, test_copy);
TCase *tc_exceptions = tcase_create("Standard Runtime Exception Handling");
+ tcase_add_checked_fixture(tc_exceptions, setUp, tearDown);
suite_add_tcase(s, tc_exceptions);
tcase_add_test(tc_exceptions, test_LFObjCExceptionHandling);
tcase_add_test(tc_exceptions, test_NSExceptionLegacyExceptions);
@@ -293,6 +257,7 @@
tcase_add_test(tc_exceptions, test_NS_VALUERETURN);
TCase *tc_uncaught = tcase_create("Uncaught Exception Handling");
+ tcase_add_checked_fixture(tc_uncaught, setUp, tearDown);
suite_add_tcase(s, tc_uncaught);
tcase_add_test_raise_signal(tc_uncaught, test_NSSetUncaughtExceptionHandler, SIGILL);
tcase_add_test_raise_signal(tc_uncaught, test_default_NSUncaughtExceptionHandler, SIGTRAP);
Added: trunk/Tests/NSSimpleString.m
===================================================================
--- trunk/Tests/NSSimpleString.m (rev 0)
+++ trunk/Tests/NSSimpleString.m 2006-09-02 00:10:31 UTC (rev 294)
@@ -0,0 +1,115 @@
+/*
+ * NSSimpleString.m
+ * Foundation Unit Tests
+ *
+ * Copyright (C) 2005 - 2006 Landon Fuller <la...@op...>
+ * All rights reserved.
+ *
+ * This file is part of libFoundation.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose and without fee is hereby granted, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation.
+ *
+ * We disclaim all warranties with regard to this software, including all
+ * implied warranties of merchantability and fitness, in no event shall
+ * we be liable for any special, indirect or consequential damages or any
+ * damages whatsoever resulting from loss of use, data or profits, whether in
+ * an action of contract, negligence or other tortious action, arising out of
+ * or in connection with the use or performance of this software.
+ */
+
+#include <check.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
+
+#include <Foundation/NSString.h>
+#include <Foundation/NSData.h>
+#include <Foundation/NSException.h>
+#include <Foundation/NSAutoreleasePool.h>
+#include "LFObjCRuntime.h"
+
+/*
+ * Tests that are specific to the NSSimpleString concrete implementation.
+ *
+ * These tests make some assumptions about NSString instantiation, and thus may
+ * not directly apply to other Foundation implementations -- but they certainly
+ * shouldn't fail.
+ */
+
+const char TEST_STRING[] = "Hello, World.";
+
+static NSAutoreleasePool *releasePool;
+
+static void setUp(void) {
+ releasePool = [[NSAutoreleasePool alloc] init];
+}
+
+static void tearDown(void) {
+ [releasePool release];
+}
+
+/* Encoding */
+START_TEST (test_cStringUsingEncoding) {
+ NSString *string;
+ const char *cStr;
+
+ string = [[NSString alloc] initWithCString: TEST_STRING];
+ fail_if(string == nil);
+
+ cStr = [string cStringUsingEncoding: NSASCIIStringEncoding];
+ fail_if(cStr == NULL, "-[NSString cStringUsingEncoding:] returned nil.");
+ fail_unless(strcmp(cStr, TEST_STRING) == 0, "-[NSString cString] returned unexpected string (Expected: %s, got: %s).", TEST_STRING, cStr);
+}
+END_TEST
+
+START_TEST (test_UTF8String) {
+ NSString *string;
+ const char *cStr;
+
+ string = [[NSString alloc] initWithCString: TEST_STRING];
+ fail_if(string == nil);
+
+ cStr = [string UTF8String];
+ fail_if(cStr == NULL, "-[NSString UTF8String] returned nil.");
+
+ fail_unless(strcmp(cStr, TEST_STRING) == 0, "-[NSString UTF8String] returned unexpected string (Expected: %s, got: %s).", TEST_STRING, cStr);
+}
+END_TEST
+
+START_TEST (test_cString) {
+ NSString *string;
+ const char *cStr;
+
+ string = [[NSString alloc] initWithCString: TEST_STRING];
+ 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 *NSSimpleString_suite(void) {
+ Suite *s = suite_create("NSSimpleString");
+
+ TCase *tc_encoding = tcase_create("Encodings");
+ tcase_add_checked_fixture(tc_encoding, setUp, tearDown);
+ suite_add_tcase(s, tc_encoding);
+ tcase_add_test(tc_encoding, test_cStringUsingEncoding);
+ tcase_add_test(tc_encoding, test_UTF8String);
+ tcase_add_test(tc_encoding, test_cString);
+
+ return (s);
+}
Modified: trunk/Tests/NSString.m
===================================================================
--- trunk/Tests/NSString.m 2006-09-02 00:10:20 UTC (rev 293)
+++ trunk/Tests/NSString.m 2006-09-02 00:10:31 UTC (rev 294)
@@ -178,6 +178,33 @@
}
END_TEST
+START_TEST (test_cStringUsingEncoding) {
+ NSString *string;
+ const char *cStr;
+
+ string = [[NSDumbString alloc] init];
+ fail_if(string == nil);
+
+ cStr = [string cStringUsingEncoding: NSASCIIStringEncoding];
+ fail_if(cStr == NULL, "-[NSString cStringUsingEncoding:] returned nil.");
+ fail_unless(strcmp(cStr, TEST_STRING) == 0, "-[NSString cString] returned unexpected string (Expected: %s, got: %s).", TEST_STRING, cStr);
+}
+END_TEST
+
+START_TEST (test_UTF8String) {
+ NSString *string;
+ const char *cStr;
+
+ string = [[NSDumbString alloc] init];
+ fail_if(string == nil);
+
+ cStr = [string UTF8String];
+ fail_if(cStr == NULL, "-[NSString UTF8String] returned nil.");
+
+ fail_unless(strcmp(cStr, TEST_STRING) == 0, "-[NSString UTF8String] returned unexpected string (Expected: %s, got: %s).", TEST_STRING, cStr);
+}
+END_TEST
+
START_TEST (test_cString) {
NSString *string;
const char *cStr;
@@ -205,6 +232,8 @@
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_cStringUsingEncoding);
+ tcase_add_test(tc_encoding, test_UTF8String);
tcase_add_test(tc_encoding, test_cString);
TCase *tc_init = tcase_create("Initialization");
Modified: trunk/Tests/NSZone.m
===================================================================
--- trunk/Tests/NSZone.m 2006-09-02 00:10:20 UTC (rev 293)
+++ trunk/Tests/NSZone.m 2006-09-02 00:10:31 UTC (rev 294)
@@ -180,7 +180,7 @@
NSZone *zone = NSCreateZone(10, 10, YES);
NSSetZoneName(zone, @"Monkey");
name = NSZoneName(zone);
- fail_unless(strcmp("Monkey", [name cString]) == 0, "NSSetZoneName failed, NZZoneName returned incorrect name (expected: Monkey, got: %s)", [name cString]);
+ fail_unless(strcmp("Monkey", [name UTF8String]) == 0, "NSSetZoneName failed, NZZoneName returned incorrect name (expected: Monkey, got: %s)", [name UTF8String]);
}
END_TEST
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|