1995-03-12 19:54:14 +00:00
|
|
|
/* Implementation of GNUSTEP string class
|
1996-01-19 16:12:12 +00:00
|
|
|
Copyright (C) 1995, 1996 Free Software Foundation, Inc.
|
1995-03-12 19:54:14 +00:00
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1995-03-12 19:54:14 +00:00
|
|
|
Date: January 1995
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1995-03-12 19:54:14 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
/* Caveats:
|
|
|
|
|
|
|
|
Many method unimplemented.
|
|
|
|
|
|
|
|
Only supports C Strings. Some implementations will need to be
|
|
|
|
changed when we get other string backing classes.
|
|
|
|
|
1996-09-02 13:20:20 +00:00
|
|
|
Does not support all justification directives for `%@' in format strings
|
|
|
|
on non-GNU-libc systems.
|
1995-04-03 01:35:42 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
1996-04-17 15:34:35 +00:00
|
|
|
#include <gnustep/base/preface.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
#include <gnustep/base/Coding.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/NSString.h>
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSCharacterSet.h>
|
1996-03-26 19:35:47 +00:00
|
|
|
#include <Foundation/NSException.h>
|
1996-02-22 15:18:57 +00:00
|
|
|
#include <Foundation/NSValue.h>
|
1996-10-31 17:03:44 +00:00
|
|
|
#include <Foundation/NSDictionary.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
#include <gnustep/base/IndexedCollection.h>
|
|
|
|
#include <gnustep/base/IndexedCollectionPrivate.h>
|
|
|
|
#include <gnustep/base/behavior.h>
|
1995-04-03 01:35:42 +00:00
|
|
|
#include <limits.h>
|
1996-07-15 18:41:44 +00:00
|
|
|
#include <string.h> // for strstr()
|
1996-10-31 17:03:44 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
1996-10-31 21:24:43 +00:00
|
|
|
/* xxx Temporarily set HAVE_REGISTER_PRINTF_FUNCTION function to 0
|
|
|
|
because I can't seem to figure out which versions of libc pass a
|
|
|
|
`va_list' to the output handler, and which pass a `void**' to the
|
|
|
|
output handler. Once I figure this out, these lines should be
|
|
|
|
removed. */
|
|
|
|
#undef HAVE_REGISTER_PRINTF_FUNCTION
|
|
|
|
#define HAVE_REGISTER_PRINTF_FUNCTION 0
|
|
|
|
|
1995-03-12 19:54:14 +00:00
|
|
|
|
|
|
|
@implementation NSString
|
1995-04-03 01:35:42 +00:00
|
|
|
|
1995-08-09 16:07:19 +00:00
|
|
|
/* For unichar strings. (Not implemented---using cStrings) */
|
|
|
|
static Class NSString_concrete_class;
|
|
|
|
static Class NSMutableString_concrete_class;
|
|
|
|
|
|
|
|
/* For CString's */
|
|
|
|
static Class NSString_c_concrete_class;
|
|
|
|
static Class NSMutableString_c_concrete_class;
|
|
|
|
|
|
|
|
+ (void) _setConcreteClass: (Class)c
|
|
|
|
{
|
|
|
|
NSString_concrete_class = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) _setConcreteCClass: (Class)c
|
|
|
|
{
|
|
|
|
NSString_c_concrete_class = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) _setMutableConcreteClass: (Class)c
|
|
|
|
{
|
|
|
|
NSMutableString_concrete_class = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) _setMutableConcreteCClass: (Class)c
|
|
|
|
{
|
|
|
|
NSMutableString_c_concrete_class = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (Class) _concreteClass
|
|
|
|
{
|
|
|
|
return NSString_concrete_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (Class) _concreteCClass
|
|
|
|
{
|
|
|
|
return NSString_c_concrete_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (Class) _mutableConcreteClass
|
|
|
|
{
|
|
|
|
return NSMutableString_concrete_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (Class) _mutableConcreteCClass
|
|
|
|
{
|
|
|
|
return NSMutableString_c_concrete_class;
|
|
|
|
}
|
|
|
|
|
1996-01-23 23:30:05 +00:00
|
|
|
#if HAVE_REGISTER_PRINTF_FUNCTION
|
1996-01-23 23:41:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <printf.h>
|
|
|
|
#include <stdarg.h>
|
1996-05-06 18:59:01 +00:00
|
|
|
|
1996-07-15 18:41:44 +00:00
|
|
|
/* <sattler@volker.cs.Uni-Magdeburg.DE>, with libc-5.3.9 thinks this
|
|
|
|
flag PRINTF_ATSIGN_VA_LIST should be 0, but for me, with libc-5.0.9,
|
|
|
|
it crashes. -mccallum */
|
|
|
|
#define PRINTF_ATSIGN_VA_LIST \
|
|
|
|
(defined(_LINUX_C_LIB_VERSION_MINOR) \
|
|
|
|
&& _LINUX_C_LIB_VERSION_MAJOR <= 5 \
|
|
|
|
&& _LINUX_C_LIB_VERSION_MINOR < 2)
|
1996-05-07 01:14:54 +00:00
|
|
|
|
|
|
|
#if ! PRINTF_ATSIGN_VA_LIST
|
1996-05-06 18:59:01 +00:00
|
|
|
static int
|
|
|
|
arginfo_func (const struct printf_info *info,
|
|
|
|
size_t n,
|
|
|
|
int *argtypes) {
|
|
|
|
*argtypes = PA_POINTER;
|
|
|
|
return 1;
|
|
|
|
}
|
1996-07-15 18:41:44 +00:00
|
|
|
#endif /* !PRINTF_ATSIGN_VA_LIST */
|
1996-05-06 18:59:01 +00:00
|
|
|
|
|
|
|
static int
|
1996-01-23 23:41:02 +00:00
|
|
|
handle_printf_atsign (FILE *stream,
|
|
|
|
const struct printf_info *info,
|
1996-05-07 01:14:54 +00:00
|
|
|
#if PRINTF_ATSIGN_VA_LIST
|
|
|
|
va_list *ap_pointer)
|
|
|
|
#else
|
|
|
|
const void **const args)
|
|
|
|
#endif
|
1996-01-23 23:30:05 +00:00
|
|
|
{
|
1996-05-07 01:14:54 +00:00
|
|
|
#if ! PRINTF_ATSIGN_VA_LIST
|
1996-05-06 18:59:01 +00:00
|
|
|
const void *ptr = *args;
|
1996-05-07 01:14:54 +00:00
|
|
|
#endif
|
1996-01-23 23:30:05 +00:00
|
|
|
id string_object;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* xxx This implementation may not pay pay attention to as much
|
|
|
|
of printf_info as it should. */
|
|
|
|
|
1996-05-07 01:14:54 +00:00
|
|
|
#if PRINTF_ATSIGN_VA_LIST
|
|
|
|
string_object = va_arg (*ap_pointer, id);
|
|
|
|
#else
|
1996-07-15 18:41:44 +00:00
|
|
|
string_object = *((id*) ptr);
|
1996-05-07 01:14:54 +00:00
|
|
|
#endif
|
1996-01-23 23:30:05 +00:00
|
|
|
len = fprintf(stream, "%*s",
|
|
|
|
(info->left ? - info->width : info->width),
|
|
|
|
[string_object cStringNoCopy]);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_REGISTER_PRINTF_FUNCTION */
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
1996-11-24 17:20:45 +00:00
|
|
|
if (self == [NSString class])
|
1995-04-03 01:35:42 +00:00
|
|
|
{
|
1995-08-09 16:07:19 +00:00
|
|
|
NSString_concrete_class = [NSGCString class];
|
|
|
|
NSString_c_concrete_class = [NSGCString class];
|
|
|
|
NSMutableString_concrete_class = [NSGMutableCString class];
|
|
|
|
NSMutableString_c_concrete_class = [NSGMutableCString class];
|
1996-01-23 23:30:05 +00:00
|
|
|
|
|
|
|
#if HAVE_REGISTER_PRINTF_FUNCTION
|
1996-01-23 23:41:02 +00:00
|
|
|
if (register_printf_function ('@',
|
|
|
|
(printf_function)handle_printf_atsign,
|
1996-05-07 01:14:54 +00:00
|
|
|
#if PRINTF_ATSIGN_VA_LIST
|
|
|
|
0))
|
|
|
|
#else
|
|
|
|
(printf_arginfo_function)arginfo_func))
|
|
|
|
#endif
|
1996-03-26 19:35:47 +00:00
|
|
|
[NSException raise: NSGenericException
|
|
|
|
format: @"register printf handling of %%@ failed"];
|
1996-01-23 23:30:05 +00:00
|
|
|
#endif /* HAVE_REGISTER_PRINTF_FUNCTION */
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-11-19 20:29:39 +00:00
|
|
|
+ allocWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
return NSAllocateObject([self _concreteClass], 0, z);
|
|
|
|
}
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
// Creating Temporary Strings
|
|
|
|
|
|
|
|
+ (NSString*) localizedStringWithFormat: (NSString*) format, ...
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString*) stringWithCString: (const char*) byteString
|
|
|
|
{
|
1995-11-19 20:29:39 +00:00
|
|
|
return [[[self alloc] initWithCString:byteString]
|
1995-04-03 01:35:42 +00:00
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString*) stringWithCString: (const char*)byteString
|
|
|
|
length: (unsigned int)length
|
|
|
|
{
|
1995-11-19 20:29:39 +00:00
|
|
|
return [[[self alloc]
|
1995-08-09 16:07:19 +00:00
|
|
|
initWithCString:byteString length:length]
|
1995-04-03 01:35:42 +00:00
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString*) stringWithCharacters: (const unichar*)chars
|
|
|
|
length: (unsigned int)length
|
|
|
|
{
|
1996-09-02 19:31:44 +00:00
|
|
|
[self notImplemented:_cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString*) stringWithFormat: (NSString*)format,...
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
id ret;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
1995-11-19 20:29:39 +00:00
|
|
|
ret = [[[self alloc] initWithFormat:format arguments:ap]
|
1995-04-03 01:35:42 +00:00
|
|
|
autorelease];
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString*) stringWithFormat: (NSString*)format
|
|
|
|
arguments: (va_list)argList
|
|
|
|
{
|
1995-11-19 20:29:39 +00:00
|
|
|
return [[[self alloc]
|
1995-08-09 16:07:19 +00:00
|
|
|
initWithFormat:format arguments:argList]
|
1995-04-03 01:35:42 +00:00
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initializing Newly Allocated Strings
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
return [self initWithCString:""];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCString: (const char*)byteString
|
|
|
|
{
|
1996-01-19 16:12:12 +00:00
|
|
|
return [self initWithCString:byteString
|
|
|
|
length:(byteString ? strlen(byteString) : 0)];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCString: (const char*)byteString
|
|
|
|
length: (unsigned int)length
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
OBJC_MALLOC(s, char, length+1);
|
1996-01-19 16:12:12 +00:00
|
|
|
if (byteString)
|
|
|
|
memcpy(s, byteString, length);
|
1995-04-03 01:35:42 +00:00
|
|
|
s[length] = '\0';
|
1995-10-18 16:47:59 +00:00
|
|
|
return [self initWithCStringNoCopy:s length:length freeWhenDone:YES];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the designated initializer for CStrings. */
|
|
|
|
- (id) initWithCStringNoCopy: (char*)byteString
|
|
|
|
length: (unsigned int)length
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
{
|
1995-08-09 16:26:44 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCharacters: (const unichar*)chars
|
|
|
|
length: (unsigned int)length
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the designated initializer for unichar Strings. */
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
|
|
|
length: (unsigned int)length
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
{
|
1995-08-09 16:26:44 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithContentsOfFile: (NSString*)path
|
|
|
|
{
|
1996-10-31 17:03:44 +00:00
|
|
|
struct stat buf;
|
|
|
|
int fd;
|
|
|
|
char *s;
|
|
|
|
|
1996-11-24 17:20:45 +00:00
|
|
|
stat ([path cString], &buf);
|
1996-10-31 17:03:44 +00:00
|
|
|
|
1996-11-24 17:20:45 +00:00
|
|
|
OBJC_MALLOC (s, char, buf.st_size + 1);
|
|
|
|
fd = open ([path cString], O_RDONLY);
|
1996-10-31 17:03:44 +00:00
|
|
|
if ( fd < 0 )
|
1996-11-24 17:20:45 +00:00
|
|
|
[NSException raise: NSGenericException
|
|
|
|
format: @"Could not open file %s", [path cString]];
|
1996-10-31 17:03:44 +00:00
|
|
|
|
1996-11-24 17:20:45 +00:00
|
|
|
read (fd,(void *)s,buf.st_size);
|
1996-10-31 17:03:44 +00:00
|
|
|
s[buf.st_size] = (char)0;
|
|
|
|
|
1996-11-24 17:20:45 +00:00
|
|
|
return [self initWithCStringNoCopy:s length: buf.st_size freeWhenDone: YES];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithData: (NSData*)data
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithFormat: (NSString*)format,...
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
self = [self initWithFormat:format arguments:ap];
|
|
|
|
va_end(ap);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* xxx Change this when we have non-CString classes */
|
|
|
|
- (id) initWithFormat: (NSString*)format
|
1996-07-15 18:41:44 +00:00
|
|
|
arguments: (va_list)arg_list
|
1995-04-03 01:35:42 +00:00
|
|
|
{
|
|
|
|
#if HAVE_VSPRINTF
|
1996-05-31 16:12:24 +00:00
|
|
|
const char *format_cp = [format cStringNoCopy];
|
1996-05-31 15:10:53 +00:00
|
|
|
int format_len = strlen (format_cp);
|
1996-07-15 18:41:44 +00:00
|
|
|
/* xxx horrible disgusting BUFFER_EXTRA arbitrary limit; fix this! */
|
|
|
|
#define BUFFER_EXTRA 1024
|
|
|
|
char buf[format_len + BUFFER_EXTRA];
|
|
|
|
int printed_len = 0;
|
|
|
|
|
|
|
|
#if ! HAVE_REGISTER_PRINTF_FUNCTION
|
|
|
|
/* If the available libc doesn't have `register_printf_function()', then
|
|
|
|
the `%@' printf directive isn't available with printf() and friends.
|
|
|
|
Here we make a feable attempt to handle it. */
|
|
|
|
{
|
|
|
|
/* We need a local copy since we change it. (Changing and undoing
|
|
|
|
the change doesn't work because some format strings are constant
|
|
|
|
strings, placed in a non-writable section of the executable, and
|
1996-09-02 13:20:20 +00:00
|
|
|
writing to them will cause a segfault.) */
|
1996-07-15 18:41:44 +00:00
|
|
|
char format_cp_copy[format_len+1];
|
1996-09-25 13:45:31 +00:00
|
|
|
char *atsign_pos; /* points to a location inside format_cp_copy */
|
1996-07-15 18:41:44 +00:00
|
|
|
char *format_to_go = format_cp_copy;
|
|
|
|
strcpy (format_cp_copy, format_cp);
|
|
|
|
/* Loop once for each `%@' in the format string. */
|
|
|
|
while ((atsign_pos = strstr (format_to_go, "%@")))
|
|
|
|
{
|
|
|
|
const char *cstring;
|
|
|
|
/* If there is a "%%@", then do the right thing: print it literally. */
|
|
|
|
if ((*(atsign_pos-1) == '%')
|
|
|
|
&& atsign_pos != format_cp_copy)
|
|
|
|
continue;
|
|
|
|
/* Temporarily terminate the string before the `%@'. */
|
|
|
|
*atsign_pos = '\0';
|
|
|
|
/* Print the part before the '%@' */
|
1996-10-31 18:27:04 +00:00
|
|
|
printed_len += VSPRINTF_LENGTH (vsprintf (buf+printed_len,
|
|
|
|
format_to_go, arg_list));
|
1996-07-15 18:41:44 +00:00
|
|
|
/* Get a C-string (char*) from the String object, and print it. */
|
|
|
|
cstring = [(id) va_arg (arg_list, id) cStringNoCopy];
|
|
|
|
strcat (buf+printed_len, cstring);
|
|
|
|
printed_len += strlen (cstring);
|
|
|
|
/* Skip over this `%@', and look for another one. */
|
|
|
|
format_to_go = atsign_pos + 2;
|
|
|
|
}
|
|
|
|
/* Print the rest of the string after the last `%@'. */
|
1996-10-31 18:27:04 +00:00
|
|
|
printed_len += VSPRINTF_LENGTH (vsprintf (buf+printed_len,
|
|
|
|
format_to_go, arg_list));
|
1996-07-15 18:41:44 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* The available libc has `register_printf_function()', so the `%@'
|
|
|
|
printf directive is handled by printf and friends. */
|
1996-10-31 18:27:04 +00:00
|
|
|
printed_len = VSPRINTF_LENGTH (vsprintf (buf, format_cp, arg_list));
|
1996-07-15 18:41:44 +00:00
|
|
|
#endif /* !HAVE_REGISTER_PRINTF_FUNCTION */
|
1996-05-31 15:10:53 +00:00
|
|
|
|
1996-07-15 18:41:44 +00:00
|
|
|
/* Raise an exception if we overran our buffer. */
|
1996-05-31 15:10:53 +00:00
|
|
|
NSParameterAssert (printed_len < format_len + BUFFER_EXTRA - 1);
|
1995-04-03 01:35:42 +00:00
|
|
|
return [self initWithCString:buf];
|
1996-07-15 18:41:44 +00:00
|
|
|
#else /* HAVE_VSPRINTF */
|
|
|
|
[self notImplemented: _cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
return self;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithFormat: (NSString*)format
|
|
|
|
locale: (NSDictionary*)dictionary
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithFormat: (NSString*)format
|
|
|
|
locale: (NSDictionary*)dictionary
|
|
|
|
arguments: (va_list)argList
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* xxx Change this when we have non-CString classes */
|
|
|
|
- (id) initWithString: (NSString*)string
|
|
|
|
{
|
|
|
|
return [self initWithCString:[string _cStringContents]];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Getting a String's Length
|
|
|
|
|
|
|
|
/* xxx Change this when we have non-CString classes */
|
|
|
|
- (unsigned int) length
|
|
|
|
{
|
|
|
|
return [self cStringLength];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Accessing Characters
|
|
|
|
|
|
|
|
/* xxx Change this when we have non-CString classes */
|
|
|
|
- (unichar) characterAtIndex: (unsigned int)index
|
|
|
|
{
|
|
|
|
/* xxx raise NSException instead of assert. */
|
|
|
|
assert(index < [self cStringLength]);
|
|
|
|
return (unichar) [self _cStringContents][index];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inefficient. Should be overridden */
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
|
|
|
{
|
|
|
|
[self getCharacters:buffer range:((NSRange){0,[self length]})];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inefficient. Should be overridden */
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
|
|
|
range: (NSRange)aRange
|
|
|
|
{
|
|
|
|
int i;
|
1996-09-02 13:38:19 +00:00
|
|
|
for (i = 0; i < aRange.length; i++)
|
1995-04-03 01:35:42 +00:00
|
|
|
{
|
1996-09-02 13:38:19 +00:00
|
|
|
buffer[i] = [self characterAtIndex: aRange.location+i];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Combining Strings
|
|
|
|
|
|
|
|
- (NSString*) stringByAppendingFormat: (NSString*)format,...
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
id ret;
|
|
|
|
va_start(ap, format);
|
|
|
|
ret = [self stringByAppendingString:
|
|
|
|
[NSString stringWithFormat:format arguments:ap]];
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* xxx Change this when we have non-CString classes */
|
|
|
|
- (NSString*) stringByAppendingString: (NSString*)aString
|
|
|
|
{
|
|
|
|
unsigned len = [self cStringLength];
|
|
|
|
char *s = alloca(len + [aString cStringLength] + 1);
|
|
|
|
s = strcpy(s, [self _cStringContents]);
|
|
|
|
strcpy(s + len, [aString _cStringContents]);
|
|
|
|
return [NSString stringWithCString:s];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Dividing Strings into Substrings
|
|
|
|
|
|
|
|
- (NSArray*) componentsSeparatedByString: (NSString*)separator
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange search;
|
|
|
|
NSRange found;
|
1996-03-19 01:44:01 +00:00
|
|
|
NSMutableArray *array = [NSMutableArray array];
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
|
1996-03-19 01:44:01 +00:00
|
|
|
search = NSMakeRange (0, [self length]);
|
|
|
|
found = [self rangeOfString: separator];
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
while (found.length)
|
|
|
|
{
|
|
|
|
NSRange current;
|
1996-03-19 01:44:01 +00:00
|
|
|
current = NSMakeRange (search.location,
|
|
|
|
found.location - search.location);
|
|
|
|
[array addObject: [self substringFromRange: current]];
|
|
|
|
search = NSMakeRange (found.location+1,
|
|
|
|
search.length - found.location);
|
|
|
|
found = [self rangeOfString: separator
|
|
|
|
options: 0
|
|
|
|
range: search];
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Need to make mutable array into non-mutable array?
|
|
|
|
return array;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) substringFromIndex: (unsigned int)index
|
|
|
|
{
|
|
|
|
return [self substringFromRange:((NSRange){index, [self length]-index})];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) substringFromRange: (NSRange)aRange
|
|
|
|
{
|
1996-09-02 19:31:44 +00:00
|
|
|
char buffer[aRange.length];
|
1996-09-02 15:53:47 +00:00
|
|
|
int count = [self length];
|
1996-09-02 13:43:45 +00:00
|
|
|
|
1996-09-02 15:53:47 +00:00
|
|
|
if (aRange.location > count)
|
1996-09-02 13:43:45 +00:00
|
|
|
[NSException raise: NSRangeException format: @"Invalid location."];
|
1996-09-02 15:53:47 +00:00
|
|
|
if (aRange.length > (count - aRange.location))
|
1996-09-02 13:43:45 +00:00
|
|
|
[NSException raise: NSRangeException format: @"Invalid location+length."];
|
1996-09-02 19:31:44 +00:00
|
|
|
/* This will only DTRT for CString's... but that's all we have right now. */
|
|
|
|
[self getCString: buffer
|
|
|
|
maxLength: aRange.length
|
|
|
|
range: aRange
|
|
|
|
remainingRange: NULL];
|
|
|
|
return [[self class] stringWithCString: buffer length: aRange.length];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) substringToIndex: (unsigned int)index
|
|
|
|
{
|
|
|
|
return [self substringFromRange:((NSRange){0,index+1})];;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Finding Ranges of Characters and Substrings
|
|
|
|
|
|
|
|
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange all = NSMakeRange(0, [self length]);
|
|
|
|
return [self rangeOfCharacterFromSet:aSet
|
|
|
|
options:0
|
|
|
|
range:all];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
|
|
|
options: (unsigned int)mask
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange all = NSMakeRange(0, [self length]);
|
|
|
|
return [self rangeOfCharacterFromSet:aSet
|
|
|
|
options:mask
|
|
|
|
range:all];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
/* FIXME: how do you do a case insensitive search? what's an anchored
|
|
|
|
search? what's a literal search? */
|
1995-04-03 01:35:42 +00:00
|
|
|
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
|
|
|
options: (unsigned int)mask
|
|
|
|
range: (NSRange)aRange
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
int i, start, stop, step;
|
|
|
|
NSRange range;
|
|
|
|
|
|
|
|
/* xxx check to make sure aRange is within self; raise NSStringBoundsError */
|
|
|
|
assert(NSMaxRange(aRange) < [self length]);
|
|
|
|
|
|
|
|
if ((mask & NSBackwardsSearch) == NSBackwardsSearch)
|
|
|
|
{
|
|
|
|
start = NSMaxRange(aRange); stop = aRange.location; step = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start = aRange.location; stop = NSMaxRange(aRange); step = 1;
|
|
|
|
}
|
|
|
|
range.length = 0;
|
|
|
|
for (i = start; i < stop; i+=step)
|
|
|
|
{
|
|
|
|
unichar letter = [self characterAtIndex:i];
|
|
|
|
if ([aSet characterIsMember:letter])
|
|
|
|
{
|
|
|
|
range = NSMakeRange(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return range;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRange) rangeOfString: (NSString*)string
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange all = NSMakeRange(0, [self length]);
|
|
|
|
return [self rangeOfString:string
|
|
|
|
options:0
|
|
|
|
range:all];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRange) rangeOfString: (NSString*)string
|
|
|
|
options: (unsigned int)mask
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange all = NSMakeRange(0, [self length]);
|
|
|
|
return [self rangeOfString:string
|
|
|
|
options:mask
|
|
|
|
range:all];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
1996-09-02 13:38:19 +00:00
|
|
|
- (NSRange) rangeOfString:(NSString *) aString
|
|
|
|
options:(unsigned int) mask
|
|
|
|
range:(NSRange) aRange
|
1995-04-03 01:35:42 +00:00
|
|
|
{
|
1996-09-02 13:38:19 +00:00
|
|
|
int stepDirection;
|
|
|
|
unsigned int myIndex, myLength, myEndIndex;
|
|
|
|
unsigned int strLength;
|
|
|
|
unichar strFirstCharacter;
|
|
|
|
|
|
|
|
/* Check that the search range is reasonable */
|
|
|
|
myLength = [self length];
|
|
|
|
if (aRange.location > myLength)
|
|
|
|
[NSException raise: NSRangeException format:@"Invalid location."];
|
|
|
|
if (aRange.length > (myLength - aRange.location))
|
|
|
|
[NSException raise: NSRangeException format:@"Invalid location+length."];
|
|
|
|
|
|
|
|
/* Ensure the string can be found */
|
|
|
|
strLength = [aString length];
|
|
|
|
if (strLength > aRange.length)
|
|
|
|
return (NSRange){0, 0};
|
1995-04-03 01:35:42 +00:00
|
|
|
|
1996-09-02 13:38:19 +00:00
|
|
|
/* Decide where to start and end the search */
|
|
|
|
if (mask & NSBackwardsSearch)
|
|
|
|
{
|
|
|
|
stepDirection = -1;
|
|
|
|
myIndex = aRange.location + aRange.length - strLength;
|
|
|
|
myEndIndex = aRange.location;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stepDirection = 1;
|
|
|
|
myIndex = aRange.location;
|
|
|
|
myEndIndex = aRange.location + aRange.length - strLength;
|
|
|
|
}
|
|
|
|
/* FIXME: I am guessing that this is what NSAnchoredSearch does. */
|
|
|
|
if (mask & NSAnchoredSearch)
|
|
|
|
myEndIndex = myIndex;
|
|
|
|
|
|
|
|
/* Start searching. For efficiency there are separate loops for
|
|
|
|
case-sensitive and case-insensitive searches. */
|
|
|
|
strFirstCharacter = [aString characterAtIndex:0];
|
|
|
|
if (mask & NSCaseInsensitiveSearch)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
unsigned int i = 1;
|
|
|
|
unichar myCharacter = [self characterAtIndex:myIndex];
|
|
|
|
unichar strCharacter = strFirstCharacter;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* FIXME: I have no idea how to make case-insensitive
|
|
|
|
comparisons work over the full range of Unicode characters. */
|
|
|
|
if ((myCharacter != strCharacter) &&
|
|
|
|
(!isascii (myCharacter)
|
|
|
|
|| !isalpha (myCharacter)
|
|
|
|
|| !isascii (strCharacter)
|
|
|
|
|| !isalpha (strCharacter)
|
|
|
|
|| (tolower (myCharacter) != tolower (strCharacter))))
|
|
|
|
break;
|
|
|
|
if (i == strLength)
|
|
|
|
return (NSRange){myIndex, strLength};
|
|
|
|
myCharacter = [self characterAtIndex:myIndex + i];
|
|
|
|
strCharacter = [aString characterAtIndex:i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (myIndex == myEndIndex)
|
|
|
|
break;
|
|
|
|
myIndex += stepDirection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
unsigned int i = 1;
|
|
|
|
unichar myCharacter = [self characterAtIndex:myIndex];
|
|
|
|
unichar strCharacter = strFirstCharacter;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (myCharacter != strCharacter)
|
|
|
|
break;
|
|
|
|
if (i == strLength)
|
|
|
|
return (NSRange){myIndex, strLength};
|
|
|
|
myCharacter = [self characterAtIndex:myIndex + i];
|
|
|
|
strCharacter = [aString characterAtIndex:i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (myIndex == myEndIndex)
|
|
|
|
break;
|
|
|
|
myIndex += stepDirection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (NSRange){0, 0};
|
|
|
|
}
|
1995-04-03 01:35:42 +00:00
|
|
|
|
|
|
|
// Determining Composed Character Sequences
|
|
|
|
|
|
|
|
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (unsigned int)anIndex
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return ((NSRange){0,0});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Identifying and Comparing Strings
|
|
|
|
|
|
|
|
- (NSComparisonResult) caseInsensitiveCompare: (NSString*)aString
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
return [self compare:aString options:NSCaseInsensitiveSearch
|
|
|
|
range:((NSRange){0, [self length]})];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSComparisonResult) compare: (NSString*)aString
|
|
|
|
{
|
|
|
|
return [self compare:aString options:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSComparisonResult) compare: (NSString*)aString
|
|
|
|
options: (unsigned int)mask
|
|
|
|
{
|
|
|
|
return [self compare:aString options:mask
|
1995-10-18 16:47:59 +00:00
|
|
|
range:((NSRange){0, MAX([self length], [aString length])})];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSComparisonResult) compare: (NSString*)aString
|
|
|
|
options: (unsigned int)mask
|
|
|
|
range: (NSRange)aRange
|
|
|
|
{
|
1995-05-05 18:29:12 +00:00
|
|
|
/* xxx ignores NSAnchoredSearch in mask. Fix this. */
|
|
|
|
/* xxx only handles C-string encoding */
|
|
|
|
|
|
|
|
int i, start, end, increment;
|
|
|
|
const char *s1 = [self _cStringContents];
|
|
|
|
const char *s2 = [aString _cStringContents];
|
|
|
|
|
|
|
|
if (mask & NSBackwardsSearch)
|
|
|
|
{
|
|
|
|
start = aRange.location + aRange.length;
|
|
|
|
end = aRange.location;
|
|
|
|
increment = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start = aRange.location;
|
|
|
|
end = aRange.location + aRange.length;
|
|
|
|
increment = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mask & NSCaseInsensitiveSearch)
|
|
|
|
{
|
|
|
|
for (i = start; i < end; i += increment)
|
|
|
|
{
|
|
|
|
int c1 = tolower(s1[i]);
|
|
|
|
int c2 = tolower(s2[i]);
|
|
|
|
if (c1 < c2) return NSOrderedAscending;
|
|
|
|
if (c1 > c2) return NSOrderedDescending;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = start; i < end; i += increment)
|
|
|
|
{
|
|
|
|
if (s1[i] < s2[i]) return NSOrderedAscending;
|
|
|
|
if (s1[i] > s2[i]) return NSOrderedDescending;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NSOrderedSame;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasPrefix: (NSString*)aString
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange range;
|
|
|
|
range = [self rangeOfString:aString];
|
|
|
|
return (range.location == 0) ? YES : NO;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasSuffix: (NSString*)aString
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange range;
|
|
|
|
range = [self rangeOfString:aString options:NSBackwardsSearch];
|
|
|
|
return (range.location == ([self length] - [aString length])) ? YES : NO;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned int) hash
|
|
|
|
{
|
1996-02-22 21:50:35 +00:00
|
|
|
unsigned ret = 0;
|
|
|
|
unsigned ctr = 0;
|
|
|
|
unsigned char_count = 0;
|
|
|
|
const char *s = [self _cStringContents];
|
|
|
|
|
|
|
|
while (*s && char_count++ < NSHashStringLength)
|
|
|
|
{
|
|
|
|
ret ^= *s++ << ctr;
|
|
|
|
ctr = (ctr + 1) % sizeof (void*);
|
|
|
|
}
|
|
|
|
return ret;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)anObject
|
|
|
|
{
|
|
|
|
if ([anObject isKindOf:[NSString class]])
|
|
|
|
return [self isEqualToString:anObject];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isEqualToString: (NSString*)aString
|
|
|
|
{
|
|
|
|
return ! strcmp([self _cStringContents], [aString _cStringContents]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Storing the String
|
|
|
|
|
|
|
|
- (NSString*) description
|
|
|
|
{
|
1996-05-13 16:37:10 +00:00
|
|
|
const char *src = [self cString];
|
|
|
|
char *dest;
|
|
|
|
char *src_ptr,*dest_ptr;
|
|
|
|
int len,quote;
|
|
|
|
unsigned char ch;
|
|
|
|
|
|
|
|
/* xxx Really should make this work with unichars. */
|
|
|
|
|
|
|
|
#define inrange(ch,min,max) ((ch)>=(min) && (ch)<=(max))
|
|
|
|
#define noquote(ch) (inrange(ch,'a','z') || inrange(ch,'A','Z') || inrange(ch,'0','9') || ((ch)=='_') || ((ch)=='.') || ((ch)=='$'))
|
|
|
|
#define charesc(ch) (inrange(ch,07,014) || ((ch)=='\"') || ((ch)=='\\'))
|
|
|
|
#define numesc(ch) (((ch)<=06) || inrange(ch,015,037) || ((ch)>0176))
|
|
|
|
|
1996-05-13 17:30:01 +00:00
|
|
|
for (src_ptr = (char*)src, len=0,quote=0;
|
|
|
|
(ch=*src_ptr);
|
|
|
|
src_ptr++, len++)
|
1996-05-13 16:37:10 +00:00
|
|
|
{
|
1996-05-13 17:30:01 +00:00
|
|
|
if (!noquote(ch))
|
1996-05-13 16:37:10 +00:00
|
|
|
{
|
|
|
|
quote=1;
|
1996-05-13 17:30:01 +00:00
|
|
|
if (charesc(ch))
|
1996-05-13 16:37:10 +00:00
|
|
|
len++;
|
1996-05-13 17:30:01 +00:00
|
|
|
else if (numesc(ch))
|
1996-05-13 16:37:10 +00:00
|
|
|
len+=3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (quote)
|
|
|
|
len+=2;
|
|
|
|
|
|
|
|
dest = (char*) malloc (len+1);
|
|
|
|
|
|
|
|
src_ptr = (char*) src;
|
|
|
|
dest_ptr = dest;
|
|
|
|
if (quote)
|
|
|
|
*(dest_ptr++) = '\"';
|
1996-05-13 17:30:01 +00:00
|
|
|
for (; (ch=*src_ptr); src_ptr++,dest_ptr++)
|
1996-05-13 16:37:10 +00:00
|
|
|
{
|
1996-05-13 17:30:01 +00:00
|
|
|
if (charesc(ch))
|
1996-05-13 16:37:10 +00:00
|
|
|
{
|
|
|
|
*(dest_ptr++) = '\\';
|
1996-05-13 17:30:01 +00:00
|
|
|
switch (ch)
|
1996-05-13 16:37:10 +00:00
|
|
|
{
|
|
|
|
case '\a': *dest_ptr = 'a'; break;
|
|
|
|
case '\b': *dest_ptr = 'b'; break;
|
|
|
|
case '\t': *dest_ptr = 't'; break;
|
|
|
|
case '\n': *dest_ptr = 'n'; break;
|
|
|
|
case '\v': *dest_ptr = 'v'; break;
|
|
|
|
case '\f': *dest_ptr = 'f'; break;
|
|
|
|
default: *dest_ptr = ch; /* " or \ */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (numesc(ch))
|
|
|
|
{
|
|
|
|
*(dest_ptr++) = '\\';
|
|
|
|
*(dest_ptr++) = '0' + ((ch>>6)&07);
|
|
|
|
*(dest_ptr++) = '0' + ((ch>>3)&07);
|
|
|
|
*dest_ptr = '0' + (ch&07);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* copy literally */
|
|
|
|
*dest_ptr = ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (quote)
|
|
|
|
*(dest_ptr++) = '\"';
|
|
|
|
*dest_ptr = '\0';
|
|
|
|
|
|
|
|
return [NSString stringWithCString:dest];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) writeToFile: (NSString*)filename
|
|
|
|
atomically: (BOOL)useAuxiliaryFile
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Getting a Shared Prefix
|
|
|
|
|
|
|
|
- (NSString*) commonPrefixWithString: (NSString*)aString
|
|
|
|
options: (unsigned int)mask
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Changing Case
|
|
|
|
|
|
|
|
- (NSString*) capitalizedString
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) lowercaseString
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) uppercaseString
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Getting C Strings
|
|
|
|
|
|
|
|
- (const char*) cString
|
|
|
|
{
|
1995-10-26 01:29:04 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned int) cStringLength
|
|
|
|
{
|
1995-10-26 01:29:04 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
{
|
|
|
|
[self getCString:buffer maxLength:NSMaximumStringLength
|
|
|
|
range:((NSRange){0, [self length]})
|
|
|
|
remainingRange:NULL];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
{
|
|
|
|
[self getCString:buffer maxLength:maxLength
|
|
|
|
range:((NSRange){0, [self length]})
|
|
|
|
remainingRange:NULL];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
range: (NSRange)aRange
|
|
|
|
remainingRange: (NSRange*)leftoverRange
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* xxx check to make sure aRange is within self; raise NSStringBoundsError */
|
1996-09-02 19:31:44 +00:00
|
|
|
assert(aRange.location + aRange.length <= [self cStringLength]);
|
1995-04-03 01:35:42 +00:00
|
|
|
if (maxLength < aRange.length)
|
|
|
|
{
|
|
|
|
len = maxLength;
|
|
|
|
if (leftoverRange)
|
|
|
|
{
|
|
|
|
leftoverRange->location = 0;
|
|
|
|
leftoverRange->length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = aRange.length;
|
|
|
|
if (leftoverRange)
|
|
|
|
{
|
|
|
|
leftoverRange->location = aRange.location + maxLength;
|
|
|
|
leftoverRange->length = aRange.length - maxLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(buffer, [self _cStringContents] + aRange.location, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Getting Numeric Values
|
|
|
|
|
|
|
|
- (double) doubleValue
|
|
|
|
{
|
|
|
|
return atof([self _cStringContents]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (float) floatValue
|
|
|
|
{
|
|
|
|
return (float) atof([self _cStringContents]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) intValue
|
|
|
|
{
|
|
|
|
return atoi([self _cStringContents]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Working With Encodings
|
|
|
|
|
|
|
|
+ (NSStringEncoding) defaultCStringEncoding
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) canBeConvertedToEncoding: (NSStringEncoding)encoding
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData*) dataUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData*) dataUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
allowLossyConversion: (BOOL)flag
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSStringEncoding) fastestEncoding
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSStringEncoding) smallestEncoding
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Converting String Contents into a Property List
|
|
|
|
|
|
|
|
- (id)propertyList
|
|
|
|
{
|
1996-05-13 16:37:10 +00:00
|
|
|
id obj;
|
|
|
|
void *bufstate;
|
|
|
|
bufstate = (void *)pl_scan_string([self cString]);
|
|
|
|
obj = (id)plparse();
|
|
|
|
pl_delete_buffer(bufstate);
|
|
|
|
return obj;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary*) propertyListFromStringsFileFormat
|
|
|
|
{
|
1996-10-31 17:03:44 +00:00
|
|
|
id dict = [[[NSMutableDictionary alloc] init] autorelease];
|
|
|
|
void *bufstate;
|
|
|
|
|
|
|
|
bufstate = (void *)sf_scan_string([self cString]);
|
|
|
|
sfSetDict(dict);
|
|
|
|
sfparse(dict);
|
|
|
|
sf_delete_buffer(bufstate);
|
|
|
|
return dict;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Manipulating File System Paths
|
|
|
|
|
|
|
|
- (unsigned int) completePathIntoString: (NSString**)outputName
|
|
|
|
caseSensitive: (BOOL)flag
|
|
|
|
matchesIntoArray: (NSArray**)outputArray
|
|
|
|
filterTypes: (NSArray*)filterTypes
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
/* Returns a new string containing the last path component of the receiver. The
|
|
|
|
path component is any substring after the last '/' character. If the last
|
|
|
|
character is a '/', then the substring before the last '/', but after the
|
|
|
|
second-to-last '/' is returned. Returns the receiver if there are no '/'
|
|
|
|
characters. Returns the null string if the receiver only contains a '/'
|
|
|
|
character. */
|
1995-04-03 01:35:42 +00:00
|
|
|
- (NSString*) lastPathComponent
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange range;
|
|
|
|
NSString *substring = nil;
|
|
|
|
|
|
|
|
range = [self rangeOfString:@"/" options:NSBackwardsSearch];
|
|
|
|
if (range.length == 0)
|
|
|
|
substring = self;
|
|
|
|
else if (range.location == [self length] - 1)
|
|
|
|
{
|
|
|
|
if (range.location == 0)
|
|
|
|
substring = [NSString new];
|
|
|
|
else
|
|
|
|
substring = [[self substringToIndex:range.location-1]
|
|
|
|
lastPathComponent];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
substring = [self substringFromIndex:range.location+1];
|
|
|
|
|
|
|
|
return substring;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
/* Returns a new string containing the path extension of the receiver. The
|
|
|
|
path extension is a suffix on the last path component which starts with
|
|
|
|
a '.' (for example .tiff is the pathExtension for /foo/bar.tiff). Returns
|
|
|
|
a null string if no such extension exists. */
|
1995-04-03 01:35:42 +00:00
|
|
|
- (NSString*) pathExtension
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange range;
|
|
|
|
NSString *substring = nil;
|
|
|
|
|
|
|
|
range = [self rangeOfString:@"." options:NSBackwardsSearch];
|
|
|
|
if (range.length == 0
|
|
|
|
|| range.location
|
|
|
|
< ([self rangeOfString:@"/" options:NSBackwardsSearch]).location)
|
|
|
|
substring = [NSString new];
|
|
|
|
else
|
|
|
|
substring = [self substringFromIndex:range.location+1];
|
|
|
|
return substring;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) stringByAbbreviatingWithTildeInPath
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
/* Returns a new string with the path component given in aString
|
|
|
|
appended to the receiver. Assumes that aString is NOT prefixed by
|
|
|
|
a '/'. Checks the receiver to see if the last letter is a '/', if it
|
|
|
|
is not, a '/' is appended before appending aString */
|
1995-04-03 01:35:42 +00:00
|
|
|
- (NSString*) stringByAppendingPathComponent: (NSString*)aString
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange range;
|
|
|
|
NSString *newstring;
|
|
|
|
|
|
|
|
range = [self rangeOfString:@"/" options:NSBackwardsSearch];
|
|
|
|
if (range.length != 0 && range.location != [self length] - 1)
|
|
|
|
newstring = [self stringByAppendingString:@"/"];
|
|
|
|
else
|
|
|
|
newstring = self;
|
|
|
|
|
|
|
|
return [newstring stringByAppendingString:aString];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
/* Returns a new string with the path extension given in aString
|
|
|
|
appended to the receiver. Assumes that aString is NOT prefixed by
|
|
|
|
a '.'. Checks the receiver to see if the last letter is a '.', if it
|
|
|
|
is not, a '.' is appended before appending aString */
|
1995-04-03 01:35:42 +00:00
|
|
|
- (NSString*) stringByAppendingPathExtension: (NSString*)aString
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange range;
|
|
|
|
NSString *newstring;
|
|
|
|
|
|
|
|
range = [aString rangeOfString:@"." options:NSBackwardsSearch];
|
|
|
|
if (range.length != 0 && range.location != [self length] - 1)
|
|
|
|
newstring = [self stringByAppendingString:@"."];
|
|
|
|
else
|
|
|
|
newstring = self;
|
|
|
|
|
|
|
|
return [newstring stringByAppendingString:aString];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
/* Returns a new string with the last path component removed from the
|
|
|
|
receiver. See lastPathComponent for a definition of a path component */
|
1995-04-03 01:35:42 +00:00
|
|
|
- (NSString*) stringByDeletingLastPathComponent
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange range;
|
|
|
|
NSString *substring;
|
|
|
|
|
|
|
|
range = [self rangeOfString:[self lastPathComponent]
|
|
|
|
options:NSBackwardsSearch];
|
|
|
|
if (range.length != 0)
|
|
|
|
substring = [self substringToIndex:range.location-2];
|
|
|
|
else
|
|
|
|
substring = self;
|
|
|
|
return substring;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
/* Returns a new string with the path extension removed from the receiver.
|
|
|
|
See pathExtension for a definition of the path extension */
|
1995-04-03 01:35:42 +00:00
|
|
|
- (NSString*) stringByDeletingPathExtension
|
|
|
|
{
|
(componentsSeparatedByString:, substringFromRange:,
rangeOfCharacterFromSet:, rangeOfCharacterFromSet:options:,
rangeOfCharacterFromSet:options:range:, rangeOfString:,
rangeOfString:options:, caseInsensitiveCompare:, hasPrefix:,
hasSuffix:, lastPathComponent, pathExtension,
stringByAppendingPathComponent:, stringByAppendingPathExtension:,
stringByDeletingLastPathComponent, stringByDeletingPathExtension):
Implemented methods.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@505 72102866-910b-0410-8b05-ffd578937521
1995-08-02 15:10:41 +00:00
|
|
|
NSRange range;
|
|
|
|
NSString *substring;
|
|
|
|
|
|
|
|
range = [self rangeOfString:[self pathExtension] options:NSBackwardsSearch];
|
|
|
|
if (range.length != 0)
|
|
|
|
substring = [self substringToIndex:range.location-2];
|
|
|
|
else
|
|
|
|
substring = self;
|
|
|
|
return substring;
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) stringByExpandingTildeInPath
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) stringByResolvingSymlinksInPath
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) stringByStandardizingPath
|
|
|
|
{
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NSCopying Protocol */
|
|
|
|
|
|
|
|
- copyWithZone: (NSZone*)zone
|
|
|
|
{
|
|
|
|
return [[[self class] allocWithZone:zone] initWithString:self];
|
|
|
|
}
|
|
|
|
|
1996-09-24 17:41:03 +00:00
|
|
|
/* xxx Temporarily put this NSObject-like implementation here, so
|
|
|
|
we don't get String's Collection implementation.
|
|
|
|
When we separate Core from NonCore methods, this problem will
|
|
|
|
go away. */
|
|
|
|
- copy
|
|
|
|
{
|
|
|
|
return [self copyWithZone: NS_NOZONE];
|
|
|
|
}
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
- mutableCopyWithZone: (NSZone*)zone
|
|
|
|
{
|
1995-08-09 16:07:19 +00:00
|
|
|
return [[[[self class] _mutableConcreteClass] allocWithZone:zone]
|
|
|
|
initWithString:self];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
1995-04-09 01:53:53 +00:00
|
|
|
/* NSCoding Protocol */
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: anEncoder
|
|
|
|
{
|
|
|
|
[super encodeWithCoder:anEncoder];
|
|
|
|
}
|
|
|
|
|
|
|
|
- initWithCoder: aDecoder
|
|
|
|
{
|
|
|
|
return [super initWithCoder:aDecoder];
|
|
|
|
}
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSString (NSCStringAccess)
|
|
|
|
- (const char *) _cStringContents
|
|
|
|
{
|
1995-08-09 16:26:44 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
1996-03-26 20:59:42 +00:00
|
|
|
/* We don't actually implement (GNU) Category, in order to avoid warning
|
|
|
|
about "unimplemented methods"; they come from the behavior. */
|
|
|
|
@implementation NSString (GNUCollection)
|
1995-04-03 01:35:42 +00:00
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- objectAtIndex: (unsigned)index
|
1995-04-03 01:35:42 +00:00
|
|
|
{
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, [self cStringLength]);
|
1996-02-22 15:18:57 +00:00
|
|
|
return [NSNumber numberWithChar: [self _cStringContents][index]];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The rest are handled by the class_add_behavior() call in +initialize. */
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSMutableString
|
|
|
|
|
1995-11-19 20:29:39 +00:00
|
|
|
+ allocWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
return NSAllocateObject([self _mutableConcreteClass], 0, z);
|
|
|
|
}
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
/* xxx This method may be removed in future. */
|
|
|
|
- (void) setCString: (const char *)byteString length: (unsigned)length
|
|
|
|
{
|
1995-08-09 16:26:44 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initializing Newly Allocated Strings
|
|
|
|
|
|
|
|
- initWithCapacity:(unsigned)capacity
|
|
|
|
{
|
1995-08-09 16:26:44 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creating Temporary Strings
|
|
|
|
|
|
|
|
+ (NSMutableString*) stringWithCapacity:(unsigned)capacity
|
|
|
|
{
|
1995-11-19 20:29:39 +00:00
|
|
|
return [[[self alloc] initWithCapacity:capacity]
|
1995-04-03 01:35:42 +00:00
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inefficient. */
|
|
|
|
+ (NSString*) stringWithCharacters: (const unichar*)characters
|
|
|
|
length: (unsigned)length
|
|
|
|
{
|
|
|
|
id n;
|
|
|
|
n = [self stringWithCapacity:length];
|
|
|
|
[n setString: [NSString stringWithCharacters:characters length:length]];
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString*) stringWithCString: (const char*)byteString
|
|
|
|
{
|
|
|
|
return [self stringWithCString:byteString length:strlen(byteString)];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString*) stringWithCString: (const char*)bytes
|
|
|
|
length:(unsigned)length
|
|
|
|
{
|
1995-11-19 20:29:39 +00:00
|
|
|
id n = [[self alloc] initWithCapacity:length];
|
1995-04-03 01:35:42 +00:00
|
|
|
[n setCString:bytes length:length];
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* xxx Change this when we have non-CString classes */
|
|
|
|
+ (NSString*) stringWithFormat: (NSString*)format, ...
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
self = [super stringWithFormat:format arguments:ap];
|
|
|
|
va_end(ap);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Modify A String
|
|
|
|
|
|
|
|
/* Inefficient. */
|
|
|
|
- (void) appendString: (NSString*)aString
|
|
|
|
{
|
|
|
|
id tmp = [self stringByAppendingString:aString];
|
|
|
|
[self setString:tmp];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inefficient. */
|
|
|
|
- (void) appendFormat: (NSString*)format, ...
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
id tmp;
|
|
|
|
va_start(ap, format);
|
|
|
|
tmp = [NSString stringWithFormat:format arguments:ap];
|
|
|
|
va_end(ap);
|
|
|
|
[self appendString:tmp];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) deleteCharactersInRange: (NSRange)range
|
|
|
|
{
|
1995-08-09 16:26:44 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertString: (NSString*)aString atIndex:(unsigned)loc
|
|
|
|
{
|
1995-08-09 16:26:44 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Inefficient. */
|
|
|
|
- (void) replaceCharactersInRange: (NSRange)range
|
|
|
|
withString: (NSString*)aString
|
|
|
|
{
|
|
|
|
[self deleteCharactersInRange:range];
|
|
|
|
[self insertString:aString atIndex:range.location];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* xxx Change this when we have non-CString classes */
|
|
|
|
- (void) setString: (NSString*)aString
|
|
|
|
{
|
|
|
|
const char *s = [aString _cStringContents];
|
|
|
|
[self setCString:s length:strlen(s)];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NXConstantString
|
1995-08-02 15:25:34 +00:00
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (const char*) cString
|
|
|
|
{
|
|
|
|
return _contents_chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
- retain
|
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1995-08-09 16:07:19 +00:00
|
|
|
- (oneway void) release
|
1995-08-02 15:25:34 +00:00
|
|
|
{
|
1995-08-09 16:07:19 +00:00
|
|
|
return;
|
1995-08-02 15:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- autorelease
|
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- copyWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1995-03-12 19:54:14 +00:00
|
|
|
@end
|