mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-24 09:19:15 +00:00
Tidy memory allocation for empty strings.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@4507 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
a712b04807
commit
601742f5f9
4 changed files with 323 additions and 186 deletions
|
@ -1,9 +1,12 @@
|
|||
Fre Jul 2 12:10:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
||||
Fri Jul 2 14:20:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
||||
|
||||
* Source/GSeq.h: Fix boundary error when searching for string in
|
||||
string.
|
||||
* Source/NSString.m: Don't allocate contents for string with 0 length.
|
||||
* Source/NSGString.m: ditto
|
||||
* Source/NSGCString.m: ditto
|
||||
|
||||
Fre Jul 2 8:45:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
||||
Fri Jul 2 8:45:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
||||
|
||||
* Source/NSUnarchiver.m: In designated initialiser, catch exceptions
|
||||
and deallocate self before re-throwing in order to prevent memory
|
||||
|
|
|
@ -92,14 +92,14 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
}
|
||||
}
|
||||
|
||||
+ allocWithZone: (NSZone*)z
|
||||
+ (id) allocWithZone: (NSZone*)z
|
||||
{
|
||||
return NSAllocateObject (self, 0, z);
|
||||
return NSAllocateObject(self, 0, z);
|
||||
}
|
||||
|
||||
+ alloc
|
||||
+ (id) alloc
|
||||
{
|
||||
return NSAllocateObject (self, 0, NSDefaultMallocZone());
|
||||
return NSAllocateObject(self, 0, NSDefaultMallocZone());
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
|
@ -144,7 +144,7 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
{
|
||||
NSZone *z;
|
||||
|
||||
if (flag)
|
||||
if (flag && byteString)
|
||||
{
|
||||
z = NSZoneFromPointer(byteString);
|
||||
}
|
||||
|
@ -198,7 +198,7 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
}
|
||||
}
|
||||
|
||||
- (void) encodeWithCoder: aCoder
|
||||
- (void) encodeWithCoder: (NSCoder*)aCoder
|
||||
{
|
||||
[aCoder encodeValueOfObjCType:@encode(unsigned) at:&_count];
|
||||
if (_count > 0)
|
||||
|
@ -209,7 +209,7 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
}
|
||||
}
|
||||
|
||||
- initWithCoder: aCoder
|
||||
- (id) initWithCoder: (NSCoder*)aCoder
|
||||
{
|
||||
[aCoder decodeValueOfObjCType:@encode(unsigned)
|
||||
at:&_count];
|
||||
|
@ -224,7 +224,7 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
return self;
|
||||
}
|
||||
|
||||
- copy
|
||||
- (id) copy
|
||||
{
|
||||
NSZone *z = NSDefaultMallocZone();
|
||||
|
||||
|
@ -234,8 +234,16 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
char *tmp;
|
||||
|
||||
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
|
||||
tmp = NSZoneMalloc(z, _count);
|
||||
memcpy(tmp, _contents_chars, _count);
|
||||
if (_count)
|
||||
{
|
||||
tmp = NSZoneMalloc(z, _count);
|
||||
memcpy(tmp, _contents_chars, _count);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp = 0;
|
||||
z = 0;
|
||||
}
|
||||
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
|
||||
if (_hash && obj)
|
||||
{
|
||||
|
@ -249,7 +257,7 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
}
|
||||
}
|
||||
|
||||
- copyWithZone: (NSZone*)z
|
||||
- (id) copyWithZone: (NSZone*)z
|
||||
{
|
||||
if (NSShouldRetainWithZone(self, z) == NO)
|
||||
{
|
||||
|
@ -257,8 +265,16 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
char *tmp;
|
||||
|
||||
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
|
||||
tmp = NSZoneMalloc(z, _count);
|
||||
memcpy(tmp, _contents_chars, _count);
|
||||
if (_count)
|
||||
{
|
||||
tmp = NSZoneMalloc(z, _count);
|
||||
memcpy(tmp, _contents_chars, _count);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp = 0;
|
||||
z = 0;
|
||||
}
|
||||
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
|
||||
if (_hash && obj)
|
||||
{
|
||||
|
@ -272,7 +288,7 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
}
|
||||
}
|
||||
|
||||
- mutableCopy
|
||||
- (id) mutableCopy
|
||||
{
|
||||
NSGMutableCString *obj;
|
||||
|
||||
|
@ -293,7 +309,7 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
return obj;
|
||||
}
|
||||
|
||||
- mutableCopyWithZone: (NSZone*)z
|
||||
- (id) mutableCopyWithZone: (NSZone*)z
|
||||
{
|
||||
NSGMutableCString *obj;
|
||||
|
||||
|
@ -525,12 +541,22 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
|
||||
- (id) initWithString: (NSString*)string
|
||||
{
|
||||
NSZone *z = fastZone(self);
|
||||
unsigned length = [string cStringLength];
|
||||
char *buf = NSZoneMalloc(z, length+1); // getCString appends a nul.
|
||||
unsigned length = [string cStringLength];
|
||||
NSZone *z;
|
||||
char *buf;
|
||||
|
||||
[string getCString: buf];
|
||||
return [self initWithCStringNoCopy: buf length: length fromZone: z];
|
||||
if (length > 0)
|
||||
{
|
||||
z = fastZone(self);
|
||||
buf = NSZoneMalloc(z, length+1); // getCString appends a nul.
|
||||
[string getCString: buf];
|
||||
}
|
||||
else
|
||||
{
|
||||
z = 0;
|
||||
buf = 0;
|
||||
}
|
||||
return [self initWithCStringNoCopy: buf length: length fromZone: z];
|
||||
}
|
||||
|
||||
- (void) descriptionTo: (id<GNUDescriptionDestination>)output
|
||||
|
@ -747,14 +773,14 @@ static IMP msInitImp; /* designated initialiser for mutable */
|
|||
|
||||
@implementation NSGMutableCString
|
||||
|
||||
+ allocWithZone: (NSZone*)z
|
||||
+ (id) allocWithZone: (NSZone*)z
|
||||
{
|
||||
return NSAllocateObject (self, 0, z);
|
||||
return NSAllocateObject(self, 0, z);
|
||||
}
|
||||
|
||||
+ alloc
|
||||
+ (id) alloc
|
||||
{
|
||||
return NSAllocateObject (self, 0, NSDefaultMallocZone());
|
||||
return NSAllocateObject(self, 0, NSDefaultMallocZone());
|
||||
}
|
||||
|
||||
+ (void) initialize
|
||||
|
@ -822,7 +848,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
|
|||
}
|
||||
|
||||
/* This is the designated initializer for this class */
|
||||
- initWithCapacity: (unsigned)capacity
|
||||
- (id) initWithCapacity: (unsigned)capacity
|
||||
{
|
||||
_count = 0;
|
||||
_capacity = capacity;
|
||||
|
@ -873,15 +899,23 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
|
|||
return a;
|
||||
}
|
||||
|
||||
- copy
|
||||
- (id) copy
|
||||
{
|
||||
char *tmp;
|
||||
NSGCString *obj;
|
||||
NSZone *z = NSDefaultMallocZone();
|
||||
|
||||
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
|
||||
tmp = NSZoneMalloc(z, _count);
|
||||
memcpy(tmp, _contents_chars, _count);
|
||||
if (_count)
|
||||
{
|
||||
tmp = NSZoneMalloc(z, _count);
|
||||
memcpy(tmp, _contents_chars, _count);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp = 0;
|
||||
z = 0;
|
||||
}
|
||||
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
|
||||
if (_hash && obj)
|
||||
{
|
||||
|
@ -892,14 +926,22 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
|
|||
return obj;
|
||||
}
|
||||
|
||||
- copyWithZone: (NSZone*)z
|
||||
- (id) copyWithZone: (NSZone*)z
|
||||
{
|
||||
char *tmp;
|
||||
NSGCString *obj;
|
||||
|
||||
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
|
||||
tmp = NSZoneMalloc(z, _count);
|
||||
memcpy(tmp, _contents_chars, _count);
|
||||
if (_count)
|
||||
{
|
||||
tmp = NSZoneMalloc(z, _count);
|
||||
memcpy(tmp, _contents_chars, _count);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp = 0;
|
||||
z = 0;
|
||||
}
|
||||
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
|
||||
if (_hash && obj)
|
||||
{
|
||||
|
@ -910,7 +952,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
|
|||
return obj;
|
||||
}
|
||||
|
||||
- mutableCopy
|
||||
- (id) mutableCopy
|
||||
{
|
||||
NSGMutableCString *obj;
|
||||
|
||||
|
@ -955,7 +997,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
|
|||
|
||||
// xxx This should be primitive method
|
||||
- (void) replaceCharactersInRange: (NSRange)range
|
||||
withString: (NSString*)aString
|
||||
withString: (NSString*)aString
|
||||
{
|
||||
[self deleteCharactersInRange:range];
|
||||
[self insertString:aString atIndex:range.location];
|
||||
|
@ -1030,7 +1072,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
|
|||
range.location, range.length);
|
||||
}
|
||||
|
||||
- initWithCoder: aCoder
|
||||
- (id) initWithCoder: (NSCoder*)aCoder
|
||||
{
|
||||
unsigned cap;
|
||||
|
||||
|
|
|
@ -162,13 +162,14 @@
|
|||
length: (unsigned int)length
|
||||
fromZone: (NSZone*)zone
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_count = length;
|
||||
_contents_chars = chars;
|
||||
_zone = chars ? zone : 0;
|
||||
self = [super init];
|
||||
if (self)
|
||||
{
|
||||
_count = length;
|
||||
_contents_chars = chars;
|
||||
_zone = chars ? zone : 0;
|
||||
}
|
||||
return self;
|
||||
return self;
|
||||
}
|
||||
|
||||
/* This is the OpenStep designated initializer for this class. */
|
||||
|
@ -176,29 +177,40 @@
|
|||
length: (unsigned int)length
|
||||
freeWhenDone: (BOOL)flag
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_count = length;
|
||||
_contents_chars = chars;
|
||||
if (flag) {
|
||||
_zone = NSZoneFromPointer(chars);
|
||||
self = [super init];
|
||||
if (self)
|
||||
{
|
||||
_count = length;
|
||||
_contents_chars = chars;
|
||||
if (flag && chars)
|
||||
{
|
||||
_zone = NSZoneFromPointer(chars);
|
||||
}
|
||||
else {
|
||||
_zone = 0;
|
||||
else
|
||||
{
|
||||
_zone = 0;
|
||||
}
|
||||
}
|
||||
return self;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id) initWithCharacters: (const unichar*)chars
|
||||
length: (unsigned int)length
|
||||
{
|
||||
NSZone *z = fastZone(self);
|
||||
unichar *s = NSZoneMalloc(z, length*sizeof(unichar));
|
||||
NSZone *z = fastZone(self);
|
||||
unichar *s;
|
||||
|
||||
if (chars)
|
||||
if (length)
|
||||
{
|
||||
s = NSZoneMalloc(z, length*sizeof(unichar));
|
||||
if (chars)
|
||||
memcpy(s, chars, sizeof(unichar)*length);
|
||||
return [self initWithCharactersNoCopy:s length:length fromZone:z];
|
||||
}
|
||||
else
|
||||
{
|
||||
s = 0;
|
||||
}
|
||||
return [self initWithCharactersNoCopy:s length:length fromZone:z];
|
||||
}
|
||||
|
||||
- (id) initWithCStringNoCopy: (char*)byteString
|
||||
|
@ -304,7 +316,7 @@
|
|||
|
||||
/* NSCoding Protocol */
|
||||
|
||||
- (void) encodeWithCoder: aCoder
|
||||
- (void) encodeWithCoder: (NSCoder*)aCoder
|
||||
{
|
||||
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &_count];
|
||||
if (_count > 0)
|
||||
|
@ -315,7 +327,7 @@
|
|||
}
|
||||
}
|
||||
|
||||
- initWithCoder: aCoder
|
||||
- (id) initWithCoder: (NSCoder*)aCoder
|
||||
{
|
||||
[aCoder decodeValueOfObjCType: @encode(unsigned) at: &_count];
|
||||
if (_count)
|
||||
|
@ -467,14 +479,14 @@
|
|||
|
||||
// @protocol NSMutableString <NSString>
|
||||
|
||||
+ allocWithZone: (NSZone*)z
|
||||
+ (id) allocWithZone: (NSZone*)z
|
||||
{
|
||||
return NSAllocateObject (self, 0, z);
|
||||
return NSAllocateObject(self, 0, z);
|
||||
}
|
||||
|
||||
+ alloc
|
||||
+ (id) alloc
|
||||
{
|
||||
return NSAllocateObject (self, 0, NSDefaultMallocZone());
|
||||
return NSAllocateObject(self, 0, NSDefaultMallocZone());
|
||||
}
|
||||
|
||||
+ (void) initialize
|
||||
|
@ -540,47 +552,54 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
|
|||
length: (unsigned int)length
|
||||
fromZone: (NSZone*)zone
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_count = length;
|
||||
_capacity = length;
|
||||
_contents_chars = chars;
|
||||
_zone = zone;
|
||||
self = [super init];
|
||||
if (self)
|
||||
{
|
||||
_count = length;
|
||||
_capacity = length;
|
||||
_contents_chars = chars;
|
||||
_zone = zone;
|
||||
}
|
||||
return self;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id) initWithCharactersNoCopy: (unichar*)chars
|
||||
length: (unsigned int)length
|
||||
freeWhenDone: (BOOL)flag
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_count = length;
|
||||
_capacity = length;
|
||||
_contents_chars = chars;
|
||||
if (flag) {
|
||||
_zone = NSZoneFromPointer(chars);
|
||||
self = [super init];
|
||||
if (self)
|
||||
{
|
||||
_count = length;
|
||||
_capacity = length;
|
||||
_contents_chars = chars;
|
||||
if (flag && chars)
|
||||
{
|
||||
_zone = NSZoneFromPointer(chars);
|
||||
}
|
||||
else {
|
||||
_zone = 0;
|
||||
else
|
||||
{
|
||||
_zone = 0;
|
||||
}
|
||||
}
|
||||
return self;
|
||||
return self;
|
||||
}
|
||||
|
||||
- initWithCapacity: (unsigned)capacity
|
||||
- (id) initWithCapacity: (unsigned)capacity
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
if (capacity < 2)
|
||||
capacity = 2;
|
||||
_count = 0;
|
||||
_capacity = capacity;
|
||||
_zone = fastZone(self);
|
||||
_contents_chars = NSZoneMalloc(_zone, sizeof(unichar)*capacity);
|
||||
self = [super init];
|
||||
if (self)
|
||||
{
|
||||
if (capacity < 2)
|
||||
{
|
||||
capacity = 2;
|
||||
}
|
||||
_count = 0;
|
||||
_capacity = capacity;
|
||||
_zone = fastZone(self);
|
||||
_contents_chars = NSZoneMalloc(_zone, sizeof(unichar)*capacity);
|
||||
}
|
||||
return self;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id) initWithCStringNoCopy: (char*)byteString
|
||||
|
@ -612,7 +631,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
|
|||
}
|
||||
|
||||
- (void) replaceCharactersInRange: (NSRange)aRange
|
||||
withString: (NSString*)aString
|
||||
withString: (NSString*)aString
|
||||
{
|
||||
int offset;
|
||||
unsigned stringLength;
|
||||
|
@ -683,7 +702,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
|
|||
range.location, range.length);
|
||||
}
|
||||
|
||||
- initWithCoder: aCoder // *** changed to unichar
|
||||
- (id) initWithCoder: (NSCoder*)aCoder
|
||||
{
|
||||
unsigned cap;
|
||||
|
||||
|
|
|
@ -384,13 +384,23 @@ handle_printf_atsign (FILE *stream,
|
|||
- (id) initWithCharacters: (const unichar*)chars
|
||||
length: (unsigned)length
|
||||
{
|
||||
NSZone *z = [self zone];
|
||||
unichar *s = NSZoneMalloc(z, sizeof(unichar)*length);
|
||||
NSZone *z;
|
||||
unichar *s;
|
||||
|
||||
if (chars)
|
||||
if (length > 0)
|
||||
{
|
||||
z = [self zone];
|
||||
s = NSZoneMalloc(z, sizeof(unichar)*length);
|
||||
if (chars)
|
||||
memcpy(s, chars, sizeof(unichar)*length);
|
||||
}
|
||||
else
|
||||
{
|
||||
s = 0;
|
||||
z = 0;
|
||||
}
|
||||
|
||||
return [self initWithCharactersNoCopy: s length: length fromZone: z];
|
||||
return [self initWithCharactersNoCopy: s length: length fromZone: z];
|
||||
}
|
||||
|
||||
- (id) initWithCStringNoCopy: (char*)byteString
|
||||
|
@ -400,7 +410,7 @@ handle_printf_atsign (FILE *stream,
|
|||
if (flag)
|
||||
return [self initWithCStringNoCopy: byteString
|
||||
length: length
|
||||
fromZone: NSZoneFromPointer(byteString)];
|
||||
fromZone: length?NSZoneFromPointer(byteString):0];
|
||||
else
|
||||
return [self initWithCStringNoCopy: byteString
|
||||
length: length
|
||||
|
@ -418,12 +428,25 @@ handle_printf_atsign (FILE *stream,
|
|||
|
||||
- (id) initWithCString: (const char*)byteString length: (unsigned)length
|
||||
{
|
||||
NSZone *z = [self zone];
|
||||
char *s = NSZoneMalloc(z, length);
|
||||
NSZone *z;
|
||||
char *s;
|
||||
|
||||
if (byteString)
|
||||
memcpy(s, byteString, length);
|
||||
return [self initWithCStringNoCopy: s length: length fromZone: z];
|
||||
if (length > 0)
|
||||
{
|
||||
z = [self zone];
|
||||
s = NSZoneMalloc(z, length);
|
||||
if (byteString)
|
||||
{
|
||||
memcpy(s, byteString, length);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
s = 0;
|
||||
z = 0;
|
||||
}
|
||||
|
||||
return [self initWithCStringNoCopy: s length: length fromZone: z];
|
||||
}
|
||||
|
||||
- (id) initWithCString: (const char*)byteString
|
||||
|
@ -434,14 +457,24 @@ handle_printf_atsign (FILE *stream,
|
|||
|
||||
- (id) initWithString: (NSString*)string
|
||||
{
|
||||
NSZone *z = [self zone];
|
||||
unsigned length = [string length];
|
||||
unichar *s = NSZoneMalloc(z, sizeof(unichar)*length);
|
||||
unsigned length = [string length];
|
||||
NSZone *z;
|
||||
unichar *s;
|
||||
|
||||
[string getCharacters: s];
|
||||
return [self initWithCharactersNoCopy: s
|
||||
length: length
|
||||
fromZone: z];
|
||||
if (length > 0)
|
||||
{
|
||||
z = [self zone];
|
||||
s = NSZoneMalloc(z, sizeof(unichar)*length);
|
||||
[string getCharacters: s];
|
||||
}
|
||||
else
|
||||
{
|
||||
s = 0;
|
||||
z = 0;
|
||||
}
|
||||
return [self initWithCharactersNoCopy: s
|
||||
length: length
|
||||
fromZone: z];
|
||||
}
|
||||
|
||||
- (id) initWithFormat: (NSString*)format,...
|
||||
|
@ -568,34 +601,50 @@ handle_printf_atsign (FILE *stream,
|
|||
}
|
||||
|
||||
- (id) initWithFormat: (NSString*)format
|
||||
locale: (NSDictionary*)dictionary
|
||||
arguments: (va_list)argList
|
||||
locale: (NSDictionary*)dictionary
|
||||
arguments: (va_list)argList
|
||||
{
|
||||
[self notImplemented: _cmd];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id) initWithData: (NSData*)data
|
||||
encoding: (NSStringEncoding)encoding
|
||||
encoding: (NSStringEncoding)encoding
|
||||
{
|
||||
if ((encoding==[NSString defaultCStringEncoding])
|
||||
|| (encoding==NSASCIIStringEncoding))
|
||||
{
|
||||
NSZone *z = fastZone(self);
|
||||
int len=[data length];
|
||||
char *s = NSZoneMalloc(z, len+1);
|
||||
unsigned len=[data length];
|
||||
NSZone *z;
|
||||
char *s;
|
||||
|
||||
[data getBytes: s];
|
||||
if (len > 0)
|
||||
{
|
||||
z = fastZone(self);
|
||||
s = NSZoneMalloc(z, len);
|
||||
[data getBytes: s];
|
||||
}
|
||||
else
|
||||
{
|
||||
s = 0;
|
||||
z = 0;
|
||||
}
|
||||
return [self initWithCStringNoCopy: s length: len fromZone: z];
|
||||
}
|
||||
else
|
||||
{
|
||||
NSZone *z = fastZone(self);
|
||||
int len=[data length];
|
||||
unichar *u = NSZoneMalloc(z, sizeof(unichar)*(len+1));
|
||||
int count;
|
||||
const unsigned char *b=[data bytes];
|
||||
unsigned len = [data length];
|
||||
NSZone *z;
|
||||
unichar *u;
|
||||
unsigned count;
|
||||
const unsigned char *b;
|
||||
|
||||
if (len < 2)
|
||||
return [self initWithCStringNoCopy: 0 length: 0 fromZone: z];
|
||||
|
||||
z = fastZone(self);
|
||||
b=[data bytes];
|
||||
u = NSZoneMalloc(z, sizeof(unichar)*(len+1));
|
||||
if (encoding==NSUnicodeStringEncoding)
|
||||
{
|
||||
if ((b[0]==0xFE)&(b[1]==0xFF))
|
||||
|
@ -617,10 +666,14 @@ handle_printf_atsign (FILE *stream,
|
|||
- (id) initWithContentsOfFile: (NSString*)path
|
||||
{
|
||||
NSStringEncoding enc;
|
||||
id d = [NSData dataWithContentsOfFile: path];
|
||||
const unsigned char *test=[d bytes];
|
||||
id d = [NSData dataWithContentsOfFile: path];
|
||||
const unsigned char *test;
|
||||
|
||||
if (d == nil) return nil;
|
||||
if (d == nil)
|
||||
return nil;
|
||||
if ([d length] < 2)
|
||||
return @"";
|
||||
test = [d bytes];
|
||||
if (test && (((test[0]==0xFF) && (test[1]==0xFE)) || ((test[1]==0xFF) && (test[0]==0xFE))))
|
||||
enc = NSUnicodeStringEncoding;
|
||||
else
|
||||
|
@ -630,8 +683,8 @@ handle_printf_atsign (FILE *stream,
|
|||
|
||||
- (id) init
|
||||
{
|
||||
self = [super init];
|
||||
return self;
|
||||
self = [super init];
|
||||
return self;
|
||||
}
|
||||
|
||||
// Getting a String's Length
|
||||
|
@ -912,19 +965,23 @@ handle_printf_atsign (FILE *stream,
|
|||
|
||||
- (BOOL) isEqual: (id)anObject
|
||||
{
|
||||
if (anObject == self) {
|
||||
return YES;
|
||||
if (anObject == self)
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
if (anObject != nil) {
|
||||
Class c = fastClassOfInstance(anObject);
|
||||
if (anObject != nil)
|
||||
{
|
||||
Class c = fastClassOfInstance(anObject);
|
||||
|
||||
if (c != nil) {
|
||||
if (fastClassIsKindOfClass(c, NSString_class)) {
|
||||
return [self isEqualToString: anObject];
|
||||
if (c != nil)
|
||||
{
|
||||
if (fastClassIsKindOfClass(c, NSString_class))
|
||||
{
|
||||
return [self isEqualToString: anObject];
|
||||
}
|
||||
}
|
||||
}
|
||||
return NO;
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL) isEqualToString: (NSString*)aString
|
||||
|
@ -1244,54 +1301,63 @@ handle_printf_atsign (FILE *stream,
|
|||
// but this will work in most cases
|
||||
- (NSString*) capitalizedString
|
||||
{
|
||||
NSZone *z = fastZone(self);
|
||||
unichar *s;
|
||||
int count=0;
|
||||
BOOL found=YES;
|
||||
int len=[self length];
|
||||
NSZone *z;
|
||||
unichar *s;
|
||||
unsigned count = 0;
|
||||
BOOL found = YES;
|
||||
unsigned len = [self length];
|
||||
|
||||
if (len == 0)
|
||||
return self;
|
||||
if (whitespce == nil)
|
||||
setupWhitespce();
|
||||
|
||||
s = NSZoneMalloc(z, sizeof(unichar)*(len+1));
|
||||
z = fastZone(self);
|
||||
s = NSZoneMalloc(z, sizeof(unichar)*len);
|
||||
[self getCharacters: s];
|
||||
s[len] = (unichar)0;
|
||||
while (count<len)
|
||||
{
|
||||
if ((*whitespceImp)(whitespce, cMemberSel, s[count]))
|
||||
{
|
||||
count++;
|
||||
found=YES;
|
||||
while ((*whitespceImp)(whitespce, cMemberSel, s[count]) && (count < len))
|
||||
count++;
|
||||
};
|
||||
if (found)
|
||||
{
|
||||
s[count]=uni_toupper(s[count]);
|
||||
count++;
|
||||
if ((*whitespceImp)(whitespce, cMemberSel, s[count]))
|
||||
{
|
||||
count++;
|
||||
found=YES;
|
||||
while ((*whitespceImp)(whitespce, cMemberSel, s[count])
|
||||
&& (count < len))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (found)
|
||||
{
|
||||
s[count] = uni_toupper(s[count]);
|
||||
count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!(*whitespceImp)(whitespce, cMemberSel, s[count])
|
||||
&& (count < len))
|
||||
{
|
||||
s[count] = uni_tolower(s[count]);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
found=NO;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!(*whitespceImp)(whitespce, cMemberSel, s[count]) && (count < len))
|
||||
{
|
||||
s[count]=uni_tolower(s[count]);
|
||||
count++;
|
||||
};
|
||||
};
|
||||
found=NO;
|
||||
};
|
||||
return AUTORELEASE([[NSString allocWithZone: NSDefaultMallocZone()]
|
||||
initWithCharactersNoCopy: s length: len fromZone: z]);
|
||||
}
|
||||
|
||||
- (NSString*) lowercaseString
|
||||
{
|
||||
NSZone *z = fastZone(self);
|
||||
NSZone *z;
|
||||
unichar *s;
|
||||
unsigned count;
|
||||
unsigned len = [self length];
|
||||
|
||||
s = NSZoneMalloc(z, sizeof(unichar)*(len+1));
|
||||
if (len == 0)
|
||||
return self;
|
||||
z = fastZone(self);
|
||||
s = NSZoneMalloc(z, sizeof(unichar)*len);
|
||||
for (count = 0; count < len; count++)
|
||||
s[count] = uni_tolower([self characterAtIndex: count]);
|
||||
return AUTORELEASE([[NSString_concrete_class
|
||||
|
@ -1301,12 +1367,15 @@ handle_printf_atsign (FILE *stream,
|
|||
|
||||
- (NSString*) uppercaseString;
|
||||
{
|
||||
NSZone *z = fastZone(self);
|
||||
NSZone *z;
|
||||
unichar *s;
|
||||
unsigned count;
|
||||
unsigned len = [self length];
|
||||
|
||||
s = NSZoneMalloc(z, sizeof(unichar)*(len+1));
|
||||
if (len == 0)
|
||||
return self;
|
||||
z = fastZone(self);
|
||||
s = NSZoneMalloc(z, sizeof(unichar)*len);
|
||||
for (count = 0; count < len; count++)
|
||||
s[count] = uni_toupper([self characterAtIndex: count]);
|
||||
return AUTORELEASE([[NSString_concrete_class
|
||||
|
@ -1353,11 +1422,12 @@ handle_printf_atsign (FILE *stream,
|
|||
|
||||
// xxx FIXME adjust range for composite sequence
|
||||
- (void) getCString: (char*)buffer
|
||||
maxLength: (unsigned)maxLength
|
||||
range: (NSRange)aRange
|
||||
remainingRange: (NSRange*)leftoverRange
|
||||
maxLength: (unsigned)maxLength
|
||||
range: (NSRange)aRange
|
||||
remainingRange: (NSRange*)leftoverRange
|
||||
{
|
||||
int len, count;
|
||||
unsigned len;
|
||||
unsigned count;
|
||||
|
||||
len = [self cStringLength];
|
||||
GS_RANGE_CHECK(aRange, len);
|
||||
|
@ -1380,12 +1450,12 @@ handle_printf_atsign (FILE *stream,
|
|||
leftoverRange->length = aRange.length - maxLength;
|
||||
}
|
||||
}
|
||||
count=0;
|
||||
while (count<len)
|
||||
{
|
||||
buffer[count]=unitochar([self characterAtIndex: aRange.location + count]);
|
||||
count++;
|
||||
}
|
||||
count = 0;
|
||||
while (count < len)
|
||||
{
|
||||
buffer[count]=unitochar([self characterAtIndex: aRange.location + count]);
|
||||
count++;
|
||||
}
|
||||
buffer[len] = '\0';
|
||||
}
|
||||
|
||||
|
@ -1396,9 +1466,9 @@ handle_printf_atsign (FILE *stream,
|
|||
|
||||
- (BOOL) boolValue
|
||||
{
|
||||
if ([self caseInsensitiveCompare: @"YES"] == NSOrderedSame)
|
||||
return YES;
|
||||
return [self intValue] != 0 ? YES : NO;
|
||||
if ([self caseInsensitiveCompare: @"YES"] == NSOrderedSame)
|
||||
return YES;
|
||||
return [self intValue] != 0 ? YES : NO;
|
||||
}
|
||||
|
||||
- (double) doubleValue
|
||||
|
@ -1465,6 +1535,9 @@ handle_printf_atsign (FILE *stream,
|
|||
int count=0;
|
||||
int len = [self length];
|
||||
|
||||
if (len == 0)
|
||||
return [NSData data];
|
||||
|
||||
if ((encoding==NSASCIIStringEncoding)
|
||||
|| (encoding==NSISOLatin1StringEncoding)
|
||||
|| (encoding==NSNEXTSTEPStringEncoding)
|
||||
|
@ -2167,7 +2240,7 @@ handle_printf_atsign (FILE *stream,
|
|||
return RETAIN(self);
|
||||
}
|
||||
|
||||
- mutableCopyWithZone: (NSZone*)zone
|
||||
- (id) mutableCopyWithZone: (NSZone*)zone
|
||||
{
|
||||
return [[[[self class] _mutableConcreteClass] allocWithZone: zone]
|
||||
initWithString: self];
|
||||
|
@ -2175,15 +2248,15 @@ handle_printf_atsign (FILE *stream,
|
|||
|
||||
/* NSCoding Protocol */
|
||||
|
||||
- (void) encodeWithCoder: anEncoder
|
||||
- (void) encodeWithCoder: (NSCoder*)anEncoder
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
[self subclassResponsibility: _cmd];
|
||||
}
|
||||
|
||||
- initWithCoder: aDecoder
|
||||
- (id) initWithCoder: (NSCoder*)aDecoder
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return self;
|
||||
[self subclassResponsibility: _cmd];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (Class) classForArchiver
|
||||
|
@ -2266,7 +2339,7 @@ handle_printf_atsign (FILE *stream,
|
|||
|
||||
@implementation NSMutableString
|
||||
|
||||
+ allocWithZone: (NSZone*)z
|
||||
+ (id) allocWithZone: (NSZone*)z
|
||||
{
|
||||
if ([self class] == [NSMutableString class])
|
||||
return NSAllocateObject([self _mutableConcreteClass], 0, z);
|
||||
|
@ -2283,7 +2356,7 @@ handle_printf_atsign (FILE *stream,
|
|||
|
||||
/* Inefficient. */
|
||||
+ (NSString*) stringWithCharacters: (const unichar*)characters
|
||||
length: (unsigned)length
|
||||
length: (unsigned)length
|
||||
{
|
||||
id n;
|
||||
n = [[self allocWithZone: NSDefaultMallocZone()]
|
||||
|
@ -2316,7 +2389,7 @@ handle_printf_atsign (FILE *stream,
|
|||
|
||||
// Initializing Newly Allocated Strings
|
||||
|
||||
- initWithCapacity: (unsigned)capacity
|
||||
- (id) initWithCapacity: (unsigned)capacity
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return self;
|
||||
|
|
Loading…
Reference in a new issue