2001-12-17 14:31:42 +00:00
|
|
|
|
/** Implementation of GNUSTEP string class
|
1998-10-15 05:03:16 +00:00
|
|
|
|
Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
|
2002-04-29 12:20:08 +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
|
1997-05-03 18:05:21 +00:00
|
|
|
|
|
1998-01-08 15:25:59 +00:00
|
|
|
|
Unicode implementation by Stevo Crvenkovski <stevo@btinternet.com>
|
1997-05-03 18:05:21 +00:00
|
|
|
|
Date: February 1997
|
|
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
|
Optimisations by Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
2000-10-29 14:52:33 +00:00
|
|
|
|
Date: October 1998 - 2000
|
1998-10-15 05:03:16 +00:00
|
|
|
|
|
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.
|
2002-04-29 12:20:08 +00:00
|
|
|
|
|
1995-03-12 19:54:14 +00:00
|
|
|
|
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.
|
1997-05-03 18:05:21 +00:00
|
|
|
|
|
1995-03-12 19:54:14 +00:00
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
|
License along with this library; if not, write to the Free
|
1999-08-25 14:47:19 +00:00
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
|
|
<title>NSString class reference</title>
|
|
|
|
|
$Date$ $Revision$
|
2002-04-29 12:20:08 +00:00
|
|
|
|
*/
|
1995-03-12 19:54:14 +00:00
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/* Caveats:
|
1995-04-03 01:35:42 +00:00
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
Some implementations will need to be changed.
|
2002-04-29 12:20:08 +00:00
|
|
|
|
Does not support all justification directives for `%@' in format strings
|
1996-09-02 13:20:20 +00:00
|
|
|
|
on non-GNU-libc systems.
|
1995-04-03 01:35:42 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2001-03-20 02:13:19 +00:00
|
|
|
|
/*
|
|
|
|
|
Locales somewhat supported.
|
1997-05-03 18:05:21 +00:00
|
|
|
|
Limited choice of default encodings.
|
|
|
|
|
*/
|
|
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "config.h"
|
2000-06-14 04:03:56 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/preface.h"
|
2003-08-07 18:22:03 +00:00
|
|
|
|
#include "Foundation/NSAutoreleasePool.h"
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "Foundation/NSString.h"
|
|
|
|
|
#include "Foundation/NSCalendarDate.h"
|
|
|
|
|
#include "Foundation/NSArray.h"
|
|
|
|
|
#include "Foundation/NSCharacterSet.h"
|
|
|
|
|
#include "Foundation/NSException.h"
|
|
|
|
|
#include "Foundation/NSValue.h"
|
|
|
|
|
#include "Foundation/NSDictionary.h"
|
|
|
|
|
#include "Foundation/NSFileManager.h"
|
|
|
|
|
#include "Foundation/NSPortCoder.h"
|
|
|
|
|
#include "Foundation/NSPathUtilities.h"
|
|
|
|
|
#include "Foundation/NSRange.h"
|
|
|
|
|
#include "Foundation/NSException.h"
|
|
|
|
|
#include "Foundation/NSData.h"
|
|
|
|
|
#include "Foundation/NSBundle.h"
|
|
|
|
|
#include "Foundation/NSURL.h"
|
|
|
|
|
#include "Foundation/NSMapTable.h"
|
|
|
|
|
#include "Foundation/NSLock.h"
|
|
|
|
|
#include "Foundation/NSUserDefaults.h"
|
|
|
|
|
#include "Foundation/NSDebug.h"
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/GSMime.h"
|
|
|
|
|
#include "GSFormat.h"
|
1995-04-03 01:35:42 +00:00
|
|
|
|
#include <limits.h>
|
1996-10-31 17:03:44 +00:00
|
|
|
|
#include <sys/stat.h>
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_UNISTD_H
|
1996-10-31 17:03:44 +00:00
|
|
|
|
#include <unistd.h>
|
2002-02-20 06:42:05 +00:00
|
|
|
|
#endif
|
1996-10-31 17:03:44 +00:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/Unicode.h"
|
1997-05-03 18:05:21 +00:00
|
|
|
|
|
2002-03-13 09:58:43 +00:00
|
|
|
|
#include "GSPrivate.h"
|
2001-11-10 17:31:39 +00:00
|
|
|
|
|
2003-07-08 08:39:45 +00:00
|
|
|
|
extern BOOL GSScanDouble(unichar*, unsigned, double*);
|
|
|
|
|
|
2000-10-23 06:18:03 +00:00
|
|
|
|
@class GSString;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
@class GSMutableString;
|
|
|
|
|
@class GSPlaceholderString;
|
2001-01-08 16:45:36 +00:00
|
|
|
|
@class GSMutableArray;
|
2001-01-09 09:17:31 +00:00
|
|
|
|
@class GSMutableDictionary;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
1998-10-09 04:24:56 +00:00
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
|
/*
|
2000-10-23 11:44:34 +00:00
|
|
|
|
* Cache classes and method implementations for speed.
|
2000-08-07 22:00:31 +00:00
|
|
|
|
*/
|
2000-10-23 11:44:34 +00:00
|
|
|
|
static Class NSDataClass;
|
2000-10-23 06:18:03 +00:00
|
|
|
|
static Class NSStringClass;
|
|
|
|
|
static Class NSMutableStringClass;
|
|
|
|
|
|
|
|
|
|
static Class GSStringClass;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
static Class GSMutableStringClass;
|
|
|
|
|
static Class GSPlaceholderStringClass;
|
|
|
|
|
|
|
|
|
|
static GSPlaceholderString *defaultPlaceholderString;
|
|
|
|
|
static NSMapTable *placeholderMap;
|
|
|
|
|
static NSLock *placeholderLock;
|
1999-05-10 11:02:28 +00:00
|
|
|
|
|
2000-10-23 11:44:34 +00:00
|
|
|
|
static Class plArray;
|
|
|
|
|
static id (*plAdd)(id, SEL, id) = 0;
|
|
|
|
|
|
|
|
|
|
static Class plDictionary;
|
|
|
|
|
static id (*plSet)(id, SEL, id, id) = 0;
|
|
|
|
|
|
|
|
|
|
static id (*plAlloc)(Class, SEL, NSZone*) = 0;
|
2002-03-13 13:46:12 +00:00
|
|
|
|
static id (*plInit)(id, SEL, unichar*, unsigned int) = 0;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
2000-10-30 18:00:27 +00:00
|
|
|
|
static SEL plSel;
|
2000-11-01 12:31:21 +00:00
|
|
|
|
static SEL cMemberSel = 0;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
2001-04-26 23:54:01 +00:00
|
|
|
|
|
|
|
|
|
#define IS_BIT_SET(a,i) ((((a) & (1<<(i)))) > 0)
|
|
|
|
|
|
|
|
|
|
static unsigned const char *hexdigitsBitmapRep = NULL;
|
|
|
|
|
#define GS_IS_HEXDIGIT(X) IS_BIT_SET(hexdigitsBitmapRep[(X)/8], (X) % 8)
|
|
|
|
|
|
2002-11-10 06:19:17 +00:00
|
|
|
|
static void setupHexdigits(void)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
2001-04-26 23:54:01 +00:00
|
|
|
|
if (hexdigitsBitmapRep == NULL)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
2001-04-26 23:54:01 +00:00
|
|
|
|
NSCharacterSet *hexdigits;
|
2001-04-28 13:57:23 +00:00
|
|
|
|
NSData *bitmap;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
|
2000-10-23 11:44:34 +00:00
|
|
|
|
hexdigits = [NSCharacterSet characterSetWithCharactersInString:
|
|
|
|
|
@"0123456789abcdefABCDEF"];
|
2001-04-28 13:57:23 +00:00
|
|
|
|
bitmap = RETAIN([hexdigits bitmapRepresentation]);
|
|
|
|
|
hexdigitsBitmapRep = [bitmap bytes];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2001-04-26 23:54:01 +00:00
|
|
|
|
static NSCharacterSet *quotables = nil;
|
2002-06-28 06:44:27 +00:00
|
|
|
|
static NSCharacterSet *oldQuotables = nil;
|
2001-04-26 23:54:01 +00:00
|
|
|
|
static unsigned const char *quotablesBitmapRep = NULL;
|
|
|
|
|
#define GS_IS_QUOTABLE(X) IS_BIT_SET(quotablesBitmapRep[(X)/8], (X) % 8)
|
|
|
|
|
|
2002-11-10 06:19:17 +00:00
|
|
|
|
static void setupQuotables(void)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
2001-04-26 23:54:01 +00:00
|
|
|
|
if (quotablesBitmapRep == NULL)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
|
|
|
|
NSMutableCharacterSet *s;
|
2002-06-28 06:44:27 +00:00
|
|
|
|
NSData *bitmap;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
|
|
|
|
s = [[NSCharacterSet characterSetWithCharactersInString:
|
2001-09-27 18:33:07 +00:00
|
|
|
|
@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
@"abcdefghijklmnopqrstuvwxyz!#$%&*+-./:?@|~_^"]
|
2000-10-23 11:44:34 +00:00
|
|
|
|
mutableCopy];
|
|
|
|
|
[s invert];
|
|
|
|
|
quotables = [s copy];
|
|
|
|
|
RELEASE(s);
|
2001-04-28 13:57:23 +00:00
|
|
|
|
bitmap = RETAIN([quotables bitmapRepresentation]);
|
|
|
|
|
quotablesBitmapRep = [bitmap bytes];
|
2002-06-28 06:44:27 +00:00
|
|
|
|
s = [[NSCharacterSet characterSetWithCharactersInString:
|
|
|
|
|
@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
@"abcdefghijklmnopqrstuvwxyz$./_"]
|
|
|
|
|
mutableCopy];
|
|
|
|
|
[s invert];
|
|
|
|
|
oldQuotables = [s copy];
|
|
|
|
|
RELEASE(s);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2001-04-26 23:54:01 +00:00
|
|
|
|
static unsigned const char *whitespaceBitmapRep = NULL;
|
|
|
|
|
#define GS_IS_WHITESPACE(X) IS_BIT_SET(whitespaceBitmapRep[(X)/8], (X) % 8)
|
|
|
|
|
|
2002-11-10 06:19:17 +00:00
|
|
|
|
static void setupWhitespace(void)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
2001-04-26 23:54:01 +00:00
|
|
|
|
if (whitespaceBitmapRep == NULL)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
2001-04-26 23:54:01 +00:00
|
|
|
|
NSCharacterSet *whitespace;
|
2001-04-28 13:57:23 +00:00
|
|
|
|
NSData *bitmap;
|
2001-04-26 23:54:01 +00:00
|
|
|
|
|
2001-04-28 13:57:23 +00:00
|
|
|
|
/*
|
2001-04-28 15:32:01 +00:00
|
|
|
|
We can not use whitespaceAndNewlineCharacterSet here as this would lead
|
|
|
|
|
to a recursion, as this also reads in a property list.
|
|
|
|
|
whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
|
|
|
|
|
*/
|
2001-04-26 23:54:01 +00:00
|
|
|
|
whitespace = [NSCharacterSet characterSetWithCharactersInString:
|
|
|
|
|
@" \t\r\n\f\b"];
|
2001-04-28 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
bitmap = RETAIN([whitespace bitmapRepresentation]);
|
|
|
|
|
whitespaceBitmapRep = [bitmap bytes];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-05-20 09:20:46 +00:00
|
|
|
|
/*
|
|
|
|
|
* Include sequence handling code with instructions to generate search
|
|
|
|
|
* and compare functions for NSString objects.
|
|
|
|
|
*/
|
|
|
|
|
#define GSEQ_STRCOMP strCompNsNs
|
|
|
|
|
#define GSEQ_STRRANGE strRangeNsNs
|
|
|
|
|
#define GSEQ_O GSEQ_NS
|
|
|
|
|
#define GSEQ_S GSEQ_NS
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "GSeq.h"
|
1997-05-03 18:05:21 +00:00
|
|
|
|
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
|
|
|
|
static id GSPropertyList(NSString *string);
|
|
|
|
|
static id GSPropertyListFromStringsFormat(NSString *string);
|
1999-02-04 13:49:27 +00:00
|
|
|
|
|
1999-09-03 11:05:33 +00:00
|
|
|
|
static NSCharacterSet *myPathSeps = nil;
|
1999-01-20 13:28:28 +00:00
|
|
|
|
/*
|
2002-04-29 12:20:08 +00:00
|
|
|
|
* The pathSeps character set is used for parsing paths ... it *must*
|
|
|
|
|
* contain the '/' character, which is the internal path separator,
|
|
|
|
|
* and *may* contain additiona system specific separators.
|
|
|
|
|
*
|
|
|
|
|
* We can't have a 'pathSeps' variable initialized in the +initialize
|
|
|
|
|
* method 'cos that would cause recursion.
|
1999-01-20 13:28:28 +00:00
|
|
|
|
*/
|
|
|
|
|
static NSCharacterSet*
|
2002-11-10 06:19:17 +00:00
|
|
|
|
pathSeps(void)
|
1999-01-20 13:28:28 +00:00
|
|
|
|
{
|
1999-09-03 11:05:33 +00:00
|
|
|
|
if (myPathSeps == nil)
|
1999-01-20 13:28:28 +00:00
|
|
|
|
{
|
2000-06-12 05:17:41 +00:00
|
|
|
|
#if defined(__MINGW__)
|
1999-09-03 11:05:33 +00:00
|
|
|
|
myPathSeps = [NSCharacterSet characterSetWithCharactersInString: @"/\\"];
|
1997-09-13 17:52:31 +00:00
|
|
|
|
#else
|
1999-09-03 11:05:33 +00:00
|
|
|
|
myPathSeps = [NSCharacterSet characterSetWithCharactersInString: @"/"];
|
1999-01-20 13:28:28 +00:00
|
|
|
|
#endif
|
1999-09-28 19:35:09 +00:00
|
|
|
|
IF_NO_GC(RETAIN(myPathSeps));
|
1999-01-20 13:28:28 +00:00
|
|
|
|
}
|
1999-09-03 11:05:33 +00:00
|
|
|
|
return myPathSeps;
|
|
|
|
|
}
|
|
|
|
|
|
2001-04-06 22:51:48 +00:00
|
|
|
|
inline static BOOL
|
1999-09-03 11:05:33 +00:00
|
|
|
|
pathSepMember(unichar c)
|
|
|
|
|
{
|
2001-04-06 22:51:48 +00:00
|
|
|
|
|
|
|
|
|
#if defined(__MINGW__)
|
|
|
|
|
if (c == (unichar)'\\' || c == (unichar)'/')
|
2002-04-29 12:20:08 +00:00
|
|
|
|
#else
|
2001-04-06 22:51:48 +00:00
|
|
|
|
if (c == (unichar)'/')
|
2002-04-29 12:20:08 +00:00
|
|
|
|
#endif
|
2001-04-06 22:51:48 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
1999-01-20 13:28:28 +00:00
|
|
|
|
}
|
1997-09-13 17:52:31 +00:00
|
|
|
|
|
2001-04-06 22:51:48 +00:00
|
|
|
|
|
|
|
|
|
|
2001-03-27 17:30:07 +00:00
|
|
|
|
/* Convert a high-low surrogate pair into Unicode scalar code-point */
|
|
|
|
|
static inline gsu32
|
|
|
|
|
surrogatePairValue(unichar high, unichar low)
|
|
|
|
|
{
|
2001-04-12 09:11:31 +00:00
|
|
|
|
return ((high - (unichar)0xD800) * (unichar)400)
|
|
|
|
|
+ ((low - (unichar)0xDC00) + (unichar)10000);
|
2001-03-27 17:30:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-03-12 19:54:14 +00:00
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* NSString objects represent an immutable string of characters.
|
|
|
|
|
* NSString itself is an abstract class which provides factory
|
|
|
|
|
* methods to generate objects of unspecified subclasses.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* A constant NSString can be created using the following syntax:
|
|
|
|
|
* <code>@"..."</code>, where the contents of the quotes are the
|
|
|
|
|
* string, using only ASCII characters.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* To create a concrete subclass of NSString, you must have your
|
|
|
|
|
* class inherit from NSString and override at least the two
|
|
|
|
|
* primitive methods - length and characterAtIndex:
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* In general the rule is that your subclass must override any
|
|
|
|
|
* initialiser that you want to use with it. The GNUstep
|
|
|
|
|
* implementation relaxes that to say that, you may override
|
|
|
|
|
* only the <em>designated initialiser</em> and the other
|
|
|
|
|
* initialisation methods should work.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
1995-03-12 19:54:14 +00:00
|
|
|
|
@implementation NSString
|
1995-04-03 01:35:42 +00:00
|
|
|
|
|
1998-01-08 15:25:59 +00:00
|
|
|
|
static NSStringEncoding _DefaultStringEncoding;
|
2002-03-16 09:54:50 +00:00
|
|
|
|
static BOOL _ByteEncodingOk;
|
2001-03-27 17:30:07 +00:00
|
|
|
|
static const unichar byteOrderMark = 0xFEFF;
|
|
|
|
|
static const unichar byteOrderMarkSwapped = 0xFFFE;
|
|
|
|
|
|
|
|
|
|
/* UTF-16 Surrogate Ranges */
|
|
|
|
|
static NSRange highSurrogateRange = {0xD800, 1024};
|
|
|
|
|
static NSRange lowSurrogateRange = {0xDC00, 1024};
|
|
|
|
|
|
1998-01-08 15:25:59 +00:00
|
|
|
|
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef 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
|
|
|
|
|
2002-04-29 12:20:08 +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,
|
1997-09-01 21:59:51 +00:00
|
|
|
|
it crashes. -mccallum
|
|
|
|
|
|
|
|
|
|
Apparently GNU libc 2.xx needs this to be 0 also, along with Linux
|
|
|
|
|
libc versions 5.2.xx and higher (including libc6, which is just GNU
|
|
|
|
|
libc). -chung */
|
1996-07-15 18:41:44 +00:00
|
|
|
|
#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
|
1997-09-01 21:59:51 +00:00
|
|
|
|
arginfo_func (const struct printf_info *info, size_t n, int *argtypes)
|
|
|
|
|
{
|
1996-05-06 18:59:01 +00:00
|
|
|
|
*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
|
2002-04-29 12:20:08 +00:00
|
|
|
|
handle_printf_atsign (FILE *stream,
|
1996-01-23 23:41:02 +00:00
|
|
|
|
const struct printf_info *info,
|
1996-05-07 01:14:54 +00:00
|
|
|
|
#if PRINTF_ATSIGN_VA_LIST
|
|
|
|
|
va_list *ap_pointer)
|
1997-09-01 21:59:51 +00:00
|
|
|
|
#elif defined(_LINUX_C_LIB_VERSION_MAJOR) \
|
|
|
|
|
&& _LINUX_C_LIB_VERSION_MAJOR < 6
|
1996-05-07 01:14:54 +00:00
|
|
|
|
const void **const args)
|
1997-09-01 21:59:51 +00:00
|
|
|
|
#else /* GNU libc needs the following. */
|
|
|
|
|
const void *const *args)
|
1996-05-07 01:14:54 +00:00
|
|
|
|
#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;
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/* xxx This implementation may not pay pay attention to as much
|
1996-01-23 23:30:05 +00:00
|
|
|
|
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),
|
2000-11-06 16:44:54 +00:00
|
|
|
|
[[string_object description] lossyCString]);
|
1996-01-23 23:30:05 +00:00
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
#endif /* HAVE_REGISTER_PRINTF_FUNCTION */
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
2001-04-26 16:19:12 +00:00
|
|
|
|
/*
|
|
|
|
|
* Flag required as we call this method explicitly from GSBuildStrings()
|
|
|
|
|
* to ensure that NSString is initialised properly.
|
|
|
|
|
*/
|
|
|
|
|
static BOOL beenHere = NO;
|
|
|
|
|
|
|
|
|
|
if (self == [NSString class] && beenHere == NO)
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2001-04-26 16:19:12 +00:00
|
|
|
|
beenHere = YES;
|
2000-10-30 18:00:27 +00:00
|
|
|
|
plSel = @selector(initWithCharacters:length:);
|
|
|
|
|
cMemberSel = @selector(characterIsMember:);
|
2001-05-23 14:32:56 +00:00
|
|
|
|
caiSel = @selector(characterAtIndex:);
|
|
|
|
|
gcrSel = @selector(getCharacters:range:);
|
|
|
|
|
ranSel = @selector(rangeOfComposedCharacterSequenceAtIndex:);
|
|
|
|
|
|
1998-01-08 15:25:59 +00:00
|
|
|
|
_DefaultStringEncoding = GetDefEncoding();
|
2002-03-16 09:54:50 +00:00
|
|
|
|
_ByteEncodingOk = GSIsByteEncoding(_DefaultStringEncoding);
|
|
|
|
|
|
2000-10-23 06:18:03 +00:00
|
|
|
|
NSStringClass = self;
|
2000-10-30 18:00:27 +00:00
|
|
|
|
[self setVersion: 1];
|
2000-10-23 06:18:03 +00:00
|
|
|
|
NSMutableStringClass = [NSMutableString class];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
NSDataClass = [NSData class];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSPlaceholderStringClass = [GSPlaceholderString class];
|
2000-10-23 06:18:03 +00:00
|
|
|
|
GSStringClass = [GSString class];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSMutableStringClass = [GSMutableString class];
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Set up infrastructure for placeholder strings.
|
|
|
|
|
*/
|
|
|
|
|
defaultPlaceholderString = (GSPlaceholderString*)
|
|
|
|
|
NSAllocateObject(GSPlaceholderStringClass, 0, NSDefaultMallocZone());
|
|
|
|
|
placeholderMap = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks,
|
|
|
|
|
NSNonRetainedObjectMapValueCallBacks, 0);
|
|
|
|
|
placeholderLock = [NSLock new];
|
1996-01-23 23:30:05 +00:00
|
|
|
|
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_REGISTER_PRINTF_FUNCTION
|
2002-04-29 12:20:08 +00:00
|
|
|
|
if (register_printf_function ('@',
|
|
|
|
|
handle_printf_atsign,
|
1996-05-07 01:14:54 +00:00
|
|
|
|
#if PRINTF_ATSIGN_VA_LIST
|
|
|
|
|
0))
|
|
|
|
|
#else
|
1997-09-01 21:59:51 +00:00
|
|
|
|
arginfo_func))
|
1996-05-07 01:14:54 +00:00
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-26 19:43:43 +00:00
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1995-11-19 20:29:39 +00:00
|
|
|
|
{
|
2000-10-23 06:18:03 +00:00
|
|
|
|
if (self == NSStringClass)
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* For a constant string, we return a placeholder object that can
|
|
|
|
|
* be converted to a real object when its initialisation method
|
|
|
|
|
* is called.
|
|
|
|
|
*/
|
|
|
|
|
if (z == NSDefaultMallocZone() || z == 0)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* As a special case, we can return a placeholder for a string
|
|
|
|
|
* in the default malloc zone extremely efficiently.
|
|
|
|
|
*/
|
|
|
|
|
return defaultPlaceholderString;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
id obj;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* For anything other than the default zone, we need to
|
|
|
|
|
* locate the correct placeholder in the (lock protected)
|
|
|
|
|
* table of placeholders.
|
|
|
|
|
*/
|
|
|
|
|
[placeholderLock lock];
|
|
|
|
|
obj = (id)NSMapGet(placeholderMap, (void*)z);
|
|
|
|
|
if (obj == nil)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* There is no placeholder object for this zone, so we
|
|
|
|
|
* create a new one and use that.
|
|
|
|
|
*/
|
|
|
|
|
obj = (id)NSAllocateObject(GSPlaceholderStringClass, 0, z);
|
|
|
|
|
NSMapInsert(placeholderMap, (void*)z, (void*)obj);
|
|
|
|
|
}
|
|
|
|
|
[placeholderLock unlock];
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (GSObjCIsKindOf(self, GSStringClass) == YES)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Called +allocWithZone: on private string class"];
|
|
|
|
|
return nil; /* NOT REACHED */
|
2000-08-07 22:00:31 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* For user provided strings, we simply allocate an object of
|
|
|
|
|
* the given class.
|
|
|
|
|
*/
|
2000-08-07 22:00:31 +00:00
|
|
|
|
return NSAllocateObject (self, 0, z);
|
|
|
|
|
}
|
1995-11-19 20:29:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-10-02 13:29:54 +00:00
|
|
|
|
/**
|
2002-10-02 14:05:07 +00:00
|
|
|
|
* Return the class used to store constant strings (those ascii strings
|
|
|
|
|
* placed in the source code using the @"this is a string" syntax).<br />
|
|
|
|
|
* Use this method to obtain the constant string class rather than
|
|
|
|
|
* using the obsolete name <em>NXConstantString</em> in your code ...
|
|
|
|
|
* with more recent compiler versions the name of this class is variable
|
|
|
|
|
* (and will automatically be changed by GNUstep to avoid conflicts
|
|
|
|
|
* with the default implementation in the Objective-C runtime library).
|
2002-10-02 13:29:54 +00:00
|
|
|
|
*/
|
2001-06-06 15:18:28 +00:00
|
|
|
|
+ (Class) constantStringClass
|
|
|
|
|
{
|
2002-10-02 14:05:07 +00:00
|
|
|
|
return [NXConstantString class];
|
2001-06-06 15:18:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
// Creating Temporary Strings
|
|
|
|
|
|
1999-11-26 19:43:43 +00:00
|
|
|
|
+ (id) string
|
1997-05-03 17:05:57 +00:00
|
|
|
|
{
|
1999-06-03 10:59:25 +00:00
|
|
|
|
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] init]);
|
1997-05-03 17:05:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-26 19:43:43 +00:00
|
|
|
|
+ (id) stringWithString: (NSString*)aString
|
1997-12-08 20:04:16 +00:00
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
NSString *obj;
|
|
|
|
|
|
|
|
|
|
obj = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
obj = [obj initWithString: aString];
|
|
|
|
|
return AUTORELEASE(obj);
|
1997-12-08 20:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-26 19:43:43 +00:00
|
|
|
|
+ (id) stringWithCharacters: (const unichar*)chars
|
2002-03-13 13:46:12 +00:00
|
|
|
|
length: (unsigned int)length
|
1997-05-03 18:05:21 +00:00
|
|
|
|
{
|
2000-10-29 14:52:33 +00:00
|
|
|
|
NSString *obj;
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
obj = [self allocWithZone: NSDefaultMallocZone()];
|
2000-10-29 14:52:33 +00:00
|
|
|
|
obj = [obj initWithCharacters: chars length: length];
|
|
|
|
|
return AUTORELEASE(obj);
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-26 19:43:43 +00:00
|
|
|
|
+ (id) stringWithCString: (const char*) byteString
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2000-10-29 14:52:33 +00:00
|
|
|
|
NSString *obj;
|
2001-10-18 15:42:29 +00:00
|
|
|
|
unsigned length = byteString ? strlen(byteString) : 0;
|
2000-10-29 14:52:33 +00:00
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
obj = [self allocWithZone: NSDefaultMallocZone()];
|
2000-10-29 14:52:33 +00:00
|
|
|
|
obj = [obj initWithCString: byteString length: length];
|
|
|
|
|
return AUTORELEASE(obj);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-26 19:43:43 +00:00
|
|
|
|
+ (id) stringWithCString: (const char*)byteString
|
2002-03-13 13:46:12 +00:00
|
|
|
|
length: (unsigned int)length
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2000-10-29 14:52:33 +00:00
|
|
|
|
NSString *obj;
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
obj = [self allocWithZone: NSDefaultMallocZone()];
|
2000-10-29 14:52:33 +00:00
|
|
|
|
obj = [obj initWithCString: byteString length: length];
|
|
|
|
|
return AUTORELEASE(obj);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-09-30 04:54:43 +00:00
|
|
|
|
+ (id) stringWithUTF8String: (const char *)bytes
|
2000-09-12 23:09:50 +00:00
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
NSString *obj;
|
|
|
|
|
|
|
|
|
|
obj = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
obj = [obj initWithUTF8String: bytes];
|
|
|
|
|
return AUTORELEASE(obj);
|
2000-09-12 23:09:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-26 19:43:43 +00:00
|
|
|
|
+ (id) stringWithContentsOfFile: (NSString *)path
|
1997-03-03 20:07:35 +00:00
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
NSString *obj;
|
|
|
|
|
|
|
|
|
|
obj = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
obj = [obj initWithContentsOfFile: path];
|
|
|
|
|
return AUTORELEASE(obj);
|
1997-03-03 20:07:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-09-12 23:09:50 +00:00
|
|
|
|
+ (id) stringWithContentsOfURL: (NSURL *)url
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
NSString *obj;
|
|
|
|
|
|
|
|
|
|
obj = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
obj = [obj initWithContentsOfURL: url];
|
|
|
|
|
return AUTORELEASE(obj);
|
2000-09-12 23:09:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-26 19:43:43 +00:00
|
|
|
|
+ (id) stringWithFormat: (NSString*)format,...
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
id ret;
|
|
|
|
|
|
|
|
|
|
va_start(ap, format);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
if (format == nil)
|
|
|
|
|
ret = nil;
|
|
|
|
|
else
|
1999-06-03 10:59:25 +00:00
|
|
|
|
ret = AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
|
initWithFormat: format arguments: ap]);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
va_end(ap);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-26 19:43:43 +00:00
|
|
|
|
+ (id) stringWithFormat: (NSString*)format
|
|
|
|
|
arguments: (va_list)argList
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-06-03 10:59:25 +00:00
|
|
|
|
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
|
initWithFormat: format arguments: argList]);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
// Initializing Newly Allocated Strings
|
1995-04-03 01:35:42 +00:00
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/** <init />
|
|
|
|
|
* This is the most basic initialiser for unicode strings.
|
|
|
|
|
* In the GNUstep implementation, your subclasses may override
|
|
|
|
|
* this initialiser in order to have all others function.
|
|
|
|
|
*/
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
2002-03-13 13:46:12 +00:00
|
|
|
|
length: (unsigned int)length
|
1999-11-26 19:43:43 +00:00
|
|
|
|
freeWhenDone: (BOOL)flag
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2000-09-27 15:26:16 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCharacters: (const unichar*)chars
|
2002-03-13 13:46:12 +00:00
|
|
|
|
length: (unsigned int)length
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-07-02 13:26:37 +00:00
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
2003-01-03 20:14:47 +00:00
|
|
|
|
unsigned int i;
|
|
|
|
|
BOOL isAscii = YES;
|
2000-09-27 15:26:16 +00:00
|
|
|
|
|
2000-10-20 10:30:51 +00:00
|
|
|
|
if (chars == 0)
|
2000-09-27 15:26:16 +00:00
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
2002-08-20 15:07:58 +00:00
|
|
|
|
format: @"null pointer but non-zero length"];
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (chars[i] >= 128)
|
|
|
|
|
{
|
|
|
|
|
isAscii = NO;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (isAscii == YES)
|
|
|
|
|
{
|
|
|
|
|
char *s;
|
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
|
s = NSZoneMalloc(GSObjCZone(self), length);
|
2000-10-20 10:30:51 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
s[i] = (unsigned char)chars[i];
|
|
|
|
|
}
|
|
|
|
|
self = [self initWithCStringNoCopy: s
|
|
|
|
|
length: length
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unichar *s;
|
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
|
s = NSZoneMalloc(GSObjCZone(self), sizeof(unichar)*length);
|
2000-10-20 10:30:51 +00:00
|
|
|
|
|
2000-09-27 15:26:16 +00:00
|
|
|
|
memcpy(s, chars, sizeof(unichar)*length);
|
2000-10-20 10:30:51 +00:00
|
|
|
|
self = [self initWithCharactersNoCopy: s
|
|
|
|
|
length: length
|
|
|
|
|
freeWhenDone: YES];
|
2000-09-27 15:26:16 +00:00
|
|
|
|
}
|
1999-07-02 13:26:37 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-03-10 12:57:43 +00:00
|
|
|
|
self = [self initWithCharactersNoCopy: (unichar*)0
|
2001-07-02 16:15:22 +00:00
|
|
|
|
length: 0
|
|
|
|
|
freeWhenDone: NO];
|
1999-07-02 13:26:37 +00:00
|
|
|
|
}
|
1998-10-15 05:03:16 +00:00
|
|
|
|
|
2000-09-27 15:26:16 +00:00
|
|
|
|
return self;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (id) initWithCStringNoCopy: (char*)byteString
|
2002-03-13 13:46:12 +00:00
|
|
|
|
length: (unsigned int)length
|
2000-09-27 15:26:16 +00:00
|
|
|
|
freeWhenDone: (BOOL)flag
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unichar *buf = 0;
|
|
|
|
|
unsigned int l = 0;
|
2000-10-31 19:07:35 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (GSToUnicode(&buf, &l, byteString, length, _DefaultStringEncoding,
|
|
|
|
|
[self zone], 0) == NO)
|
|
|
|
|
{
|
|
|
|
|
DESTROY(self);
|
|
|
|
|
}
|
|
|
|
|
else
|
2000-10-31 19:07:35 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (flag == YES && byteString != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(byteString), byteString);
|
|
|
|
|
}
|
|
|
|
|
self = [self initWithCharactersNoCopy: buf length: l freeWhenDone: YES];
|
2000-10-31 19:07:35 +00:00
|
|
|
|
}
|
1995-04-03 01:35:42 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (id) initWithCString: (const char*)byteString length: (unsigned int)length
|
1997-05-03 18:05:21 +00:00
|
|
|
|
{
|
1999-07-02 13:26:37 +00:00
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
2000-10-31 16:17:33 +00:00
|
|
|
|
char *s = NSZoneMalloc(GSObjCZone(self), length);
|
2000-09-27 15:26:16 +00:00
|
|
|
|
|
|
|
|
|
if (byteString != 0)
|
1999-07-02 13:26:37 +00:00
|
|
|
|
{
|
|
|
|
|
memcpy(s, byteString, length);
|
|
|
|
|
}
|
2000-09-27 15:26:16 +00:00
|
|
|
|
self = [self initWithCStringNoCopy: s length: length freeWhenDone: YES];
|
1999-07-02 13:26:37 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2000-09-27 15:26:16 +00:00
|
|
|
|
self = [self initWithCStringNoCopy: 0 length: 0 freeWhenDone: NO];
|
1999-07-02 13:26:37 +00:00
|
|
|
|
}
|
1998-10-15 05:03:16 +00:00
|
|
|
|
|
2000-09-27 15:26:16 +00:00
|
|
|
|
return self;
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
1997-01-09 16:24:07 +00:00
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (id) initWithCString: (const char*)byteString
|
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
return [self initWithCString: byteString
|
2000-09-27 15:26:16 +00:00
|
|
|
|
length: (byteString ? strlen(byteString) : 0)];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (id) initWithString: (NSString*)string
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-07-02 13:26:37 +00:00
|
|
|
|
unsigned length = [string length];
|
1998-10-15 05:03:16 +00:00
|
|
|
|
|
1999-07-02 13:26:37 +00:00
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
2000-10-31 16:17:33 +00:00
|
|
|
|
unichar *s = NSZoneMalloc(GSObjCZone(self), sizeof(unichar)*length);
|
2000-09-27 15:26:16 +00:00
|
|
|
|
|
1999-07-02 13:26:37 +00:00
|
|
|
|
[string getCharacters: s];
|
2000-09-27 15:26:16 +00:00
|
|
|
|
self = [self initWithCharactersNoCopy: s
|
|
|
|
|
length: length
|
|
|
|
|
freeWhenDone: YES];
|
1999-07-02 13:26:37 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-03-10 12:57:43 +00:00
|
|
|
|
self = [self initWithCharactersNoCopy: (unichar*)0
|
2000-09-27 15:26:16 +00:00
|
|
|
|
length: 0
|
|
|
|
|
freeWhenDone: NO];
|
1999-07-02 13:26:37 +00:00
|
|
|
|
}
|
2000-09-27 15:26:16 +00:00
|
|
|
|
return self;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-09-30 04:54:43 +00:00
|
|
|
|
- (id) initWithUTF8String: (const char *)bytes
|
2000-09-12 23:09:50 +00:00
|
|
|
|
{
|
2001-02-11 22:45:01 +00:00
|
|
|
|
unsigned length = 0;
|
2001-03-08 05:26:32 +00:00
|
|
|
|
|
|
|
|
|
if (bytes == NULL)
|
2001-02-11 22:45:01 +00:00
|
|
|
|
{
|
|
|
|
|
NSDebugMLog(@"bytes is NULL");
|
|
|
|
|
}
|
|
|
|
|
else
|
2001-03-08 05:26:32 +00:00
|
|
|
|
{
|
|
|
|
|
length = strlen(bytes);
|
|
|
|
|
}
|
2000-09-18 23:28:00 +00:00
|
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned i = 0;
|
2000-09-27 15:26:16 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (_ByteEncodingOk)
|
2000-09-30 04:54:43 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
/*
|
|
|
|
|
* If it's ok to store ascii strings as internal C strings,
|
|
|
|
|
* check to see if we have in fact got an ascii string.
|
|
|
|
|
*/
|
|
|
|
|
while (i < length)
|
2000-09-30 04:54:43 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (((unsigned char*)bytes)[i] > 127)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
2000-09-30 04:54:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
|
2000-09-30 04:54:43 +00:00
|
|
|
|
if (i == length)
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithCString: bytes length: length];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned int l = 0;
|
2000-09-30 04:54:43 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, bytes, length, NSUTF8StringEncoding,
|
|
|
|
|
GSObjCZone(self), 0) == NO)
|
|
|
|
|
{
|
|
|
|
|
DESTROY(self);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithCharactersNoCopy: u
|
|
|
|
|
length: l
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
}
|
2000-09-30 04:54:43 +00:00
|
|
|
|
}
|
2000-09-18 23:28:00 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-03-10 12:57:43 +00:00
|
|
|
|
self = [self initWithCharactersNoCopy: (unichar*)0
|
2000-09-27 15:26:16 +00:00
|
|
|
|
length: 0
|
|
|
|
|
freeWhenDone: NO];
|
2000-09-18 23:28:00 +00:00
|
|
|
|
}
|
2000-09-27 15:26:16 +00:00
|
|
|
|
return self;
|
2000-09-12 23:09:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Invokes -initWithFormat:locale:arguments: with a nil locale.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (id) initWithFormat: (NSString*)format,...
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, format);
|
2000-07-07 19:19:26 +00:00
|
|
|
|
self = [self initWithFormat: format locale: nil arguments: ap];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
va_end(ap);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Invokes -initWithFormat:locale:arguments:
|
|
|
|
|
*/
|
2000-07-07 19:19:26 +00:00
|
|
|
|
- (id) initWithFormat: (NSString*)format
|
2001-11-12 13:01:09 +00:00
|
|
|
|
locale: (NSDictionary*)locale, ...
|
2000-07-07 19:19:26 +00:00
|
|
|
|
{
|
2001-11-12 13:01:09 +00:00
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, locale);
|
2001-07-07 04:45:38 +00:00
|
|
|
|
return [self initWithFormat: format locale: locale arguments: ap];
|
2001-11-12 13:01:09 +00:00
|
|
|
|
va_end(ap);
|
|
|
|
|
return self;
|
2000-07-07 19:19:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Invokes -initWithFormat:locale:arguments: with a nil locale.
|
|
|
|
|
*/
|
2000-07-07 19:19:26 +00:00
|
|
|
|
- (id) initWithFormat: (NSString*)format
|
2002-06-09 07:31:58 +00:00
|
|
|
|
arguments: (va_list)argList
|
2000-07-07 19:19:26 +00:00
|
|
|
|
{
|
2002-06-09 07:31:58 +00:00
|
|
|
|
return [self initWithFormat: format locale: nil arguments: argList];
|
2000-07-07 19:19:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Initialises the string using the specified format and locale
|
|
|
|
|
* to format the following arguments.
|
|
|
|
|
*/
|
2001-02-02 06:14:42 +00:00
|
|
|
|
- (id) initWithFormat: (NSString*)format
|
|
|
|
|
locale: (NSDictionary*)locale
|
2002-06-09 07:31:58 +00:00
|
|
|
|
arguments: (va_list)argList
|
2001-02-02 06:14:42 +00:00
|
|
|
|
{
|
|
|
|
|
FormatBuf_t f;
|
|
|
|
|
unichar *fmt;
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
len = [format length];
|
|
|
|
|
fmt = objc_malloc((len+1)*sizeof(unichar));
|
|
|
|
|
[format getCharacters: fmt];
|
|
|
|
|
fmt[len] = '\0';
|
|
|
|
|
f.z = NSDefaultMallocZone();
|
|
|
|
|
f.buf = NSZoneMalloc(f.z, 100*sizeof(unichar));
|
|
|
|
|
f.len = 0;
|
|
|
|
|
f.size = 100;
|
2002-06-09 07:31:58 +00:00
|
|
|
|
GSFormat(&f, fmt, argList, locale);
|
2001-02-02 06:14:42 +00:00
|
|
|
|
objc_free(fmt);
|
|
|
|
|
// don't use noCopy because f.size > f.len!
|
|
|
|
|
self = [self initWithCharacters: f.buf length: f.len];
|
|
|
|
|
NSZoneFree(f.z, f.buf);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
1995-04-03 01:35:42 +00:00
|
|
|
|
/* xxx Change this when we have non-CString classes */
|
|
|
|
|
- (id) initWithFormat: (NSString*)format
|
2000-07-07 19:19:26 +00:00
|
|
|
|
locale: (NSDictionary*)locale
|
2002-06-09 07:31:58 +00:00
|
|
|
|
arguments: (va_list)argList
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2000-03-24 00:25:42 +00:00
|
|
|
|
#if defined(HAVE_VSPRINTF) || defined(HAVE_VASPRINTF)
|
2000-12-14 09:47:02 +00:00
|
|
|
|
const char *format_cp = [format lossyCString];
|
1996-05-31 15:10:53 +00:00
|
|
|
|
int format_len = strlen (format_cp);
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_VASPRINTF
|
2000-03-24 00:25:42 +00:00
|
|
|
|
char *buf;
|
|
|
|
|
int printed_len = 0;
|
|
|
|
|
NSString *ret;
|
|
|
|
|
|
2002-05-08 05:43:15 +00:00
|
|
|
|
#ifndef HAVE_REGISTER_PRINTF_FUNCTION
|
2000-10-31 16:17:33 +00:00
|
|
|
|
NSZone *z = GSObjCZone(self);
|
2000-03-24 00:25:42 +00:00
|
|
|
|
|
|
|
|
|
/* 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
|
2002-04-29 12:20:08 +00:00
|
|
|
|
strings, placed in a non-writable section of the executable, and
|
|
|
|
|
writing to them will cause a segfault.) */
|
2000-03-24 00:25:42 +00:00
|
|
|
|
char format_cp_copy[format_len+1];
|
|
|
|
|
char *atsign_pos; /* points to a location inside format_cp_copy */
|
|
|
|
|
char *format_to_go = format_cp_copy;
|
|
|
|
|
char *buf_l;
|
|
|
|
|
#define _PRINTF_BUF_LEN 256
|
|
|
|
|
int printed_local_len, avail_len = _PRINTF_BUF_LEN;
|
|
|
|
|
int cstring_len;
|
|
|
|
|
|
|
|
|
|
buf = NSZoneMalloc(z, _PRINTF_BUF_LEN);
|
|
|
|
|
strcpy (format_cp_copy, format_cp);
|
|
|
|
|
/* Loop once for each `%@' in the format string. */
|
|
|
|
|
while ((atsign_pos = strstr (format_to_go, "%@")))
|
|
|
|
|
{
|
|
|
|
|
const char *cstring;
|
|
|
|
|
char *formatter_pos; // Position for formatter.
|
2002-04-29 12:20:08 +00:00
|
|
|
|
|
2000-03-24 00:25:42 +00:00
|
|
|
|
/* 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 '%@' */
|
|
|
|
|
printed_local_len = VASPRINTF_LENGTH (vasprintf (&buf_l,
|
2002-06-09 07:31:58 +00:00
|
|
|
|
format_to_go, argList));
|
2000-03-24 00:25:42 +00:00
|
|
|
|
if(buf_l)
|
|
|
|
|
{
|
|
|
|
|
if(avail_len < printed_local_len+1)
|
|
|
|
|
{
|
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
|
|
|
|
buf = NSZoneRealloc(z, buf,
|
2000-09-08 18:04:51 +00:00
|
|
|
|
printed_len+printed_local_len+_PRINTF_BUF_LEN);
|
2000-03-24 00:25:42 +00:00
|
|
|
|
avail_len += _PRINTF_BUF_LEN;
|
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
free(buf_l);
|
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
}
|
|
|
|
|
memcpy(&buf[printed_len], buf_l, printed_local_len+1);
|
|
|
|
|
avail_len -= printed_local_len;
|
|
|
|
|
printed_len += printed_local_len;
|
|
|
|
|
free(buf_l);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSMallocException
|
2000-09-08 18:04:51 +00:00
|
|
|
|
format: @"No available memory"];
|
2000-03-24 00:25:42 +00:00
|
|
|
|
}
|
|
|
|
|
/* Skip arguments used in last vsprintf(). */
|
|
|
|
|
while ((formatter_pos = strchr(format_to_go, '%')))
|
|
|
|
|
{
|
|
|
|
|
char *spec_pos; // Position of conversion specifier.
|
2002-04-29 12:20:08 +00:00
|
|
|
|
|
2000-03-24 00:25:42 +00:00
|
|
|
|
if (*(formatter_pos+1) == '%')
|
|
|
|
|
{
|
|
|
|
|
format_to_go = formatter_pos+2;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
spec_pos = strpbrk(formatter_pos+1, "dioxXucsfeEgGpn\0");
|
|
|
|
|
switch (*spec_pos)
|
|
|
|
|
{
|
|
|
|
|
#ifndef powerpc
|
|
|
|
|
/* FIXME: vsprintf on powerpc apparently advances the arg list
|
2002-04-29 12:20:08 +00:00
|
|
|
|
so this doesn't need to be done. Make a more general check
|
2000-03-24 00:25:42 +00:00
|
|
|
|
for this */
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case 'd': case 'i': case 'o':
|
|
|
|
|
case 'x': case 'X': case 'u': case 'c':
|
2002-06-09 07:31:58 +00:00
|
|
|
|
va_arg(argList, int);
|
2000-03-24 00:25:42 +00:00
|
|
|
|
break;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case 's':
|
2000-03-24 00:25:42 +00:00
|
|
|
|
if (*(spec_pos - 1) == '*')
|
2002-06-09 07:31:58 +00:00
|
|
|
|
va_arg(argList, int*);
|
|
|
|
|
va_arg(argList, char*);
|
2000-03-24 00:25:42 +00:00
|
|
|
|
break;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case 'f': case 'e': case 'E': case 'g': case 'G':
|
2002-06-09 07:31:58 +00:00
|
|
|
|
va_arg(argList, double);
|
2000-03-24 00:25:42 +00:00
|
|
|
|
break;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case 'p':
|
2002-06-09 07:31:58 +00:00
|
|
|
|
va_arg(argList, void*);
|
2000-03-24 00:25:42 +00:00
|
|
|
|
break;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case 'n':
|
2002-06-09 07:31:58 +00:00
|
|
|
|
va_arg(argList, int*);
|
2000-03-24 00:25:42 +00:00
|
|
|
|
break;
|
|
|
|
|
#endif /* NOT powerpc */
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case '\0':
|
2000-03-24 00:25:42 +00:00
|
|
|
|
spec_pos--;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
format_to_go = spec_pos+1;
|
|
|
|
|
}
|
|
|
|
|
/* Get a C-string (char*) from the String object, and print it. */
|
2002-06-09 07:31:58 +00:00
|
|
|
|
cstring = [[(id) va_arg (argList, id) description] lossyCString];
|
2000-03-24 00:25:42 +00:00
|
|
|
|
if (!cstring)
|
|
|
|
|
cstring = "<null string>";
|
|
|
|
|
cstring_len = strlen(cstring);
|
|
|
|
|
|
|
|
|
|
if(cstring_len)
|
|
|
|
|
{
|
|
|
|
|
if(avail_len < cstring_len+1)
|
|
|
|
|
{
|
|
|
|
|
buf = NSZoneRealloc(z, buf,
|
|
|
|
|
printed_len+cstring_len+_PRINTF_BUF_LEN);
|
|
|
|
|
avail_len += _PRINTF_BUF_LEN;
|
|
|
|
|
}
|
|
|
|
|
memcpy(&buf[printed_len], cstring, cstring_len+1);
|
|
|
|
|
avail_len -= cstring_len;
|
|
|
|
|
printed_len += cstring_len;
|
|
|
|
|
}
|
|
|
|
|
/* Skip over this `%@', and look for another one. */
|
|
|
|
|
format_to_go = atsign_pos + 2;
|
|
|
|
|
}
|
|
|
|
|
/* Print the rest of the string after the last `%@'. */
|
|
|
|
|
printed_local_len = VASPRINTF_LENGTH (vasprintf (&buf_l,
|
2002-06-09 07:31:58 +00:00
|
|
|
|
format_to_go, argList));
|
2000-03-24 00:25:42 +00:00
|
|
|
|
if(buf_l)
|
|
|
|
|
{
|
|
|
|
|
if(avail_len < printed_local_len+1)
|
|
|
|
|
{
|
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
|
|
|
|
buf = NSZoneRealloc(z, buf,
|
2000-09-08 18:04:51 +00:00
|
|
|
|
printed_len+printed_local_len+_PRINTF_BUF_LEN);
|
2000-03-24 00:25:42 +00:00
|
|
|
|
avail_len += _PRINTF_BUF_LEN;
|
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
free(buf_l);
|
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
}
|
|
|
|
|
memcpy(&buf[printed_len], buf_l, printed_local_len+1);
|
|
|
|
|
avail_len -= printed_local_len;
|
|
|
|
|
printed_len += printed_local_len;
|
|
|
|
|
free(buf_l);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSMallocException
|
|
|
|
|
format: @"No available memory"];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else /* HAVE_VSPRINTF */
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/* The available libc has `register_printf_function()', so the `%@'
|
2000-03-24 00:25:42 +00:00
|
|
|
|
printf directive is handled by printf and friends. */
|
2002-06-09 07:31:58 +00:00
|
|
|
|
printed_len = VASPRINTF_LENGTH (vasprintf (&buf, format_cp, argList));
|
2000-03-24 00:25:42 +00:00
|
|
|
|
|
|
|
|
|
if(!buf)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSMallocException
|
|
|
|
|
format: @"No available memory"];
|
|
|
|
|
}
|
|
|
|
|
#endif /* !HAVE_REGISTER_PRINTF_FUNCTION */
|
|
|
|
|
|
|
|
|
|
ret = [self initWithCString: buf];
|
2002-05-08 05:43:15 +00:00
|
|
|
|
#ifndef HAVE_REGISTER_PRINTF_FUNCTION
|
2000-03-24 00:25:42 +00:00
|
|
|
|
NSZoneFree(z, buf);
|
|
|
|
|
#else
|
|
|
|
|
free(buf);
|
|
|
|
|
#endif
|
|
|
|
|
return ret;
|
|
|
|
|
#else
|
1996-07-15 18:41:44 +00:00
|
|
|
|
/* xxx horrible disgusting BUFFER_EXTRA arbitrary limit; fix this! */
|
1997-01-09 16:01:52 +00:00
|
|
|
|
#define BUFFER_EXTRA 1024*500
|
1996-07-15 18:41:44 +00:00
|
|
|
|
char buf[format_len + BUFFER_EXTRA];
|
|
|
|
|
int printed_len = 0;
|
|
|
|
|
|
2002-05-08 05:43:15 +00:00
|
|
|
|
#ifndef HAVE_REGISTER_PRINTF_FUNCTION
|
1996-07-15 18:41:44 +00:00
|
|
|
|
/* 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
|
2002-04-29 12:20:08 +00:00
|
|
|
|
strings, placed in a non-writable section of the executable, and
|
|
|
|
|
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;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
char *formatter_pos; // Position for formatter.
|
|
|
|
|
|
1996-07-15 18:41:44 +00:00
|
|
|
|
/* 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,
|
2002-06-09 07:31:58 +00:00
|
|
|
|
format_to_go, argList));
|
1997-09-01 21:59:51 +00:00
|
|
|
|
/* Skip arguments used in last vsprintf(). */
|
|
|
|
|
while ((formatter_pos = strchr(format_to_go, '%')))
|
|
|
|
|
{
|
|
|
|
|
char *spec_pos; // Position of conversion specifier.
|
|
|
|
|
|
|
|
|
|
if (*(formatter_pos+1) == '%')
|
|
|
|
|
{
|
|
|
|
|
format_to_go = formatter_pos+2;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
spec_pos = strpbrk(formatter_pos+1, "dioxXucsfeEgGpn\0");
|
|
|
|
|
switch (*spec_pos)
|
|
|
|
|
{
|
1999-08-25 14:47:19 +00:00
|
|
|
|
#ifndef powerpc
|
|
|
|
|
/* FIXME: vsprintf on powerpc apparently advances the arg list
|
2002-04-29 12:20:08 +00:00
|
|
|
|
so this doesn't need to be done. Make a more general check
|
1999-08-25 14:47:19 +00:00
|
|
|
|
for this */
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case 'd': case 'i': case 'o':
|
|
|
|
|
case 'x': case 'X': case 'u': case 'c':
|
2002-06-09 07:31:58 +00:00
|
|
|
|
(void)va_arg(argList, int);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
break;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case 's':
|
1997-09-18 14:56:47 +00:00
|
|
|
|
if (*(spec_pos - 1) == '*')
|
2002-06-09 07:31:58 +00:00
|
|
|
|
(void)va_arg(argList, int*);
|
|
|
|
|
(void)va_arg(argList, char*);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
break;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case 'f': case 'e': case 'E': case 'g': case 'G':
|
2002-06-09 07:31:58 +00:00
|
|
|
|
(void)va_arg(argList, double);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
break;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case 'p':
|
2002-06-09 07:31:58 +00:00
|
|
|
|
(void)va_arg(argList, void*);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
break;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case 'n':
|
2002-06-09 07:31:58 +00:00
|
|
|
|
(void)va_arg(argList, int*);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
break;
|
1999-08-25 14:47:19 +00:00
|
|
|
|
#endif /* NOT powerpc */
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case '\0':
|
1997-09-01 21:59:51 +00:00
|
|
|
|
spec_pos--;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
format_to_go = spec_pos+1;
|
|
|
|
|
}
|
1996-07-15 18:41:44 +00:00
|
|
|
|
/* Get a C-string (char*) from the String object, and print it. */
|
2002-06-09 07:31:58 +00:00
|
|
|
|
cstring = [[(id) va_arg (argList, id) description] lossyCString];
|
1997-11-12 15:37:27 +00:00
|
|
|
|
if (!cstring)
|
|
|
|
|
cstring = "<null string>";
|
1996-07-15 18:41:44 +00:00
|
|
|
|
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,
|
2002-06-09 07:31:58 +00:00
|
|
|
|
format_to_go, argList));
|
1996-07-15 18:41:44 +00:00
|
|
|
|
}
|
|
|
|
|
#else
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/* The available libc has `register_printf_function()', so the `%@'
|
1996-07-15 18:41:44 +00:00
|
|
|
|
printf directive is handled by printf and friends. */
|
2002-06-09 07:31:58 +00:00
|
|
|
|
printed_len = VSPRINTF_LENGTH (vsprintf (buf, format_cp, argList));
|
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);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
return [self initWithCString: buf];
|
2000-03-24 00:25:42 +00:00
|
|
|
|
#endif /* HAVE_VASPRINTF */
|
|
|
|
|
#else /* HAVE_VSPRINTF || HAVE_VASPRINTF */
|
1996-07-15 18:41:44 +00:00
|
|
|
|
[self notImplemented: _cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
return self;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2001-02-02 06:14:42 +00:00
|
|
|
|
#endif
|
1995-04-03 01:35:42 +00:00
|
|
|
|
|
2002-10-04 09:32:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Initialises the receiver with the supplied data, using the
|
|
|
|
|
* specified encoding.<br />
|
|
|
|
|
* For NSUnicodeStringEncoding and NSUTF8String encoding, a Byte Order
|
|
|
|
|
* Marker (if present at the start of the data) is removed automatically.<br />
|
|
|
|
|
* If the data can not be interpreted using the encoding, the receiver
|
|
|
|
|
* is released and nil is returned.
|
|
|
|
|
*/
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (id) initWithData: (NSData*)data
|
1999-07-02 13:26:37 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2002-03-08 06:57:03 +00:00
|
|
|
|
unsigned len = [data length];
|
|
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
|
{
|
2003-03-10 12:57:43 +00:00
|
|
|
|
self = [self initWithCharactersNoCopy: (unichar*)0
|
2002-03-16 09:54:50 +00:00
|
|
|
|
length: 0
|
|
|
|
|
freeWhenDone: NO];
|
2002-03-08 06:57:03 +00:00
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
else if (_ByteEncodingOk == YES
|
|
|
|
|
&& (encoding==_DefaultStringEncoding || encoding==NSASCIIStringEncoding))
|
1998-10-15 05:03:16 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
char *s;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
/*
|
|
|
|
|
* We can only create an internal C string if the default C string
|
|
|
|
|
* encoding is Ok, and the specified encoding matches it.
|
2002-04-29 12:20:08 +00:00
|
|
|
|
*/
|
2002-03-16 09:54:50 +00:00
|
|
|
|
s = NSZoneMalloc(GSObjCZone(self), len);
|
2002-03-08 06:57:03 +00:00
|
|
|
|
[data getBytes: s];
|
|
|
|
|
self = [self initWithCStringNoCopy: s length: len freeWhenDone: YES];
|
2002-04-29 12:20:08 +00:00
|
|
|
|
}
|
2000-09-30 04:54:43 +00:00
|
|
|
|
else if (encoding == NSUTF8StringEncoding)
|
|
|
|
|
{
|
2002-10-04 09:32:48 +00:00
|
|
|
|
const unsigned char *bytes = [data bytes];
|
|
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the data begins with the UTF8 Byte Order Marker (as a
|
|
|
|
|
* signature for UTF8 data) we must remove it.
|
|
|
|
|
*/
|
|
|
|
|
if (len > 2 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
|
|
|
|
|
{
|
|
|
|
|
len -= 3;
|
|
|
|
|
bytes += 3;
|
|
|
|
|
}
|
2000-09-30 04:54:43 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (_ByteEncodingOk)
|
2000-09-30 04:54:43 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
/*
|
|
|
|
|
* If it's ok to store ascii strings as internal C strings,
|
|
|
|
|
* check to see if we have in fact got an ascii string.
|
|
|
|
|
*/
|
|
|
|
|
while (i < len)
|
2000-09-30 04:54:43 +00:00
|
|
|
|
{
|
2002-10-04 09:32:48 +00:00
|
|
|
|
if ((bytes)[i] > 127)
|
2002-03-16 09:54:50 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
2000-09-30 04:54:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
|
2002-03-08 06:57:03 +00:00
|
|
|
|
if (i == len)
|
2000-09-30 04:54:43 +00:00
|
|
|
|
{
|
2002-03-08 06:57:03 +00:00
|
|
|
|
self = [self initWithCString: bytes length: len];
|
2000-09-30 04:54:43 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned int l = 0;
|
2000-09-30 04:54:43 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, bytes, len, NSUTF8StringEncoding,
|
|
|
|
|
GSObjCZone(self), 0) == NO)
|
2002-03-08 06:57:03 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
DESTROY(self);
|
2002-03-08 06:57:03 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
self = [self initWithCharactersNoCopy: u
|
|
|
|
|
length: l
|
|
|
|
|
freeWhenDone: YES];
|
2002-03-08 06:57:03 +00:00
|
|
|
|
}
|
2000-09-30 04:54:43 +00:00
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
else if (encoding == NSUnicodeStringEncoding)
|
1998-01-08 15:25:59 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (len%2 != 0)
|
2001-04-11 16:51:17 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
DESTROY(self); // Not valid unicode data.
|
2001-04-11 16:51:17 +00:00
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
BOOL swapped = NO;
|
|
|
|
|
unsigned char *b;
|
|
|
|
|
unichar *uptr;
|
1999-07-02 13:26:37 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
b = (unsigned char*)[data bytes];
|
|
|
|
|
uptr = (unichar*)b;
|
2002-04-06 06:33:34 +00:00
|
|
|
|
if (*uptr == byteOrderMark)
|
2000-09-27 15:26:16 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
b = (unsigned char*)++uptr;
|
|
|
|
|
len -= sizeof(unichar);
|
2000-09-27 15:26:16 +00:00
|
|
|
|
}
|
2002-04-06 06:33:34 +00:00
|
|
|
|
else if (*uptr == byteOrderMarkSwapped)
|
2000-09-27 15:26:16 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
b = (unsigned char*)++uptr;
|
|
|
|
|
len -= sizeof(unichar);
|
|
|
|
|
swapped = YES;
|
2000-09-27 15:26:16 +00:00
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (len == 0)
|
2002-03-08 06:57:03 +00:00
|
|
|
|
{
|
2003-03-10 12:57:43 +00:00
|
|
|
|
self = [self initWithCharactersNoCopy: (unichar*)0
|
2002-03-16 09:54:50 +00:00
|
|
|
|
length: 0
|
|
|
|
|
freeWhenDone: NO];
|
2002-03-08 06:57:03 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned char *u;
|
|
|
|
|
|
|
|
|
|
u = (unsigned char*)NSZoneMalloc(GSObjCZone(self), len);
|
|
|
|
|
if (swapped == YES)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; i += 2)
|
|
|
|
|
{
|
|
|
|
|
u[i] = b[i + 1];
|
|
|
|
|
u[i + 1] = b[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
memcpy(u, b, len);
|
|
|
|
|
}
|
|
|
|
|
self = [self initWithCharactersNoCopy: (unichar*)u
|
|
|
|
|
length: len/2
|
2002-03-08 06:57:03 +00:00
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
}
|
2000-09-27 15:26:16 +00:00
|
|
|
|
}
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned char *b;
|
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned l = 0;
|
|
|
|
|
|
|
|
|
|
b = (unsigned char*)[data bytes];
|
2002-03-18 08:31:39 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, b, len, encoding, GSObjCZone(self), 0) == NO)
|
2002-03-16 09:54:50 +00:00
|
|
|
|
{
|
|
|
|
|
DESTROY(self);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithCharactersNoCopy: u
|
|
|
|
|
length: l
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
}
|
|
|
|
|
}
|
1997-05-03 18:05:21 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-04 09:08:09 +00:00
|
|
|
|
/**
|
2002-10-04 09:32:48 +00:00
|
|
|
|
* <p>Initialises the receiver with the contents of the file at path.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>Invokes [NSData-initWithContentsOfFile:] to read the file, then
|
2002-10-04 09:08:09 +00:00
|
|
|
|
* examines the data to infer its encoding type, and converts the
|
2002-10-04 09:32:48 +00:00
|
|
|
|
* data to a string using -initWithData:encoding:
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>The encoding to use is determined as follows ... if the data begins
|
|
|
|
|
* with the 16-bit unicode Byte Order Marker, then it is assumed to be
|
|
|
|
|
* unicode data in the appropriate ordering and converted as such.<br />
|
|
|
|
|
* If it begins with a UTF8 representation of the BOM, the UTF8 encoding
|
|
|
|
|
* is used.<br />
|
|
|
|
|
* Otherwise, the default C String encoding is used.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>Releases the receiver and returns nil if the file could not be read
|
2002-10-04 09:08:09 +00:00
|
|
|
|
* and converted to a string.
|
2002-10-04 09:32:48 +00:00
|
|
|
|
* </p>
|
2002-10-04 09:08:09 +00:00
|
|
|
|
*/
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (id) initWithContentsOfFile: (NSString*)path
|
1998-01-08 15:25:59 +00:00
|
|
|
|
{
|
2002-10-04 09:08:09 +00:00
|
|
|
|
NSStringEncoding enc = _DefaultStringEncoding;
|
|
|
|
|
NSData *d;
|
|
|
|
|
unsigned int len;
|
2001-03-20 03:42:41 +00:00
|
|
|
|
const unichar *test;
|
1997-05-03 18:05:21 +00:00
|
|
|
|
|
2002-10-04 09:08:09 +00:00
|
|
|
|
d = [[NSDataClass alloc] initWithContentsOfFile: path];
|
1999-07-02 13:26:37 +00:00
|
|
|
|
if (d == nil)
|
2002-10-04 09:08:09 +00:00
|
|
|
|
{
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
len = [d length];
|
2001-03-20 03:42:41 +00:00
|
|
|
|
if (len == 0)
|
2002-10-04 09:08:09 +00:00
|
|
|
|
{
|
2002-10-04 09:32:48 +00:00
|
|
|
|
RELEASE(d);
|
|
|
|
|
RELEASE(self);
|
2002-10-04 09:08:09 +00:00
|
|
|
|
return @"";
|
|
|
|
|
}
|
1999-07-02 13:26:37 +00:00
|
|
|
|
test = [d bytes];
|
2002-10-04 09:08:09 +00:00
|
|
|
|
if ((test != NULL) && (len > 1))
|
2001-03-20 03:42:41 +00:00
|
|
|
|
{
|
2002-10-04 09:08:09 +00:00
|
|
|
|
if ((test[0] == byteOrderMark) || (test[0] == byteOrderMarkSwapped))
|
|
|
|
|
{
|
|
|
|
|
/* somebody set up us the BOM! */
|
|
|
|
|
enc = NSUnicodeStringEncoding;
|
|
|
|
|
}
|
2002-10-04 09:32:48 +00:00
|
|
|
|
else if (len > 2 && test[0] == 0xEF && test[1] == 0xBB && test[2] == 0xBF)
|
|
|
|
|
{
|
|
|
|
|
enc = NSUTF8StringEncoding;
|
|
|
|
|
}
|
2001-03-20 03:42:41 +00:00
|
|
|
|
}
|
2002-10-04 09:08:09 +00:00
|
|
|
|
self = [self initWithData: d encoding: enc];
|
|
|
|
|
RELEASE(d);
|
|
|
|
|
if (self == nil)
|
2001-03-20 03:42:41 +00:00
|
|
|
|
{
|
2002-10-04 09:08:09 +00:00
|
|
|
|
NSWarnMLog(@"Contents of file '%@' are not string data", path);
|
2001-03-20 03:42:41 +00:00
|
|
|
|
}
|
2002-10-04 09:08:09 +00:00
|
|
|
|
return self;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-09-12 23:09:50 +00:00
|
|
|
|
- (id) initWithContentsOfURL: (NSURL*)url
|
|
|
|
|
{
|
2002-10-04 09:08:09 +00:00
|
|
|
|
NSStringEncoding enc = _DefaultStringEncoding;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
NSData *d = [NSDataClass dataWithContentsOfURL: url];
|
2001-03-20 03:42:41 +00:00
|
|
|
|
unsigned int len = [d length];
|
2003-06-07 23:33:25 +00:00
|
|
|
|
const unichar *test;
|
2000-09-12 23:09:50 +00:00
|
|
|
|
|
|
|
|
|
if (d == nil)
|
2002-10-04 09:08:09 +00:00
|
|
|
|
{
|
|
|
|
|
NSWarnMLog(@"Contents of URL '%@' are not readable", url);
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
2001-03-20 03:42:41 +00:00
|
|
|
|
if (len == 0)
|
2002-10-04 09:08:09 +00:00
|
|
|
|
{
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return @"";
|
|
|
|
|
}
|
2000-09-12 23:09:50 +00:00
|
|
|
|
test = [d bytes];
|
2002-10-04 09:08:09 +00:00
|
|
|
|
if ((test != NULL) && (len > 1))
|
2001-03-20 03:42:41 +00:00
|
|
|
|
{
|
2002-10-04 09:08:09 +00:00
|
|
|
|
if ((test[0] == byteOrderMark) || (test[0] == byteOrderMarkSwapped))
|
|
|
|
|
{
|
|
|
|
|
enc = NSUnicodeStringEncoding;
|
|
|
|
|
}
|
2002-10-04 09:32:48 +00:00
|
|
|
|
else if (len > 2 && test[0] == 0xEF && test[1] == 0xBB && test[2] == 0xBF)
|
|
|
|
|
{
|
|
|
|
|
enc = NSUTF8StringEncoding;
|
|
|
|
|
}
|
2001-03-20 03:42:41 +00:00
|
|
|
|
}
|
2002-10-04 09:08:09 +00:00
|
|
|
|
self = [self initWithData: d encoding: enc];
|
|
|
|
|
if (self == nil)
|
2001-03-20 03:42:41 +00:00
|
|
|
|
{
|
2002-10-04 09:08:09 +00:00
|
|
|
|
NSWarnMLog(@"Contents of URL '%@' are not string data", url);
|
2001-03-20 03:42:41 +00:00
|
|
|
|
}
|
2002-10-04 09:08:09 +00:00
|
|
|
|
return self;
|
2000-09-12 23:09:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (id) init
|
|
|
|
|
{
|
2003-03-10 12:57:43 +00:00
|
|
|
|
self = [self initWithCharactersNoCopy: (unichar*)0
|
2001-07-02 16:15:22 +00:00
|
|
|
|
length: 0
|
|
|
|
|
freeWhenDone: 0];
|
1999-07-02 13:26:37 +00:00
|
|
|
|
return self;
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
1995-04-03 01:35:42 +00:00
|
|
|
|
|
|
|
|
|
// Getting a String's Length
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (unsigned int) length
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-05-03 18:05:21 +00:00
|
|
|
|
return 0;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Accessing Characters
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (unichar) characterAtIndex: (unsigned int)index
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-05-03 18:05:21 +00:00
|
|
|
|
return (unichar)0;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Inefficient. Should be overridden */
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
|
|
|
|
{
|
2000-10-05 15:17:18 +00:00
|
|
|
|
[self getCharacters: buffer range: ((NSRange){0, [self length]})];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Inefficient. Should be overridden */
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
1999-05-20 09:20:46 +00:00
|
|
|
|
range: (NSRange)aRange
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-06-04 15:15:59 +00:00
|
|
|
|
unsigned l = [self length];
|
|
|
|
|
unsigned i;
|
2002-03-13 13:46:12 +00:00
|
|
|
|
unichar (*caiImp)(NSString*, SEL, unsigned int);
|
1999-06-04 15:15:59 +00:00
|
|
|
|
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, l);
|
1999-06-04 15:15:59 +00:00
|
|
|
|
|
2002-11-10 06:19:17 +00:00
|
|
|
|
caiImp = (unichar (*)(NSString*,SEL,unsigned))
|
|
|
|
|
[self methodForSelector: caiSel];
|
2000-10-05 15:17:18 +00:00
|
|
|
|
|
1996-09-02 13:38:19 +00:00
|
|
|
|
for (i = 0; i < aRange.length; i++)
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
buffer[i] = (*caiImp)(self, caiSel, aRange.location + i);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Combining Strings
|
|
|
|
|
|
|
|
|
|
- (NSString*) stringByAppendingFormat: (NSString*)format,...
|
|
|
|
|
{
|
2001-04-12 09:11:31 +00:00
|
|
|
|
va_list ap;
|
|
|
|
|
id ret;
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
va_start(ap, format);
|
2002-04-29 12:20:08 +00:00
|
|
|
|
ret = [self stringByAppendingString:
|
2001-04-12 09:11:31 +00:00
|
|
|
|
[NSString stringWithFormat: format arguments: ap]];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
va_end(ap);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) stringByAppendingString: (NSString*)aString
|
|
|
|
|
{
|
2000-09-27 15:26:16 +00:00
|
|
|
|
unsigned len = [self length];
|
|
|
|
|
unsigned otherLength = [aString length];
|
2000-10-31 16:17:33 +00:00
|
|
|
|
NSZone *z = GSObjCZone(self);
|
2000-09-27 15:26:16 +00:00
|
|
|
|
unichar *s = NSZoneMalloc(z, (len+otherLength)*sizeof(unichar));
|
|
|
|
|
NSString *tmp;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self getCharacters: s];
|
2000-09-27 15:26:16 +00:00
|
|
|
|
[aString getCharacters: s + len];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
tmp = [[NSStringClass allocWithZone: z] initWithCharactersNoCopy: s
|
2000-10-23 06:18:03 +00:00
|
|
|
|
length: len + otherLength freeWhenDone: YES];
|
1999-06-03 10:59:25 +00:00
|
|
|
|
return AUTORELEASE(tmp);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Dividing Strings into Substrings
|
|
|
|
|
|
|
|
|
|
- (NSArray*) componentsSeparatedByString: (NSString*)separator
|
|
|
|
|
{
|
2001-04-12 09:11:31 +00:00
|
|
|
|
NSRange search;
|
|
|
|
|
NSRange complete;
|
|
|
|
|
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
|
|
|
|
|
1997-03-03 20:07:35 +00:00
|
|
|
|
search = NSMakeRange (0, [self length]);
|
1997-09-13 17:52:31 +00:00
|
|
|
|
complete = search;
|
1996-03-19 01:44:01 +00:00
|
|
|
|
found = [self rangeOfString: separator];
|
2001-04-12 09:11:31 +00:00
|
|
|
|
while (found.length != 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
|
|
|
|
{
|
|
|
|
|
NSRange current;
|
1997-09-13 17:52:31 +00:00
|
|
|
|
|
1996-03-19 01:44:01 +00:00
|
|
|
|
current = NSMakeRange (search.location,
|
2001-04-12 09:11:31 +00:00
|
|
|
|
found.location - search.location);
|
1999-06-21 08:30:26 +00:00
|
|
|
|
[array addObject: [self substringWithRange: current]];
|
1997-09-13 17:52:31 +00:00
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
search = NSMakeRange (found.location + found.length,
|
2001-04-12 09:11:31 +00:00
|
|
|
|
complete.length - found.location - found.length);
|
2002-04-29 12:20:08 +00:00
|
|
|
|
found = [self rangeOfString: separator
|
2001-04-12 09:11:31 +00:00
|
|
|
|
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
|
|
|
|
}
|
1997-03-03 20:07:35 +00:00
|
|
|
|
// Add the last search string range
|
1999-06-21 08:30:26 +00:00
|
|
|
|
[array addObject: [self substringWithRange: 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
|
|
|
|
}
|
|
|
|
|
|
2003-01-24 12:06:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a substring of the receiver from character at the specified
|
|
|
|
|
* index to the end of the string.<br />
|
|
|
|
|
* So, supplying an index of 3 would return a substring consisting of
|
|
|
|
|
* the entire string apart from the first three character (those would
|
|
|
|
|
* be at index 0, 1, and 2).<br />
|
|
|
|
|
* If the supplied index is greater than or equal to the length of the
|
|
|
|
|
* receiver an exception is raised.
|
|
|
|
|
*/
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (NSString*) substringFromIndex: (unsigned int)index
|
1999-06-21 08:30:26 +00:00
|
|
|
|
{
|
|
|
|
|
return [self substringWithRange: ((NSRange){index, [self length]-index})];
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-24 12:06:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a substring of the receiver from the start of the
|
|
|
|
|
* string to (but not including) the specified index position.<br />
|
|
|
|
|
* So, supplying an index of 3 would return a substring consisting of
|
|
|
|
|
* the first three characters of the receiver.<br />
|
|
|
|
|
* If the supplied index is greater than the length of the receiver
|
|
|
|
|
* an exception is raised.
|
|
|
|
|
*/
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (NSString*) substringToIndex: (unsigned int)index
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
return [self substringWithRange: ((NSRange){0,index})];;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-24 12:06:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* An obsolete name for -substringWithRange: ... deprecated.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSString*) substringFromRange: (NSRange)aRange
|
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
return [self substringWithRange: aRange];
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-24 12:06:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a substring of the receiver containing the characters
|
|
|
|
|
* in aRange.<br />
|
|
|
|
|
* If aRange specifies any character position not
|
|
|
|
|
* present in the receiver, an exception is raised.<br />
|
|
|
|
|
* If aRange has a length of zero, an empty string is returned.
|
|
|
|
|
*/
|
1999-06-21 08:30:26 +00:00
|
|
|
|
- (NSString*) substringWithRange: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
unichar *buf;
|
|
|
|
|
id ret;
|
|
|
|
|
unsigned len = [self length];
|
|
|
|
|
|
|
|
|
|
GS_RANGE_CHECK(aRange, len);
|
1997-12-08 20:04:16 +00:00
|
|
|
|
|
|
|
|
|
if (aRange.length == 0)
|
|
|
|
|
return @"";
|
2000-10-31 16:17:33 +00:00
|
|
|
|
buf = NSZoneMalloc(GSObjCZone(self), sizeof(unichar)*aRange.length);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self getCharacters: buf range: aRange];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
ret = [[NSStringClass allocWithZone: NSDefaultMallocZone()]
|
2000-09-27 15:26:16 +00:00
|
|
|
|
initWithCharactersNoCopy: buf length: aRange.length freeWhenDone: YES];
|
1999-06-03 10:59:25 +00:00
|
|
|
|
return AUTORELEASE(ret);
|
1997-12-08 20:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
// 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]);
|
2001-04-12 09:11:31 +00:00
|
|
|
|
|
1999-05-06 14:42:26 +00:00
|
|
|
|
return [self rangeOfCharacterFromSet: aSet
|
2001-04-12 09:11:31 +00:00
|
|
|
|
options: 0
|
|
|
|
|
range: all];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
2002-03-13 13:46:12 +00:00
|
|
|
|
options: (unsigned int)mask
|
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
|
|
|
|
NSRange all = NSMakeRange(0, [self length]);
|
2001-04-12 09:11:31 +00:00
|
|
|
|
|
1999-05-06 14:42:26 +00:00
|
|
|
|
return [self rangeOfCharacterFromSet: aSet
|
2001-04-12 09:11:31 +00:00
|
|
|
|
options: mask
|
|
|
|
|
range: all];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
2002-03-13 13:46:12 +00:00
|
|
|
|
options: (unsigned int)mask
|
1998-11-19 20:42:06 +00:00
|
|
|
|
range: (NSRange)aRange
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2003-01-03 20:14:47 +00:00
|
|
|
|
unsigned int i;
|
|
|
|
|
unsigned int start;
|
|
|
|
|
unsigned int stop;
|
2001-04-12 09:11:31 +00:00
|
|
|
|
int step;
|
|
|
|
|
NSRange range;
|
2002-03-13 13:46:12 +00:00
|
|
|
|
unichar (*cImp)(id, SEL, unsigned int);
|
1999-02-04 13:49:27 +00:00
|
|
|
|
BOOL (*mImp)(id, SEL, unichar);
|
(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
|
|
|
|
|
1998-11-19 20:42:06 +00:00
|
|
|
|
i = [self length];
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, i);
|
(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
|
|
|
|
|
|
|
|
|
if ((mask & NSBackwardsSearch) == NSBackwardsSearch)
|
|
|
|
|
{
|
1998-07-15 12:48:57 +00:00
|
|
|
|
start = NSMaxRange(aRange)-1; stop = aRange.location-1; step = -1;
|
(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
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
start = aRange.location; stop = NSMaxRange(aRange); step = 1;
|
|
|
|
|
}
|
2002-02-05 18:20:47 +00:00
|
|
|
|
range.location = NSNotFound;
|
(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
|
|
|
|
range.length = 0;
|
1999-02-04 13:49:27 +00:00
|
|
|
|
|
2002-11-10 06:19:17 +00:00
|
|
|
|
cImp = (unichar(*)(id,SEL,unsigned int))
|
|
|
|
|
[self methodForSelector: caiSel];
|
1999-02-04 13:49:27 +00:00
|
|
|
|
mImp = (BOOL(*)(id,SEL,unichar))
|
|
|
|
|
[aSet methodForSelector: cMemberSel];
|
|
|
|
|
|
|
|
|
|
for (i = start; i != stop; i += step)
|
(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
|
|
|
|
{
|
1999-05-19 20:05:49 +00:00
|
|
|
|
unichar letter = (unichar)(*cImp)(self, caiSel, i);
|
2001-04-12 09:11:31 +00:00
|
|
|
|
|
1999-02-04 13:49:27 +00:00
|
|
|
|
if ((*mImp)(aSet, cMemberSel, letter))
|
(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
|
|
|
|
{
|
|
|
|
|
range = NSMakeRange(i, 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return range;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Invokes -rangeOfString:options: with the options mask
|
|
|
|
|
* set to zero.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSRange) rangeOfString: (NSString*)string
|
|
|
|
|
{
|
2001-04-12 09:11:31 +00:00
|
|
|
|
NSRange all = NSMakeRange(0, [self length]);
|
|
|
|
|
|
1999-05-06 14:42:26 +00:00
|
|
|
|
return [self rangeOfString: string
|
2001-04-12 09:11:31 +00:00
|
|
|
|
options: 0
|
|
|
|
|
range: all];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Invokes -rangeOfString:options:range: with the range set
|
|
|
|
|
* set to the range of the whole of the reciever.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSRange) rangeOfString: (NSString*)string
|
2002-03-13 13:46:12 +00:00
|
|
|
|
options: (unsigned int)mask
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2001-04-12 09:11:31 +00:00
|
|
|
|
NSRange all = NSMakeRange(0, [self length]);
|
|
|
|
|
|
1999-05-06 14:42:26 +00:00
|
|
|
|
return [self rangeOfString: string
|
2001-04-12 09:11:31 +00:00
|
|
|
|
options: mask
|
|
|
|
|
range: all];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the range giving the location and length of the first
|
|
|
|
|
* occurrence of aString within aRange.
|
|
|
|
|
* <br/>
|
|
|
|
|
* If aString does not exist in the receiver (an empty
|
|
|
|
|
* string is never considered to exist in the receiver),
|
|
|
|
|
* the length of the returned range is zero.
|
|
|
|
|
* <br/>
|
|
|
|
|
* If aString is nil, an exception is raised.
|
|
|
|
|
* <br/>
|
|
|
|
|
* If any part of aRange lies outside the range of the
|
|
|
|
|
* receiver, an exception is raised.
|
|
|
|
|
* <br/>
|
|
|
|
|
* The options mask may contain the following options -
|
|
|
|
|
* <list>
|
|
|
|
|
* <item>NSCaseInsensitiveSearch</item>
|
|
|
|
|
* <item>NSLiteralSearch</item>
|
|
|
|
|
* <item>NSBackwardsSearch</item>
|
|
|
|
|
* <item>NSAnchoredSearch</item>
|
|
|
|
|
* </list>
|
|
|
|
|
*/
|
|
|
|
|
- (NSRange) rangeOfString: (NSString *)aString
|
|
|
|
|
options: (unsigned int)mask
|
|
|
|
|
range: (NSRange)aRange
|
1999-05-20 09:20:46 +00:00
|
|
|
|
{
|
1999-05-25 14:27:42 +00:00
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException format: @"range of nil"];
|
1999-05-20 09:20:46 +00:00
|
|
|
|
return strRangeNsNs(self, aString, mask, aRange);
|
1996-09-02 13:38:19 +00:00
|
|
|
|
}
|
1995-04-03 01:35:42 +00:00
|
|
|
|
|
2000-06-06 16:50:52 +00:00
|
|
|
|
- (unsigned int) indexOfString: (NSString *)substring
|
|
|
|
|
{
|
2001-04-12 09:11:31 +00:00
|
|
|
|
NSRange range = {0, [self length]};
|
2000-06-06 16:50:52 +00:00
|
|
|
|
|
2001-04-12 09:11:31 +00:00
|
|
|
|
range = [self rangeOfString: substring options: 0 range: range];
|
|
|
|
|
return range.length ? range.location : NSNotFound;
|
2000-06-06 16:50:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (unsigned int) indexOfString: (NSString*)substring
|
|
|
|
|
fromIndex: (unsigned int)index
|
2000-06-06 16:50:52 +00:00
|
|
|
|
{
|
2001-04-12 09:11:31 +00:00
|
|
|
|
NSRange range = {index, [self length] - index};
|
2000-06-06 16:50:52 +00:00
|
|
|
|
|
2001-04-12 09:11:31 +00:00
|
|
|
|
range = [self rangeOfString: substring options: 0 range: range];
|
|
|
|
|
return range.length ? range.location : NSNotFound;
|
2000-06-06 16:50:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
// Determining Composed Character Sequences
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (unsigned int)anIndex
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-05-10 06:35:41 +00:00
|
|
|
|
unsigned start;
|
|
|
|
|
unsigned end;
|
|
|
|
|
unsigned length = [self length];
|
2001-03-19 10:48:26 +00:00
|
|
|
|
unichar ch;
|
2002-03-13 13:46:12 +00:00
|
|
|
|
unichar (*caiImp)(NSString*, SEL, unsigned int);
|
2001-03-19 10:48:26 +00:00
|
|
|
|
NSCharacterSet *nbSet = [NSCharacterSet nonBaseCharacterSet];
|
1997-05-03 18:05:21 +00:00
|
|
|
|
|
1999-05-10 06:35:41 +00:00
|
|
|
|
if (anIndex >= length)
|
|
|
|
|
[NSException raise: NSRangeException format:@"Invalid location."];
|
2002-11-10 06:19:17 +00:00
|
|
|
|
caiImp = (unichar (*)(NSString*,SEL,unsigned))
|
|
|
|
|
[self methodForSelector: caiSel];
|
2001-03-19 10:48:26 +00:00
|
|
|
|
|
|
|
|
|
for (start = anIndex; start > 0; start--)
|
|
|
|
|
{
|
|
|
|
|
ch = (*caiImp)(self, caiSel, start);
|
|
|
|
|
if ([nbSet characterIsMember: ch] == NO)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
for (end = start+1; end < length; end++)
|
|
|
|
|
{
|
|
|
|
|
ch = (*caiImp)(self, caiSel, end);
|
|
|
|
|
if ([nbSet characterIsMember: ch] == NO)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
return NSMakeRange(start, end-start);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
// Identifying and Comparing Strings
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSComparisonResult) compare: (NSString*)aString
|
|
|
|
|
{
|
1999-05-06 14:42:26 +00:00
|
|
|
|
return [self compare: aString options: 0];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
- (NSComparisonResult) compare: (NSString*)aString
|
2002-03-13 13:46:12 +00:00
|
|
|
|
options: (unsigned int)mask
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
return [self compare: aString options: mask
|
1999-05-19 20:05:49 +00:00
|
|
|
|
range: ((NSRange){0, [self length]})];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
// xxx Should implement full POSIX.2 collate
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSComparisonResult) compare: (NSString*)aString
|
2002-03-13 13:46:12 +00:00
|
|
|
|
options: (unsigned int)mask
|
1999-05-19 20:05:49 +00:00
|
|
|
|
range: (NSRange)aRange
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-05-25 14:27:42 +00:00
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException format: @"compare with nil"];
|
1999-05-20 09:20:46 +00:00
|
|
|
|
return strCompNsNs(self, aString, mask, aRange);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) hasPrefix: (NSString*)aString
|
|
|
|
|
{
|
2001-04-12 09:11:31 +00:00
|
|
|
|
NSRange range;
|
|
|
|
|
|
2001-03-27 17:30:07 +00:00
|
|
|
|
range = [self rangeOfString: aString options: NSAnchoredSearch];
|
|
|
|
|
return (range.length > 0) ? YES : NO;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) hasSuffix: (NSString*)aString
|
|
|
|
|
{
|
2001-04-12 09:11:31 +00:00
|
|
|
|
NSRange range;
|
|
|
|
|
|
2001-03-27 17:30:07 +00:00
|
|
|
|
range = [self rangeOfString: aString
|
|
|
|
|
options: NSAnchoredSearch | NSBackwardsSearch];
|
|
|
|
|
return (range.length > 0) ? YES : NO;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (BOOL) isEqual: (id)anObject
|
|
|
|
|
{
|
1999-07-02 13:26:37 +00:00
|
|
|
|
if (anObject == self)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
1998-10-09 04:24:56 +00:00
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (anObject != nil && GSObjCIsInstance(anObject) == YES)
|
1999-07-02 13:26:37 +00:00
|
|
|
|
{
|
2000-10-31 16:17:33 +00:00
|
|
|
|
Class c = GSObjCClass(anObject);
|
1998-10-09 04:24:56 +00:00
|
|
|
|
|
1999-07-02 13:26:37 +00:00
|
|
|
|
if (c != nil)
|
|
|
|
|
{
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, NSStringClass))
|
1999-07-02 13:26:37 +00:00
|
|
|
|
{
|
|
|
|
|
return [self isEqualToString: anObject];
|
1998-10-09 04:24:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-07-02 13:26:37 +00:00
|
|
|
|
return NO;
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqualToString: (NSString*)aString
|
|
|
|
|
{
|
1998-08-03 15:31:33 +00:00
|
|
|
|
if ([self hash] != [aString hash])
|
|
|
|
|
return NO;
|
1999-05-20 09:20:46 +00:00
|
|
|
|
if (strCompNsNs(self, aString, 0, (NSRange){0, [self length]})
|
|
|
|
|
== NSOrderedSame)
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return YES;
|
1999-05-20 09:20:46 +00:00
|
|
|
|
return NO;
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-30 09:32:46 +00:00
|
|
|
|
/**
|
2000-10-05 15:17:18 +00:00
|
|
|
|
* Return 28-bit hash value (in 32-bit integer). The top few bits are used
|
|
|
|
|
* for other purposes in a bitfield in the concrete string subclasses, so we
|
|
|
|
|
* must not use the full unsigned integer.
|
|
|
|
|
*/
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (unsigned int) hash
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned ret = 0;
|
1998-01-08 15:25:59 +00:00
|
|
|
|
|
|
|
|
|
int len = [self length];
|
|
|
|
|
|
1999-05-19 20:05:49 +00:00
|
|
|
|
if (len > NSHashStringLength)
|
2001-04-12 09:11:31 +00:00
|
|
|
|
{
|
|
|
|
|
len = NSHashStringLength;
|
|
|
|
|
}
|
|
|
|
|
if (len > 0)
|
1998-01-08 15:25:59 +00:00
|
|
|
|
{
|
1999-05-19 20:05:49 +00:00
|
|
|
|
unichar buf[len * MAXDEC + 1];
|
|
|
|
|
GSeqStruct s = { buf, len, len * MAXDEC, 0 };
|
|
|
|
|
unichar *p;
|
|
|
|
|
unsigned char_count = 0;
|
1998-01-08 15:25:59 +00:00
|
|
|
|
|
1999-05-19 20:05:49 +00:00
|
|
|
|
[self getCharacters: buf range: NSMakeRange(0,len)];
|
|
|
|
|
GSeq_normalize(&s);
|
1998-10-06 15:11:27 +00:00
|
|
|
|
|
1999-05-19 20:05:49 +00:00
|
|
|
|
p = buf;
|
1998-10-06 15:11:27 +00:00
|
|
|
|
|
1999-05-19 20:05:49 +00:00
|
|
|
|
while (*p && char_count++ < NSHashStringLength)
|
|
|
|
|
{
|
|
|
|
|
ret = (ret << 5) + ret + *p++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The hash caching in our concrete string classes uses zero to denote
|
|
|
|
|
* an empty cache value, so we MUST NOT return a hash of zero.
|
|
|
|
|
*/
|
|
|
|
|
if (ret == 0)
|
2000-10-05 15:17:18 +00:00
|
|
|
|
ret = 0x0fffffff;
|
|
|
|
|
else
|
|
|
|
|
ret &= 0x0fffffff;
|
1999-05-19 20:05:49 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
1998-01-08 15:25:59 +00:00
|
|
|
|
else
|
2000-10-05 15:17:18 +00:00
|
|
|
|
return 0x0ffffffe; /* Hash for an empty string. */
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1995-04-03 01:35:42 +00:00
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
// Getting a Shared Prefix
|
|
|
|
|
|
|
|
|
|
- (NSString*) commonPrefixWithString: (NSString*)aString
|
2002-03-13 13:46:12 +00:00
|
|
|
|
options: (unsigned int)mask
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-05-19 20:05:49 +00:00
|
|
|
|
if (mask & NSLiteralSearch)
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-05-19 20:05:49 +00:00
|
|
|
|
int prefix_len = 0;
|
|
|
|
|
unichar *u,*w;
|
|
|
|
|
unichar a1[[self length]+1];
|
|
|
|
|
unichar *s1 = a1;
|
|
|
|
|
unichar a2[[aString length]+1];
|
|
|
|
|
unichar *s2 = a2;
|
|
|
|
|
|
|
|
|
|
u = s1;
|
|
|
|
|
[self getCharacters: s1];
|
|
|
|
|
s1[[self length]] = (unichar)0;
|
|
|
|
|
[aString getCharacters: s2];
|
|
|
|
|
s2[[aString length]] = (unichar)0;
|
|
|
|
|
u = s1;
|
|
|
|
|
w = s2;
|
|
|
|
|
|
|
|
|
|
if (mask & NSCaseInsensitiveSearch)
|
|
|
|
|
{
|
|
|
|
|
while (*s1 && *s2 && (uni_tolower(*s1) == uni_tolower(*s2)))
|
|
|
|
|
{
|
|
|
|
|
s1++;
|
|
|
|
|
s2++;
|
|
|
|
|
prefix_len++;
|
|
|
|
|
}
|
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
else
|
1999-05-19 20:05:49 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
while (*s1 && *s2 && (*s1 == *s2))
|
1999-05-19 20:05:49 +00:00
|
|
|
|
{
|
|
|
|
|
s1++;
|
|
|
|
|
s2++;
|
|
|
|
|
prefix_len++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-10-29 14:52:33 +00:00
|
|
|
|
return [NSStringClass stringWithCharacters: u length: prefix_len];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-05-19 20:05:49 +00:00
|
|
|
|
else
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2002-03-13 13:46:12 +00:00
|
|
|
|
unichar (*scImp)(NSString*, SEL, unsigned int);
|
|
|
|
|
unichar (*ocImp)(NSString*, SEL, unsigned int);
|
1999-07-12 04:27:18 +00:00
|
|
|
|
void (*sgImp)(NSString*, SEL, unichar*, NSRange) = 0;
|
|
|
|
|
void (*ogImp)(NSString*, SEL, unichar*, NSRange) = 0;
|
2002-03-13 13:46:12 +00:00
|
|
|
|
NSRange (*srImp)(NSString*, SEL, unsigned int) = 0;
|
|
|
|
|
NSRange (*orImp)(NSString*, SEL, unsigned int) = 0;
|
1999-05-19 20:05:49 +00:00
|
|
|
|
BOOL gotRangeImps = NO;
|
|
|
|
|
BOOL gotFetchImps = NO;
|
|
|
|
|
NSRange sRange;
|
|
|
|
|
NSRange oRange;
|
|
|
|
|
unsigned sLength = [self length];
|
|
|
|
|
unsigned oLength = [aString length];
|
|
|
|
|
unsigned sIndex = 0;
|
|
|
|
|
unsigned oIndex = 0;
|
|
|
|
|
|
|
|
|
|
if (!sLength)
|
|
|
|
|
return self;
|
|
|
|
|
if (!oLength)
|
|
|
|
|
return aString;
|
|
|
|
|
|
2002-11-10 06:19:17 +00:00
|
|
|
|
scImp = (unichar (*)(NSString*,SEL,unsigned))
|
|
|
|
|
[self methodForSelector: caiSel];
|
|
|
|
|
ocImp = (unichar (*)(NSString*,SEL,unsigned))
|
|
|
|
|
[aString methodForSelector: caiSel];
|
1999-05-19 20:05:49 +00:00
|
|
|
|
|
|
|
|
|
while ((sIndex < sLength) && (oIndex < oLength))
|
|
|
|
|
{
|
|
|
|
|
unichar sc = (*scImp)(self, caiSel, sIndex);
|
|
|
|
|
unichar oc = (*ocImp)(aString, caiSel, oIndex);
|
|
|
|
|
|
|
|
|
|
if (sc == oc)
|
|
|
|
|
{
|
|
|
|
|
sIndex++;
|
|
|
|
|
oIndex++;
|
|
|
|
|
}
|
|
|
|
|
else if ((mask & NSCaseInsensitiveSearch)
|
|
|
|
|
&& (uni_tolower(sc) == uni_tolower(oc)))
|
|
|
|
|
{
|
|
|
|
|
sIndex++;
|
|
|
|
|
oIndex++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (gotRangeImps == NO)
|
|
|
|
|
{
|
|
|
|
|
gotRangeImps = YES;
|
|
|
|
|
srImp=(NSRange (*)())[self methodForSelector: ranSel];
|
|
|
|
|
orImp=(NSRange (*)())[aString methodForSelector: ranSel];
|
|
|
|
|
}
|
|
|
|
|
sRange = (*srImp)(self, ranSel, sIndex);
|
|
|
|
|
oRange = (*orImp)(aString, ranSel, oIndex);
|
|
|
|
|
|
|
|
|
|
if ((sRange.length < 2) || (oRange.length < 2))
|
1999-06-21 08:30:26 +00:00
|
|
|
|
return [self substringWithRange: NSMakeRange(0, sIndex)];
|
1999-05-19 20:05:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GSEQ_MAKE(sBuf, sSeq, sRange.length);
|
|
|
|
|
GSEQ_MAKE(oBuf, oSeq, oRange.length);
|
|
|
|
|
|
|
|
|
|
if (gotFetchImps == NO)
|
|
|
|
|
{
|
|
|
|
|
gotFetchImps = YES;
|
|
|
|
|
sgImp=(void (*)())[self methodForSelector: gcrSel];
|
|
|
|
|
ogImp=(void (*)())[aString methodForSelector: gcrSel];
|
|
|
|
|
}
|
|
|
|
|
(*sgImp)(self, gcrSel, sBuf, sRange);
|
|
|
|
|
(*ogImp)(aString, gcrSel, oBuf, oRange);
|
|
|
|
|
|
|
|
|
|
if (GSeq_compare(&sSeq, &oSeq) == NSOrderedSame)
|
|
|
|
|
{
|
|
|
|
|
sIndex += sRange.length;
|
|
|
|
|
oIndex += oRange.length;
|
|
|
|
|
}
|
|
|
|
|
else if (mask & NSCaseInsensitiveSearch)
|
|
|
|
|
{
|
|
|
|
|
GSeq_lowercase(&sSeq);
|
|
|
|
|
GSeq_lowercase(&oSeq);
|
|
|
|
|
if (GSeq_compare(&sSeq, &oSeq) == NSOrderedSame)
|
|
|
|
|
{
|
|
|
|
|
sIndex += sRange.length;
|
|
|
|
|
oIndex += oRange.length;
|
|
|
|
|
}
|
|
|
|
|
else
|
1999-06-21 08:30:26 +00:00
|
|
|
|
return [self substringWithRange: NSMakeRange(0,sIndex)];
|
1999-05-19 20:05:49 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
1999-06-21 08:30:26 +00:00
|
|
|
|
return [self substringWithRange: NSMakeRange(0,sIndex)];
|
1999-05-19 20:05:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-06-21 08:30:26 +00:00
|
|
|
|
return [self substringWithRange: NSMakeRange(0, sIndex)];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-30 09:32:46 +00:00
|
|
|
|
/**
|
|
|
|
|
* Determines the smallest range of lines containing aRange and returns
|
|
|
|
|
* the information as a range.<br />
|
|
|
|
|
* Calls -getLineStart:end:contentsEnd:forRange: to do the work.
|
|
|
|
|
*/
|
1999-05-19 20:05:49 +00:00
|
|
|
|
- (NSRange) lineRangeForRange: (NSRange)aRange
|
1998-01-08 15:25:59 +00:00
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
unsigned startIndex;
|
|
|
|
|
unsigned lineEndIndex;
|
1998-01-08 15:25:59 +00:00
|
|
|
|
|
|
|
|
|
[self getLineStart: &startIndex
|
|
|
|
|
end: &lineEndIndex
|
|
|
|
|
contentsEnd: NULL
|
1999-05-06 14:42:26 +00:00
|
|
|
|
forRange: aRange];
|
1998-01-08 15:25:59 +00:00
|
|
|
|
return NSMakeRange(startIndex, lineEndIndex - startIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-30 09:32:46 +00:00
|
|
|
|
/**
|
|
|
|
|
* Determines the smallest range of lines containing aRange and returns
|
|
|
|
|
* the locations in that range.<br />
|
|
|
|
|
* Lines are delimited by any of these character sequences, the longest
|
|
|
|
|
* (CRLF) sequence preferred.
|
|
|
|
|
* <list>
|
|
|
|
|
* <item>U+000A (linefeed)</item>
|
|
|
|
|
* <item>U+000D (carriage return)</item>
|
|
|
|
|
* <item>U+2028 (Unicode line separator)</item>
|
|
|
|
|
* <item>U+2029 (Unicode paragraph separator)</item>
|
2002-08-30 14:54:07 +00:00
|
|
|
|
* <item>U+000D U+000A (CRLF)</item>
|
2002-08-30 09:32:46 +00:00
|
|
|
|
* </list>
|
|
|
|
|
* The index of the first character of the line at or before aRange is
|
|
|
|
|
* returned in startIndex.<br />
|
|
|
|
|
* The index of the first character of the next line after the line terminator
|
|
|
|
|
* is returned in endIndex.<br />
|
2002-08-30 14:54:07 +00:00
|
|
|
|
* The index of the last character before the line terminator is returned
|
2002-08-30 09:32:46 +00:00
|
|
|
|
* contentsEndIndex.<br />
|
|
|
|
|
* Raises an NSRangeException if the range is invalid, but permits the index
|
|
|
|
|
* arguments to be null pointers (in which case no value is returned in that
|
|
|
|
|
* argument).
|
|
|
|
|
*/
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (void) getLineStart: (unsigned int *)startIndex
|
|
|
|
|
end: (unsigned int *)lineEndIndex
|
|
|
|
|
contentsEnd: (unsigned int *)contentsEndIndex
|
1999-11-26 19:43:43 +00:00
|
|
|
|
forRange: (NSRange)aRange
|
1998-01-08 15:25:59 +00:00
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
unichar thischar;
|
2002-07-29 19:37:40 +00:00
|
|
|
|
unsigned start, end, len, termlen;
|
2002-03-13 13:46:12 +00:00
|
|
|
|
unichar (*caiImp)(NSString*, SEL, unsigned int);
|
1998-01-08 15:25:59 +00:00
|
|
|
|
|
|
|
|
|
len = [self length];
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, len);
|
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
|
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
1999-06-21 08:30:26 +00:00
|
|
|
|
start = aRange.location;
|
1998-01-08 15:25:59 +00:00
|
|
|
|
|
1999-02-04 13:49:27 +00:00
|
|
|
|
if (startIndex)
|
1999-06-24 19:30:29 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (start == 0)
|
1999-06-24 19:30:29 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
*startIndex = 0;
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
start--;
|
|
|
|
|
while (start > 0)
|
|
|
|
|
{
|
|
|
|
|
BOOL done = NO;
|
1999-06-21 08:30:26 +00:00
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
|
thischar = (*caiImp)(self, caiSel, start);
|
|
|
|
|
switch (thischar)
|
1999-06-24 19:30:29 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case (unichar)0x000A:
|
|
|
|
|
case (unichar)0x000D:
|
|
|
|
|
case (unichar)0x2028:
|
|
|
|
|
case (unichar)0x2029:
|
1999-06-24 19:30:29 +00:00
|
|
|
|
done = YES;
|
|
|
|
|
break;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
default:
|
1999-06-24 19:30:29 +00:00
|
|
|
|
start--;
|
|
|
|
|
break;
|
2001-04-12 09:11:31 +00:00
|
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
|
if (done)
|
|
|
|
|
break;
|
2001-04-12 09:11:31 +00:00
|
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
|
if (start == 0)
|
|
|
|
|
{
|
2001-06-01 16:27:06 +00:00
|
|
|
|
thischar = (*caiImp)(self, caiSel, start);
|
|
|
|
|
switch (thischar)
|
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case (unichar)0x000A:
|
|
|
|
|
case (unichar)0x000D:
|
|
|
|
|
case (unichar)0x2028:
|
|
|
|
|
case (unichar)0x2029:
|
2001-06-01 16:27:06 +00:00
|
|
|
|
start++;
|
|
|
|
|
break;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
default:
|
2001-06-01 16:27:06 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2001-06-01 16:27:06 +00:00
|
|
|
|
{
|
|
|
|
|
start++;
|
|
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
|
*startIndex = start;
|
|
|
|
|
}
|
|
|
|
|
}
|
1998-01-08 15:25:59 +00:00
|
|
|
|
|
1999-02-04 13:49:27 +00:00
|
|
|
|
if (lineEndIndex || contentsEndIndex)
|
1998-01-08 15:25:59 +00:00
|
|
|
|
{
|
2002-07-29 19:37:40 +00:00
|
|
|
|
BOOL found = NO;
|
2002-08-28 21:40:31 +00:00
|
|
|
|
end = aRange.location;
|
|
|
|
|
if(aRange.length)
|
|
|
|
|
{
|
|
|
|
|
end += (aRange.length - 1);
|
|
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
|
while (end < len)
|
1999-06-21 08:30:26 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
thischar = (*caiImp)(self, caiSel, end);
|
|
|
|
|
switch (thischar)
|
1999-06-21 08:30:26 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
case (unichar)0x000A:
|
|
|
|
|
case (unichar)0x000D:
|
|
|
|
|
case (unichar)0x2028:
|
|
|
|
|
case (unichar)0x2029:
|
2002-07-29 19:37:40 +00:00
|
|
|
|
found = YES;
|
1999-06-21 08:30:26 +00:00
|
|
|
|
break;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
default:
|
1999-06-21 08:30:26 +00:00
|
|
|
|
break;
|
2001-04-12 09:11:31 +00:00
|
|
|
|
}
|
1999-06-21 08:30:26 +00:00
|
|
|
|
end++;
|
2002-07-29 19:37:40 +00:00
|
|
|
|
if (found)
|
1999-06-21 08:30:26 +00:00
|
|
|
|
break;
|
2001-04-12 09:11:31 +00:00
|
|
|
|
}
|
2002-07-29 19:37:40 +00:00
|
|
|
|
termlen = 1;
|
2001-06-01 16:27:06 +00:00
|
|
|
|
if (lineEndIndex)
|
1999-06-21 08:30:26 +00:00
|
|
|
|
{
|
2001-06-01 16:27:06 +00:00
|
|
|
|
if (end < len
|
2002-07-29 19:37:40 +00:00
|
|
|
|
&& ((*caiImp)(self, caiSel, end-1) == (unichar)0x000D)
|
|
|
|
|
&& ((*caiImp)(self, caiSel, end) == (unichar)0x000A))
|
1999-06-21 08:30:26 +00:00
|
|
|
|
{
|
2001-06-01 16:27:06 +00:00
|
|
|
|
*lineEndIndex = end+1;
|
2002-07-29 19:37:40 +00:00
|
|
|
|
termlen = 2;
|
1999-06-21 08:30:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2001-06-01 16:27:06 +00:00
|
|
|
|
{
|
|
|
|
|
*lineEndIndex = end;
|
|
|
|
|
}
|
1999-06-21 08:30:26 +00:00
|
|
|
|
}
|
2001-06-01 16:27:06 +00:00
|
|
|
|
if (contentsEndIndex)
|
1999-06-21 08:30:26 +00:00
|
|
|
|
{
|
2002-07-29 19:37:40 +00:00
|
|
|
|
if (found)
|
2001-06-01 16:27:06 +00:00
|
|
|
|
{
|
2002-07-29 19:37:40 +00:00
|
|
|
|
*contentsEndIndex = end-termlen;
|
2001-06-01 16:27:06 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* xxx OPENSTEP documentation does not say what to do if last
|
|
|
|
|
line is not terminated. Assume this */
|
|
|
|
|
*contentsEndIndex = end;
|
|
|
|
|
}
|
1999-06-21 08:30:26 +00:00
|
|
|
|
}
|
1998-01-08 15:25:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
// Changing Case
|
|
|
|
|
|
|
|
|
|
// xxx There is more than this in word capitalization in Unicode,
|
|
|
|
|
// but this will work in most cases
|
|
|
|
|
- (NSString*) capitalizedString
|
|
|
|
|
{
|
1999-07-02 13:26:37 +00:00
|
|
|
|
unichar *s;
|
|
|
|
|
unsigned count = 0;
|
|
|
|
|
BOOL found = YES;
|
|
|
|
|
unsigned len = [self length];
|
1999-02-04 13:49:27 +00:00
|
|
|
|
|
1999-07-02 13:26:37 +00:00
|
|
|
|
if (len == 0)
|
|
|
|
|
return self;
|
2001-04-26 23:54:01 +00:00
|
|
|
|
if (whitespaceBitmapRep == NULL)
|
|
|
|
|
setupWhitespace();
|
1998-01-08 15:25:59 +00:00
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
|
s = NSZoneMalloc(GSObjCZone(self), sizeof(unichar)*len);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self getCharacters: s];
|
1999-07-17 14:41:31 +00:00
|
|
|
|
while (count < len)
|
1998-01-08 15:25:59 +00:00
|
|
|
|
{
|
2001-04-26 23:54:01 +00:00
|
|
|
|
if (GS_IS_WHITESPACE(s[count]))
|
1999-07-02 13:26:37 +00:00
|
|
|
|
{
|
|
|
|
|
count++;
|
1999-07-17 14:41:31 +00:00
|
|
|
|
found = YES;
|
|
|
|
|
while (count < len
|
2001-04-26 23:54:01 +00:00
|
|
|
|
&& GS_IS_WHITESPACE(s[count]))
|
1999-07-02 13:26:37 +00:00
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-07-17 14:41:31 +00:00
|
|
|
|
if (count < len)
|
1999-07-02 13:26:37 +00:00
|
|
|
|
{
|
1999-07-17 14:41:31 +00:00
|
|
|
|
if (found)
|
1999-07-02 13:26:37 +00:00
|
|
|
|
{
|
1999-07-17 14:41:31 +00:00
|
|
|
|
s[count] = uni_toupper(s[count]);
|
1999-07-02 13:26:37 +00:00
|
|
|
|
count++;
|
|
|
|
|
}
|
1999-07-17 14:41:31 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
while (count < len
|
2001-04-26 23:54:01 +00:00
|
|
|
|
&& !GS_IS_WHITESPACE(s[count]))
|
1999-07-17 14:41:31 +00:00
|
|
|
|
{
|
|
|
|
|
s[count] = uni_tolower(s[count]);
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-07-02 13:26:37 +00:00
|
|
|
|
}
|
1999-07-17 14:41:31 +00:00
|
|
|
|
found = NO;
|
1998-01-08 15:25:59 +00:00
|
|
|
|
}
|
1999-06-03 10:59:25 +00:00
|
|
|
|
return AUTORELEASE([[NSString allocWithZone: NSDefaultMallocZone()]
|
2000-09-27 15:26:16 +00:00
|
|
|
|
initWithCharactersNoCopy: s length: len freeWhenDone: YES]);
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a copy of the receiver with all characters converted
|
|
|
|
|
* to lowercase.
|
|
|
|
|
*/
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (NSString*) lowercaseString
|
|
|
|
|
{
|
1999-06-03 10:59:25 +00:00
|
|
|
|
unichar *s;
|
|
|
|
|
unsigned count;
|
|
|
|
|
unsigned len = [self length];
|
2002-03-13 13:46:12 +00:00
|
|
|
|
unichar (*caiImp)(NSString*, SEL, unsigned int);
|
1999-06-03 10:59:25 +00:00
|
|
|
|
|
1999-07-02 13:26:37 +00:00
|
|
|
|
if (len == 0)
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
2001-03-12 14:42:52 +00:00
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
|
s = NSZoneMalloc(GSObjCZone(self), sizeof(unichar)*len);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
1999-06-03 10:59:25 +00:00
|
|
|
|
for (count = 0; count < len; count++)
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
|
|
|
|
s[count] = uni_tolower((*caiImp)(self, caiSel, count));
|
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return AUTORELEASE([[NSStringClass allocWithZone: NSDefaultMallocZone()]
|
2000-09-27 15:26:16 +00:00
|
|
|
|
initWithCharactersNoCopy: s length: len freeWhenDone: YES]);
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a copy of the receiver with all characters converted
|
|
|
|
|
* to uppercase.
|
|
|
|
|
*/
|
2001-12-17 14:31:42 +00:00
|
|
|
|
- (NSString*) uppercaseString
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-06-03 10:59:25 +00:00
|
|
|
|
unichar *s;
|
|
|
|
|
unsigned count;
|
|
|
|
|
unsigned len = [self length];
|
2002-03-13 13:46:12 +00:00
|
|
|
|
unichar (*caiImp)(NSString*, SEL, unsigned int);
|
1999-06-03 10:59:25 +00:00
|
|
|
|
|
1999-07-02 13:26:37 +00:00
|
|
|
|
if (len == 0)
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
s = NSZoneMalloc(GSObjCZone(self), sizeof(unichar)*len);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
1999-06-03 10:59:25 +00:00
|
|
|
|
for (count = 0; count < len; count++)
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
|
|
|
|
s[count] = uni_toupper((*caiImp)(self, caiSel, count));
|
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return AUTORELEASE([[NSStringClass allocWithZone: NSDefaultMallocZone()]
|
2000-09-27 15:26:16 +00:00
|
|
|
|
initWithCharactersNoCopy: s length: len freeWhenDone: YES]);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Storing the String
|
|
|
|
|
|
|
|
|
|
- (NSString*) description
|
|
|
|
|
{
|
1997-03-03 20:07:35 +00:00
|
|
|
|
return self;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Getting C Strings
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
2002-08-20 15:07:58 +00:00
|
|
|
|
* Returns a pointer to a null terminated string of 8-bit
|
2002-08-20 10:22:05 +00:00
|
|
|
|
* characters in the default encoding. The memory pointed
|
|
|
|
|
* to is not owned by the caller, so the caller must copy
|
|
|
|
|
* its contents to keep it.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (const char*) cString
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
NSData *d;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
NSMutableData *m;
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
|
|
d = [self dataUsingEncoding: _DefaultStringEncoding
|
2001-04-12 09:11:31 +00:00
|
|
|
|
allowLossyConversion: NO];
|
1999-08-25 14:47:19 +00:00
|
|
|
|
if (d == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"unable to convert to cString"];
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
m = [d mutableCopy];
|
|
|
|
|
[m appendBytes: "" length: 1];
|
|
|
|
|
AUTORELEASE(m);
|
|
|
|
|
return (const char*)[m bytes];
|
1999-08-25 14:47:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char*) lossyCString
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
NSData *d;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
NSMutableData *m;
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
|
|
d = [self dataUsingEncoding: _DefaultStringEncoding
|
2001-03-19 10:48:26 +00:00
|
|
|
|
allowLossyConversion: YES];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
m = [d mutableCopy];
|
|
|
|
|
[m appendBytes: "" length: 1];
|
|
|
|
|
AUTORELEASE(m);
|
|
|
|
|
return (const char*)[m bytes];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-09-30 04:54:43 +00:00
|
|
|
|
- (const char *) UTF8String
|
2000-09-12 23:09:50 +00:00
|
|
|
|
{
|
|
|
|
|
NSData *d;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
NSMutableData *m;
|
2000-09-12 23:09:50 +00:00
|
|
|
|
|
|
|
|
|
d = [self dataUsingEncoding: NSUTF8StringEncoding
|
2001-03-19 10:48:26 +00:00
|
|
|
|
allowLossyConversion: NO];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
m = [d mutableCopy];
|
|
|
|
|
[m appendBytes: "" length: 1];
|
|
|
|
|
AUTORELEASE(m);
|
|
|
|
|
return (const char*)[m bytes];
|
2000-09-12 23:09:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (unsigned int) cStringLength
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [self dataUsingEncoding: _DefaultStringEncoding
|
2001-03-19 10:48:26 +00:00
|
|
|
|
allowLossyConversion: NO];
|
2000-08-07 22:00:31 +00:00
|
|
|
|
return [d length];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-10-21 13:59:16 +00:00
|
|
|
|
/**
|
|
|
|
|
* Retrieve the contents of the receiver into the buffer.<br />
|
|
|
|
|
* The buffer must be large enought to contain the CString representation
|
|
|
|
|
* of the characters in the receiver, plus a null terminator which this
|
|
|
|
|
* method adds.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
{
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self getCString: buffer maxLength: NSMaximumStringLength
|
2000-08-07 22:00:31 +00:00
|
|
|
|
range: ((NSRange){0, [self length]})
|
|
|
|
|
remainingRange: NULL];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-10-21 13:59:16 +00:00
|
|
|
|
/**
|
|
|
|
|
* Retrieve up to maxLength characters from the receiver into the buffer.<br />
|
|
|
|
|
* The buffer must be at least maxLength characters long, so that it has
|
|
|
|
|
* room for the null terminator that this method adds.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (void) getCString: (char*)buffer
|
2002-03-13 13:46:12 +00:00
|
|
|
|
maxLength: (unsigned int)maxLength
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
[self getCString: buffer maxLength: maxLength
|
2000-08-07 22:00:31 +00:00
|
|
|
|
range: ((NSRange){0, [self length]})
|
|
|
|
|
remainingRange: NULL];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
2002-03-13 13:46:12 +00:00
|
|
|
|
maxLength: (unsigned int)maxLength
|
1999-07-02 13:26:37 +00:00
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
remainingRange: (NSRange*)leftoverRange
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-07-02 13:26:37 +00:00
|
|
|
|
unsigned len;
|
|
|
|
|
unsigned count;
|
2002-03-13 13:46:12 +00:00
|
|
|
|
unichar (*caiImp)(NSString*, SEL, unsigned int);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
|
1998-11-19 20:42:06 +00:00
|
|
|
|
len = [self cStringLength];
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, len);
|
1998-11-19 20:42:06 +00:00
|
|
|
|
|
2000-10-05 15:17:18 +00:00
|
|
|
|
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-07-02 13:26:37 +00:00
|
|
|
|
count = 0;
|
|
|
|
|
while (count < len)
|
|
|
|
|
{
|
2000-12-14 09:47:02 +00:00
|
|
|
|
buffer[count] = encode_unitochar(
|
|
|
|
|
(*caiImp)(self, caiSel, aRange.location + count),
|
|
|
|
|
_DefaultStringEncoding);
|
|
|
|
|
if (buffer[count] == 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"unable to convert to cString"];
|
|
|
|
|
}
|
1999-07-02 13:26:37 +00:00
|
|
|
|
count++;
|
|
|
|
|
}
|
1997-05-03 18:05:21 +00:00
|
|
|
|
buffer[len] = '\0';
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Getting Numeric Values
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
// xxx Sould we use NSScanner here ?
|
|
|
|
|
|
2002-08-07 11:06:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* If the string consists of the words 'true' or 'yes' (case insensitive)
|
|
|
|
|
* or begins with a non-zero numeric value, return YES, otherwise return
|
|
|
|
|
* NO.
|
|
|
|
|
*/
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (BOOL) boolValue
|
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
if ([self caseInsensitiveCompare: @"YES"] == NSOrderedSame)
|
2002-08-07 11:06:49 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
if ([self caseInsensitiveCompare: @"true"] == NSOrderedSame)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
1999-07-02 13:26:37 +00:00
|
|
|
|
return [self intValue] != 0 ? YES : NO;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-07 09:05:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the strings content as a double. Skips leading whitespace.<br />
|
|
|
|
|
* Conversion is not localised (ie uses '.' as the decimal separator).<br />
|
|
|
|
|
* Returns 0.0 on underflow or if the string does not contain a number.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (double) doubleValue
|
|
|
|
|
{
|
2003-07-07 09:05:53 +00:00
|
|
|
|
unichar buf[32];
|
|
|
|
|
unsigned len = [self length];
|
|
|
|
|
double d = 0.0;
|
|
|
|
|
|
|
|
|
|
if (len > 32) len = 32;
|
|
|
|
|
[self getCharacters: buf range: NSMakeRange(0, len)];
|
|
|
|
|
GSScanDouble(buf, len, &d);
|
|
|
|
|
return d;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-07 09:05:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the strings content as a double. Skips leading whitespace.<br />
|
|
|
|
|
* Conversion is not localised (ie uses '.' as the decimal separator).<br />
|
|
|
|
|
* Returns 0.0 on underflow or if the string does not contain a number.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (float) floatValue
|
|
|
|
|
{
|
2003-07-07 09:05:53 +00:00
|
|
|
|
unichar buf[32];
|
|
|
|
|
unsigned len = [self length];
|
|
|
|
|
double d = 0.0;
|
|
|
|
|
|
|
|
|
|
if (len > 32) len = 32;
|
|
|
|
|
[self getCharacters: buf range: NSMakeRange(0, len)];
|
|
|
|
|
GSScanDouble(buf, len, &d);
|
|
|
|
|
return (float)d;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (int) intValue
|
|
|
|
|
{
|
2000-12-14 09:47:02 +00:00
|
|
|
|
return atoi([self lossyCString]);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Working With Encodings
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* Returns the encoding used for any method accepting a C string.
|
|
|
|
|
* This value is determined automatically from the programs
|
|
|
|
|
* environment and cannot be changed programmatically.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* You should <em>NOT</em> override this method in an attempt to
|
|
|
|
|
* change the encoding being used... it won't work.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* In GNUstep, this encoding is determined by the initial value
|
|
|
|
|
* of the <code>GNUSTEP_STRING_ENCODING</code> environment
|
|
|
|
|
* variable. If this is not defined,
|
|
|
|
|
* <code>NSISOLatin1StringEncoding</code> is assumed.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
+ (NSStringEncoding) defaultCStringEncoding
|
|
|
|
|
{
|
1998-01-08 15:25:59 +00:00
|
|
|
|
return _DefaultStringEncoding;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an array of all available string encodings,
|
|
|
|
|
* terminated by a null value.
|
|
|
|
|
*/
|
1999-11-26 19:43:43 +00:00
|
|
|
|
+ (NSStringEncoding*) availableStringEncodings
|
1998-01-08 15:25:59 +00:00
|
|
|
|
{
|
2000-09-30 18:55:15 +00:00
|
|
|
|
return GetAvailableEncodings();
|
1998-01-08 15:25:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the localized name of the encoding specified.
|
|
|
|
|
*/
|
1999-11-26 19:43:43 +00:00
|
|
|
|
+ (NSString*) localizedNameOfStringEncoding: (NSStringEncoding)encoding
|
1998-01-08 15:25:59 +00:00
|
|
|
|
{
|
|
|
|
|
id ourbundle;
|
|
|
|
|
id ourname;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Should be path to localizable.strings file.
|
2001-03-19 10:48:26 +00:00
|
|
|
|
Until we have it, just make sure that bundle
|
1998-01-08 15:25:59 +00:00
|
|
|
|
is initialized.
|
|
|
|
|
*/
|
2001-03-19 10:48:26 +00:00
|
|
|
|
ourbundle = [NSBundle gnustepBundle];
|
1998-01-08 15:25:59 +00:00
|
|
|
|
|
|
|
|
|
ourname = GetEncodingName(encoding);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
return [ourbundle localizedStringForKey: ourname
|
|
|
|
|
value: ourname
|
|
|
|
|
table: nil];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) canBeConvertedToEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
2000-09-30 04:54:43 +00:00
|
|
|
|
id d = [self dataUsingEncoding: encoding allowLossyConversion: NO];
|
2001-04-12 09:11:31 +00:00
|
|
|
|
|
|
|
|
|
return d != nil ? YES : NO;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSData*) dataUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return [self dataUsingEncoding: encoding allowLossyConversion: NO];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSData*) dataUsingEncoding: (NSStringEncoding)encoding
|
1999-02-04 22:06:59 +00:00
|
|
|
|
allowLossyConversion: (BOOL)flag
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
unsigned int count = 0;
|
|
|
|
|
unsigned int len = [self length];
|
2002-03-13 13:46:12 +00:00
|
|
|
|
unichar (*caiImp)(NSString*, SEL, unsigned int);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-07-02 13:26:37 +00:00
|
|
|
|
if (len == 0)
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return [NSDataClass data];
|
2000-08-07 22:00:31 +00:00
|
|
|
|
}
|
1999-07-02 13:26:37 +00:00
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
|
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
|
|
|
|
if ((encoding == NSASCIIStringEncoding)
|
|
|
|
|
|| (encoding == NSISOLatin1StringEncoding)
|
|
|
|
|
|| (encoding == NSISOLatin2StringEncoding)
|
|
|
|
|
|| (encoding == NSNEXTSTEPStringEncoding)
|
|
|
|
|
|| (encoding == NSNonLossyASCIIStringEncoding)
|
|
|
|
|
|| (encoding == NSSymbolStringEncoding)
|
2002-10-22 14:29:34 +00:00
|
|
|
|
|| (encoding == NSISOCyrillicStringEncoding)
|
|
|
|
|
|| (encoding == NSISOThaiStringEncoding))
|
1999-02-04 22:06:59 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
char t;
|
|
|
|
|
unsigned char *buff;
|
1999-02-04 22:06:59 +00:00
|
|
|
|
|
1999-08-25 14:47:19 +00:00
|
|
|
|
buff = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), len+1);
|
1999-02-04 22:06:59 +00:00
|
|
|
|
if (!flag)
|
|
|
|
|
{
|
|
|
|
|
for (count = 0; count < len; count++)
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
t = encode_unitochar((*caiImp)(self, caiSel, count), encoding);
|
1999-02-04 22:06:59 +00:00
|
|
|
|
if (t)
|
|
|
|
|
{
|
|
|
|
|
buff[count] = t;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), buff);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else /* lossy */
|
|
|
|
|
{
|
|
|
|
|
for (count = 0; count < len; count++)
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
t = encode_unitochar((*caiImp)(self, caiSel, count), encoding);
|
1999-02-04 22:06:59 +00:00
|
|
|
|
if (t)
|
|
|
|
|
{
|
|
|
|
|
buff[count] = t;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* xxx should handle decomposed characters */
|
|
|
|
|
/* OpenStep documentation is unclear on what to do
|
|
|
|
|
* if there is no simple replacement for character
|
|
|
|
|
*/
|
|
|
|
|
buff[count] = '*';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-08-25 14:47:19 +00:00
|
|
|
|
buff[count] = '\0';
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return [NSDataClass dataWithBytesNoCopy: buff length: count];
|
1999-02-04 22:06:59 +00:00
|
|
|
|
}
|
2001-03-27 17:30:07 +00:00
|
|
|
|
else if (encoding == NSUTF8StringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unsigned char *buff;
|
|
|
|
|
unsigned i, j;
|
|
|
|
|
unichar ch, ch2;
|
|
|
|
|
gsu32 cp;
|
|
|
|
|
|
|
|
|
|
buff = (unsigned char *)NSZoneMalloc(NSDefaultMallocZone(), len*3);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Each UTF-16 character maps to at most 3 bytes of UTF-8, so we simply
|
|
|
|
|
* allocate three times as many bytes as UTF-16 characters, then use
|
|
|
|
|
* NSZoneRealloc() later to trim the excess. Most Unix virtual memory
|
|
|
|
|
* implementations allocate address space, and actual memory pages are
|
|
|
|
|
* not actually allocated until used, so this method shouldn't cause
|
|
|
|
|
* memory problems on most Unix systems. On other systems, it may prove
|
|
|
|
|
* advantageous to scan the UTF-16 string to determine the UTF-8 string
|
|
|
|
|
* length before allocating memory.
|
|
|
|
|
*/
|
|
|
|
|
for (i = j = 0; i < len; i++)
|
|
|
|
|
{
|
|
|
|
|
ch = (*caiImp)(self, caiSel, i);
|
|
|
|
|
if (NSLocationInRange(ch, highSurrogateRange) && ((i+1) < len))
|
|
|
|
|
{
|
|
|
|
|
ch2 = (*caiImp)(self, caiSel, i+1);
|
|
|
|
|
if (NSLocationInRange(ch2, lowSurrogateRange))
|
|
|
|
|
{
|
|
|
|
|
cp = surrogatePairValue(ch, ch2);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
cp = (gsu32)ch;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
cp = (gsu32)ch;
|
|
|
|
|
|
|
|
|
|
if (cp < 0x80)
|
|
|
|
|
{
|
|
|
|
|
buff[j++] = cp;
|
|
|
|
|
}
|
|
|
|
|
else if (cp < 0x800)
|
|
|
|
|
{
|
|
|
|
|
buff[j++] = 0xC0 | ch>>6;
|
|
|
|
|
buff[j++] = 0x80 | (ch & 0x3F);
|
|
|
|
|
}
|
|
|
|
|
else if (cp < 0x10000)
|
|
|
|
|
{
|
|
|
|
|
buff[j++] = 0xE0 | ch>>12;
|
|
|
|
|
buff[j++] = 0x80 | (ch>>6 & 0x3F);
|
|
|
|
|
buff[j++] = 0x80 | (ch & 0x3F);
|
|
|
|
|
}
|
|
|
|
|
else if (cp < 0x200000)
|
|
|
|
|
{
|
|
|
|
|
buff[j++] = 0xF0 | ch>>18;
|
|
|
|
|
buff[j++] = 0x80 | (ch>>12 & 0x3F);
|
|
|
|
|
buff[j++] = 0x80 | (ch>>6 & 0x3F);
|
|
|
|
|
buff[j++] = 0x80 | (ch & 0x3F);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSZoneRealloc(NSDefaultMallocZone(), buff, j);
|
|
|
|
|
|
|
|
|
|
return [NSDataClass dataWithBytesNoCopy: buff
|
2002-02-12 17:53:04 +00:00
|
|
|
|
length: j];
|
2001-03-27 17:30:07 +00:00
|
|
|
|
}
|
1999-02-04 22:06:59 +00:00
|
|
|
|
else if (encoding == NSUnicodeStringEncoding)
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
unichar *buff;
|
1999-02-04 22:06:59 +00:00
|
|
|
|
|
2000-10-23 11:44:34 +00:00
|
|
|
|
buff = (unichar*)NSZoneMalloc(NSDefaultMallocZone(),
|
|
|
|
|
sizeof(unichar)*(len+1));
|
2002-04-06 06:33:34 +00:00
|
|
|
|
buff[0] = byteOrderMark;
|
2002-03-16 09:54:50 +00:00
|
|
|
|
[self getCharacters: &buff[1]];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return [NSDataClass dataWithBytesNoCopy: buff
|
|
|
|
|
length: sizeof(unichar)*(len+1)];
|
1999-02-04 22:06:59 +00:00
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
else
|
1999-02-04 22:06:59 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned char *b = 0;
|
|
|
|
|
int l = 0;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
unichar *u;
|
|
|
|
|
|
|
|
|
|
u = (unichar*)NSZoneMalloc(NSDefaultMallocZone(), len*sizeof(unichar));
|
|
|
|
|
[self getCharacters: u];
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (GSFromUnicode(&b, &l, u, len, encoding, NSDefaultMallocZone(),
|
|
|
|
|
(flag == NO) ? GSUniStrict : 0)
|
|
|
|
|
== NO)
|
2001-08-03 12:24:25 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
return [NSDataClass dataWithBytesNoCopy: b length: l];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1998-01-08 15:25:59 +00:00
|
|
|
|
return nil;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) fastestEncoding
|
|
|
|
|
{
|
2000-10-23 06:18:03 +00:00
|
|
|
|
return NSUnicodeStringEncoding;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) smallestEncoding
|
|
|
|
|
{
|
2000-10-23 06:18:03 +00:00
|
|
|
|
return NSUnicodeStringEncoding;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Manipulating File System Paths
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (unsigned int) completePathIntoString: (NSString**)outputName
|
2002-04-29 12:20:08 +00:00
|
|
|
|
caseSensitive: (BOOL)flag
|
|
|
|
|
matchesIntoArray: (NSArray**)outputArray
|
|
|
|
|
filterTypes: (NSArray*)filterTypes
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2003-08-01 10:15:11 +00:00
|
|
|
|
NSString *base_path = [self stringByDeletingLastPathComponent];
|
|
|
|
|
NSString *last_compo = [self lastPathComponent];
|
|
|
|
|
NSString *tmp_path;
|
1999-07-12 04:27:18 +00:00
|
|
|
|
NSDirectoryEnumerator *e;
|
|
|
|
|
NSMutableArray *op = nil;
|
2003-08-01 10:15:11 +00:00
|
|
|
|
unsigned match_count = 0;
|
1998-10-15 18:46:27 +00:00
|
|
|
|
|
1998-11-19 20:42:06 +00:00
|
|
|
|
if (outputArray != 0)
|
2003-08-01 10:15:11 +00:00
|
|
|
|
{
|
|
|
|
|
op = (NSMutableArray*)[NSMutableArray array];
|
|
|
|
|
}
|
1998-11-19 20:42:06 +00:00
|
|
|
|
|
1999-07-12 04:27:18 +00:00
|
|
|
|
if (outputName != NULL)
|
2003-08-01 10:15:11 +00:00
|
|
|
|
{
|
|
|
|
|
*outputName = nil;
|
|
|
|
|
}
|
1998-10-15 18:46:27 +00:00
|
|
|
|
|
1999-07-12 04:27:18 +00:00
|
|
|
|
if ([base_path length] == 0)
|
2003-08-01 10:15:11 +00:00
|
|
|
|
{
|
|
|
|
|
base_path = @".";
|
|
|
|
|
}
|
1998-10-15 18:46:27 +00:00
|
|
|
|
|
|
|
|
|
e = [[NSFileManager defaultManager] enumeratorAtPath: base_path];
|
|
|
|
|
while (tmp_path = [e nextObject], tmp_path)
|
|
|
|
|
{
|
|
|
|
|
/* Prefix matching */
|
2003-08-01 10:15:11 +00:00
|
|
|
|
if (flag == YES)
|
1998-10-15 18:46:27 +00:00
|
|
|
|
{ /* Case sensitive */
|
2003-08-01 10:15:11 +00:00
|
|
|
|
if ([tmp_path hasPrefix: last_compo] == NO)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
1998-10-15 18:46:27 +00:00
|
|
|
|
}
|
2003-08-01 10:15:11 +00:00
|
|
|
|
else if ([[tmp_path uppercaseString]
|
|
|
|
|
hasPrefix: [last_compo uppercaseString]] == NO)
|
1998-10-15 18:46:27 +00:00
|
|
|
|
{
|
2003-08-01 10:15:11 +00:00
|
|
|
|
continue;
|
1998-10-15 18:46:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Extensions filtering */
|
2003-08-01 10:15:11 +00:00
|
|
|
|
if (filterTypes
|
|
|
|
|
&& ([filterTypes containsObject: [tmp_path pathExtension]] == NO))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
1998-10-15 18:46:27 +00:00
|
|
|
|
|
|
|
|
|
/* Found a completion */
|
|
|
|
|
match_count++;
|
|
|
|
|
if (outputArray != NULL)
|
2003-08-01 10:15:11 +00:00
|
|
|
|
{
|
|
|
|
|
[op addObject: tmp_path];
|
|
|
|
|
}
|
2002-04-29 12:20:08 +00:00
|
|
|
|
|
|
|
|
|
if ((outputName != NULL) &&
|
1998-11-19 20:42:06 +00:00
|
|
|
|
((*outputName == nil) || (([*outputName length] < [tmp_path length]))))
|
2003-08-01 10:15:11 +00:00
|
|
|
|
{
|
|
|
|
|
*outputName = tmp_path;
|
|
|
|
|
}
|
1998-10-15 18:46:27 +00:00
|
|
|
|
}
|
1998-11-19 20:42:06 +00:00
|
|
|
|
if (outputArray != NULL)
|
2003-08-01 10:15:11 +00:00
|
|
|
|
{
|
|
|
|
|
*outputArray = AUTORELEASE([op copy]);
|
|
|
|
|
}
|
1998-10-15 18:46:27 +00:00
|
|
|
|
return match_count;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-11-03 01:40:03 +00:00
|
|
|
|
/* Return a string for passing to OS calls to handle file system objects. */
|
1999-11-26 19:43:43 +00:00
|
|
|
|
- (const char*) fileSystemRepresentation
|
1997-11-03 01:40:03 +00:00
|
|
|
|
{
|
2001-04-21 18:12:06 +00:00
|
|
|
|
static NSFileManager *fm = nil;
|
|
|
|
|
|
|
|
|
|
if (fm == nil)
|
|
|
|
|
{
|
2002-04-18 07:51:46 +00:00
|
|
|
|
fm = RETAIN([NSFileManager defaultManager]);
|
2001-04-21 18:12:06 +00:00
|
|
|
|
}
|
2002-04-29 12:20:08 +00:00
|
|
|
|
|
2001-04-21 18:12:06 +00:00
|
|
|
|
return [fm fileSystemRepresentationWithPath: self];
|
1997-11-03 01:40:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (BOOL) getFileSystemRepresentation: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)size
|
1997-11-03 01:40:03 +00:00
|
|
|
|
{
|
2000-09-22 04:20:52 +00:00
|
|
|
|
const char* ptr = [self fileSystemRepresentation];
|
1997-11-03 01:40:03 +00:00
|
|
|
|
if (strlen(ptr) > size)
|
|
|
|
|
return NO;
|
|
|
|
|
strcpy(buffer, ptr);
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a string containing the last path component of the receiver.<br />
|
|
|
|
|
* The path component is the last non-empty substring delimited by the ends
|
|
|
|
|
* of the string or by path * separator ('/') characters.<br />
|
|
|
|
|
* If the receiver is an empty string, it is simply returned.<br />
|
|
|
|
|
* If there are no non-empty substrings, the root string is returned.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSString*) lastPathComponent
|
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
NSString *substring;
|
|
|
|
|
unsigned int l = [self length];
|
(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
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
if (l == 0)
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
substring = self; // self is empty
|
2002-04-18 07:51:46 +00:00
|
|
|
|
}
|
2002-04-29 12:20:08 +00:00
|
|
|
|
else
|
(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
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
NSRange range;
|
|
|
|
|
|
|
|
|
|
range = [self rangeOfCharacterFromSet: pathSeps()
|
|
|
|
|
options: NSBackwardsSearch];
|
|
|
|
|
if (range.length == 0)
|
|
|
|
|
{
|
|
|
|
|
substring = self; // No '/' in self
|
|
|
|
|
}
|
|
|
|
|
else if (range.location == (l - 1))
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
if (range.location == 0)
|
|
|
|
|
{
|
|
|
|
|
substring = self; // Just '/'
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
l = range.location;
|
|
|
|
|
while (l > 0 && [self characterAtIndex: l - 1] == '/')
|
|
|
|
|
{
|
|
|
|
|
l--;
|
|
|
|
|
}
|
|
|
|
|
if (l > 0)
|
|
|
|
|
{
|
|
|
|
|
substring = [[self substringToIndex: l] lastPathComponent];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
substring = @"/"; // Multiple '/' characters.
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-04-18 07:51:46 +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
|
|
|
|
else
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
substring = [self substringFromIndex: range.location + 1];
|
2002-04-18 07:51:46 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return substring;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
1997-09-13 17:52:31 +00:00
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a new string containing the path extension of the receiver.<br />
|
|
|
|
|
* The path extension is a suffix on the last path component which starts
|
|
|
|
|
* with the extension separator (a '.') (for example .tiff is the
|
|
|
|
|
* pathExtension for /foo/bar.tiff).<br />
|
|
|
|
|
* Returns an empty string if no such extension exists.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSString*) pathExtension
|
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
NSRange range;
|
2003-09-26 15:39:14 +00:00
|
|
|
|
NSString *substring = @"";
|
2002-04-29 12:20:08 +00:00
|
|
|
|
unsigned int length = [self length];
|
(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
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/*
|
|
|
|
|
* Step past trailing path separators.
|
|
|
|
|
*/
|
|
|
|
|
while (length > 1 && pathSepMember([self characterAtIndex: length-1]) == YES)
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
length--;
|
2002-04-18 07:51:46 +00:00
|
|
|
|
}
|
2002-04-29 12:20:08 +00:00
|
|
|
|
range = NSMakeRange(0, length);
|
2003-09-26 15:39:14 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Look for a dot in the path ... if there isn't one, there is no extension.
|
|
|
|
|
*/
|
2002-04-29 12:20:08 +00:00
|
|
|
|
range = [self rangeOfString: @"." options: NSBackwardsSearch range: range];
|
2003-09-26 15:39:14 +00:00
|
|
|
|
if (range.length > 0)
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
2003-09-26 15:39:14 +00:00
|
|
|
|
NSRange sepRange;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Found a dot, so we determine the range of the (possible)
|
|
|
|
|
* path extension, then cvheck to see if we have a path
|
|
|
|
|
* separator within it ... if we have a path separator then
|
|
|
|
|
* the dot is inside the last path component and there is
|
|
|
|
|
* thereofore no extension.
|
|
|
|
|
*/
|
2002-04-29 12:20:08 +00:00
|
|
|
|
range.location++;
|
|
|
|
|
range.length = length - range.location;
|
2003-09-26 15:39:14 +00:00
|
|
|
|
sepRange = [self rangeOfCharacterFromSet: pathSeps()
|
|
|
|
|
options: NSBackwardsSearch
|
|
|
|
|
range: range];
|
|
|
|
|
if (sepRange.length == 0)
|
|
|
|
|
{
|
|
|
|
|
substring = [self substringFromRange: range];
|
|
|
|
|
}
|
2002-04-18 07:51:46 +00:00
|
|
|
|
}
|
2002-04-29 12:20:08 +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
|
|
|
|
return substring;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a new string with the path component given in aString
|
2000-03-23 18:57:43 +00:00
|
|
|
|
* appended to the receiver.
|
|
|
|
|
* Removes trailing separators and multiple separators.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSString*) stringByAppendingPathComponent: (NSString*)aString
|
|
|
|
|
{
|
2000-03-23 18:57:43 +00:00
|
|
|
|
unsigned length = [self length];
|
|
|
|
|
unsigned aLength = [aString length];
|
|
|
|
|
unichar buf[length+aLength+1];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
2000-03-23 18:57:43 +00:00
|
|
|
|
[self getCharacters: buf];
|
2002-04-29 12:20:08 +00:00
|
|
|
|
while (length > 1 && pathSepMember(buf[length-1]) == YES)
|
|
|
|
|
{
|
|
|
|
|
length--;
|
|
|
|
|
}
|
2000-03-23 18:57:43 +00:00
|
|
|
|
if (aLength > 0)
|
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
if (length > 0 && pathSepMember(buf[length-1]) == NO)
|
2000-03-24 11:50:18 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
buf[length++] = '/';
|
2000-03-24 11:50:18 +00:00
|
|
|
|
}
|
2000-03-23 18:57:43 +00:00
|
|
|
|
[aString getCharacters: &buf[length]];
|
|
|
|
|
}
|
|
|
|
|
length += aLength;
|
|
|
|
|
while (length > 1 && pathSepMember(buf[length-1]) == YES)
|
|
|
|
|
{
|
|
|
|
|
length--;
|
|
|
|
|
}
|
2000-04-04 18:36:46 +00:00
|
|
|
|
if (length > 0)
|
2000-03-23 18:57:43 +00:00
|
|
|
|
{
|
2000-04-04 18:36:46 +00:00
|
|
|
|
aLength = length - 1;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
while (aLength > 0)
|
2000-03-23 18:57:43 +00:00
|
|
|
|
{
|
2000-04-04 18:36:46 +00:00
|
|
|
|
if (pathSepMember(buf[aLength]) == YES)
|
2000-03-23 18:57:43 +00:00
|
|
|
|
{
|
2000-04-04 18:36:46 +00:00
|
|
|
|
if (pathSepMember(buf[aLength-1]) == YES)
|
2000-03-23 18:57:43 +00:00
|
|
|
|
{
|
2000-04-04 18:36:46 +00:00
|
|
|
|
unsigned pos;
|
|
|
|
|
|
|
|
|
|
for (pos = aLength+1; pos < length; pos++)
|
|
|
|
|
{
|
|
|
|
|
buf[pos-1] = buf[pos];
|
|
|
|
|
}
|
|
|
|
|
length--;
|
2000-03-23 18:57:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2000-04-04 18:36:46 +00:00
|
|
|
|
aLength--;
|
2000-03-23 18:57:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2000-10-23 06:18:03 +00:00
|
|
|
|
return [NSStringClass stringWithCharacters: buf length: length];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a new string with the path extension given in aString
|
|
|
|
|
* appended to the receiver after the extensionSeparator ('.').<br />
|
|
|
|
|
* If the receiver has trailing '/' characters which are not part of the
|
|
|
|
|
* root directory, those '/' characters are stripped before the extension
|
|
|
|
|
* separator is added.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSString*) stringByAppendingPathExtension: (NSString*)aString
|
|
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
|
if ([aString length] == 0)
|
2002-04-29 12:20:08 +00:00
|
|
|
|
{
|
|
|
|
|
return [self stringByAppendingString: @"."];
|
|
|
|
|
}
|
(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
|
|
|
|
else
|
2002-04-29 12:20:08 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned length = [self length];
|
|
|
|
|
unsigned len = length;
|
|
|
|
|
NSString *base = self;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Step past trailing path separators.
|
|
|
|
|
*/
|
|
|
|
|
while (len > 1 && pathSepMember([self characterAtIndex: len-1]) == YES)
|
|
|
|
|
{
|
|
|
|
|
len--;
|
|
|
|
|
}
|
|
|
|
|
if (length != len)
|
|
|
|
|
{
|
|
|
|
|
NSRange range = NSMakeRange(0, len);
|
|
|
|
|
|
|
|
|
|
base = [base substringFromRange: range];
|
|
|
|
|
}
|
|
|
|
|
return [base stringByAppendingFormat: @".%@", aString];
|
|
|
|
|
}
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a new string with the last path component (including any final
|
|
|
|
|
* path separators) removed from the receiver.<br />
|
|
|
|
|
* A string without a path component other than the root is returned
|
|
|
|
|
* without alteration.<br />
|
|
|
|
|
* See -lastPathComponent for a definition of a path component.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSString*) stringByDeletingLastPathComponent
|
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
NSRange range;
|
|
|
|
|
NSString *substring;
|
|
|
|
|
unsigned int length = [self length];
|
(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
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/*
|
|
|
|
|
* Step past trailing path separators.
|
|
|
|
|
*/
|
|
|
|
|
while (length > 1 && pathSepMember([self characterAtIndex: length-1]) == YES)
|
|
|
|
|
{
|
|
|
|
|
length--;
|
|
|
|
|
}
|
|
|
|
|
range = NSMakeRange(0, length);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/*
|
|
|
|
|
* Locate path separator preceeding last path component.
|
|
|
|
|
*/
|
|
|
|
|
range = [self rangeOfCharacterFromSet: pathSeps()
|
|
|
|
|
options: NSBackwardsSearch
|
|
|
|
|
range: range];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
if (range.length == 0)
|
2002-04-29 12:20:08 +00:00
|
|
|
|
{
|
|
|
|
|
substring = @"";
|
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
else if (range.location == 0)
|
2002-04-29 12:20:08 +00:00
|
|
|
|
{
|
|
|
|
|
substring = @"/";
|
|
|
|
|
}
|
(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
|
|
|
|
else
|
2002-04-29 12:20:08 +00:00
|
|
|
|
{
|
|
|
|
|
substring = [self substringToIndex: range.location];
|
|
|
|
|
}
|
(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 substring;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a new string with the path extension removed from the receiver.<br />
|
2001-05-15 13:35:28 +00:00
|
|
|
|
* Strips any trailing path separators before checking for the extension
|
2002-04-29 12:20:08 +00:00
|
|
|
|
* separator.<br />
|
|
|
|
|
* Does not consider a string starting with the extension separator ('.') to
|
|
|
|
|
* be a path extension.
|
2001-05-15 13:35:28 +00:00
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSString*) stringByDeletingPathExtension
|
|
|
|
|
{
|
2001-05-15 13:35:28 +00:00
|
|
|
|
NSRange range;
|
|
|
|
|
NSRange r0;
|
|
|
|
|
NSRange r1;
|
|
|
|
|
NSString *substring;
|
|
|
|
|
unsigned length = [self length];
|
(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
|
|
|
|
|
2001-05-15 13:35:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* Skip past any trailing path separators... but not a leading one.
|
|
|
|
|
*/
|
|
|
|
|
while (length > 1 && pathSepMember([self characterAtIndex: length-1]) == YES)
|
|
|
|
|
{
|
|
|
|
|
length--;
|
|
|
|
|
}
|
|
|
|
|
range = NSMakeRange(0, length);
|
|
|
|
|
/*
|
|
|
|
|
* Locate path extension.
|
|
|
|
|
*/
|
|
|
|
|
r0 = [self rangeOfString: @"."
|
|
|
|
|
options: NSBackwardsSearch
|
|
|
|
|
range: range];
|
|
|
|
|
/*
|
|
|
|
|
* Locate a path separator.
|
|
|
|
|
*/
|
|
|
|
|
r1 = [self rangeOfCharacterFromSet: pathSeps()
|
|
|
|
|
options: NSBackwardsSearch
|
|
|
|
|
range: range];
|
|
|
|
|
/*
|
|
|
|
|
* Assuming the extension separator was found in the last path
|
|
|
|
|
* component, set the length of the substring we want.
|
|
|
|
|
*/
|
|
|
|
|
if (r0.length > 0 && (r1.length == 0 || r1.location < r0.location))
|
|
|
|
|
{
|
|
|
|
|
length = r0.location;
|
|
|
|
|
}
|
|
|
|
|
substring = [self substringToIndex: length];
|
(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 substring;
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a string created by expanding the initial tilde ('~') and any
|
|
|
|
|
* following username to be the home directory of the current user or the
|
|
|
|
|
* named user.<br />
|
|
|
|
|
* Returns the receiver if it was not possible to expand it.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSString*) stringByExpandingTildeInPath
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
NSString *homedir;
|
|
|
|
|
NSRange first_slash_range;
|
2003-10-08 14:27:11 +00:00
|
|
|
|
unsigned length;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
|
2003-10-08 14:27:11 +00:00
|
|
|
|
if ((length = [self length]) == 0)
|
2002-04-29 12:20:08 +00:00
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
1998-02-03 14:20:00 +00:00
|
|
|
|
if ([self characterAtIndex: 0] != 0x007E)
|
2002-04-29 12:20:08 +00:00
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
1996-11-24 21:04:24 +00:00
|
|
|
|
|
2003-10-08 14:27:11 +00:00
|
|
|
|
/*
|
|
|
|
|
* Anything beginning '~@' is assumed to be a windows path specification
|
|
|
|
|
* which can't be expanded.
|
|
|
|
|
*/
|
|
|
|
|
if (length > 1 && [self characterAtIndex: 1] == 0x0040)
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
first_slash_range = [self rangeOfCharacterFromSet: pathSeps()];
|
1996-11-24 21:04:24 +00:00
|
|
|
|
|
|
|
|
|
if (first_slash_range.location != 1)
|
|
|
|
|
{
|
|
|
|
|
/* It is of the form `~username/blah/...' */
|
2001-04-23 09:56:33 +00:00
|
|
|
|
int uname_len;
|
|
|
|
|
NSString *uname;
|
1996-11-24 21:04:24 +00:00
|
|
|
|
|
|
|
|
|
if (first_slash_range.length != 0)
|
2001-04-23 09:56:33 +00:00
|
|
|
|
{
|
2002-02-13 12:15:15 +00:00
|
|
|
|
uname_len = first_slash_range.location - 1;
|
2001-04-23 09:56:33 +00:00
|
|
|
|
}
|
1996-11-24 21:04:24 +00:00
|
|
|
|
else
|
2001-04-23 09:56:33 +00:00
|
|
|
|
{
|
|
|
|
|
/* It is actually of the form `~username' */
|
|
|
|
|
uname_len = [self length] - 1;
|
|
|
|
|
first_slash_range.location = [self length];
|
|
|
|
|
}
|
1999-06-21 08:30:26 +00:00
|
|
|
|
uname = [self substringWithRange: ((NSRange){1, uname_len})];
|
1996-11-24 21:04:24 +00:00
|
|
|
|
homedir = NSHomeDirectoryForUser (uname);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* It is of the form `~/blah/...' */
|
|
|
|
|
homedir = NSHomeDirectory ();
|
|
|
|
|
}
|
2002-04-29 12:20:08 +00:00
|
|
|
|
if (homedir != nil)
|
|
|
|
|
{
|
2003-10-08 14:27:11 +00:00
|
|
|
|
return [homedir stringByAppendingPathComponent:
|
2002-04-29 12:20:08 +00:00
|
|
|
|
[self substringFromIndex: first_slash_range.location]];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
1996-11-24 21:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a string where a prefix of the current user's home directory is
|
|
|
|
|
* abbreviated by '~', or returns the receiver if it was not found to have
|
|
|
|
|
* the home directory as a prefix.
|
|
|
|
|
*/
|
1996-11-24 21:04:24 +00:00
|
|
|
|
- (NSString*) stringByAbbreviatingWithTildeInPath
|
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
NSString *homedir = NSHomeDirectory ();
|
1996-11-24 21:04:24 +00:00
|
|
|
|
|
|
|
|
|
if (![self hasPrefix: homedir])
|
2002-04-29 12:20:08 +00:00
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
if ([self length] == [homedir length])
|
|
|
|
|
{
|
|
|
|
|
return @"~";
|
|
|
|
|
}
|
2003-10-08 14:27:11 +00:00
|
|
|
|
return [@"~" stringByAppendingPathComponent:
|
2000-08-07 22:00:31 +00:00
|
|
|
|
[self substringFromIndex: [homedir length] + 1]];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-30 08:36:20 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a string formed by extending or truncating the receiver to
|
|
|
|
|
* newLength characters. If the new string is larger, it is padded
|
|
|
|
|
* by appending characters from padString (appending it as many times
|
|
|
|
|
* as required). The first character from padString to be appended
|
|
|
|
|
* is specified by padIndex.<br />
|
|
|
|
|
*/
|
|
|
|
|
- (NSString*) stringByPaddingToLength: (unsigned int)newLength
|
|
|
|
|
withString: (NSString*)padString
|
|
|
|
|
startingAtIndex: (unsigned int)padIndex
|
|
|
|
|
{
|
|
|
|
|
unsigned length = [self length];
|
|
|
|
|
unsigned padLength;
|
|
|
|
|
|
|
|
|
|
if (padString == nil || [padString isKindOfClass: [NSString class]] == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"%@ - Illegal pad string", NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
padLength = [padString length];
|
|
|
|
|
if (padIndex >= padLength)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
|
format: @"%@ - pad index larger too big", NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
if (newLength == length)
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
else if (newLength < length)
|
|
|
|
|
{
|
|
|
|
|
return [self substringToIndex: newLength];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
length = newLength - length; // What we want to add.
|
|
|
|
|
if (length <= (padLength - padIndex))
|
|
|
|
|
{
|
2002-08-30 09:03:33 +00:00
|
|
|
|
NSRange r;
|
|
|
|
|
|
|
|
|
|
r = NSMakeRange(padIndex, length);
|
2002-08-30 08:36:20 +00:00
|
|
|
|
return [self stringByAppendingString:
|
2002-08-30 09:03:33 +00:00
|
|
|
|
[padString substringWithRange: r]];
|
2002-08-30 08:36:20 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NSMutableString *m = [self mutableCopy];
|
|
|
|
|
|
|
|
|
|
if (padIndex > 0)
|
|
|
|
|
{
|
2002-08-30 09:03:33 +00:00
|
|
|
|
NSRange r;
|
|
|
|
|
|
|
|
|
|
r = NSMakeRange(padIndex, padLength - padIndex);
|
|
|
|
|
[m appendString: [padString substringWithRange: r]];
|
|
|
|
|
length -= r.length;
|
2002-08-30 08:36:20 +00:00
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* In case we have to append a small string lots of times,
|
|
|
|
|
* we cache the method impllementation to do it.
|
|
|
|
|
*/
|
|
|
|
|
if (length >= padLength)
|
|
|
|
|
{
|
|
|
|
|
void (*appImp)(NSMutableString*, SEL, NSString*);
|
|
|
|
|
SEL appSel;
|
|
|
|
|
|
|
|
|
|
appSel = @selector(appendString:);
|
|
|
|
|
appImp = (void (*)(NSMutableString*, SEL, NSString*))
|
|
|
|
|
[m methodForSelector: appSel];
|
|
|
|
|
while (length >= padLength)
|
|
|
|
|
{
|
|
|
|
|
(*appImp)(m, appSel, padString);
|
|
|
|
|
length -= padLength;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
|
|
|
|
[m appendString:
|
|
|
|
|
[padString substringWithRange: NSMakeRange(0, length)]];
|
|
|
|
|
}
|
|
|
|
|
return AUTORELEASE(m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSString*) stringByResolvingSymlinksInPath
|
|
|
|
|
{
|
2000-06-12 05:17:41 +00:00
|
|
|
|
#if defined(__MINGW__)
|
1995-04-03 01:35:42 +00:00
|
|
|
|
return self;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
#else
|
2000-06-13 14:50:40 +00:00
|
|
|
|
#ifndef MAX_PATH
|
|
|
|
|
#define MAX_PATH 1024
|
|
|
|
|
#endif
|
1999-07-20 09:03:50 +00:00
|
|
|
|
char new_buf[MAX_PATH];
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_REALPATH
|
1998-10-15 13:46:35 +00:00
|
|
|
|
|
1999-07-20 09:03:50 +00:00
|
|
|
|
if (realpath([self cString], new_buf) == 0)
|
1999-07-12 04:21:05 +00:00
|
|
|
|
return self;
|
|
|
|
|
#else
|
1999-07-20 09:03:50 +00:00
|
|
|
|
char extra[MAX_PATH];
|
|
|
|
|
char *dest;
|
|
|
|
|
const char *name = [self cString];
|
|
|
|
|
const char *start;
|
|
|
|
|
const char *end;
|
|
|
|
|
unsigned num_links = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (name[0] != '/')
|
|
|
|
|
{
|
|
|
|
|
if (!getcwd(new_buf, MAX_PATH))
|
|
|
|
|
return self; /* Couldn't get directory. */
|
|
|
|
|
dest = strchr(new_buf, '\0');
|
|
|
|
|
}
|
|
|
|
|
else
|
1998-10-15 13:46:35 +00:00
|
|
|
|
{
|
1999-07-20 09:03:50 +00:00
|
|
|
|
new_buf[0] = '/';
|
|
|
|
|
dest = &new_buf[1];
|
|
|
|
|
}
|
1998-10-15 13:46:35 +00:00
|
|
|
|
|
1999-07-20 09:03:50 +00:00
|
|
|
|
for (start = end = name; *start; start = end)
|
|
|
|
|
{
|
|
|
|
|
struct stat st;
|
|
|
|
|
int n;
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
/* Elide repeated path separators */
|
|
|
|
|
while (*start == '/')
|
|
|
|
|
start++;
|
|
|
|
|
|
|
|
|
|
/* Locate end of path component */
|
|
|
|
|
end = start;
|
|
|
|
|
while (*end && *end != '/')
|
|
|
|
|
end++;
|
|
|
|
|
|
|
|
|
|
len = end - start;
|
|
|
|
|
if (len == 0)
|
1998-10-15 13:46:35 +00:00
|
|
|
|
{
|
1999-07-20 09:03:50 +00:00
|
|
|
|
break; /* End of path. */
|
1998-10-15 13:46:35 +00:00
|
|
|
|
}
|
1999-07-20 09:03:50 +00:00
|
|
|
|
else if (len == 1 && *start == '.')
|
1998-10-15 13:46:35 +00:00
|
|
|
|
{
|
1999-07-20 09:03:50 +00:00
|
|
|
|
/* Elide '/./' sequence by ignoring it. */
|
1998-10-15 13:46:35 +00:00
|
|
|
|
}
|
1999-07-20 09:03:50 +00:00
|
|
|
|
else if (len == 2 && strncmp(start, "..", len) == 0)
|
1998-10-15 13:46:35 +00:00
|
|
|
|
{
|
1999-07-20 09:03:50 +00:00
|
|
|
|
/*
|
|
|
|
|
* Backup - if we are not at the root, remove the last component.
|
|
|
|
|
*/
|
|
|
|
|
if (dest > &new_buf[1])
|
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
dest--;
|
|
|
|
|
}
|
|
|
|
|
while (dest[-1] != '/');
|
|
|
|
|
}
|
1998-10-15 13:46:35 +00:00
|
|
|
|
}
|
1999-07-20 09:03:50 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (dest[-1] != '/')
|
|
|
|
|
*dest++ = '/';
|
|
|
|
|
|
|
|
|
|
if (&dest[len] >= &new_buf[MAX_PATH])
|
|
|
|
|
return self; /* Resolved name would be too long. */
|
|
|
|
|
|
|
|
|
|
memcpy(dest, start, len);
|
|
|
|
|
dest += len;
|
|
|
|
|
*dest = '\0';
|
|
|
|
|
|
|
|
|
|
if (lstat(new_buf, &st) < 0)
|
|
|
|
|
return self; /* Unable to stat file. */
|
|
|
|
|
|
|
|
|
|
if (S_ISLNK(st.st_mode))
|
|
|
|
|
{
|
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
|
|
|
|
|
|
if (++num_links > MAXSYMLINKS)
|
|
|
|
|
return self; /* Too many symbolic links. */
|
|
|
|
|
|
|
|
|
|
n = readlink(new_buf, buf, MAX_PATH);
|
|
|
|
|
if (n < 0)
|
|
|
|
|
return self; /* Couldn't resolve links. */
|
|
|
|
|
|
|
|
|
|
buf[n] = '\0';
|
|
|
|
|
|
|
|
|
|
if ((n + strlen(end)) >= MAX_PATH)
|
|
|
|
|
return self; /* Path would be too long. */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Concatenate the resolved name with the string still to
|
|
|
|
|
* be processed, and start using the result as input.
|
|
|
|
|
*/
|
|
|
|
|
strcat(buf, end);
|
|
|
|
|
strcpy(extra, buf);
|
|
|
|
|
name = end = extra;
|
|
|
|
|
|
|
|
|
|
if (buf[0] == '/')
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* For an absolute link, we start at root again.
|
|
|
|
|
*/
|
|
|
|
|
dest = new_buf + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Backup - remove the last component.
|
|
|
|
|
*/
|
|
|
|
|
if (dest > new_buf + 1)
|
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
dest--;
|
|
|
|
|
}
|
|
|
|
|
while (dest[-1] != '/');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
num_links = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
1998-10-15 13:46:35 +00:00
|
|
|
|
}
|
1999-07-20 09:03:50 +00:00
|
|
|
|
if (dest > new_buf + 1 && dest[-1] == '/')
|
|
|
|
|
--dest;
|
|
|
|
|
*dest = '\0';
|
1999-07-12 04:21:05 +00:00
|
|
|
|
#endif
|
1999-07-20 09:03:50 +00:00
|
|
|
|
if (strncmp(new_buf, "/private/", 9) == 0)
|
|
|
|
|
{
|
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
|
|
if (lstat(&new_buf[8], &st) == 0)
|
|
|
|
|
strcpy(new_buf, &new_buf[8]);
|
|
|
|
|
}
|
2000-10-29 14:52:33 +00:00
|
|
|
|
return [NSStringClass stringWithCString: new_buf];
|
2002-04-29 12:20:08 +00:00
|
|
|
|
#endif /* (__MINGW__) */
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-02-20 09:28:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a standardised form of the receiver, with unnecessary parts
|
|
|
|
|
* removed, tilde characters expanded, and symbolic links resolved
|
|
|
|
|
* where possible.<br />
|
|
|
|
|
* If the string is an invalid path, the unmodified receiver is returned.<br />
|
|
|
|
|
* <p>
|
|
|
|
|
* Uses -stringByExpandingTildeInPath to expand tilde expressions.<br />
|
|
|
|
|
* Simplifies '//' and '/./' sequences.<br />
|
|
|
|
|
* Removes any '/private' prefix.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* For absolute paths, uses -stringByResolvingSymlinksInPath to resolve
|
|
|
|
|
* any links, then gets rid of '/../' sequences.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (NSString*) stringByStandardizingPath
|
|
|
|
|
{
|
1999-07-12 04:21:05 +00:00
|
|
|
|
NSMutableString *s;
|
|
|
|
|
NSRange r;
|
2002-03-13 13:46:12 +00:00
|
|
|
|
unichar (*caiImp)(NSString*, SEL, unsigned int);
|
1996-11-24 21:04:24 +00:00
|
|
|
|
|
|
|
|
|
/* Expand `~' in the path */
|
2002-02-20 09:28:51 +00:00
|
|
|
|
s = AUTORELEASE([[self stringByExpandingTildeInPath] mutableCopy]);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
caiImp = (unichar (*)())[s methodForSelector: caiSel];
|
1996-11-24 21:04:24 +00:00
|
|
|
|
|
1999-07-20 09:03:50 +00:00
|
|
|
|
/* Condense `//' and '/./' */
|
1999-07-12 11:23:06 +00:00
|
|
|
|
r = NSMakeRange(0, [s length]);
|
1999-07-12 04:21:05 +00:00
|
|
|
|
while ((r = [s rangeOfCharacterFromSet: pathSeps()
|
|
|
|
|
options: 0
|
|
|
|
|
range: r]).length)
|
|
|
|
|
{
|
1999-07-20 09:03:50 +00:00
|
|
|
|
unsigned length = [s length];
|
1996-11-24 21:04:24 +00:00
|
|
|
|
|
1999-07-20 09:03:50 +00:00
|
|
|
|
if (r.location + r.length + 1 <= length
|
2000-08-07 22:00:31 +00:00
|
|
|
|
&& pathSepMember((*caiImp)(s, caiSel, r.location + 1)) == YES)
|
1999-07-20 09:03:50 +00:00
|
|
|
|
{
|
|
|
|
|
[s deleteCharactersInRange: r];
|
|
|
|
|
}
|
|
|
|
|
else if (r.location + r.length + 2 <= length
|
2000-08-07 22:00:31 +00:00
|
|
|
|
&& (*caiImp)(s, caiSel, r.location + 1) == (unichar)'.'
|
|
|
|
|
&& pathSepMember((*caiImp)(s, caiSel, r.location + 2)) == YES)
|
1999-07-12 04:21:05 +00:00
|
|
|
|
{
|
|
|
|
|
r.length++;
|
|
|
|
|
[s deleteCharactersInRange: r];
|
|
|
|
|
}
|
|
|
|
|
else
|
1999-07-20 09:03:50 +00:00
|
|
|
|
{
|
|
|
|
|
r.location++;
|
|
|
|
|
}
|
1999-07-12 04:21:05 +00:00
|
|
|
|
if ((r.length = [s length]) > r.location)
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
|
|
|
|
r.length -= r.location;
|
|
|
|
|
}
|
1999-07-12 04:21:05 +00:00
|
|
|
|
else
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
1996-11-24 21:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-07-12 04:21:05 +00:00
|
|
|
|
if ([s isAbsolutePath] == NO)
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
|
|
|
|
return s;
|
|
|
|
|
}
|
1999-07-12 04:21:05 +00:00
|
|
|
|
|
2002-02-20 09:28:51 +00:00
|
|
|
|
/* Remove `/private' */
|
|
|
|
|
if ([s hasPrefix: @"/private"])
|
|
|
|
|
{
|
|
|
|
|
[s deleteCharactersInRange: ((NSRange){0,7})];
|
|
|
|
|
}
|
|
|
|
|
|
1999-07-20 09:03:50 +00:00
|
|
|
|
/*
|
2000-06-12 05:17:41 +00:00
|
|
|
|
* For absolute paths, we must resolve symbolic links or (on MINGW)
|
1999-07-20 09:03:50 +00:00
|
|
|
|
* remove '/../' sequences and their matching parent directories.
|
|
|
|
|
*/
|
2000-06-12 05:17:41 +00:00
|
|
|
|
#if defined(__MINGW__)
|
1996-11-24 21:04:24 +00:00
|
|
|
|
/* Condense `/../' */
|
1999-07-12 11:23:06 +00:00
|
|
|
|
r = NSMakeRange(0, [s length]);
|
1999-07-12 04:21:05 +00:00
|
|
|
|
while ((r = [s rangeOfCharacterFromSet: pathSeps()
|
|
|
|
|
options: 0
|
|
|
|
|
range: r]).length)
|
1996-11-24 21:04:24 +00:00
|
|
|
|
{
|
1999-07-12 04:21:05 +00:00
|
|
|
|
if (r.location + r.length + 3 <= [s length]
|
2000-08-07 22:00:31 +00:00
|
|
|
|
&& (*caiImp)(s, caiSel, r.location + 1) == (unichar)'.'
|
|
|
|
|
&& (*caiImp)(s, caiSel, r.location + 2) == (unichar)'.'
|
|
|
|
|
&& pathSepMember((*caiImp)(s, caiSel, r.location + 3)) == YES)
|
1999-01-20 13:28:28 +00:00
|
|
|
|
{
|
1999-07-12 04:21:05 +00:00
|
|
|
|
if (r.location > 0)
|
|
|
|
|
{
|
|
|
|
|
NSRange r2 = {0, r.location};
|
|
|
|
|
r = [s rangeOfCharacterFromSet: pathSeps()
|
|
|
|
|
options: NSBackwardsSearch
|
|
|
|
|
range: r2];
|
|
|
|
|
if (r.length == 0)
|
2002-02-20 09:28:51 +00:00
|
|
|
|
{
|
|
|
|
|
r = r2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
r.length = r2.length - r.location - 1;
|
|
|
|
|
}
|
1999-07-12 04:21:05 +00:00
|
|
|
|
r.length += 4; /* Add the `/../' */
|
|
|
|
|
}
|
|
|
|
|
[s deleteCharactersInRange: r];
|
1999-01-20 13:28:28 +00:00
|
|
|
|
}
|
1999-07-12 04:21:05 +00:00
|
|
|
|
else
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
|
|
|
|
r.location++;
|
|
|
|
|
}
|
|
|
|
|
|
1999-07-12 04:21:05 +00:00
|
|
|
|
if ((r.length = [s length]) > r.location)
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
|
|
|
|
r.length -= r.location;
|
|
|
|
|
}
|
1999-07-12 04:21:05 +00:00
|
|
|
|
else
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
1996-11-24 21:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s;
|
1999-07-20 09:03:50 +00:00
|
|
|
|
#else
|
|
|
|
|
return [s stringByResolvingSymlinksInPath];
|
|
|
|
|
#endif
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-30 08:36:20 +00:00
|
|
|
|
/**
|
|
|
|
|
* Return a string formed by removing characters from the ends of the
|
|
|
|
|
* receiver. Characters are removed only if they are in aSet.<br />
|
|
|
|
|
* If the string consists entirely of characters in aSet, an empty
|
|
|
|
|
* string is returned.<br />
|
|
|
|
|
* The aSet argument nust not be nil.<br />
|
|
|
|
|
*/
|
|
|
|
|
- (NSString*) stringByTrimmingCharactersInSet: (NSCharacterSet*)aSet
|
|
|
|
|
{
|
|
|
|
|
unsigned length = [self length];
|
|
|
|
|
unsigned end = length;
|
|
|
|
|
unsigned start = 0;
|
|
|
|
|
|
|
|
|
|
if (aSet == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"%@ - nil character set argument", NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
|
|
|
|
unichar (*caiImp)(NSString*, SEL, unsigned int);
|
|
|
|
|
BOOL (*mImp)(id, SEL, unichar);
|
|
|
|
|
unichar letter;
|
|
|
|
|
|
|
|
|
|
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
|
|
|
|
mImp = (BOOL(*)(id,SEL,unichar)) [aSet methodForSelector: cMemberSel];
|
|
|
|
|
|
|
|
|
|
while (end > 0)
|
|
|
|
|
{
|
|
|
|
|
letter = (*caiImp)(self, caiSel, end-1);
|
|
|
|
|
if ((*mImp)(aSet, cMemberSel, letter) == NO)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
end--;
|
|
|
|
|
}
|
|
|
|
|
while (start < end)
|
|
|
|
|
{
|
|
|
|
|
letter = (*caiImp)(self, caiSel, start);
|
|
|
|
|
if ((*mImp)(aSet, cMemberSel, letter) == NO)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
start++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (start == 0 && end == length)
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
if (start == end)
|
|
|
|
|
{
|
|
|
|
|
return @"";
|
|
|
|
|
}
|
|
|
|
|
return [self substringFromRange: NSMakeRange(start, end - start)];
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
// private methods for Unicode level 3 implementation
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (int) _baseLength
|
|
|
|
|
{
|
1999-04-09 17:07:21 +00:00
|
|
|
|
int blen = 0;
|
|
|
|
|
unsigned len = [self length];
|
|
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
|
{
|
2003-01-03 20:14:47 +00:00
|
|
|
|
unsigned int count = 0;
|
2002-03-13 13:46:12 +00:00
|
|
|
|
unichar (*caiImp)(NSString*, SEL, unsigned int);
|
1999-04-09 17:07:21 +00:00
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
|
caiImp = (unichar (*)())[self methodForSelector: caiSel];
|
1999-04-09 17:07:21 +00:00
|
|
|
|
while (count < len)
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
|
|
|
|
if (!uni_isnonsp((*caiImp)(self, caiSel, count++)))
|
|
|
|
|
{
|
|
|
|
|
blen++;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-04-09 17:07:21 +00:00
|
|
|
|
}
|
1997-05-03 18:05:21 +00:00
|
|
|
|
return blen;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
}
|
1997-05-03 18:05:21 +00:00
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Concatenates the strings in the components array placing a path
|
|
|
|
|
* separator between each one and returns the result.
|
|
|
|
|
*/
|
1998-01-21 15:09:22 +00:00
|
|
|
|
+ (NSString*) pathWithComponents: (NSArray*)components
|
|
|
|
|
{
|
1999-06-03 10:59:25 +00:00
|
|
|
|
NSString *s;
|
|
|
|
|
unsigned c;
|
|
|
|
|
unsigned i;
|
1998-01-21 15:09:22 +00:00
|
|
|
|
|
1999-06-03 10:59:25 +00:00
|
|
|
|
c = [components count];
|
|
|
|
|
if (c == 0)
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
|
|
|
|
return @"";
|
|
|
|
|
}
|
1999-06-03 10:59:25 +00:00
|
|
|
|
s = [components objectAtIndex: 0];
|
2002-04-29 12:20:08 +00:00
|
|
|
|
if ([s length] == 0)
|
2002-04-18 07:51:46 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
s = @"/";
|
2002-04-18 07:51:46 +00:00
|
|
|
|
}
|
1999-06-03 10:59:25 +00:00
|
|
|
|
for (i = 1; i < c; i++)
|
|
|
|
|
{
|
|
|
|
|
s = [s stringByAppendingPathComponent: [components objectAtIndex: i]];
|
1998-01-21 15:09:22 +00:00
|
|
|
|
}
|
1999-06-03 10:59:25 +00:00
|
|
|
|
return s;
|
1998-01-21 15:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/*
|
|
|
|
|
* Returs YES if the receiver represents an absolute path ... ie if it begins
|
|
|
|
|
* with a '/' or a '~'<br />
|
|
|
|
|
* Returns NO otherwise.
|
|
|
|
|
*/
|
1998-01-21 15:09:22 +00:00
|
|
|
|
- (BOOL) isAbsolutePath
|
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
unichar c;
|
|
|
|
|
|
1999-05-06 05:53:51 +00:00
|
|
|
|
if ([self length] == 0)
|
2001-03-17 11:45:37 +00:00
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2002-04-29 12:20:08 +00:00
|
|
|
|
c = [self characterAtIndex: 0];
|
2000-06-12 05:17:41 +00:00
|
|
|
|
#if defined(__MINGW__)
|
2002-04-29 12:20:08 +00:00
|
|
|
|
if (isalpha(c) && [self indexOfString: @":"] == 1)
|
2000-09-22 04:20:52 +00:00
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
return YES;
|
2000-09-22 04:20:52 +00:00
|
|
|
|
}
|
2002-04-29 12:20:08 +00:00
|
|
|
|
#endif
|
|
|
|
|
if (c == (unichar)'/' || c == (unichar)'~')
|
2001-03-17 11:45:37 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
1999-05-06 05:53:51 +00:00
|
|
|
|
return NO;
|
1998-01-21 15:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the path components of the reciever separated into an array.<br />
|
|
|
|
|
* If the receiver begins with a '/' character then that is used as the
|
|
|
|
|
* first element in the array.<br />
|
|
|
|
|
* Empty components are removed.
|
|
|
|
|
*/
|
1998-01-21 15:09:22 +00:00
|
|
|
|
- (NSArray*) pathComponents
|
|
|
|
|
{
|
2000-03-23 18:57:43 +00:00
|
|
|
|
NSMutableArray *a;
|
|
|
|
|
NSArray *r;
|
1998-01-21 15:09:22 +00:00
|
|
|
|
|
2001-01-21 16:03:22 +00:00
|
|
|
|
if ([self length] == 0)
|
|
|
|
|
{
|
|
|
|
|
return [NSArray array];
|
|
|
|
|
}
|
2002-04-29 12:20:08 +00:00
|
|
|
|
a = [[self componentsSeparatedByString: @"/"] mutableCopy];
|
2000-03-23 18:57:43 +00:00
|
|
|
|
if ([a count] > 0)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
1998-01-21 15:09:22 +00:00
|
|
|
|
|
2001-05-15 13:35:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* If the path began with a '/' then the first path component must
|
2000-03-23 18:57:43 +00:00
|
|
|
|
* be a '/' rather than an empty string so that our output could be
|
|
|
|
|
* fed into [+pathWithComponents: ]
|
|
|
|
|
*/
|
|
|
|
|
if ([[a objectAtIndex: 0] length] == 0)
|
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
[a replaceObjectAtIndex: 0 withObject: @"/"];
|
1998-01-21 15:09:22 +00:00
|
|
|
|
}
|
2001-05-15 13:35:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* Similarly if the path ended with a path separator (other than the
|
|
|
|
|
* leading one).
|
|
|
|
|
*/
|
|
|
|
|
if ([[a objectAtIndex: [a count]-1] length] == 0)
|
|
|
|
|
{
|
|
|
|
|
if ([self length] > 1)
|
|
|
|
|
{
|
2002-04-29 12:20:08 +00:00
|
|
|
|
[a replaceObjectAtIndex: [a count]-1 withObject: @"/"];
|
2001-05-15 13:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2001-01-21 16:03:22 +00:00
|
|
|
|
/* Any empty path components must be removed. */
|
|
|
|
|
for (i = [a count] - 1; i > 0; i--)
|
2000-03-23 18:57:43 +00:00
|
|
|
|
{
|
|
|
|
|
if ([[a objectAtIndex: i] length] == 0)
|
|
|
|
|
{
|
|
|
|
|
[a removeObjectAtIndex: i];
|
1998-01-21 15:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-03-23 18:57:43 +00:00
|
|
|
|
r = [a copy];
|
|
|
|
|
RELEASE(a);
|
|
|
|
|
return AUTORELEASE(r);
|
1998-01-21 15:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an array of strings made by appending the values in paths
|
|
|
|
|
* to the receiver.
|
|
|
|
|
*/
|
1998-01-21 15:09:22 +00:00
|
|
|
|
- (NSArray*) stringsByAppendingPaths: (NSArray*)paths
|
|
|
|
|
{
|
1999-05-06 14:42:26 +00:00
|
|
|
|
NSMutableArray *a;
|
|
|
|
|
NSArray *r;
|
2001-04-06 22:51:48 +00:00
|
|
|
|
unsigned i, count = [paths count];
|
1998-01-21 15:09:22 +00:00
|
|
|
|
|
1999-05-06 14:42:26 +00:00
|
|
|
|
a = [[NSMutableArray allocWithZone: NSDefaultMallocZone()]
|
2001-04-06 22:51:48 +00:00
|
|
|
|
initWithCapacity: count];
|
|
|
|
|
for (i = 0; i < count; i++)
|
1999-05-06 14:42:26 +00:00
|
|
|
|
{
|
|
|
|
|
NSString *s = [paths objectAtIndex: i];
|
1998-01-21 15:09:22 +00:00
|
|
|
|
|
1999-05-06 14:42:26 +00:00
|
|
|
|
s = [self stringByAppendingPathComponent: s];
|
|
|
|
|
[a addObject: s];
|
1998-01-21 15:09:22 +00:00
|
|
|
|
}
|
1999-05-06 14:42:26 +00:00
|
|
|
|
r = [a copy];
|
1999-06-03 10:59:25 +00:00
|
|
|
|
RELEASE(a);
|
|
|
|
|
return AUTORELEASE(r);
|
1998-01-21 15:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
+ (NSString*) localizedStringWithFormat: (NSString*) format, ...
|
|
|
|
|
{
|
2001-03-20 02:13:19 +00:00
|
|
|
|
va_list ap;
|
|
|
|
|
id ret;
|
|
|
|
|
NSDictionary *dict;
|
|
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
|
if (format == nil)
|
|
|
|
|
{
|
|
|
|
|
ret = nil;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2001-11-10 17:31:39 +00:00
|
|
|
|
dict = GSUserDefaultsDictionaryRepresentation();
|
2001-03-20 02:13:19 +00:00
|
|
|
|
ret = AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
|
initWithFormat: format locale: dict arguments: ap]);
|
|
|
|
|
}
|
|
|
|
|
va_end(ap);
|
|
|
|
|
return ret;
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSComparisonResult) caseInsensitiveCompare: (NSString*)aString
|
|
|
|
|
{
|
1999-11-26 19:43:43 +00:00
|
|
|
|
return [self compare: aString
|
2002-04-29 12:20:08 +00:00
|
|
|
|
options: NSCaseInsensitiveSearch
|
1999-11-26 19:43:43 +00:00
|
|
|
|
range: ((NSRange){0, [self length]})];
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
- (NSComparisonResult) compare: (NSString *)string
|
|
|
|
|
options: (unsigned int)mask
|
|
|
|
|
range: (NSRange)compareRange
|
2000-09-30 04:54:43 +00:00
|
|
|
|
locale: (NSDictionary *)dict
|
2000-09-12 23:09:50 +00:00
|
|
|
|
{
|
|
|
|
|
// FIXME: This does only a normal compare
|
|
|
|
|
return [self compare: string
|
|
|
|
|
options: mask
|
|
|
|
|
range: compareRange];
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-30 04:54:43 +00:00
|
|
|
|
- (NSComparisonResult) localizedCompare: (NSString *)string
|
2000-09-12 23:09:50 +00:00
|
|
|
|
{
|
2001-11-10 17:31:39 +00:00
|
|
|
|
NSDictionary *dict = GSUserDefaultsDictionaryRepresentation();
|
2001-03-20 02:13:19 +00:00
|
|
|
|
|
2000-09-12 23:09:50 +00:00
|
|
|
|
return [self compare: string
|
2001-03-20 02:13:19 +00:00
|
|
|
|
options: 0
|
|
|
|
|
range: ((NSRange){0, [self length]})
|
|
|
|
|
locale: dict];
|
2000-09-12 23:09:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-09-30 04:54:43 +00:00
|
|
|
|
- (NSComparisonResult) localizedCaseInsensitiveCompare: (NSString *)string
|
2000-09-12 23:09:50 +00:00
|
|
|
|
{
|
2001-11-10 17:31:39 +00:00
|
|
|
|
NSDictionary *dict = GSUserDefaultsDictionaryRepresentation();
|
2001-03-20 02:13:19 +00:00
|
|
|
|
|
2000-09-12 23:09:50 +00:00
|
|
|
|
return [self compare: string
|
2002-04-29 12:20:08 +00:00
|
|
|
|
options: NSCaseInsensitiveSearch
|
2001-03-20 02:13:19 +00:00
|
|
|
|
range: ((NSRange){0, [self length]})
|
|
|
|
|
locale: dict];
|
2000-09-12 23:09:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (BOOL) writeToFile: (NSString*)filename
|
1999-11-26 19:43:43 +00:00
|
|
|
|
atomically: (BOOL)useAuxiliaryFile
|
1997-05-03 18:05:21 +00:00
|
|
|
|
{
|
2001-04-12 09:11:31 +00:00
|
|
|
|
id d = [self dataUsingEncoding: _DefaultStringEncoding];
|
|
|
|
|
|
|
|
|
|
if (d == nil)
|
|
|
|
|
{
|
|
|
|
|
d = [self dataUsingEncoding: NSUnicodeStringEncoding];
|
|
|
|
|
}
|
1998-01-08 15:25:59 +00:00
|
|
|
|
return [d writeToFile: filename atomically: useAuxiliaryFile];
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-09-30 04:54:43 +00:00
|
|
|
|
- (BOOL) writeToURL: (NSURL*)anURL atomically: (BOOL)atomically
|
2000-09-12 23:09:50 +00:00
|
|
|
|
{
|
2001-04-12 09:11:31 +00:00
|
|
|
|
id d = [self dataUsingEncoding: _DefaultStringEncoding];
|
|
|
|
|
|
|
|
|
|
if (d == nil)
|
|
|
|
|
{
|
|
|
|
|
d = [self dataUsingEncoding: NSUnicodeStringEncoding];
|
|
|
|
|
}
|
2000-09-12 23:09:50 +00:00
|
|
|
|
return [d writeToURL: anURL atomically: atomically];
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
/* NSCopying Protocol */
|
|
|
|
|
|
1999-11-26 19:43:43 +00:00
|
|
|
|
- (id) copyWithZone: (NSZone*)zone
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1998-03-12 14:21:20 +00:00
|
|
|
|
if ([self isKindOfClass: [NSMutableString class]] ||
|
2000-10-23 06:18:03 +00:00
|
|
|
|
NSShouldRetainWithZone(self, zone) == NO)
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return [[NSStringClass allocWithZone: zone] initWithString: self];
|
1998-03-12 14:21:20 +00:00
|
|
|
|
else
|
1999-06-03 10:59:25 +00:00
|
|
|
|
return RETAIN(self);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-07-02 13:26:37 +00:00
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)zone
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return [[GSMutableStringClass allocWithZone: zone] initWithString: self];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-09 01:53:53 +00:00
|
|
|
|
/* NSCoding Protocol */
|
|
|
|
|
|
2000-10-09 04:41:18 +00:00
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
1995-04-09 01:53:53 +00:00
|
|
|
|
{
|
2000-10-09 04:41:18 +00:00
|
|
|
|
unsigned count = [self length];
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned int) at: &count];
|
2000-10-09 04:41:18 +00:00
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
NSStringEncoding enc = NSUnicodeStringEncoding;
|
|
|
|
|
unichar *chars;
|
|
|
|
|
|
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(NSStringEncoding) at: &enc];
|
|
|
|
|
|
|
|
|
|
chars = NSZoneMalloc(NSDefaultMallocZone(), count*sizeof(unichar));
|
|
|
|
|
[self getCharacters: chars];
|
|
|
|
|
[aCoder encodeArrayOfObjCType: @encode(unichar)
|
|
|
|
|
count: count
|
|
|
|
|
at: chars];
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), chars);
|
|
|
|
|
}
|
1995-04-09 01:53:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 04:41:18 +00:00
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
1995-04-09 01:53:53 +00:00
|
|
|
|
{
|
2000-10-09 04:41:18 +00:00
|
|
|
|
unsigned count;
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(unsigned int) at: &count];
|
2001-04-11 16:51:17 +00:00
|
|
|
|
|
2000-10-09 04:41:18 +00:00
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
NSStringEncoding enc;
|
|
|
|
|
NSZone *zone;
|
2002-04-29 12:20:08 +00:00
|
|
|
|
|
2000-10-09 04:41:18 +00:00
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(NSStringEncoding) at: &enc];
|
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
2000-10-31 16:17:33 +00:00
|
|
|
|
zone = GSObjCZone(self);
|
2000-10-09 04:41:18 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (enc == NSUnicodeStringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unichar *chars;
|
|
|
|
|
|
|
|
|
|
chars = NSZoneMalloc(zone, count*sizeof(unichar));
|
|
|
|
|
[aCoder decodeArrayOfObjCType: @encode(unichar)
|
|
|
|
|
count: count
|
|
|
|
|
at: chars];
|
|
|
|
|
self = [self initWithCharactersNoCopy: chars
|
|
|
|
|
length: count
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
}
|
2001-04-12 09:11:31 +00:00
|
|
|
|
else if (enc == NSASCIIStringEncoding
|
|
|
|
|
|| enc == _DefaultStringEncoding)
|
2000-10-09 04:41:18 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned char *chars;
|
|
|
|
|
|
|
|
|
|
chars = NSZoneMalloc(zone, count+1);
|
|
|
|
|
[aCoder decodeArrayOfObjCType: @encode(unsigned char)
|
|
|
|
|
count: count
|
|
|
|
|
at: chars];
|
|
|
|
|
self = [self initWithCStringNoCopy: chars
|
|
|
|
|
length: count
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
}
|
|
|
|
|
else if (enc == NSUTF8StringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unsigned char *chars;
|
|
|
|
|
|
|
|
|
|
chars = NSZoneMalloc(zone, count+1);
|
|
|
|
|
[aCoder decodeArrayOfObjCType: @encode(unsigned char)
|
|
|
|
|
count: count
|
|
|
|
|
at: chars];
|
|
|
|
|
chars[count] = '\0';
|
|
|
|
|
self = [self initWithUTF8String: chars];
|
|
|
|
|
NSZoneFree(zone, chars);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned char *chars;
|
|
|
|
|
NSData *data;
|
|
|
|
|
|
|
|
|
|
chars = NSZoneMalloc(zone, count);
|
|
|
|
|
[aCoder decodeArrayOfObjCType: @encode(unsigned char)
|
|
|
|
|
count: count
|
|
|
|
|
at: chars];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
data = [NSDataClass allocWithZone: zone];
|
2000-10-09 04:41:18 +00:00
|
|
|
|
data = [data initWithBytesNoCopy: chars length: count];
|
|
|
|
|
self = [self initWithData: data encoding: enc];
|
|
|
|
|
RELEASE(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2001-07-02 16:15:22 +00:00
|
|
|
|
self = [self initWithCStringNoCopy: "" length: 0 freeWhenDone: NO];
|
2000-10-09 04:41:18 +00:00
|
|
|
|
}
|
1999-07-02 13:26:37 +00:00
|
|
|
|
return self;
|
1995-04-09 01:53:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
|
- (Class) classForCoder
|
|
|
|
|
{
|
2000-10-23 06:18:03 +00:00
|
|
|
|
return NSStringClass;
|
1998-12-18 17:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) replacementObjectForPortCoder: (NSPortCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
if ([aCoder isByref] == NO)
|
|
|
|
|
return self;
|
|
|
|
|
return [super replacementObjectForPortCoder: aCoder];
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-11 10:18:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>Attempts to interpret the receiver as a <em>property list</em>
|
|
|
|
|
* and returns the result. If the receiver does not contain a
|
|
|
|
|
* string representation of a <em>property list</em> then the method
|
|
|
|
|
* returns nil.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>There are three readable <em>property list</em> storage formats -
|
|
|
|
|
* The binary format used by [NSSerializer] does not concern us here,
|
|
|
|
|
* but there are two 'human readable' formats, the <em>traditional</em>
|
|
|
|
|
* OpenStep format (which is extended in GNUstep) and the <em>XML</em> format.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>The [NSArray-descriptionWithLocale:indent:] and
|
|
|
|
|
* [NSDictionary-descriptionWithLocale:indent:] methods
|
|
|
|
|
* both generate strings containing traditional style <em>property lists</em>,
|
|
|
|
|
* but [NSArray-writeToFile:atomically:] and
|
|
|
|
|
* [NSDictionary-writeToFile:atomically:] generate either traditional or
|
|
|
|
|
* XML style <em>property lists</em> depending on the value of the
|
|
|
|
|
* GSMacOSXCompatible and NSWriteOldStylePropertyLists user defaults.<br />
|
|
|
|
|
* If GSMacOSXCompatible is YES then XML <em>property lists</em> are
|
|
|
|
|
* written unless NSWriteOldStylePropertyLists is also YES.<br />
|
|
|
|
|
* By default GNUstep writes old style data and always supports reading of
|
|
|
|
|
* either style.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>The traditional format is more compact and more easily readable by
|
|
|
|
|
* people, but (without the GNUstep extensions) cannot represent date and
|
|
|
|
|
* number objects (except as strings). The XML format is more verbose and
|
|
|
|
|
* less readable, but can be fed into modern XML tools and thus used to
|
|
|
|
|
* pass data to non-OpenStep applications more readily.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>The traditional format is strictly ascii encoded, with any unicode
|
|
|
|
|
* characters represented by escape sequences. The XML format is encoded
|
|
|
|
|
* as UTF8 data.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>Both the traditional format and the XML format permit comments to be
|
|
|
|
|
* placed in <em>property list</em> documents. In traditional format the
|
|
|
|
|
* comment notations used in ObjectiveC programming are supported, while
|
|
|
|
|
* in XML format, the standard SGML comment sequences are used.
|
|
|
|
|
* </p>
|
|
|
|
|
* A <em>property list</em> may only be one of the following classes -
|
|
|
|
|
* <deflist>
|
|
|
|
|
* <term>[NSArray]</term>
|
|
|
|
|
* <desc>
|
|
|
|
|
* An array which is either empty or contains only <em>property list</em>
|
|
|
|
|
* objects.<br />
|
|
|
|
|
* An array is delimited by round brackets and its contents are comma
|
|
|
|
|
* <em>separated</em> (there is no comma after the last array element).
|
|
|
|
|
* <example>
|
|
|
|
|
* ( "one", "two", "three" )
|
|
|
|
|
* </example>
|
|
|
|
|
* In XML format, an array is an element whose name is <code>array</code>
|
|
|
|
|
* and whose content is the array content.
|
|
|
|
|
* <example>
|
|
|
|
|
* <array><string>one</string><string>two</string><string>three</string></array>
|
|
|
|
|
* </example>
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>[NSData]</term>
|
|
|
|
|
* <desc>
|
|
|
|
|
* An array is represented as a series of pairs of hexadecimal characters
|
|
|
|
|
* (each pair representing a byte of data) enclosed in angle brackets.
|
|
|
|
|
* Spaces are ignored).
|
|
|
|
|
* <example>
|
|
|
|
|
* < 54637374 696D67 >
|
|
|
|
|
* </example>
|
|
|
|
|
* In XML format, a data object is an element whose name is
|
|
|
|
|
* <code>data</code> and whose content is a stream of base64 encoded bytes.
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>[NSDate]</term>
|
|
|
|
|
* <desc>
|
|
|
|
|
* Date objects were not traditionally allowed in <em>property lists</em>
|
|
|
|
|
* but were added when the XML format was intoroduced. GNUstep provides
|
|
|
|
|
* an extension to the traditional <em>property list</em> format to
|
|
|
|
|
* support date objects, but older code will not read
|
|
|
|
|
* <em>property lists</em> containing this extension.<br />
|
|
|
|
|
* This format consists of an asterisk follwed by the letter 'D' then a
|
|
|
|
|
* date/time in YYYY-MM-DD HH:MM:SS +/-ZZZZ format, all enclosed within
|
|
|
|
|
* angle brackets.
|
|
|
|
|
* <example>
|
|
|
|
|
* <*D2002-03-22 11:30:00 +0100>
|
|
|
|
|
* </example>
|
|
|
|
|
* In XML format, a date object is an element whose name is
|
|
|
|
|
* <code>date</code> and whose content is a date in the above format.
|
|
|
|
|
* <example>
|
|
|
|
|
* <date>2002-03-22 11:30:00 +0100</date>
|
|
|
|
|
* </example>
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>[NSDictionary]</term>
|
|
|
|
|
* <desc>
|
|
|
|
|
* A dictionary which is either empty or contains only <em>string</em>
|
|
|
|
|
* keys and <em>property list</em> objects.<br />
|
|
|
|
|
* A dictionary is delimited by curly brackets and its contents are
|
|
|
|
|
* semicolon <em>terminated</em> (there is a semicolon after each value).
|
|
|
|
|
* Each item in the dictionary is a key/value pair with an equals sign
|
|
|
|
|
* after the key and before the value.
|
|
|
|
|
* <example>
|
|
|
|
|
* {
|
|
|
|
|
* "key1" = "value1";
|
|
|
|
|
* }
|
|
|
|
|
* </example>
|
|
|
|
|
* In XML format, a dictionary is an element whose name is
|
|
|
|
|
* <code>dictionary</code> and whose content consists of pairs of
|
|
|
|
|
* strings and other <em>property list</em> objects.
|
|
|
|
|
* <example>
|
|
|
|
|
* <dictionary>
|
|
|
|
|
* <string>key1</string>
|
|
|
|
|
* <string>value1</string>
|
|
|
|
|
* </dictionary>
|
|
|
|
|
* </example>
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>[NSNumber]</term>
|
|
|
|
|
* <desc>
|
|
|
|
|
* Number objects were not traditionally allowed in <em>property lists</em>
|
|
|
|
|
* but were added when the XML format was intoroduced. GNUstep provides
|
|
|
|
|
* an extension to the traditional <em>property list</em> format to
|
|
|
|
|
* support number objects, but older code will not read
|
|
|
|
|
* <em>property lists</em> containing this extension.<br />
|
|
|
|
|
* Numbers are stored in a variety of formats depending on their values.
|
|
|
|
|
* <list>
|
|
|
|
|
* <item>boolean ... either <code><*BY></code> for YES or
|
|
|
|
|
* <code><*BN></code> for NO.<br />
|
|
|
|
|
* In XML format this is either <code><true /></code> or
|
|
|
|
|
* <code><false /></code>
|
|
|
|
|
* </item>
|
|
|
|
|
* <item>integer ... <code><*INNN></code> where NNN is an
|
|
|
|
|
* integer.<br />
|
|
|
|
|
* In XML format this is <code><integer>NNN<integer></code>
|
|
|
|
|
* </item>
|
|
|
|
|
* <item>real ... <code><*RNNN></code> where NNN is a real
|
|
|
|
|
* number.<br />
|
|
|
|
|
* In XML format this is <code><real>NNN<real></code>
|
|
|
|
|
* </item>
|
|
|
|
|
* </list>
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>[NSString]</term>
|
|
|
|
|
* <desc>
|
|
|
|
|
* A string is either stored literally (if it contains no spaces or special
|
|
|
|
|
* characters), or is stored as a quoted string with special characters
|
|
|
|
|
* escaped where necessary.<br />
|
|
|
|
|
* Escape conventions are similar to those normally used in ObjectiveC
|
|
|
|
|
* programming, using a backslash followed by -
|
|
|
|
|
* <list>
|
|
|
|
|
* <item><strong>\</strong> a backslash character</item>
|
|
|
|
|
* <item><strong>"</strong> a quote character</item>
|
|
|
|
|
* <item><strong>b</strong> a backspace character</item>
|
|
|
|
|
* <item><strong>n</strong> a newline character</item>
|
|
|
|
|
* <item><strong>r</strong> a carriage return character</item>
|
|
|
|
|
* <item><strong>t</strong> a tab character</item>
|
|
|
|
|
* <item><strong>OOO</strong> (three octal digits)
|
|
|
|
|
* an arbitrary ascii character</item>
|
|
|
|
|
* <item><strong>UXXXX</strong> (where X is a hexadecimal digit)
|
|
|
|
|
* a an arbitrary unicode character</item>
|
|
|
|
|
* </list>
|
|
|
|
|
* <example>
|
|
|
|
|
* "hello world & others"
|
|
|
|
|
* </example>
|
|
|
|
|
* In XML format, the string is simply stored in UTF8 format as the
|
|
|
|
|
* content of a <code>string</code> element, and the only character
|
|
|
|
|
* escapes required are those used by XML such as the
|
|
|
|
|
* '&lt;' markup representing a '<' character.
|
|
|
|
|
* <example>
|
|
|
|
|
* <string>hello world &amp; others</string>"
|
|
|
|
|
* </example>
|
|
|
|
|
* </desc>
|
|
|
|
|
* </deflist>
|
|
|
|
|
*/
|
1999-02-04 13:49:27 +00:00
|
|
|
|
- (id) propertyList
|
|
|
|
|
{
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return GSPropertyList(self);
|
1999-02-04 13:49:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-11 10:18:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>Reads a <em>property list</em> (see -propertyList) from a simplified
|
|
|
|
|
* file format. This format is a traditional style property list file
|
|
|
|
|
* containing a single dictionary, but with the leading '{' and trailing
|
|
|
|
|
* '}' characters omitted.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>That is to say, the file contains only semicolon separated key/value
|
|
|
|
|
* pairs (and optionally comments). As a convenience, it is possible to
|
|
|
|
|
* omit the equals sign and the value, so an entry consists of a key string
|
|
|
|
|
* followed by a semicolon. In this case, the value for that key is
|
|
|
|
|
* assumed to be an empty string.
|
|
|
|
|
* </p>
|
|
|
|
|
* <example>
|
|
|
|
|
* // Strings file entries follow -
|
|
|
|
|
* key1 = " a string value";
|
|
|
|
|
* key2; // This key has an empty string as a value.
|
|
|
|
|
* "Another key" = "a longer string value for th third key";
|
|
|
|
|
* </example>
|
|
|
|
|
*/
|
1999-02-04 13:49:27 +00:00
|
|
|
|
- (NSDictionary*) propertyListFromStringsFileFormat
|
|
|
|
|
{
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return GSPropertyListFromStringsFormat(self);
|
1999-02-04 13:49:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2002-08-30 12:30:50 +00:00
|
|
|
|
/**
|
|
|
|
|
* This is the mutable form of the NSString class.
|
|
|
|
|
*/
|
1995-04-03 01:35:42 +00:00
|
|
|
|
@implementation NSMutableString
|
|
|
|
|
|
1999-07-02 13:26:37 +00:00
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1995-11-19 20:29:39 +00:00
|
|
|
|
{
|
2000-10-23 06:18:03 +00:00
|
|
|
|
if (self == NSMutableStringClass)
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return NSAllocateObject(GSMutableStringClass, 0, z);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NSAllocateObject(self, 0, z);
|
|
|
|
|
}
|
1995-11-19 20:29:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
// Creating Temporary Strings
|
|
|
|
|
|
1999-12-01 19:36:20 +00:00
|
|
|
|
+ (NSMutableString*) string
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return AUTORELEASE([[GSMutableStringClass allocWithZone:
|
1999-12-01 19:36:20 +00:00
|
|
|
|
NSDefaultMallocZone()] initWithCapacity: 0]);
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
+ (NSMutableString*) stringWithCapacity: (unsigned int)capacity
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return AUTORELEASE([[GSMutableStringClass allocWithZone:
|
1999-12-01 19:36:20 +00:00
|
|
|
|
NSDefaultMallocZone()] initWithCapacity: capacity]);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Inefficient. */
|
|
|
|
|
+ (NSString*) stringWithCharacters: (const unichar*)characters
|
2002-03-13 13:46:12 +00:00
|
|
|
|
length: (unsigned int)length
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return AUTORELEASE([[GSMutableStringClass allocWithZone:
|
1999-12-01 19:36:20 +00:00
|
|
|
|
NSDefaultMallocZone()] initWithCharacters: characters length: length]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id) stringWithContentsOfFile: (NSString *)path
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return AUTORELEASE([[GSMutableStringClass allocWithZone:
|
1999-12-01 19:36:20 +00:00
|
|
|
|
NSDefaultMallocZone()] initWithContentsOfFile: path]);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSString*) stringWithCString: (const char*)byteString
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return AUTORELEASE([[GSMutableStringClass allocWithZone:
|
1999-06-03 10:59:25 +00:00
|
|
|
|
NSDefaultMallocZone()] initWithCString: byteString]);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-09-30 07:42:38 +00:00
|
|
|
|
+ (NSString*) stringWithCString: (const char*)byteString
|
2002-03-13 13:46:12 +00:00
|
|
|
|
length: (unsigned int)length
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return AUTORELEASE([[GSMutableStringClass allocWithZone:
|
1999-06-03 10:59:25 +00:00
|
|
|
|
NSDefaultMallocZone()] initWithCString: byteString length: length]);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSString*) stringWithFormat: (NSString*)format, ...
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, format);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
self = [super stringWithFormat: format arguments: ap];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
va_end(ap);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
// Designated initialiser
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (id) initWithCapacity: (unsigned int)capacity
|
1997-05-03 18:05:21 +00:00
|
|
|
|
{
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-05-03 18:05:21 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
1995-04-03 01:35:42 +00:00
|
|
|
|
|
2000-10-31 18:11:30 +00:00
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
2002-03-13 13:46:12 +00:00
|
|
|
|
length: (unsigned int)length
|
2000-10-31 18:11:30 +00:00
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
if ((self = [self initWithCapacity: length]) != nil && length > 0)
|
|
|
|
|
{
|
|
|
|
|
NSString *tmp;
|
|
|
|
|
|
|
|
|
|
tmp = [NSString allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
tmp = [tmp initWithCharactersNoCopy: chars
|
|
|
|
|
length: length
|
|
|
|
|
freeWhenDone: flag];
|
|
|
|
|
[self replaceCharactersInRange: NSMakeRange(0,0) withString: tmp];
|
|
|
|
|
RELEASE(tmp);
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCStringNoCopy: (char*)chars
|
2002-03-13 13:46:12 +00:00
|
|
|
|
length: (unsigned int)length
|
2000-10-31 18:11:30 +00:00
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
if ((self = [self initWithCapacity: length]) != nil && length > 0)
|
|
|
|
|
{
|
|
|
|
|
NSString *tmp;
|
|
|
|
|
|
|
|
|
|
tmp = [NSString allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
tmp = [tmp initWithCStringNoCopy: chars
|
|
|
|
|
length: length
|
|
|
|
|
freeWhenDone: flag];
|
|
|
|
|
[self replaceCharactersInRange: NSMakeRange(0,0) withString: tmp];
|
|
|
|
|
RELEASE(tmp);
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
// Modify A String
|
|
|
|
|
|
|
|
|
|
- (void) appendString: (NSString*)aString
|
|
|
|
|
{
|
1998-01-26 14:18:18 +00:00
|
|
|
|
NSRange aRange;
|
|
|
|
|
|
|
|
|
|
aRange.location = [self length];
|
|
|
|
|
aRange.length = 0;
|
|
|
|
|
[self replaceCharactersInRange: aRange withString: aString];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Inefficient. */
|
|
|
|
|
- (void) appendFormat: (NSString*)format, ...
|
|
|
|
|
{
|
2001-02-02 06:14:42 +00:00
|
|
|
|
va_list ap;
|
|
|
|
|
id tmp;
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
va_start(ap, format);
|
2001-02-02 06:14:42 +00:00
|
|
|
|
tmp = [[NSStringClass allocWithZone: NSDefaultMallocZone()]
|
1999-05-06 14:42:26 +00:00
|
|
|
|
initWithFormat: format arguments: ap];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
va_end(ap);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self appendString: tmp];
|
1999-06-03 10:59:25 +00:00
|
|
|
|
RELEASE(tmp);
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 04:41:18 +00:00
|
|
|
|
- (Class) classForCoder
|
|
|
|
|
{
|
2000-10-23 06:18:03 +00:00
|
|
|
|
return NSMutableStringClass;
|
2000-10-09 04:41:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (void) deleteCharactersInRange: (NSRange)range
|
|
|
|
|
{
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self replaceCharactersInRange: range withString: nil];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
- (void) insertString: (NSString*)aString atIndex: (unsigned int)loc
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1997-12-08 20:04:16 +00:00
|
|
|
|
NSRange range = {loc, 0};
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self replaceCharactersInRange: range withString: aString];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
- (void) replaceCharactersInRange: (NSRange)range
|
1999-11-26 19:43:43 +00:00
|
|
|
|
withString: (NSString*)aString
|
1995-04-03 01:35:42 +00:00
|
|
|
|
{
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-26 19:50:42 +00:00
|
|
|
|
/**
|
|
|
|
|
* Replaces all occurrences of the replace string with the by string,
|
|
|
|
|
* for those cases where the entire replace string lies within the
|
|
|
|
|
* specified searchRange value.<br />
|
|
|
|
|
* The value of opts determines the direction of the search is and
|
|
|
|
|
* whether only leading/trailing occurrances (anchored search) of
|
|
|
|
|
* replace are substituted.<br />
|
|
|
|
|
* Raises NSInvalidArgumentException if either string argument is nil.<br />
|
|
|
|
|
* Raises NSRangeException if part of searchRange is beyond the end
|
|
|
|
|
* of the receiver.
|
|
|
|
|
*/
|
|
|
|
|
- (unsigned int) replaceOccurrencesOfString: (NSString*)replace
|
|
|
|
|
withString: (NSString*)by
|
|
|
|
|
options: (unsigned int)opts
|
|
|
|
|
range: (NSRange)searchRange
|
|
|
|
|
{
|
|
|
|
|
NSRange range;
|
|
|
|
|
unsigned int count = 0;
|
|
|
|
|
|
|
|
|
|
if (replace == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"%@ nil search string", NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
if (by == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"%@ nil replace string", NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
range = [self rangeOfString: replace options: opts range: searchRange];
|
|
|
|
|
|
|
|
|
|
if (range.length > 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned byLen = [by length];
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
[self replaceCharactersInRange: range
|
|
|
|
|
withString: by];
|
|
|
|
|
if ((opts & NSBackwardsSearch) == NSBackwardsSearch)
|
|
|
|
|
{
|
|
|
|
|
searchRange.length = range.location - searchRange.location;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned int newEnd;
|
|
|
|
|
|
|
|
|
|
newEnd = NSMaxRange(searchRange) + byLen - range.length;
|
|
|
|
|
searchRange.location = range.location + byLen;
|
|
|
|
|
searchRange.length = newEnd - searchRange.location;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
range = [self rangeOfString: replace
|
|
|
|
|
options: opts
|
|
|
|
|
range: searchRange];
|
|
|
|
|
}
|
|
|
|
|
while (range.length > 0);
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:35:42 +00:00
|
|
|
|
- (void) setString: (NSString*)aString
|
|
|
|
|
{
|
1997-12-08 20:04:16 +00:00
|
|
|
|
NSRange range = {0, [self length]};
|
1999-05-06 14:42:26 +00:00
|
|
|
|
[self replaceCharactersInRange: range withString: aString];
|
1995-04-03 01:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2002-08-30 12:30:50 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GNUstep specific (non-standard) additions to the NSMutableString class.
|
|
|
|
|
* The methods in this category are not available in MacOS-X
|
|
|
|
|
*/
|
2000-10-23 06:18:03 +00:00
|
|
|
|
@implementation NSMutableString (GNUstep)
|
2002-02-06 13:44:03 +00:00
|
|
|
|
@class NSImmutableString;
|
|
|
|
|
@class GSImmutableString;
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
2002-09-15 18:26:56 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a proxy to the receiver which will allow access to the
|
|
|
|
|
* receiver as an NSString, but which will not allow any of the
|
|
|
|
|
* extra NSMutableString methods to be used. You can use this method
|
|
|
|
|
* to provide other code with read-only access to a mutable string
|
|
|
|
|
* you own.
|
|
|
|
|
*/
|
|
|
|
|
- (NSString*) immutableProxy
|
|
|
|
|
{
|
|
|
|
|
if ([self isKindOfClass: GSMutableStringClass])
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[GSImmutableString alloc] initWithString: self]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[NSImmutableString alloc] initWithString: self]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-02-13 00:50:41 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
|
|
|
|
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_LIBXML
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/GSXML.h"
|
2001-04-26 15:35:16 +00:00
|
|
|
|
static int XML_ELEMENT_NODE;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define inrange(ch,min,max) ((ch)>=(min) && (ch)<=(max))
|
|
|
|
|
#define char2num(ch) \
|
|
|
|
|
inrange(ch,'0','9') \
|
|
|
|
|
? ((ch)-0x30) \
|
|
|
|
|
: (inrange(ch,'a','f') \
|
|
|
|
|
? ((ch)-0x57) : ((ch)-0x37))
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
const unichar *ptr;
|
|
|
|
|
unsigned end;
|
|
|
|
|
unsigned pos;
|
|
|
|
|
unsigned lin;
|
|
|
|
|
NSString *err;
|
|
|
|
|
} pldata;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Property list parsing - skip whitespace keeping count of lines and
|
|
|
|
|
* regarding objective-c style comments as whitespace.
|
|
|
|
|
* Returns YES if there is any non-whitespace text remaining.
|
|
|
|
|
*/
|
|
|
|
|
static BOOL skipSpace(pldata *pld)
|
|
|
|
|
{
|
|
|
|
|
unichar c;
|
|
|
|
|
|
|
|
|
|
while (pld->pos < pld->end)
|
|
|
|
|
{
|
|
|
|
|
c = pld->ptr[pld->pos];
|
|
|
|
|
|
2001-04-26 23:54:01 +00:00
|
|
|
|
if (GS_IS_WHITESPACE(c) == NO)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
|
|
|
|
if (c == '/' && pld->pos < pld->end - 1)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Check for comments beginning '/' followed by '/' or '*'
|
|
|
|
|
*/
|
|
|
|
|
if (pld->ptr[pld->pos + 1] == '/')
|
|
|
|
|
{
|
|
|
|
|
pld->pos += 2;
|
|
|
|
|
while (pld->pos < pld->end)
|
|
|
|
|
{
|
|
|
|
|
c = pld->ptr[pld->pos];
|
|
|
|
|
if (c == '\n')
|
2002-04-05 17:44:44 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
pld->pos++;
|
|
|
|
|
}
|
|
|
|
|
if (pld->pos >= pld->end)
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"reached end of string in comment";
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (pld->ptr[pld->pos + 1] == '*')
|
|
|
|
|
{
|
|
|
|
|
pld->pos += 2;
|
|
|
|
|
while (pld->pos < pld->end)
|
|
|
|
|
{
|
|
|
|
|
c = pld->ptr[pld->pos];
|
|
|
|
|
if (c == '\n')
|
2002-04-05 17:44:44 +00:00
|
|
|
|
{
|
|
|
|
|
pld->lin++;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
else if (c == '*' && pld->pos < pld->end - 1
|
|
|
|
|
&& pld->ptr[pld->pos+1] == '/')
|
|
|
|
|
{
|
|
|
|
|
pld->pos++; /* Skip past '*' */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pld->pos++;
|
|
|
|
|
}
|
|
|
|
|
if (pld->pos >= pld->end)
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"reached end of string in comment";
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2002-04-05 17:44:44 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2002-04-05 17:44:44 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
if (c == '\n')
|
2002-04-05 17:44:44 +00:00
|
|
|
|
{
|
|
|
|
|
pld->lin++;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
pld->pos++;
|
|
|
|
|
}
|
|
|
|
|
pld->err = @"reached end of string";
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline id parseQuotedString(pldata* pld)
|
|
|
|
|
{
|
|
|
|
|
unsigned start = ++pld->pos;
|
|
|
|
|
unsigned escaped = 0;
|
|
|
|
|
unsigned shrink = 0;
|
|
|
|
|
BOOL hex = NO;
|
|
|
|
|
NSString *obj;
|
|
|
|
|
|
|
|
|
|
while (pld->pos < pld->end)
|
|
|
|
|
{
|
|
|
|
|
unichar c = pld->ptr[pld->pos];
|
|
|
|
|
|
|
|
|
|
if (escaped)
|
|
|
|
|
{
|
2001-04-30 05:38:03 +00:00
|
|
|
|
if (escaped == 1 && c >= '0' && c <= '7')
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
|
|
|
|
escaped = 2;
|
|
|
|
|
hex = NO;
|
|
|
|
|
}
|
2002-04-06 06:39:59 +00:00
|
|
|
|
else if (escaped == 1 && (c == 'u' || c == 'U'))
|
2001-04-30 05:38:03 +00:00
|
|
|
|
{
|
|
|
|
|
escaped = 2;
|
|
|
|
|
hex = YES;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
else if (escaped > 1)
|
|
|
|
|
{
|
2001-04-30 05:38:03 +00:00
|
|
|
|
if (hex && GS_IS_HEXDIGIT(c))
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
|
|
|
|
shrink++;
|
|
|
|
|
escaped++;
|
2001-06-09 13:31:55 +00:00
|
|
|
|
if (escaped == 6)
|
2001-04-30 05:38:03 +00:00
|
|
|
|
{
|
|
|
|
|
escaped = 0;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
else if (c >= '0' && c <= '7')
|
|
|
|
|
{
|
|
|
|
|
shrink++;
|
2001-06-09 13:31:55 +00:00
|
|
|
|
escaped++;
|
2001-04-30 05:38:03 +00:00
|
|
|
|
if (escaped == 4)
|
|
|
|
|
{
|
|
|
|
|
escaped = 0;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pld->pos--;
|
|
|
|
|
escaped = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
escaped = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (c == '\\')
|
|
|
|
|
{
|
|
|
|
|
escaped = 1;
|
|
|
|
|
shrink++;
|
|
|
|
|
}
|
|
|
|
|
else if (c == '"')
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (c == '\n')
|
|
|
|
|
pld->lin++;
|
|
|
|
|
pld->pos++;
|
|
|
|
|
}
|
|
|
|
|
if (pld->pos >= pld->end)
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"reached end of string while parsing quoted string";
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (pld->pos - start - shrink == 0)
|
|
|
|
|
{
|
|
|
|
|
obj = @"";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unichar chars[pld->pos - start - shrink];
|
|
|
|
|
unsigned j;
|
|
|
|
|
unsigned k;
|
|
|
|
|
|
|
|
|
|
escaped = 0;
|
|
|
|
|
hex = NO;
|
|
|
|
|
for (j = start, k = 0; j < pld->pos; j++)
|
|
|
|
|
{
|
|
|
|
|
unichar c = pld->ptr[j];
|
|
|
|
|
|
|
|
|
|
if (escaped)
|
|
|
|
|
{
|
2001-04-30 05:38:03 +00:00
|
|
|
|
if (escaped == 1 && c >= '0' && c <= '7')
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
|
|
|
|
chars[k] = 0;
|
|
|
|
|
hex = NO;
|
|
|
|
|
escaped++;
|
|
|
|
|
}
|
2002-04-06 06:39:59 +00:00
|
|
|
|
else if (escaped == 1 && (c == 'u' || c == 'U'))
|
2001-04-30 05:38:03 +00:00
|
|
|
|
{
|
|
|
|
|
chars[k] = 0;
|
|
|
|
|
hex = YES;
|
|
|
|
|
escaped++;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
else if (escaped > 1)
|
|
|
|
|
{
|
2001-04-30 05:38:03 +00:00
|
|
|
|
if (hex && GS_IS_HEXDIGIT(c))
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
|
|
|
|
chars[k] <<= 4;
|
|
|
|
|
chars[k] |= char2num(c);
|
|
|
|
|
escaped++;
|
2001-06-09 13:31:55 +00:00
|
|
|
|
if (escaped == 6)
|
2001-04-30 05:38:03 +00:00
|
|
|
|
{
|
|
|
|
|
escaped = 0;
|
2001-06-09 08:52:53 +00:00
|
|
|
|
k++;
|
2001-04-30 05:38:03 +00:00
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
else if (c >= '0' && c <= '7')
|
|
|
|
|
{
|
|
|
|
|
chars[k] <<= 3;
|
|
|
|
|
chars[k] |= (c - '0');
|
|
|
|
|
escaped++;
|
2001-04-30 05:38:03 +00:00
|
|
|
|
if (escaped == 4)
|
|
|
|
|
{
|
|
|
|
|
escaped = 0;
|
2001-06-09 08:52:53 +00:00
|
|
|
|
k++;
|
2001-04-30 05:38:03 +00:00
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
escaped = 0;
|
|
|
|
|
j--;
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
escaped = 0;
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case 'a' : chars[k] = '\a'; break;
|
|
|
|
|
case 'b' : chars[k] = '\b'; break;
|
|
|
|
|
case 't' : chars[k] = '\t'; break;
|
|
|
|
|
case 'r' : chars[k] = '\r'; break;
|
|
|
|
|
case 'n' : chars[k] = '\n'; break;
|
|
|
|
|
case 'v' : chars[k] = '\v'; break;
|
|
|
|
|
case 'f' : chars[k] = '\f'; break;
|
|
|
|
|
default : chars[k] = c; break;
|
|
|
|
|
}
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
chars[k] = c;
|
|
|
|
|
if (c == '\\')
|
|
|
|
|
{
|
|
|
|
|
escaped = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
obj = (*plAlloc)(NSStringClass, @selector(allocWithZone:),
|
|
|
|
|
NSDefaultMallocZone());
|
|
|
|
|
obj = (*plInit)(obj, plSel, (void*)chars, pld->pos - start - shrink);
|
|
|
|
|
}
|
|
|
|
|
pld->pos++;
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline id parseUnquotedString(pldata *pld)
|
|
|
|
|
{
|
|
|
|
|
unsigned start = pld->pos;
|
|
|
|
|
id obj;
|
|
|
|
|
|
|
|
|
|
while (pld->pos < pld->end)
|
|
|
|
|
{
|
2001-04-26 23:54:01 +00:00
|
|
|
|
if (GS_IS_QUOTABLE(pld->ptr[pld->pos]) == YES)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
break;
|
|
|
|
|
pld->pos++;
|
|
|
|
|
}
|
|
|
|
|
obj = (*plAlloc)(NSStringClass, @selector(allocWithZone:),
|
|
|
|
|
NSDefaultMallocZone());
|
|
|
|
|
obj = (*plInit)(obj, plSel, (void*)&pld->ptr[start], pld->pos-start);
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static id parsePlItem(pldata* pld)
|
|
|
|
|
{
|
2002-04-05 17:44:44 +00:00
|
|
|
|
id result = nil;
|
|
|
|
|
BOOL start = (pld->pos == 1 ? YES : NO);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
2002-04-05 17:44:44 +00:00
|
|
|
|
if (skipSpace(pld) == NO)
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
switch (pld->ptr[pld->pos])
|
|
|
|
|
{
|
|
|
|
|
case '{':
|
|
|
|
|
{
|
|
|
|
|
NSMutableDictionary *dict;
|
|
|
|
|
|
|
|
|
|
dict = [[plDictionary allocWithZone: NSDefaultMallocZone()]
|
|
|
|
|
initWithCapacity: 0];
|
|
|
|
|
pld->pos++;
|
|
|
|
|
while (skipSpace(pld) == YES && pld->ptr[pld->pos] != '}')
|
|
|
|
|
{
|
|
|
|
|
id key;
|
|
|
|
|
id val;
|
|
|
|
|
|
|
|
|
|
key = parsePlItem(pld);
|
|
|
|
|
if (key == nil)
|
2002-04-05 17:44:44 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
if (skipSpace(pld) == NO)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(key);
|
2001-02-21 23:10:16 +00:00
|
|
|
|
RELEASE(dict);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (pld->ptr[pld->pos] != '=')
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unexpected character (wanted '=')";
|
|
|
|
|
RELEASE(key);
|
2001-02-21 23:10:16 +00:00
|
|
|
|
RELEASE(dict);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
pld->pos++;
|
|
|
|
|
val = parsePlItem(pld);
|
|
|
|
|
if (val == nil)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(key);
|
2001-02-21 23:10:16 +00:00
|
|
|
|
RELEASE(dict);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (skipSpace(pld) == NO)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(key);
|
|
|
|
|
RELEASE(val);
|
2001-02-21 23:10:16 +00:00
|
|
|
|
RELEASE(dict);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (pld->ptr[pld->pos] == ';')
|
|
|
|
|
{
|
|
|
|
|
pld->pos++;
|
|
|
|
|
}
|
|
|
|
|
else if (pld->ptr[pld->pos] != '}')
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unexpected character (wanted ';' or '}')";
|
|
|
|
|
RELEASE(key);
|
|
|
|
|
RELEASE(val);
|
2001-02-21 23:10:16 +00:00
|
|
|
|
RELEASE(dict);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
(*plSet)(dict, @selector(setObject:forKey:), val, key);
|
|
|
|
|
RELEASE(key);
|
|
|
|
|
RELEASE(val);
|
|
|
|
|
}
|
|
|
|
|
if (pld->pos >= pld->end)
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unexpected end of string when parsing dictionary";
|
|
|
|
|
RELEASE(dict);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
pld->pos++;
|
2002-04-05 17:44:44 +00:00
|
|
|
|
result = dict;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
2002-04-05 17:44:44 +00:00
|
|
|
|
break;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray *array;
|
|
|
|
|
|
|
|
|
|
array = [[plArray allocWithZone: NSDefaultMallocZone()]
|
|
|
|
|
initWithCapacity: 0];
|
|
|
|
|
pld->pos++;
|
|
|
|
|
while (skipSpace(pld) == YES && pld->ptr[pld->pos] != ')')
|
|
|
|
|
{
|
|
|
|
|
id val;
|
|
|
|
|
|
|
|
|
|
val = parsePlItem(pld);
|
|
|
|
|
if (val == nil)
|
|
|
|
|
{
|
2001-02-21 23:10:16 +00:00
|
|
|
|
RELEASE(array);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (skipSpace(pld) == NO)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(val);
|
2001-02-21 23:10:16 +00:00
|
|
|
|
RELEASE(array);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (pld->ptr[pld->pos] == ',')
|
|
|
|
|
{
|
|
|
|
|
pld->pos++;
|
|
|
|
|
}
|
|
|
|
|
else if (pld->ptr[pld->pos] != ')')
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unexpected character (wanted ',' or ')')";
|
|
|
|
|
RELEASE(val);
|
2001-02-21 23:10:16 +00:00
|
|
|
|
RELEASE(array);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
(*plAdd)(array, @selector(addObject:), val);
|
|
|
|
|
RELEASE(val);
|
|
|
|
|
}
|
|
|
|
|
if (pld->pos >= pld->end)
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unexpected end of string when parsing array";
|
|
|
|
|
RELEASE(array);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
pld->pos++;
|
2002-04-05 17:44:44 +00:00
|
|
|
|
result = array;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
2002-04-05 17:44:44 +00:00
|
|
|
|
break;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
|
|
|
|
case '<':
|
2002-11-10 10:20:05 +00:00
|
|
|
|
pld->pos++;
|
|
|
|
|
if (pld->pos < pld->end && pld->ptr[pld->pos] == '*')
|
|
|
|
|
{
|
|
|
|
|
const unichar *ptr;
|
2003-01-03 20:14:47 +00:00
|
|
|
|
unsigned min;
|
|
|
|
|
unsigned len = 0;
|
|
|
|
|
unsigned i;
|
2002-11-10 10:20:05 +00:00
|
|
|
|
|
|
|
|
|
pld->pos++;
|
|
|
|
|
min = pld->pos;
|
|
|
|
|
ptr = &(pld->ptr[min]);
|
|
|
|
|
while (pld->pos < pld->end && pld->ptr[pld->pos] != '>')
|
|
|
|
|
{
|
|
|
|
|
pld->pos++;
|
|
|
|
|
}
|
|
|
|
|
len = pld->pos - min;
|
|
|
|
|
if (len > 1)
|
|
|
|
|
{
|
|
|
|
|
unichar type = *ptr++;
|
|
|
|
|
|
|
|
|
|
len--;
|
|
|
|
|
if (type == 'I')
|
|
|
|
|
{
|
|
|
|
|
char buf[len+1];
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) buf[i] = (char)ptr[i];
|
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
result = [[NSNumber alloc] initWithLong: atol(buf)];
|
|
|
|
|
}
|
|
|
|
|
else if (type == 'B')
|
|
|
|
|
{
|
|
|
|
|
if (ptr[0] == 'Y')
|
|
|
|
|
{
|
|
|
|
|
result = [[NSNumber alloc] initWithBool: YES];
|
|
|
|
|
}
|
|
|
|
|
else if (ptr[0] == 'N')
|
|
|
|
|
{
|
|
|
|
|
result = [[NSNumber alloc] initWithBool: NO];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"bad value for bool";
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (type == 'D')
|
|
|
|
|
{
|
|
|
|
|
NSString *str;
|
|
|
|
|
|
|
|
|
|
str = [[NSString alloc] initWithCharacters: ptr
|
|
|
|
|
length: len];
|
|
|
|
|
result = [[NSCalendarDate alloc] initWithString: str
|
|
|
|
|
calendarFormat: @"%Y-%m-%d %H:%M:%S %z"];
|
|
|
|
|
RELEASE(str);
|
|
|
|
|
}
|
|
|
|
|
else if (type == 'R')
|
|
|
|
|
{
|
2003-07-08 08:39:45 +00:00
|
|
|
|
unichar buf[len];
|
|
|
|
|
double d = 0.0;
|
2002-11-10 10:20:05 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) buf[i] = (char)ptr[i];
|
2003-07-08 08:39:45 +00:00
|
|
|
|
GSScanDouble(buf, len, &d);
|
|
|
|
|
result = [[NSNumber alloc] initWithDouble: d];
|
2002-11-10 10:20:05 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unrecognized type code after '<*'";
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"missing type code after '<*'";
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (pld->pos >= pld->end)
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unexpected end of string when parsing data";
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (pld->ptr[pld->pos] != '>')
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unexpected character in string";
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
pld->pos++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NSMutableData *data;
|
|
|
|
|
unsigned max = pld->end - 1;
|
|
|
|
|
unsigned char buf[BUFSIZ];
|
|
|
|
|
unsigned len = 0;
|
|
|
|
|
|
|
|
|
|
data = [[NSMutableData alloc] initWithCapacity: 0];
|
|
|
|
|
skipSpace(pld);
|
|
|
|
|
while (pld->pos < max
|
|
|
|
|
&& GS_IS_HEXDIGIT(pld->ptr[pld->pos])
|
|
|
|
|
&& GS_IS_HEXDIGIT(pld->ptr[pld->pos+1]))
|
|
|
|
|
{
|
|
|
|
|
unsigned char byte;
|
|
|
|
|
|
|
|
|
|
byte = (char2num(pld->ptr[pld->pos])) << 4;
|
|
|
|
|
pld->pos++;
|
|
|
|
|
byte |= char2num(pld->ptr[pld->pos]);
|
|
|
|
|
pld->pos++;
|
|
|
|
|
buf[len++] = byte;
|
|
|
|
|
if (len == sizeof(buf))
|
|
|
|
|
{
|
|
|
|
|
[data appendBytes: buf length: len];
|
|
|
|
|
len = 0;
|
|
|
|
|
}
|
|
|
|
|
skipSpace(pld);
|
|
|
|
|
}
|
|
|
|
|
if (pld->pos >= pld->end)
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unexpected end of string when parsing data";
|
|
|
|
|
RELEASE(data);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (pld->ptr[pld->pos] != '>')
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unexpected character in string";
|
|
|
|
|
RELEASE(data);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (len > 0)
|
|
|
|
|
{
|
|
|
|
|
[data appendBytes: buf length: len];
|
|
|
|
|
}
|
|
|
|
|
pld->pos++;
|
|
|
|
|
result = data;
|
|
|
|
|
}
|
2002-04-05 17:44:44 +00:00
|
|
|
|
break;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
|
|
|
|
case '"':
|
2002-04-05 17:44:44 +00:00
|
|
|
|
result = parseQuotedString(pld);
|
|
|
|
|
break;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
|
|
|
|
default:
|
2002-04-05 17:44:44 +00:00
|
|
|
|
result = parseUnquotedString(pld);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (start == YES && result != nil)
|
|
|
|
|
{
|
|
|
|
|
if (skipSpace(pld) == YES)
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"extra data after parsed string";
|
|
|
|
|
result = nil; // Not at end of string.
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
2002-04-05 17:44:44 +00:00
|
|
|
|
return result;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_LIBXML
|
2000-10-23 11:44:34 +00:00
|
|
|
|
static GSXMLNode*
|
|
|
|
|
elementNode(GSXMLNode* node)
|
|
|
|
|
{
|
|
|
|
|
while (node != nil)
|
|
|
|
|
{
|
|
|
|
|
if ([node type] == XML_ELEMENT_NODE)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
node = [node next];
|
|
|
|
|
}
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static id
|
|
|
|
|
nodeToObject(GSXMLNode* node)
|
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
CREATE_AUTORELEASE_POOL(arp);
|
|
|
|
|
id result = nil;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
|
|
|
|
node = elementNode(node);
|
2003-08-07 18:22:03 +00:00
|
|
|
|
if (node != nil)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
NSString *name;
|
|
|
|
|
NSString *content;
|
|
|
|
|
GSXMLNode *children;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
2003-08-07 18:22:03 +00:00
|
|
|
|
name = [node name];
|
|
|
|
|
children = [node firstChild];
|
|
|
|
|
content = [children content];
|
|
|
|
|
children = elementNode(children);
|
2001-07-16 19:43:51 +00:00
|
|
|
|
|
2003-08-07 18:22:03 +00:00
|
|
|
|
if ([name isEqualToString: @"string"]
|
|
|
|
|
|| [name isEqualToString: @"key"])
|
|
|
|
|
{
|
|
|
|
|
if (content == nil)
|
2001-07-16 19:43:51 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
content = @"";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NSRange r;
|
2001-08-10 06:39:29 +00:00
|
|
|
|
|
2003-08-07 18:22:03 +00:00
|
|
|
|
r = [content rangeOfString: @"\\"];
|
|
|
|
|
if (r.length == 1)
|
2001-07-16 19:43:51 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
unsigned len = [content length];
|
|
|
|
|
unichar buf[len];
|
|
|
|
|
unsigned pos = r.location;
|
|
|
|
|
|
|
|
|
|
[content getCharacters: buf];
|
|
|
|
|
while (pos < len)
|
2001-07-16 19:43:51 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
if (++pos < len)
|
2001-07-16 19:43:51 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
if ((buf[pos] == 'u' || buf[pos] == 'U')
|
|
|
|
|
&& (len >= pos + 4))
|
2001-07-16 19:43:51 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
unichar val = 0;
|
|
|
|
|
unsigned i;
|
|
|
|
|
BOOL ok = YES;
|
2001-08-10 06:39:29 +00:00
|
|
|
|
|
2003-08-07 18:22:03 +00:00
|
|
|
|
for (i = 1; i < 5; i++)
|
2001-08-10 06:39:29 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
unichar c = buf[pos + i];
|
|
|
|
|
|
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
|
{
|
|
|
|
|
val = (val << 4) + c - '0';
|
|
|
|
|
}
|
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
|
{
|
|
|
|
|
val = (val << 4) + c - 'A' + 10;
|
|
|
|
|
}
|
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
|
{
|
|
|
|
|
val = (val << 4) + c - 'a' + 10;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ok = NO;
|
|
|
|
|
}
|
2001-08-10 06:39:29 +00:00
|
|
|
|
}
|
2003-08-07 18:22:03 +00:00
|
|
|
|
if (ok == YES)
|
2001-08-10 06:39:29 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
len -= 5;
|
|
|
|
|
memcpy(&buf[pos], &buf[pos+5],
|
|
|
|
|
(len - pos) * sizeof(unichar));
|
|
|
|
|
buf[pos - 1] = val;
|
2001-08-10 06:39:29 +00:00
|
|
|
|
}
|
2001-07-16 19:43:51 +00:00
|
|
|
|
}
|
2003-08-07 18:22:03 +00:00
|
|
|
|
while (pos < len && buf[pos] != '\\')
|
2001-07-16 19:43:51 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
pos++;
|
2001-07-16 19:43:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-08-07 18:22:03 +00:00
|
|
|
|
content = [NSString stringWithCharacters: buf length: len];
|
2001-07-16 19:43:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2003-08-07 18:22:03 +00:00
|
|
|
|
result = content;
|
2001-07-16 19:43:51 +00:00
|
|
|
|
}
|
2003-08-07 18:22:03 +00:00
|
|
|
|
else if ([name isEqualToString: @"true"])
|
2001-08-10 06:39:29 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
result = [NSNumber numberWithBool: YES];
|
2001-08-10 06:39:29 +00:00
|
|
|
|
}
|
2003-08-07 18:22:03 +00:00
|
|
|
|
else if ([name isEqualToString: @"false"])
|
2001-08-10 06:39:29 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
result = [NSNumber numberWithBool: NO];
|
2001-08-10 06:39:29 +00:00
|
|
|
|
}
|
2003-08-07 18:22:03 +00:00
|
|
|
|
else if ([name isEqualToString: @"integer"])
|
2001-08-10 06:39:29 +00:00
|
|
|
|
{
|
2003-08-07 18:22:03 +00:00
|
|
|
|
if (content == nil)
|
|
|
|
|
{
|
|
|
|
|
content = @"0";
|
|
|
|
|
}
|
|
|
|
|
result = [NSNumber numberWithInt: [content intValue]];
|
2001-08-10 06:39:29 +00:00
|
|
|
|
}
|
2003-08-07 18:22:03 +00:00
|
|
|
|
else if ([name isEqualToString: @"real"])
|
|
|
|
|
{
|
|
|
|
|
if (content == nil)
|
|
|
|
|
{
|
|
|
|
|
content = @"0.0";
|
|
|
|
|
}
|
|
|
|
|
result = [NSNumber numberWithDouble: [content doubleValue]];
|
|
|
|
|
}
|
|
|
|
|
else if ([name isEqualToString: @"date"])
|
|
|
|
|
{
|
|
|
|
|
if (content == nil)
|
|
|
|
|
{
|
|
|
|
|
content = @"";
|
|
|
|
|
}
|
|
|
|
|
result = [NSCalendarDate dateWithString: content
|
|
|
|
|
calendarFormat: @"%Y-%m-%d %H:%M:%S %z"];
|
|
|
|
|
}
|
|
|
|
|
else if ([name isEqualToString: @"data"])
|
|
|
|
|
{
|
|
|
|
|
result = [GSMimeDocument decodeBase64String: content];
|
|
|
|
|
}
|
|
|
|
|
// container class
|
|
|
|
|
else if ([name isEqualToString: @"array"])
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray *container = [NSMutableArray array];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
2003-08-07 18:22:03 +00:00
|
|
|
|
while (children != nil)
|
|
|
|
|
{
|
|
|
|
|
id val;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
2003-08-07 18:22:03 +00:00
|
|
|
|
val = nodeToObject(children);
|
|
|
|
|
[container addObject: val];
|
|
|
|
|
children = [children nextElement];
|
|
|
|
|
}
|
|
|
|
|
result = container;
|
|
|
|
|
}
|
|
|
|
|
else if ([name isEqualToString: @"dict"])
|
|
|
|
|
{
|
|
|
|
|
NSMutableDictionary *container = [NSMutableDictionary dictionary];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
2003-08-07 18:22:03 +00:00
|
|
|
|
while (children != nil)
|
|
|
|
|
{
|
|
|
|
|
NSString *key;
|
|
|
|
|
id val;
|
|
|
|
|
|
|
|
|
|
key = nodeToObject(children);
|
|
|
|
|
children = [children nextElement];
|
|
|
|
|
val = nodeToObject(children);
|
|
|
|
|
children = [children nextElement];
|
|
|
|
|
[container setObject: val forKey: key];
|
|
|
|
|
}
|
|
|
|
|
result = container;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
2003-08-07 18:22:03 +00:00
|
|
|
|
RETAIN(result);
|
|
|
|
|
RELEASE(arp);
|
|
|
|
|
return AUTORELEASE(result);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
setupPl()
|
|
|
|
|
{
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_LIBXML
|
2001-04-26 15:35:16 +00:00
|
|
|
|
/*
|
|
|
|
|
* Cache XML node information.
|
|
|
|
|
*/
|
|
|
|
|
XML_ELEMENT_NODE = [GSXMLNode typeFromDescription: @"XML_ELEMENT_NODE"];
|
|
|
|
|
#endif
|
2000-10-23 11:44:34 +00:00
|
|
|
|
plAlloc = (id (*)(id, SEL, NSZone*))
|
|
|
|
|
[NSStringClass methodForSelector: @selector(allocWithZone:)];
|
2002-03-13 13:46:12 +00:00
|
|
|
|
plInit = (id (*)(id, SEL, unichar*, unsigned int))
|
2000-10-23 11:44:34 +00:00
|
|
|
|
[NSStringClass instanceMethodForSelector: plSel];
|
|
|
|
|
|
2001-01-08 16:45:36 +00:00
|
|
|
|
plArray = [GSMutableArray class];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
plAdd = (id (*)(id, SEL, id))
|
|
|
|
|
[plArray instanceMethodForSelector: @selector(addObject:)];
|
|
|
|
|
|
2001-01-09 09:17:31 +00:00
|
|
|
|
plDictionary = [GSMutableDictionary class];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
plSet = (id (*)(id, SEL, id, id))
|
|
|
|
|
[plDictionary instanceMethodForSelector: @selector(setObject:forKey:)];
|
|
|
|
|
|
|
|
|
|
setupHexdigits();
|
|
|
|
|
setupQuotables();
|
2001-04-26 23:54:01 +00:00
|
|
|
|
setupWhitespace();
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static id
|
|
|
|
|
GSPropertyList(NSString *string)
|
|
|
|
|
{
|
|
|
|
|
pldata _pld;
|
|
|
|
|
pldata *pld = &_pld;
|
|
|
|
|
unsigned length = [string length];
|
|
|
|
|
NSData *d;
|
2001-02-19 05:03:09 +00:00
|
|
|
|
id pl;
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_LIBXML
|
2000-10-23 11:44:34 +00:00
|
|
|
|
unsigned index = 0;
|
2001-02-19 05:03:09 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* An empty string is a nil property list.
|
|
|
|
|
*/
|
|
|
|
|
if (length == 0)
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
2001-07-16 19:43:51 +00:00
|
|
|
|
if (plAlloc == 0)
|
|
|
|
|
{
|
|
|
|
|
setupPl();
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_LIBXML
|
2001-04-26 23:54:01 +00:00
|
|
|
|
if (whitespaceBitmapRep == NULL)
|
2001-02-19 05:03:09 +00:00
|
|
|
|
{
|
2001-04-26 23:54:01 +00:00
|
|
|
|
setupWhitespace();
|
2001-02-19 05:03:09 +00:00
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
while (index < length)
|
|
|
|
|
{
|
|
|
|
|
unsigned c = [string characterAtIndex: index];
|
|
|
|
|
|
2001-04-26 23:54:01 +00:00
|
|
|
|
if (GS_IS_WHITESPACE(c) == NO)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* A string beginning with a '<?' must be an XML file
|
|
|
|
|
*/
|
|
|
|
|
if (index + 1 < length && [string characterAtIndex: index] == '<'
|
|
|
|
|
&& [string characterAtIndex: index+1] == '?')
|
|
|
|
|
{
|
|
|
|
|
NSData *data;
|
|
|
|
|
GSXMLParser *parser;
|
|
|
|
|
|
2002-04-29 12:20:08 +00:00
|
|
|
|
data = [string dataUsingEncoding: NSUTF8StringEncoding];
|
2000-11-01 12:31:21 +00:00
|
|
|
|
parser = [GSXMLParser parser];
|
|
|
|
|
[parser substituteEntities: YES];
|
|
|
|
|
[parser doValidityChecking: YES];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
if ([parser parse: data] == NO || [parser parse: nil] == NO)
|
|
|
|
|
{
|
2002-04-05 17:44:44 +00:00
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"not a property list - failed to parse as XML"];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
2002-05-23 16:03:09 +00:00
|
|
|
|
if (![[[[parser document] root] name] isEqualToString: @"plist"])
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
2002-04-05 17:44:44 +00:00
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"not a property list - because name node is %@",
|
2002-05-23 16:03:09 +00:00
|
|
|
|
[[[parser document] root] name]];
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
2002-05-23 17:04:52 +00:00
|
|
|
|
pl = RETAIN(nodeToObject([[[parser document] root] firstChild]));
|
2002-05-23 16:03:09 +00:00
|
|
|
|
return AUTORELEASE(pl);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
d = [string dataUsingEncoding: NSUnicodeStringEncoding];
|
|
|
|
|
_pld.ptr = (unichar*)[d bytes];
|
|
|
|
|
_pld.pos = 1;
|
|
|
|
|
_pld.end = length + 1;
|
|
|
|
|
_pld.err = nil;
|
|
|
|
|
_pld.lin = 1;
|
2001-02-19 05:03:09 +00:00
|
|
|
|
pl = AUTORELEASE(parsePlItem(pld));
|
|
|
|
|
if (pl == nil && _pld.err != nil)
|
|
|
|
|
{
|
2002-04-05 17:44:44 +00:00
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"Parse failed at line %d (char %d) - %@",
|
|
|
|
|
_pld.lin, _pld.pos, _pld.err];
|
2001-02-19 05:03:09 +00:00
|
|
|
|
}
|
|
|
|
|
return pl;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static id
|
|
|
|
|
GSPropertyListFromStringsFormat(NSString *string)
|
|
|
|
|
{
|
|
|
|
|
NSMutableDictionary *dict;
|
|
|
|
|
pldata _pld;
|
|
|
|
|
pldata *pld = &_pld;
|
|
|
|
|
unsigned length = [string length];
|
|
|
|
|
NSData *d;
|
|
|
|
|
|
2001-02-19 05:03:09 +00:00
|
|
|
|
/*
|
|
|
|
|
* An empty string is a nil property list.
|
|
|
|
|
*/
|
|
|
|
|
if (length == 0)
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-23 11:44:34 +00:00
|
|
|
|
d = [string dataUsingEncoding: NSUnicodeStringEncoding];
|
|
|
|
|
_pld.ptr = (unichar*)[d bytes];
|
|
|
|
|
_pld.pos = 1;
|
|
|
|
|
_pld.end = length + 1;
|
|
|
|
|
_pld.err = nil;
|
|
|
|
|
_pld.lin = 1;
|
|
|
|
|
if (plAlloc == 0)
|
2001-02-19 05:03:09 +00:00
|
|
|
|
{
|
|
|
|
|
setupPl();
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
|
|
|
|
|
dict = [[plDictionary allocWithZone: NSDefaultMallocZone()]
|
|
|
|
|
initWithCapacity: 0];
|
|
|
|
|
while (skipSpace(pld) == YES)
|
|
|
|
|
{
|
|
|
|
|
id key;
|
|
|
|
|
id val;
|
|
|
|
|
|
|
|
|
|
if (pld->ptr[pld->pos] == '"')
|
2001-02-19 05:03:09 +00:00
|
|
|
|
{
|
|
|
|
|
key = parseQuotedString(pld);
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
else
|
2001-02-19 05:03:09 +00:00
|
|
|
|
{
|
|
|
|
|
key = parseUnquotedString(pld);
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
if (key == nil)
|
2001-02-19 05:03:09 +00:00
|
|
|
|
{
|
|
|
|
|
DESTROY(dict);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
if (skipSpace(pld) == NO)
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"incomplete final entry (no semicolon?)";
|
|
|
|
|
RELEASE(key);
|
2001-02-19 05:03:09 +00:00
|
|
|
|
DESTROY(dict);
|
|
|
|
|
break;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
if (pld->ptr[pld->pos] == ';')
|
|
|
|
|
{
|
|
|
|
|
pld->pos++;
|
|
|
|
|
(*plSet)(dict, @selector(setObject:forKey:), @"", key);
|
|
|
|
|
RELEASE(key);
|
|
|
|
|
}
|
|
|
|
|
else if (pld->ptr[pld->pos] == '=')
|
|
|
|
|
{
|
|
|
|
|
pld->pos++;
|
|
|
|
|
if (skipSpace(pld) == NO)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(key);
|
2001-02-19 05:03:09 +00:00
|
|
|
|
DESTROY(dict);
|
|
|
|
|
break;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
if (pld->ptr[pld->pos] == '"')
|
2001-02-19 05:03:09 +00:00
|
|
|
|
{
|
|
|
|
|
val = parseQuotedString(pld);
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
else
|
2001-02-19 05:03:09 +00:00
|
|
|
|
{
|
|
|
|
|
val = parseUnquotedString(pld);
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
if (val == nil)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(key);
|
2001-02-19 05:03:09 +00:00
|
|
|
|
DESTROY(dict);
|
|
|
|
|
break;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
if (skipSpace(pld) == NO)
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"missing final semicolon";
|
|
|
|
|
RELEASE(key);
|
|
|
|
|
RELEASE(val);
|
2001-02-19 05:03:09 +00:00
|
|
|
|
DESTROY(dict);
|
|
|
|
|
break;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
(*plSet)(dict, @selector(setObject:forKey:), val, key);
|
|
|
|
|
RELEASE(key);
|
|
|
|
|
RELEASE(val);
|
|
|
|
|
if (pld->ptr[pld->pos] == ';')
|
2001-02-19 05:03:09 +00:00
|
|
|
|
{
|
|
|
|
|
pld->pos++;
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unexpected character (wanted ';')";
|
2001-02-19 05:03:09 +00:00
|
|
|
|
DESTROY(dict);
|
|
|
|
|
break;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pld->err = @"unexpected character (wanted '=' or ';')";
|
2001-02-19 05:03:09 +00:00
|
|
|
|
RELEASE(key);
|
|
|
|
|
DESTROY(dict);
|
|
|
|
|
break;
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2001-02-19 05:03:09 +00:00
|
|
|
|
if (dict == nil && _pld.err != nil)
|
|
|
|
|
{
|
2002-04-05 17:44:44 +00:00
|
|
|
|
RELEASE(dict);
|
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"Parse failed at line %d (char %d) - %@",
|
|
|
|
|
_pld.lin, _pld.pos, _pld.err];
|
2001-02-19 05:03:09 +00:00
|
|
|
|
}
|
2000-12-21 13:39:11 +00:00
|
|
|
|
return AUTORELEASE(dict);
|
2000-10-23 11:44:34 +00:00
|
|
|
|
}
|
2002-08-30 12:30:50 +00:00
|
|
|
|
|