mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 08:26:27 +00:00
*** empty log message ***
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1944 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
26d5358402
commit
ed85bf15d5
5 changed files with 0 additions and 1069 deletions
|
@ -1,176 +0,0 @@
|
|||
/* Some preliminary ideas about what a String class might look like.
|
||||
Copyright (C) 1993,1994, 1995, 1996 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
||||
Date: May 1993
|
||||
|
||||
This file is part of the GNUstep Base Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#ifndef __String_h_GNUSTEP_BASE_INCLUDE
|
||||
#define __String_h_GNUSTEP_BASE_INCLUDE
|
||||
|
||||
/* xxx These method names need to be fixed because we will get
|
||||
type conflicts with GNUSTEP.
|
||||
I will do the work to merge NSString and GNU String in the same
|
||||
manner as NSArray and GNU Array. */
|
||||
|
||||
#include <gnustep/base/preface.h>
|
||||
#include <gnustep/base/IndexedCollection.h>
|
||||
#include <gnustep/base/ValueHolding.h>
|
||||
//#include <Foundation/NSString.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
typedef unsigned short Character;
|
||||
|
||||
@class String;
|
||||
@class ConstantString;
|
||||
@class MutableString;
|
||||
|
||||
/* Like in SmallTalk, the String class is a subclass of Collection---a
|
||||
collection of characters. So, all the collection methods are
|
||||
available. Nice. */
|
||||
|
||||
/* Think about changing these names to avoid conflicts with OpenStep? */
|
||||
|
||||
@protocol String <NSObject, ValueGetting, IndexedCollecting>
|
||||
|
||||
// INITIALIZING NEWLY ALLOCATED STRINGS. DON'T FORGET TO RELEASE THEM!;
|
||||
- init;
|
||||
- initWithString: (id <String>)aString;
|
||||
- initWithString: (id <String>)aString range: (IndexRange)aRange;
|
||||
- initWithFormat: (id <String>)aFormatString, ...;
|
||||
- initWithFormat: (id <String>)aFormatString arguments: (va_list)arg;
|
||||
- initWithCString: (const char*)aCharPtr;
|
||||
- initWithCString: (const char*)aCharPtr range: (IndexRange)aRange;
|
||||
//- initWithStream: (Stream*)aStream;
|
||||
//- initWithStream: (Stream*)aStream length: (unsigned)aLength;
|
||||
|
||||
// GETTING NEW, AUTORELEASED STRING OBJECTS, NO NEED TO RELEASE THESE;
|
||||
+ stringWithString: (id <String>)aString;
|
||||
+ stringWithString: (id <String>)aString range: (IndexRange)aRange;
|
||||
+ stringWithFormat: (id <String>)aFormatString, ...;
|
||||
+ stringWithFormat: (id <String>)aFormatString arguments: (va_list)arg;
|
||||
+ stringWithCString: (const char*)aCharPtr;
|
||||
+ stringWithCString: (const char*)aCharPtr range: (IndexRange)aRange;
|
||||
+ stringWithCStringNoCopy: (const char*)aCharPtr
|
||||
freeWhenDone: (BOOL) f;
|
||||
+ stringWithCStringNoCopy: (const char*)aCharPtr;
|
||||
|
||||
- stringByAppendingFormat: (id <String>)aString, ...;
|
||||
- stringByAppendingFormat: (id <String>)aString arguments: (va_list)arg;
|
||||
- stringByPrependingFormat: (id <String>)aString, ...;
|
||||
- stringByPrependingFormat: (id <String>)aString arguments: (va_list)arg;
|
||||
- stringByAppendingString: (id <String>)aString;
|
||||
- stringByPrependingString: (id <String>)aString;
|
||||
|
||||
//- substringWithRange: (IndexRange)aRange;
|
||||
//- substringWithLength: (unsigned)l;
|
||||
//- substringAfterIndex: (unsigned)i;
|
||||
//- (id <IndexedCollecting>) substringsSeparatedByString: (id <String>)sep;
|
||||
|
||||
//- capitalizedString;
|
||||
//- lowercaseString;
|
||||
//- uppercaseString;
|
||||
|
||||
- mutableCopy;
|
||||
- copy;
|
||||
|
||||
// QUERYING
|
||||
- (unsigned) length;
|
||||
- (IndexRange) range;
|
||||
- (BOOL) isEqual: anObject;
|
||||
- (unsigned) hash;
|
||||
- (int) compare: anObject;
|
||||
- copy;
|
||||
- (unsigned) indexOfString: (id <String>)aString;
|
||||
- (unsigned) indexOfChar: (char)aChar;
|
||||
- (unsigned) indexOfLastChar: (char)aChar;
|
||||
//- (unsigned) indexOfCharacter: (Character)aChar;
|
||||
//- (unsigned) indexOfLastCharacter: (Character)aChar;
|
||||
|
||||
// GETTING C CHARS;
|
||||
- (char) charAtIndex: (unsigned)index;
|
||||
- (const char *) cString;
|
||||
- (const char *) cStringNoCopy;
|
||||
- (unsigned) cStringLength;
|
||||
- (void) getCString: (char*)buffer;
|
||||
- (void) getCString: (char*)buffer range: (IndexRange)aRange;
|
||||
|
||||
// FOR FILE NAMES (don't use the name "path", gnu will not use it for this);
|
||||
//- (IndexRange) fileRange;
|
||||
//- (IndexRange) directoriesRange;
|
||||
//- (IndexRange) extensionRange;
|
||||
//- (IndexRange) fileWithoutExtensionRange;
|
||||
//- (BOOL) isAbsolute;
|
||||
//- (BOOL) isRelative;
|
||||
|
||||
@end
|
||||
|
||||
@protocol MutableString <ValueSetting>
|
||||
|
||||
+ stringWithCapacity: (unsigned)capacity;
|
||||
- initWithCapacity: (unsigned)capacity;
|
||||
|
||||
/* This from IndexedCollecting: - removeRange: (IndexRange)range; */
|
||||
- (void) insertString: (id <String>)string atIndex: (unsigned)index;
|
||||
|
||||
- (void) setString: (id <String>)string;
|
||||
- (void) appendString: (id <String>)string;
|
||||
- (void) replaceRange: (IndexRange)range withString: (id <String>)string;
|
||||
|
||||
@end
|
||||
|
||||
/* Abstract string classes */
|
||||
|
||||
@interface String : IndexedCollection
|
||||
@end
|
||||
|
||||
/* To prevent complaints about protocol conformance. */
|
||||
@interface String (StringProtocol) <String>
|
||||
@end
|
||||
|
||||
@interface MutableString : String
|
||||
@end
|
||||
|
||||
/* To prevent complaints about protocol conformance. */
|
||||
@interface MutableString (MutableStringProtocol) <MutableString>
|
||||
@end
|
||||
|
||||
/* Some concrete string classes */
|
||||
|
||||
@interface CString : String
|
||||
{
|
||||
char * _contents_chars;
|
||||
int _count;
|
||||
BOOL _free_contents;
|
||||
}
|
||||
@end
|
||||
|
||||
@interface MutableCString : MutableString
|
||||
{
|
||||
char *_contents_chars;
|
||||
int _count;
|
||||
BOOL _free_contents;
|
||||
int _capacity;
|
||||
}
|
||||
@end
|
||||
|
||||
@interface ConstantString : CString
|
||||
@end
|
||||
|
||||
#endif /* __String_h_GNUSTEP_BASE_INCLUDE */
|
126
Source/CString.m
126
Source/CString.m
|
@ -1,126 +0,0 @@
|
|||
/* Implementation for GNU Objective-C CString object
|
||||
Copyright (C) 1993,1994, 1995, 1996 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
||||
Date: July 1994
|
||||
|
||||
This file is part of the GNUstep Base Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <gnustep/base/String.h>
|
||||
#include <gnustep/base/IndexedCollection.h>
|
||||
#include <gnustep/base/IndexedCollectionPrivate.h>
|
||||
#include <Foundation/NSValue.h>
|
||||
/* memcpy(), strlen(), strcmp() are gcc builtin's */
|
||||
|
||||
@implementation CString
|
||||
|
||||
/* These next two methods are the two designated initializers for this class */
|
||||
- initWithCString: (const char*)aCharPtr range: (IndexRange)aRange
|
||||
{
|
||||
_count = aRange.length;
|
||||
OBJC_MALLOC(_contents_chars, char, _count+1);
|
||||
memcpy(_contents_chars, aCharPtr + aRange.location, _count);
|
||||
_contents_chars[_count] = '\0';
|
||||
_free_contents = YES;
|
||||
return self;
|
||||
}
|
||||
|
||||
- initWithCStringNoCopy: (const char*)aCharPtr freeWhenDone: (BOOL)f
|
||||
{
|
||||
_count = strlen(aCharPtr);
|
||||
_contents_chars = (char *) aCharPtr;
|
||||
_free_contents = f;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) _collectionReleaseContents
|
||||
{
|
||||
}
|
||||
|
||||
- (void) _collectionDealloc
|
||||
{
|
||||
if (_free_contents)
|
||||
OBJC_FREE(_contents_chars);
|
||||
}
|
||||
|
||||
- (Class) classForConnectedCoder: aRmc
|
||||
{
|
||||
/* Make sure that Connection's always send us bycopy,
|
||||
i.e. as our own class, not a Proxy class. */
|
||||
return [self class];
|
||||
}
|
||||
|
||||
- (void) encodeWithCoder: aCoder
|
||||
{
|
||||
[aCoder encodeValueOfObjCType: @encode(char*)
|
||||
at: &_contents_chars
|
||||
withName: @"Concrete String content_chars"];
|
||||
}
|
||||
|
||||
- initWithCoder: aCoder
|
||||
{
|
||||
[super initWithCoder:aCoder];
|
||||
[aCoder decodeValueOfObjCType: @encode(char*)
|
||||
at: &_contents_chars
|
||||
withName: NULL];
|
||||
_count = strlen(_contents_chars);
|
||||
_free_contents = YES;
|
||||
return self;
|
||||
}
|
||||
|
||||
/* Empty copy must empty an allocCopy'ed version of self */
|
||||
- emptyCopy
|
||||
{
|
||||
CString *copy = [super emptyCopy];
|
||||
OBJC_MALLOC(copy->_contents_chars, char, _count+1);
|
||||
copy->_count = 0;
|
||||
copy->_contents_chars[0] = '\0';
|
||||
return copy;
|
||||
}
|
||||
|
||||
/* xxx This should return an autoreleased, malloc'ed copy */
|
||||
- (const char *) cString
|
||||
{
|
||||
return _contents_chars;
|
||||
}
|
||||
|
||||
- (const char *) cStringNoCopy
|
||||
{
|
||||
return _contents_chars;
|
||||
}
|
||||
|
||||
- (unsigned) count
|
||||
{
|
||||
return _count;
|
||||
}
|
||||
|
||||
- (char) charAtIndex: (unsigned)index
|
||||
{
|
||||
CHECK_INDEX_RANGE_ERROR(index, _count);
|
||||
return _contents_chars[index];
|
||||
}
|
||||
|
||||
// FOR IndexedCollection SUPPORT;
|
||||
|
||||
- objectAtIndex: (unsigned)index
|
||||
{
|
||||
CHECK_INDEX_RANGE_ERROR(index, _count);
|
||||
return [NSNumber numberWithChar: _contents_chars[index]];
|
||||
}
|
||||
|
||||
@end
|
|
@ -1,205 +0,0 @@
|
|||
/* Implementation for GNU Objective-C MutableCString object
|
||||
Copyright (C) 1995, 1996 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
||||
Created: January 1995
|
||||
|
||||
This file is part of the GNUstep Base Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <gnustep/base/String.h>
|
||||
#include <gnustep/base/IndexedCollection.h>
|
||||
#include <gnustep/base/IndexedCollectionPrivate.h>
|
||||
/* memcpy(), strlen(), strcmp() are gcc builtin's */
|
||||
|
||||
typedef struct {
|
||||
@defs(MutableCString)
|
||||
} MutableCStringStruct;
|
||||
|
||||
static inline void
|
||||
stringIncrementCountAndMakeHoleAt(MutableCStringStruct *self,
|
||||
unsigned index, unsigned size)
|
||||
{
|
||||
#ifndef STABLE_MEMCPY
|
||||
{
|
||||
int i;
|
||||
for (i = self->_count; i >= index; i--)
|
||||
self->_contents_chars[i+size] = self->_contents_chars[i];
|
||||
}
|
||||
#else
|
||||
memcpy(self->_contents_chars + index,
|
||||
self->_contents_chars + index + size,
|
||||
self->_count - index);
|
||||
#endif /* STABLE_MEMCPY */
|
||||
(self->_count) += size;
|
||||
}
|
||||
|
||||
static inline void
|
||||
stringDecrementCountAndFillHoleAt(MutableCStringStruct *self,
|
||||
unsigned index, unsigned size)
|
||||
{
|
||||
(self->_count) -= size;
|
||||
#ifndef STABLE_MEMCPY
|
||||
{
|
||||
int i;
|
||||
for (i = index; i <= self->_count; i++)
|
||||
self->_contents_chars[i] = self->_contents_chars[i+size];
|
||||
}
|
||||
#else
|
||||
memcpy(self->_contents_chars + index + size,
|
||||
self->_contents_chars + index,
|
||||
self->_count - index);
|
||||
#endif /* STABLE_MEMCPY */
|
||||
}
|
||||
|
||||
@implementation MutableCString
|
||||
|
||||
/* This is the designated initializer for this class */
|
||||
- initWithCapacity: (unsigned)capacity
|
||||
{
|
||||
_count = 0;
|
||||
_capacity = capacity;
|
||||
OBJC_MALLOC(_contents_chars, char, _capacity+1);
|
||||
_contents_chars[0] = '\0';
|
||||
_free_contents = YES;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
if (_free_contents)
|
||||
OBJC_FREE(_contents_chars);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
/* xxx This should be made to return void, but we need to change
|
||||
IndexedCollecting and its conformers */
|
||||
- (void) removeRange: (IndexRange)range
|
||||
{
|
||||
stringDecrementCountAndFillHoleAt((MutableCStringStruct*)self,
|
||||
range.location, range.length);
|
||||
}
|
||||
|
||||
- (void) insertString: (String*)string atIndex: (unsigned)index
|
||||
{
|
||||
unsigned c = [string count];
|
||||
if (_count + c >= _capacity)
|
||||
{
|
||||
_capacity = MAX(_capacity*2, _count+c);
|
||||
OBJC_REALLOC(_contents_chars, char, _capacity);
|
||||
}
|
||||
stringIncrementCountAndMakeHoleAt((MutableCStringStruct*)self, index, c);
|
||||
memcpy(_contents_chars + index, [string cString], c);
|
||||
_contents_chars[_count] = '\0';
|
||||
}
|
||||
|
||||
|
||||
- (Class) classForConnectedCoder: aRmc
|
||||
{
|
||||
/* Make sure that Connection's always send us bycopy,
|
||||
i.e. as our own class, not a Proxy class. */
|
||||
return [self class];
|
||||
}
|
||||
|
||||
- (void) encodeWithCoder: aCoder
|
||||
{
|
||||
[aCoder encodeValueOfObjCType:@encode(unsigned) at:&_capacity
|
||||
withName:@"String capacity"];
|
||||
[aCoder encodeValueOfObjCType:@encode(char*) at:&_contents_chars
|
||||
withName:@"String content_chars"];
|
||||
}
|
||||
|
||||
- initWithCoder: aCoder
|
||||
{
|
||||
unsigned cap;
|
||||
|
||||
[aCoder decodeValueOfObjCType:@encode(unsigned) at:&cap withName:NULL];
|
||||
[self initWithCapacity:cap];
|
||||
[aCoder decodeValueOfObjCType:@encode(char*) at:_contents_chars
|
||||
withName:NULL];
|
||||
_count = strlen(_contents_chars);
|
||||
_capacity = cap;
|
||||
_free_contents = YES;
|
||||
return self;
|
||||
}
|
||||
|
||||
/* Empty copy must empty an allocCopy'ed version of self */
|
||||
- emptyCopy
|
||||
{
|
||||
MutableCString *copy = [super emptyCopy];
|
||||
OBJC_MALLOC(copy->_contents_chars, char, _count+1);
|
||||
copy->_count = 0;
|
||||
copy->_contents_chars[0] = '\0';
|
||||
return copy;
|
||||
}
|
||||
|
||||
- (const char *) cString
|
||||
{
|
||||
return _contents_chars;
|
||||
}
|
||||
|
||||
- (unsigned) count
|
||||
{
|
||||
return _count;
|
||||
}
|
||||
|
||||
- (char) charAtIndex: (unsigned)index
|
||||
{
|
||||
CHECK_INDEX_RANGE_ERROR(index, _count);
|
||||
return _contents_chars[index];
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* For IndexedCollecting protocol */
|
||||
|
||||
- insertElement: (elt)newElement atIndex: (unsigned)index
|
||||
{
|
||||
CHECK_INDEX_RANGE_ERROR(index, _count+1);
|
||||
// one for the next char, one for the '\0';
|
||||
if (_count+1 >= _capacity)
|
||||
{
|
||||
_capacity *= 2;
|
||||
OBJC_REALLOC(_contents_chars, char, _capacity);
|
||||
}
|
||||
stringIncrementCountAndMakeHoleAt((MutableCStringStruct*)self, index, 1);
|
||||
_contents_chars[index] = newElement.char_u;
|
||||
_contents_chars[_count] = '\0';
|
||||
return self;
|
||||
}
|
||||
|
||||
- (elt) removeElementAtIndex: (unsigned)index
|
||||
{
|
||||
elt ret;
|
||||
|
||||
CHECK_INDEX_RANGE_ERROR(index, _count);
|
||||
ret = _contents_chars[index];
|
||||
stringDecrementCountAndFillHoleAt((MutableCStringStruct*)self, index, 1);
|
||||
_contents_chars[_count] = '\0';
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (elt) elementAtIndex: (unsigned)index
|
||||
{
|
||||
elt ret_elt;
|
||||
CHECK_INDEX_RANGE_ERROR(index, _count);
|
||||
ret_elt.char_u = _contents_chars[index];
|
||||
return ret_elt;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@end
|
|
@ -1,99 +0,0 @@
|
|||
/* Implementation for GNU Objective-C MutableString object
|
||||
Copyright (C) 1993,1994, 1996 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
||||
Date: July 1994
|
||||
|
||||
This file is part of the GNUstep Base Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <gnustep/base/String.h>
|
||||
#include <gnustep/base/IndexedCollectionPrivate.h>
|
||||
#include <gnustep/base/NSString.h>
|
||||
/* memcpy(), strlen(), strcmp() are gcc builtin's */
|
||||
#include <limits.h>
|
||||
|
||||
@implementation MutableString
|
||||
|
||||
/* This is the designated initializer */
|
||||
- initWithCapacity: (unsigned)capacity
|
||||
{
|
||||
return [self subclassResponsibility:_cmd];
|
||||
}
|
||||
|
||||
+ (MutableString*) stringWithCapacity: (unsigned)capacity
|
||||
{
|
||||
MutableCString *n = [[MutableCString alloc] initWithCapacity:capacity];
|
||||
return [n autorelease];
|
||||
}
|
||||
|
||||
/* Subclasses need to implemented the next two methods */
|
||||
|
||||
- (void) removeRange: (IndexRange)range
|
||||
{
|
||||
[self subclassResponsibility:_cmd];
|
||||
}
|
||||
|
||||
- (void) insertString: (String*)string atIndex: (unsigned)index
|
||||
{
|
||||
[self subclassResponsibility:_cmd];
|
||||
}
|
||||
|
||||
- (void) setString: (String*)string
|
||||
{
|
||||
[self replaceRange:(IndexRange){0,INT_MAX} withString:string];
|
||||
}
|
||||
|
||||
- (void) appendString: (String*)string
|
||||
{
|
||||
[self insertString:string atIndex:[self count]];
|
||||
}
|
||||
|
||||
- (void) replaceRange: (IndexRange)range withString: (String*)string
|
||||
{
|
||||
[self removeRange:range];
|
||||
[self insertString:string atIndex:range.location];
|
||||
}
|
||||
|
||||
// SETTING VALUES;
|
||||
|
||||
- (void) setIntValue: (int)anInt
|
||||
{
|
||||
[self setString:[String stringWithFormat:@"%d", anInt]];
|
||||
}
|
||||
|
||||
- (void) setFloatValue: (float)aFloat
|
||||
{
|
||||
[self setString:[String stringWithFormat:@"%f", aFloat]];
|
||||
}
|
||||
|
||||
- (void) setDoubleValue: (double)aDouble
|
||||
{
|
||||
[self setString:[String stringWithFormat:@"%f", aDouble]];
|
||||
}
|
||||
|
||||
- (void) setCStringValue: (const char *)aCString
|
||||
{
|
||||
[self setString:[String stringWithCString:aCString]];
|
||||
}
|
||||
|
||||
- (void) setStringValue: (String*)aString
|
||||
{
|
||||
[self setString:aString];
|
||||
}
|
||||
|
||||
@end
|
463
Source/String.m
463
Source/String.m
|
@ -1,463 +0,0 @@
|
|||
/* Implementation for Objective-C String object
|
||||
Copyright (C) 1993,1994,1995, 1996 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
||||
Date: May 1993
|
||||
|
||||
This file is part of the GNUstep Base Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <gnustep/base/String.h>
|
||||
#include <gnustep/base/IndexedCollectionPrivate.h>
|
||||
/* memcpy(), strlen(), strcmp() are gcc builtin's */
|
||||
#include <stdarg.h>
|
||||
#include <assert.h>
|
||||
|
||||
/* Deal with strchr: */
|
||||
#if STDC_HEADERS || HAVE_STRING_H
|
||||
#include <string.h>
|
||||
/* An ANSI string.h and pre-ANSI memory.h might conflict. */
|
||||
#if !STDC_HEADERS && HAVE_MEMORY_H
|
||||
#include <memory.h>
|
||||
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
|
||||
#define rindex strrchr
|
||||
#define bcopy(s, d, n) memcpy ((d), (s), (n))
|
||||
#define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
|
||||
#define bzero(s, n) memset ((s), 0, (n))
|
||||
#else /* not STDC_HEADERS and not HAVE_STRING_H */
|
||||
#include <strings.h>
|
||||
/* memory.h and strings.h conflict on some systems. */
|
||||
#endif /* not STDC_HEADERS and not HAVE_STRING_H */
|
||||
|
||||
|
||||
@implementation String
|
||||
|
||||
+ (void) initialize
|
||||
{
|
||||
if (self == [String class])
|
||||
{
|
||||
[self setVersion:0]; /* beta release */
|
||||
/* xxx eventually: class_add_behavior_category(NSStringStuff),
|
||||
but we'll have to be careful about these methods overriding
|
||||
the ones inherited in NSCString from NSString! */
|
||||
}
|
||||
}
|
||||
|
||||
// INITIALIZING;
|
||||
|
||||
/* For now, this is the designated initializer for this class */
|
||||
- initWithCString: (const char*)aCharPtr range: (IndexRange)aRange
|
||||
{
|
||||
[self subclassResponsibility:_cmd];
|
||||
return self;
|
||||
}
|
||||
|
||||
/* xxx This is a second "designated" initializer. */
|
||||
- initWithCStringNoCopy: (const char*) aCharPtr
|
||||
freeWhenDone: (BOOL) f
|
||||
{
|
||||
[self subclassResponsibility:_cmd];
|
||||
return self;
|
||||
}
|
||||
|
||||
/* This override in mutable string classes */
|
||||
- (void) empty
|
||||
{
|
||||
[self subclassResponsibility:_cmd];
|
||||
}
|
||||
|
||||
- initWithCString: (const char*)aCharPtr length: (unsigned)aLength
|
||||
{
|
||||
return [self initWithCString:aCharPtr
|
||||
range:((IndexRange){0,aLength})];
|
||||
}
|
||||
|
||||
- initWithCString: (const char*)aCharPtr
|
||||
{
|
||||
return [self initWithCString:aCharPtr
|
||||
range:((IndexRange){0, strlen(aCharPtr)})];
|
||||
}
|
||||
|
||||
#if HAVE_VSPRINTF
|
||||
- initWithFormat: (id <String>)aFormatString arguments: (va_list)arg
|
||||
{
|
||||
char buf[128]; /* xxx horrible, disgusting, fix this */
|
||||
int len;
|
||||
len = VSPRINTF_LENGTH (vsprintf(buf, [aFormatString cString], arg));
|
||||
assert (len < 128);
|
||||
return [self initWithCString:buf];
|
||||
}
|
||||
|
||||
- initWithCFormat: (const char*)formatCharPtr arguments: (va_list)arg
|
||||
{
|
||||
char buf[128]; /* xxx horrible, disgusting, fix this */
|
||||
int len;
|
||||
len = VSPRINTF_LENGTH (vsprintf(buf, formatCharPtr, arg));
|
||||
assert (len < 128);
|
||||
return [self initWithCString:buf];
|
||||
}
|
||||
#endif /* HAVE_VSPRINTF */
|
||||
|
||||
- initWithFormat: (id <String>)aFormatString, ...
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, aFormatString);
|
||||
[self initWithCFormat:[aFormatString cString] arguments:ap];
|
||||
va_end(ap);
|
||||
return self;
|
||||
}
|
||||
|
||||
- initWithCFormat: (const char*)formatCharPtr, ...
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, formatCharPtr);
|
||||
[self initWithCFormat:formatCharPtr arguments:ap];
|
||||
va_end(ap);
|
||||
return self;
|
||||
}
|
||||
|
||||
- init
|
||||
{
|
||||
return [self initWithCString:""];
|
||||
}
|
||||
|
||||
- initWithString: (id <String>)aString range: (IndexRange)aRange
|
||||
{
|
||||
return [self initWithCString:[aString cString] range:aRange];
|
||||
}
|
||||
|
||||
- initWithString: (id <String>)aString length: (unsigned)aLength
|
||||
{
|
||||
return [self initWithCString:[aString cString]];
|
||||
}
|
||||
|
||||
- initWithString: (id <String>)aString
|
||||
{
|
||||
return [self initWithCString:[aString cString]];
|
||||
}
|
||||
|
||||
|
||||
// GETTING NEW, AUTORELEASED STRING OBJECTS, NO NEED TO RELEASE THESE;
|
||||
|
||||
+ stringWithString: (id <String>)aString range: (IndexRange)aRange
|
||||
{
|
||||
return [[[CString alloc] initWithString:aString range:aRange]
|
||||
autorelease];
|
||||
}
|
||||
|
||||
+ stringWithString: (id <String>)aString
|
||||
{
|
||||
return [[[CString alloc] initWithString:aString]
|
||||
autorelease];
|
||||
}
|
||||
|
||||
+ stringWithFormat: (id <String>)aFormatString, ...
|
||||
{
|
||||
va_list ap;
|
||||
id ret;
|
||||
|
||||
va_start(ap, aFormatString);
|
||||
ret = [[self stringWithFormat:aFormatString arguments:ap]
|
||||
autorelease];
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
+ stringWithFormat: (id <String>)aFormatString arguments: (va_list)arg
|
||||
{
|
||||
return [[[CString alloc] initWithFormat:aFormatString arguments:arg]
|
||||
autorelease];
|
||||
}
|
||||
|
||||
+ stringWithCString: (const char*)cp range: (IndexRange)r
|
||||
{
|
||||
return [[[CString alloc] initWithCString:cp range:r]
|
||||
autorelease];
|
||||
}
|
||||
|
||||
+ stringWithCString: (const char*)aCharPtr
|
||||
{
|
||||
return [[[CString alloc] initWithCString:aCharPtr]
|
||||
autorelease];
|
||||
}
|
||||
|
||||
+ stringWithCStringNoCopy: (const char*)aCharPtr
|
||||
freeWhenDone: (BOOL) f
|
||||
{
|
||||
return [[[CString alloc]
|
||||
initWithCStringNoCopy:aCharPtr
|
||||
freeWhenDone: f]
|
||||
autorelease];
|
||||
}
|
||||
|
||||
+ stringWithCStringNoCopy: (const char*)aCharPtr
|
||||
{
|
||||
return [self stringWithCStringNoCopy:aCharPtr
|
||||
freeWhenDone: YES];
|
||||
}
|
||||
|
||||
- stringByAppendingFormat: (id <String>)aString, ...
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- stringByAppendingFormat: (id <String>)aString arguments: (va_list)arg
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- stringByPrependingFormat: (id <String>)aString, ...
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- stringByPrependingFormat: (id <String>)aString arguments: (va_list)arg
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- stringByAppendingString: (id <String>)aString
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- stringByPrependingString: (id <String>)aString
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
// COPYING;
|
||||
|
||||
/* This is the designated copier */
|
||||
- (char *) cStringCopyRange: (IndexRange)aRange;
|
||||
{
|
||||
[self subclassResponsibility:_cmd];
|
||||
return "";
|
||||
}
|
||||
|
||||
- (char *) cStringCopyLength: (unsigned)aLength
|
||||
{
|
||||
// xxx need to check aLength against _count;
|
||||
return [self cStringCopyRange:((IndexRange){0,aLength})];
|
||||
}
|
||||
|
||||
/* xxx No longer necessary because -cString does the same thing? */
|
||||
- (char *) cStringCopy
|
||||
{
|
||||
return [self cStringCopyRange:((IndexRange){0, [self count]})];
|
||||
}
|
||||
|
||||
- copyWithZone: (NSZone*)z
|
||||
{
|
||||
return [[[self class] allocWithZone:z] initWithString:(NSString*)self];
|
||||
}
|
||||
|
||||
- mutableCopyWithZone: (NSZone*)z
|
||||
{
|
||||
return [[MutableCString allocWithZone:z] initWithString:(NSString*)self];
|
||||
}
|
||||
|
||||
// TESTING;
|
||||
|
||||
- (unsigned) hash
|
||||
{
|
||||
unsigned ret = 0;
|
||||
unsigned ctr = 0;
|
||||
unsigned char_count = 0;
|
||||
const char *s = [self cStringNoCopy];
|
||||
|
||||
while (*s && char_count++ < NSHashStringLength)
|
||||
{
|
||||
ret ^= *s++ << ctr;
|
||||
ctr = (ctr + 1) % sizeof (void*);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (int) compare: anObject
|
||||
{
|
||||
if ([anObject isKindOfClass:[String class]])
|
||||
return strcmp([self cString], [anObject cString]);
|
||||
return [super compare:anObject];
|
||||
}
|
||||
|
||||
- (BOOL) isEqual: anObject
|
||||
{
|
||||
if (self == anObject)
|
||||
return YES;
|
||||
if (! [anObject isKindOf:[String class]]
|
||||
|| [self count] != [anObject count]
|
||||
|| [anObject hash] != [self hash] )
|
||||
return NO;
|
||||
return ! [self compare:anObject];
|
||||
}
|
||||
|
||||
- (unsigned) count
|
||||
{
|
||||
/* Should be overridden for efficiency. */
|
||||
return strlen([self cString]);
|
||||
}
|
||||
|
||||
- (unsigned) length
|
||||
{
|
||||
return [self count];
|
||||
}
|
||||
|
||||
- (IndexRange) stringRange
|
||||
{
|
||||
IndexRange r = {0, [self count]};
|
||||
return r;
|
||||
}
|
||||
|
||||
/* xxx These next three need error checking to handle the case in which
|
||||
we're looking for never appears */
|
||||
|
||||
- (unsigned) indexOfChar: (char)aChar
|
||||
{
|
||||
const char *s = [self cString];
|
||||
return (strchr(s, aChar) - s);
|
||||
}
|
||||
|
||||
- (unsigned) indexOfLastChar: (char)aChar
|
||||
{
|
||||
const char *s = [self cString];
|
||||
return (strrchr(s, aChar) - s);
|
||||
}
|
||||
|
||||
- (unsigned) indexOfString: (id <String>)aString
|
||||
{
|
||||
const char *s = [self cString];
|
||||
return (strstr(s, [aString cString]) - s);
|
||||
}
|
||||
|
||||
- (char) charAtIndex: (unsigned)index
|
||||
{
|
||||
[self subclassResponsibility:_cmd];
|
||||
return ' ';
|
||||
}
|
||||
|
||||
- (const char *) cString
|
||||
{
|
||||
[self subclassResponsibility:_cmd];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
- (const char *) cStringNoCopy
|
||||
{
|
||||
[self subclassResponsibility:_cmd];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
- (unsigned) cStringLength
|
||||
{
|
||||
[self subclassResponsibility:_cmd];
|
||||
return 0;
|
||||
}
|
||||
|
||||
- (void) getCString: (char*)buffer
|
||||
{
|
||||
strcpy(buffer, [self cString]);
|
||||
}
|
||||
|
||||
- (void) getCString: (char*)buffer range: (IndexRange)aRange
|
||||
{
|
||||
memcpy(buffer, ([self cString] + aRange.location), aRange.length);
|
||||
}
|
||||
|
||||
- (IndexRange) range
|
||||
{
|
||||
return (IndexRange){0, [self count]};
|
||||
}
|
||||
|
||||
// GETTING VALUES;
|
||||
|
||||
- (int) intValue
|
||||
{
|
||||
return atoi([self cString]);
|
||||
}
|
||||
|
||||
- (float) floatValue
|
||||
{
|
||||
return (float) atof([self cString]);
|
||||
}
|
||||
|
||||
- (double) doubleValue
|
||||
{
|
||||
return atof([self cString]);
|
||||
}
|
||||
|
||||
- (const char *) cStringValue
|
||||
{
|
||||
return [self cString];
|
||||
}
|
||||
|
||||
- (String *) stringValue
|
||||
{
|
||||
return self;
|
||||
}
|
||||
|
||||
// FOR FILE AND PATH NAMES;
|
||||
|
||||
- (IndexRange) fileRange
|
||||
{
|
||||
IndexRange r;
|
||||
[self notImplemented:_cmd];
|
||||
return r;
|
||||
}
|
||||
|
||||
- (IndexRange) pathRange
|
||||
{
|
||||
IndexRange r;
|
||||
[self notImplemented:_cmd];
|
||||
return r;
|
||||
}
|
||||
|
||||
- (IndexRange) extensionRange
|
||||
{
|
||||
IndexRange r;
|
||||
[self notImplemented:_cmd];
|
||||
return r;
|
||||
}
|
||||
|
||||
- (IndexRange) fileWithOutExtentionRange
|
||||
{
|
||||
IndexRange r;
|
||||
[self notImplemented:_cmd];
|
||||
return r;
|
||||
}
|
||||
|
||||
- (BOOL) isAbsolute
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL) isRelative
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return NO;
|
||||
}
|
||||
|
||||
|
||||
@end
|
Loading…
Reference in a new issue