From 5a72e1cfa8c136d876e05f080a9549ce5fdf12e1 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Fri, 6 Jul 2018 23:53:32 +0200 Subject: [PATCH] * Source/GSString.m: Remove GSString copyWithZone:. * Source/NSString.m: Raise exception when no is provided for the string. This brings our implementation closer to the Cocoa one. * Source/GSString.m: Add similar exceptions. --- ChangeLog | 7 +++++++ Source/GSString.m | 21 ++++++++++----------- Source/NSString.m | 44 ++++++++++++++++++++++++++++++++------------ 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3504a19b9..f09a88af3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2018-07-06 Fred Kiefer + + * Source/GSString.m: Remove GSString copyWithZone:. + * Source/NSString.m: Raise exception when no is provided for the + string. This brings our implementation closer to the Cocoa one. + * Source/GSString.m: Add similar exceptions. + 2018-07-02 Richard Frith-Macdonald * Tests/base/NSString/test00.m: : Move _unicodeString from here... diff --git a/Source/GSString.m b/Source/GSString.m index 55b6c6ce6..f5cc6877b 100644 --- a/Source/GSString.m +++ b/Source/GSString.m @@ -1556,6 +1556,9 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned, size_t len; GSStr me; + if (NULL == format) + [NSException raise: NSInvalidArgumentException + format: @"[GSPlaceholderString-initWithFormat:locale:arguments:]: NULL format"]; /* * First we provide an array of unichar characters containing the * format string. For performance reasons we try to use an on-stack @@ -1630,7 +1633,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned, if (string == nil) [NSException raise: NSInvalidArgumentException - format: @"-initWithString: given nil string"]; + format: @"GSPlaceholderString-initWithString: given nil string"]; if (NO == [string isKindOfClass: NSStringClass]) // may be proxy [NSException raise: NSInvalidArgumentException format: @"-initWithString: given non-string object"]; @@ -1679,10 +1682,9 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned, GSStr me; uint8_t c; - if (0 == bytes) - { - return (id)@""; - } + if (NULL == bytes) + [NSException raise: NSInvalidArgumentException + format: @"[GSPlaceholderString-initWithUTF8String:]: NULL cString"]; /* Skip leading BOM */ if (b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) @@ -3649,12 +3651,6 @@ transmute(GSStr self, NSString *aString) setup(YES); } -- (id) copyWithZone: (NSZone*)z -{ - [self subclassResponsibility: _cmd]; - return nil; -} - /* * Return a 28-bit hash value for the string contents - this * MUST match the algorithm used by the NSString base class. @@ -5062,6 +5058,9 @@ NSAssert(_flags.owned == 1 && _zone != 0, NSInternalInconsistencyException); unichar *fmt = fbuf; size_t len; + if (NULL == format) + [NSException raise: NSInvalidArgumentException + format: @"[GSMutableString-initWithFormat:locale:arguments:]: NULL format"]; /* * First we provide an array of unichar characters containing the * format string. For performance reasons we try to use an on-stack diff --git a/Source/NSString.m b/Source/NSString.m index c8c471e74..d8c369276 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -938,6 +938,9 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale) { NSString *obj; + if (NULL == aString) + [NSException raise: NSInvalidArgumentException + format: @"[NSString+stringWithString:]: NULL string"]; obj = [self allocWithZone: NSDefaultMallocZone()]; obj = [obj initWithString: aString]; return AUTORELEASE(obj); @@ -964,10 +967,12 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale) + (id) stringWithCString: (const char*)byteString { NSString *obj; - unsigned length = byteString ? strlen(byteString) : 0; + if (NULL == byteString) + [NSException raise: NSInvalidArgumentException + format: @"[NSString+stringWithCString:]: NULL cString"]; obj = [self allocWithZone: NSDefaultMallocZone()]; - obj = [obj initWithCString: byteString length: length]; + obj = [obj initWithCString: byteString]; return AUTORELEASE(obj); } @@ -1115,16 +1120,16 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale) + (id) stringWithFormat: (NSString*)format,... { va_list ap; - id ret; + NSString *obj; + if (NULL == format) + [NSException raise: NSInvalidArgumentException + format: @"[NSString+stringWithFormat:]: NULL format"]; va_start(ap, format); - if (format == nil) - ret = nil; - else - ret = AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] - initWithFormat: format arguments: ap]); + obj = [self allocWithZone: NSDefaultMallocZone()]; + obj = [obj initWithFormat: format arguments: ap]; va_end(ap); - return ret; + return AUTORELEASE(obj); } @@ -1275,8 +1280,11 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale) - (id) initWithCString: (const char*)byteString encoding: (NSStringEncoding)encoding { + if (NULL == byteString) + [NSException raise: NSInvalidArgumentException + format: @"[NSString-initWithCString:encoding:]: NULL cString"]; return [self initWithBytes: byteString - length: (byteString ? strlen(byteString) : 0) + length: strlen(byteString) encoding: encoding]; } @@ -1299,8 +1307,11 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale) */ - (id) initWithCString: (const char*)byteString { + if (NULL == byteString) + [NSException raise: NSInvalidArgumentException + format: @"[NSString-initWithCString:]: NULL cString"]; return [self initWithBytes: byteString - length: (byteString ? strlen(byteString) : 0) + length: strlen(byteString) encoding: _DefaultStringEncoding]; } @@ -1311,6 +1322,9 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale) { unsigned length = [string length]; + if (NULL == string) + [NSException raise: NSInvalidArgumentException + format: @"[NSString-initWithString:]: NULL string"]; if (length > 0) { unichar *s = NSZoneMalloc([self zone], sizeof(unichar)*length); @@ -1334,8 +1348,11 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale) */ - (id) initWithUTF8String: (const char *)bytes { + if (NULL == bytes) + [NSException raise: NSInvalidArgumentException + format: @"[NSString-initWithUTF8String:]: NULL cString"]; return [self initWithBytes: bytes - length: (bytes ? strlen(bytes) : 0) + length: strlen(bytes) encoding: NSUTF8StringEncoding]; } @@ -1387,6 +1404,9 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale) unichar *fmt = fbuf; size_t len; + if (NULL == format) + [NSException raise: NSInvalidArgumentException + format: @"[NSString-initWithFormat:locale:arguments:]: NULL format"]; /* * First we provide an array of unichar characters containing the * format string. For performance reasons we try to use an on-stack