2001-12-17 14:31:42 +00:00
|
|
|
|
/** Implementation for GNUStep of NSString concrete subclasses
|
2000-10-09 05:32:50 +00:00
|
|
|
|
Copyright (C) 1997,1998,2000 Free Software Foundation, Inc.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
Base on code written by Stevo Crvenkovski <stevo@btinternet.com>
|
2000-10-09 05:32:50 +00:00
|
|
|
|
Date: February 1997
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
Based on NSGCString and NSString
|
|
|
|
|
Written by: Andrew Kachites McCallum
|
|
|
|
|
<mccallum@gnu.ai.mit.edu>
|
|
|
|
|
Date: March 1995
|
|
|
|
|
|
|
|
|
|
Optimised by Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
|
Date: October 1998
|
|
|
|
|
|
|
|
|
|
Redesign/rewrite by Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
|
Date: September 2000
|
|
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2000-10-09 05:32:50 +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.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
|
License along with this library; if not, write to the Free
|
2005-05-22 03:32:16 +00:00
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "config.h"
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/preface.h"
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "Foundation/NSString.h"
|
|
|
|
|
#include "Foundation/NSCoder.h"
|
|
|
|
|
#include "Foundation/NSArray.h"
|
|
|
|
|
#include "Foundation/NSData.h"
|
|
|
|
|
#include "Foundation/NSDictionary.h"
|
|
|
|
|
#include "Foundation/NSCharacterSet.h"
|
|
|
|
|
#include "Foundation/NSRange.h"
|
|
|
|
|
#include "Foundation/NSException.h"
|
|
|
|
|
#include "Foundation/NSValue.h"
|
|
|
|
|
#include "Foundation/NSDebug.h"
|
|
|
|
|
#include "Foundation/NSObjCRuntime.h"
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/GSObjCRuntime.h"
|
|
|
|
|
#include "GSFormat.h"
|
2000-11-15 05:00:23 +00:00
|
|
|
|
#include <limits.h>
|
2002-03-13 09:58:43 +00:00
|
|
|
|
|
|
|
|
|
#include "GSPrivate.h"
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
/* memcpy(), strlen(), strcmp() are gcc builtin's */
|
|
|
|
|
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/Unicode.h"
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-12 15:25:37 +00:00
|
|
|
|
#ifdef NeXT_RUNTIME
|
|
|
|
|
/* Used by the Darwin/NeXT ObjC Runtime
|
|
|
|
|
until Apple Radar 2870817 is fixed. */
|
2002-01-03 20:39:12 +00:00
|
|
|
|
struct objc_class _NSConstantStringClassReference;
|
2002-03-12 15:25:37 +00:00
|
|
|
|
#endif
|
2002-01-03 20:39:12 +00:00
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* GSPlaceholderString - placeholder class for objects awaiting intialisation.
|
|
|
|
|
*/
|
|
|
|
|
@interface GSPlaceholderString : NSString
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
2000-10-20 10:30:51 +00:00
|
|
|
|
/*
|
2003-11-05 02:11:49 +00:00
|
|
|
|
GSString is the root class of our hierarchy of string classes. All our
|
|
|
|
|
classes except GSPlaceholderString inherit from it. GSString provides
|
|
|
|
|
the common part of the ivar layout. It also has safe implementations
|
|
|
|
|
of all the memory management methods subclasses should override.
|
|
|
|
|
|
|
|
|
|
Concrete subclasses of GSString are identified by two properties:
|
|
|
|
|
how the string is encoded (its structure; 8-bit data or 16-bit unicode
|
|
|
|
|
data), and how the memory for the contents is handled (its memory
|
|
|
|
|
management).
|
|
|
|
|
|
|
|
|
|
Two direct subclasses of GSString provide the code for the two structures.
|
|
|
|
|
The memory management part of the concrete subclasses is abstracted to
|
|
|
|
|
a single flag for the structure classes: free. This is set only if the
|
|
|
|
|
_contents buffer is guaranteed to remain valid at least until the instance
|
|
|
|
|
has been deallocated.
|
|
|
|
|
|
|
|
|
|
(It really should be named 'ownsContents' or something similar, but it's
|
|
|
|
|
'free' in GSMutableString, and the structures need to be interchangeable.)
|
|
|
|
|
|
|
|
|
|
Many optimizations, such as retaining instead of copying, and using pointers
|
|
|
|
|
to another strings _contents buffer, are valid only if this flag is set.
|
|
|
|
|
|
|
|
|
|
GSCString, an abstract class that stores the string as 8-bit data in the
|
2004-06-05 10:06:56 +00:00
|
|
|
|
internal encoding.
|
2003-11-05 02:11:49 +00:00
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
@interface GSCString : GSString
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2000-10-20 10:30:51 +00:00
|
|
|
|
/*
|
2003-11-05 02:11:49 +00:00
|
|
|
|
And GSUnicodeString, an abstract class that stores the string as 16-bit
|
|
|
|
|
unicode characters.
|
|
|
|
|
*/
|
|
|
|
|
@interface GSUnicodeString : GSString
|
2000-10-29 14:52:33 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
2000-10-29 14:52:33 +00:00
|
|
|
|
/*
|
2003-11-05 02:11:49 +00:00
|
|
|
|
For each memory management scheme, there is a pair of concrete subclasses
|
|
|
|
|
of the two abstract structure classes. Each subclass has a single -init...
|
|
|
|
|
method which can be used to initialize that specific subclass.
|
|
|
|
|
|
|
|
|
|
GS*BufferString, concrete subclasses that store the data in an external
|
|
|
|
|
(wrt. the instance itself) buffer. The buffer may or may not be owned
|
|
|
|
|
by the instance; the 'free' flag indicates which. If it is set,
|
|
|
|
|
we need to free the buffer when we are deallocated.
|
|
|
|
|
*/
|
|
|
|
|
@interface GSCBufferString : GSCString
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
- (id) initWithCStringNoCopy: (char*)chars
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag;
|
2000-10-20 10:30:51 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
@interface GSUnicodeBufferString : GSUnicodeString
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar *)chars
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
2000-10-29 14:52:33 +00:00
|
|
|
|
/*
|
2003-11-05 02:11:49 +00:00
|
|
|
|
GS*InlineString, concrete subclasses that store the data immediately after
|
|
|
|
|
the instance iself.
|
|
|
|
|
*/
|
|
|
|
|
@interface GSCInlineString : GSCString
|
2000-11-03 10:11:56 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
- (id) initWithCString: (const char*)chars length: (unsigned)length;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface GSUnicodeInlineString : GSUnicodeString
|
2000-10-29 14:52:33 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
- (id) initWithCharacters: (const unichar*)chars length: (unsigned)length;
|
2000-10-29 14:52:33 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
2000-10-20 10:30:51 +00:00
|
|
|
|
/*
|
2003-11-05 02:11:49 +00:00
|
|
|
|
GS*SubString, concrete subclasses that use the data in another string
|
|
|
|
|
instance.
|
|
|
|
|
*/
|
|
|
|
|
@interface GSCSubString : GSCString
|
|
|
|
|
{
|
|
|
|
|
@public
|
|
|
|
|
GSCString *_parent;
|
|
|
|
|
}
|
|
|
|
|
- (id) initWithCString: (char *)chars length: (unsigned)length
|
|
|
|
|
fromParent: (GSCString *)parent;
|
|
|
|
|
@end
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
@interface GSUnicodeSubString : GSUnicodeString
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
|
|
|
|
@public
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSUnicodeString *_parent;
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
- (id) initWithCharacters: (unichar *)chars length: (unsigned)length
|
|
|
|
|
fromParent: (GSUnicodeString *)parent;
|
2000-10-20 10:30:51 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
/*
|
|
|
|
|
* Include sequence handling code with instructions to generate search
|
|
|
|
|
* and compare functions for NSString objects.
|
|
|
|
|
*/
|
|
|
|
|
#define GSEQ_STRCOMP strCompUsNs
|
|
|
|
|
#define GSEQ_STRRANGE strRangeUsNs
|
|
|
|
|
#define GSEQ_O GSEQ_NS
|
|
|
|
|
#define GSEQ_S GSEQ_US
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "GSeq.h"
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
#define GSEQ_STRCOMP strCompUsUs
|
|
|
|
|
#define GSEQ_STRRANGE strRangeUsUs
|
|
|
|
|
#define GSEQ_O GSEQ_US
|
|
|
|
|
#define GSEQ_S GSEQ_US
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "GSeq.h"
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
#define GSEQ_STRCOMP strCompUsCs
|
|
|
|
|
#define GSEQ_STRRANGE strRangeUsCs
|
|
|
|
|
#define GSEQ_O GSEQ_CS
|
|
|
|
|
#define GSEQ_S GSEQ_US
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "GSeq.h"
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
#define GSEQ_STRCOMP strCompCsNs
|
|
|
|
|
#define GSEQ_STRRANGE strRangeCsNs
|
|
|
|
|
#define GSEQ_O GSEQ_NS
|
|
|
|
|
#define GSEQ_S GSEQ_CS
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "GSeq.h"
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
#define GSEQ_STRCOMP strCompCsUs
|
|
|
|
|
#define GSEQ_STRRANGE strRangeCsUs
|
|
|
|
|
#define GSEQ_O GSEQ_US
|
|
|
|
|
#define GSEQ_S GSEQ_CS
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "GSeq.h"
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
#define GSEQ_STRCOMP strCompCsCs
|
|
|
|
|
#define GSEQ_STRRANGE strRangeCsCs
|
|
|
|
|
#define GSEQ_O GSEQ_CS
|
|
|
|
|
#define GSEQ_S GSEQ_CS
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "GSeq.h"
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
static Class NSDataClass = 0;
|
|
|
|
|
static Class NSStringClass = 0;
|
2000-10-20 10:30:51 +00:00
|
|
|
|
static Class GSStringClass = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
static Class GSCStringClass = 0;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
static Class GSCBufferStringClass = 0;
|
2000-10-29 14:52:33 +00:00
|
|
|
|
static Class GSCInlineStringClass = 0;
|
2000-10-20 10:30:51 +00:00
|
|
|
|
static Class GSCSubStringClass = 0;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
static Class GSUnicodeStringClass = 0;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
static Class GSUnicodeBufferStringClass = 0;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
static Class GSUnicodeSubStringClass = 0;
|
|
|
|
|
static Class GSUnicodeInlineStringClass = 0;
|
|
|
|
|
static Class GSMutableStringClass = 0;
|
2001-06-06 15:18:28 +00:00
|
|
|
|
static Class NSConstantStringClass = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
static SEL cMemberSel;
|
2000-10-30 18:00:27 +00:00
|
|
|
|
static SEL convertSel;
|
|
|
|
|
static BOOL (*convertImp)(id, SEL, NSStringEncoding);
|
|
|
|
|
static SEL equalSel;
|
|
|
|
|
static BOOL (*equalImp)(id, SEL, id);
|
|
|
|
|
static SEL hashSel;
|
|
|
|
|
static unsigned (*hashImp)(id, SEL);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
static NSStringEncoding defEnc = 0;
|
2002-03-16 09:54:50 +00:00
|
|
|
|
static NSStringEncoding intEnc = NSISOLatin1StringEncoding;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2000-10-20 10:30:51 +00:00
|
|
|
|
/*
|
|
|
|
|
* The setup() function is called when any concrete string class is
|
2002-02-06 13:44:03 +00:00
|
|
|
|
* initialized, and caches classes and some method implementations.
|
2000-10-20 10:30:51 +00:00
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
static void
|
2002-11-09 06:45:31 +00:00
|
|
|
|
setup(void)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
static BOOL beenHere = NO;
|
|
|
|
|
|
|
|
|
|
if (beenHere == NO)
|
|
|
|
|
{
|
2002-11-09 06:45:31 +00:00
|
|
|
|
extern NSStringEncoding GetDefEncoding(void);
|
2001-04-12 09:29:16 +00:00
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
beenHere = YES;
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* Cache pointers to classes to work round misfeature in
|
|
|
|
|
* GNU compiler/runtime system where class lookup is very slow.
|
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
NSDataClass = [NSData class];
|
|
|
|
|
NSStringClass = [NSString class];
|
2000-10-20 10:30:51 +00:00
|
|
|
|
GSStringClass = [GSString class];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
GSCStringClass = [GSCString class];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSUnicodeStringClass = [GSUnicodeString class];
|
2003-11-05 02:11:49 +00:00
|
|
|
|
GSCBufferStringClass = [GSCBufferString class];
|
|
|
|
|
GSUnicodeBufferStringClass = [GSUnicodeBufferString class];
|
2000-10-29 14:52:33 +00:00
|
|
|
|
GSCInlineStringClass = [GSCInlineString class];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSUnicodeInlineStringClass = [GSUnicodeInlineString class];
|
2000-10-20 10:30:51 +00:00
|
|
|
|
GSCSubStringClass = [GSCSubString class];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSUnicodeSubStringClass = [GSUnicodeSubString class];
|
|
|
|
|
GSMutableStringClass = [GSMutableString class];
|
2002-10-02 13:29:54 +00:00
|
|
|
|
NSConstantStringClass = [NXConstantString class];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* Cache some selectors and method implementations for
|
|
|
|
|
* cases where we want to use the implementation
|
|
|
|
|
* provided in the abstract rolot cllass of the cluster.
|
|
|
|
|
*/
|
2002-02-06 13:44:03 +00:00
|
|
|
|
cMemberSel = @selector(characterIsMember:);
|
2000-10-30 18:00:27 +00:00
|
|
|
|
convertSel = @selector(canBeConvertedToEncoding:);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
convertImp = (BOOL (*)(id, SEL, NSStringEncoding))
|
|
|
|
|
[NSStringClass instanceMethodForSelector: convertSel];
|
2000-10-30 18:00:27 +00:00
|
|
|
|
equalSel = @selector(isEqualToString:);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
equalImp = (BOOL (*)(id, SEL, id))
|
|
|
|
|
[NSStringClass instanceMethodForSelector: equalSel];
|
2000-10-30 18:00:27 +00:00
|
|
|
|
hashSel = @selector(hash);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
hashImp = (unsigned (*)(id, SEL))
|
|
|
|
|
[NSStringClass instanceMethodForSelector: hashSel];
|
|
|
|
|
|
2001-05-23 14:32:56 +00:00
|
|
|
|
caiSel = @selector(characterAtIndex:);
|
|
|
|
|
gcrSel = @selector(getCharacters:range:);
|
|
|
|
|
ranSel = @selector(rangeOfComposedCharacterSequenceAtIndex:);
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
2002-03-16 09:54:50 +00:00
|
|
|
|
* Cache the default string encoding, and set the internal encoding
|
|
|
|
|
* used by 8-bit character strings to match if possible.
|
2000-11-03 10:11:56 +00:00
|
|
|
|
*/
|
2001-04-12 09:29:16 +00:00
|
|
|
|
defEnc = GetDefEncoding();
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (GSIsByteEncoding(defEnc) == YES)
|
|
|
|
|
{
|
|
|
|
|
intEnc = defEnc;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* The GSPlaceholderString class is used by the abstract cluster root
|
|
|
|
|
* class to provide temporary objects that will be replaced as soon
|
|
|
|
|
* as the objects are initialised. This object tries to replace
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* itself with an appropriate object whose type may vary depending
|
2000-11-03 10:11:56 +00:00
|
|
|
|
* on the initialisation method used.
|
|
|
|
|
*/
|
|
|
|
|
@implementation GSPlaceholderString
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
setup();
|
|
|
|
|
}
|
2001-01-08 16:45:36 +00:00
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
- (id) autorelease
|
|
|
|
|
{
|
2001-01-08 16:45:36 +00:00
|
|
|
|
NSWarnLog(@"-autorelease sent to uninitialised string");
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return self; // placeholders never get released.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unichar) characterAtIndex: (unsigned)index
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"attempt to use uninitialised string"];
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2003-11-05 02:11:49 +00:00
|
|
|
|
return; // Placeholders never get deallocated.
|
2000-11-03 10:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-09-09 10:39:01 +00:00
|
|
|
|
/*
|
|
|
|
|
* Replace self with an empty inline unicode string.
|
|
|
|
|
*/
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
return [self initWithCharacters: 0 length: 0];
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
2003-11-05 02:11:49 +00:00
|
|
|
|
* Replace self with an inline unicode string.
|
2000-11-03 10:11:56 +00:00
|
|
|
|
*/
|
|
|
|
|
- (id) initWithCharacters: (const unichar*)chars
|
|
|
|
|
length: (unsigned)length
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr me;
|
2006-03-26 10:59:57 +00:00
|
|
|
|
BOOL isASCII;
|
|
|
|
|
BOOL isLatin1;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
2006-03-26 11:34:47 +00:00
|
|
|
|
if (GSUnicode(chars, length, &isASCII, &isLatin1) != length)
|
2006-03-26 10:59:57 +00:00
|
|
|
|
{
|
|
|
|
|
return nil; // Invalid data
|
|
|
|
|
}
|
|
|
|
|
if (isASCII == YES
|
|
|
|
|
|| (intEnc == NSISOLatin1StringEncoding && isLatin1 == YES))
|
|
|
|
|
{
|
|
|
|
|
me = (GSStr)NSAllocateObject(GSCInlineStringClass, length,
|
|
|
|
|
GSObjCZone(self));
|
|
|
|
|
me->_contents.c = (unsigned char*)&((GSCInlineString*)me)[1];
|
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 0;
|
|
|
|
|
me->_flags.free = 1;
|
|
|
|
|
while (length-- > 0)
|
|
|
|
|
{
|
|
|
|
|
me->_contents.c[length] = (unsigned char)chars[length];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
me = (GSStr)NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
length*sizeof(unichar), GSObjCZone(self));
|
|
|
|
|
me->_contents.u = (unichar*)&((GSUnicodeInlineString*)me)[1];
|
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 1;
|
|
|
|
|
me->_flags.free = 1;
|
|
|
|
|
memcpy(me->_contents.u, chars, length*sizeof(unichar));
|
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2003-11-05 02:11:49 +00:00
|
|
|
|
* Replace self with a simple unicode string.
|
2000-11-03 10:11:56 +00:00
|
|
|
|
*/
|
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
|
|
|
|
length: (unsigned)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr me;
|
2006-03-26 10:59:57 +00:00
|
|
|
|
BOOL isASCII;
|
|
|
|
|
BOOL isLatin1;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
2006-03-26 11:34:47 +00:00
|
|
|
|
if (GSUnicode(chars, length, &isASCII, &isLatin1) != length)
|
2006-03-26 10:59:57 +00:00
|
|
|
|
{
|
2006-03-28 06:05:04 +00:00
|
|
|
|
if (flag == YES && chars != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
|
|
|
|
}
|
2006-03-26 10:59:57 +00:00
|
|
|
|
return nil; // Invalid data
|
|
|
|
|
}
|
|
|
|
|
if (isASCII == YES
|
|
|
|
|
|| (intEnc == NSISOLatin1StringEncoding && isLatin1 == YES))
|
2002-04-07 16:54:35 +00:00
|
|
|
|
{
|
2006-03-26 10:59:57 +00:00
|
|
|
|
/*
|
|
|
|
|
* OK ... we can do a more compact version
|
|
|
|
|
*/
|
|
|
|
|
me = (GSStr)NSAllocateObject(GSCInlineStringClass, length,
|
|
|
|
|
GSObjCZone(self));
|
|
|
|
|
me->_contents.c = (unsigned char*)&((GSCInlineString*)me)[1];
|
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 0;
|
2002-04-07 16:54:35 +00:00
|
|
|
|
me->_flags.free = 1;
|
2006-03-26 10:59:57 +00:00
|
|
|
|
while (length-- > 0)
|
|
|
|
|
{
|
|
|
|
|
me->_contents.c[length] = (unsigned char)chars[length];
|
|
|
|
|
}
|
2006-03-28 06:05:04 +00:00
|
|
|
|
if (flag == YES && chars != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
|
|
|
|
}
|
2006-03-26 10:59:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
me = (GSStr)NSAllocateObject(GSUnicodeBufferStringClass,
|
|
|
|
|
0, GSObjCZone(self));
|
|
|
|
|
me->_contents.u = chars;
|
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 1;
|
|
|
|
|
if (flag == YES)
|
|
|
|
|
{
|
|
|
|
|
me->_flags.free = 1;
|
|
|
|
|
}
|
2002-04-07 16:54:35 +00:00
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2003-11-05 02:11:49 +00:00
|
|
|
|
* Replace self with an inline 'C' string.
|
2000-11-03 10:11:56 +00:00
|
|
|
|
*/
|
|
|
|
|
- (id) initWithCString: (const char*)chars
|
|
|
|
|
length: (unsigned)length
|
|
|
|
|
{
|
2002-04-07 16:54:35 +00:00
|
|
|
|
if (defEnc == intEnc)
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr me;
|
2002-04-07 16:54:35 +00:00
|
|
|
|
|
2004-05-14 10:52:30 +00:00
|
|
|
|
me = (GSStr)NSAllocateObject(GSCInlineStringClass, length,
|
2002-04-07 16:54:35 +00:00
|
|
|
|
GSObjCZone(self));
|
2002-05-10 17:05:10 +00:00
|
|
|
|
me->_contents.c = (unsigned char*)&((GSCInlineString*)me)[1];
|
2002-04-07 16:54:35 +00:00
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 0;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
me->_flags.free = 1;
|
2002-04-07 16:54:35 +00:00
|
|
|
|
memcpy(me->_contents.c, chars, length);
|
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned l = 0;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
2005-07-08 11:48:37 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, (const unsigned char*)chars, length, defEnc,
|
|
|
|
|
GSObjCZone(self), 0) == NO)
|
2002-04-07 16:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
return [self initWithCharactersNoCopy: u length: l freeWhenDone: YES];
|
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Replace self with a simple 'C' string
|
|
|
|
|
*/
|
|
|
|
|
- (id) initWithCStringNoCopy: (char*)chars
|
|
|
|
|
length: (unsigned)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
2002-04-07 16:54:35 +00:00
|
|
|
|
if (defEnc == intEnc)
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr me;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
2004-05-14 10:52:30 +00:00
|
|
|
|
me = (GSStr)NSAllocateObject(GSCBufferStringClass, 0, GSObjCZone(self));
|
2002-05-10 17:05:10 +00:00
|
|
|
|
me->_contents.c = (unsigned char*)chars;
|
2002-04-07 16:54:35 +00:00
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 0;
|
|
|
|
|
if (flag == YES)
|
|
|
|
|
{
|
|
|
|
|
me->_flags.free = 1;
|
|
|
|
|
}
|
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned l = 0;
|
|
|
|
|
|
2005-07-08 11:48:37 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, (const unsigned char*)chars, length, defEnc,
|
|
|
|
|
GSObjCZone(self), 0) == NO)
|
2002-04-07 16:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
self = nil;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithCharactersNoCopy: u length: l freeWhenDone: YES];
|
|
|
|
|
}
|
2003-05-07 13:15:15 +00:00
|
|
|
|
if (flag == YES && chars != 0)
|
2002-04-07 16:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-05 10:06:56 +00:00
|
|
|
|
- (id) initWithFormat: (NSString*)format
|
|
|
|
|
locale: (NSDictionary*)locale
|
|
|
|
|
arguments: (va_list)argList
|
|
|
|
|
{
|
|
|
|
|
unsigned char buf[2048];
|
|
|
|
|
GSStr_t f;
|
|
|
|
|
unichar fbuf[1024];
|
|
|
|
|
unichar *fmt = fbuf;
|
|
|
|
|
size_t len;
|
|
|
|
|
GSStr me;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* First we provide an array of unichar characters containing the
|
|
|
|
|
* format string. For performance reasons we try to use an on-stack
|
|
|
|
|
* buffer if the format string is small enough ... it almost always
|
|
|
|
|
* will be.
|
|
|
|
|
*/
|
|
|
|
|
len = [format length];
|
|
|
|
|
if (len >= 1024)
|
|
|
|
|
{
|
|
|
|
|
fmt = objc_malloc((len+1)*sizeof(unichar));
|
|
|
|
|
}
|
|
|
|
|
[format getCharacters: fmt];
|
|
|
|
|
fmt[len] = '\0';
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Now set up 'f' as a GSMutableString object whose initial buffer is
|
|
|
|
|
* allocated on the stack. The GSFormat function can write into it.
|
|
|
|
|
*/
|
|
|
|
|
f.isa = GSMutableStringClass;
|
|
|
|
|
f._zone = NSDefaultMallocZone();
|
|
|
|
|
f._contents.c = buf;
|
|
|
|
|
f._capacity = sizeof(buf);
|
|
|
|
|
f._count = 0;
|
|
|
|
|
f._flags.wide = 0;
|
|
|
|
|
f._flags.free = 0;
|
|
|
|
|
GSFormat(&f, fmt, argList, locale);
|
|
|
|
|
if (fmt != fbuf)
|
|
|
|
|
{
|
|
|
|
|
objc_free(fmt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Don't use noCopy because f._contents.u may be memory on the stack,
|
|
|
|
|
* and even if it wasn't f._capacity may be greater than f._count so
|
|
|
|
|
* we could be wasting quite a bit of space. Better to accept a
|
|
|
|
|
* performance hit due to copying data (and allocating/deallocating
|
|
|
|
|
* the temporary buffer) for large strings. For most strings, the
|
|
|
|
|
* on-stack memory will have been used, so we will get better performance.
|
|
|
|
|
*/
|
|
|
|
|
if (f._flags.wide == 1)
|
|
|
|
|
{
|
|
|
|
|
me = (GSStr)NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
f._count*sizeof(unichar), GSObjCZone(self));
|
|
|
|
|
me->_contents.u = (unichar*)&((GSUnicodeInlineString*)me)[1];
|
|
|
|
|
me->_count = f._count;
|
|
|
|
|
me->_flags.wide = 1;
|
|
|
|
|
me->_flags.free = 1;
|
|
|
|
|
memcpy(me->_contents.u, f._contents.u, f._count*sizeof(unichar));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
me = (GSStr)NSAllocateObject(GSCInlineStringClass, f._count,
|
|
|
|
|
GSObjCZone(self));
|
|
|
|
|
me->_contents.c = (unsigned char*)&((GSCInlineString*)me)[1];
|
|
|
|
|
me->_count = f._count;
|
|
|
|
|
me->_flags.wide = 0;
|
|
|
|
|
me->_flags.free = 1;
|
|
|
|
|
memcpy(me->_contents.c, f._contents.c, f._count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the string had to grow beyond the initial buffer size, we must
|
|
|
|
|
* release any allocated memory.
|
|
|
|
|
*/
|
|
|
|
|
if (f._flags.free == 1)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(f._zone, f._contents.c);
|
|
|
|
|
}
|
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* Replace self with an inline string matching the sort of information
|
|
|
|
|
* given.
|
|
|
|
|
*/
|
|
|
|
|
- (id) initWithString: (NSString*)string
|
|
|
|
|
{
|
|
|
|
|
unsigned length;
|
|
|
|
|
Class c;
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr me;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
|
|
|
|
if (string == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"-initWithString: given nil string"];
|
|
|
|
|
c = GSObjCClass(string);
|
|
|
|
|
if (GSObjCIsKindOf(c, NSStringClass) == NO)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"-initWithString: given non-string object"];
|
|
|
|
|
|
|
|
|
|
length = [string length];
|
2001-06-06 15:18:28 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSCStringClass) == YES || c == NSConstantStringClass
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|| (GSObjCIsKindOf(c, GSMutableStringClass) == YES
|
2004-05-14 10:52:30 +00:00
|
|
|
|
&& ((GSStr)string)->_flags.wide == 0))
|
2000-11-03 10:11:56 +00:00
|
|
|
|
{
|
|
|
|
|
/*
|
2001-06-06 15:18:28 +00:00
|
|
|
|
* For a GSCString subclass, and ??ConstantString, or an 8-bit
|
2000-11-03 10:11:56 +00:00
|
|
|
|
* GSMutableString, we can copy the bytes directly into a GSCString.
|
|
|
|
|
*/
|
2004-05-14 10:52:30 +00:00
|
|
|
|
me = (GSStr)NSAllocateObject(GSCInlineStringClass,
|
2000-11-03 10:11:56 +00:00
|
|
|
|
length, GSObjCZone(self));
|
2002-05-10 17:05:10 +00:00
|
|
|
|
me->_contents.c = (unsigned char*)&((GSCInlineString*)me)[1];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 0;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
me->_flags.free = 1;
|
2004-05-14 10:52:30 +00:00
|
|
|
|
memcpy(me->_contents.c, ((GSStr)string)->_contents.c, length);
|
2000-11-03 10:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
else if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
|
|
|
|
|| GSObjCIsKindOf(c, GSMutableStringClass) == YES)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* For a GSUnicodeString subclass, or a 16-bit GSMutableString,
|
|
|
|
|
* we can copy the bytes directly into a GSUnicodeString.
|
|
|
|
|
*/
|
2004-05-14 10:52:30 +00:00
|
|
|
|
me = (GSStr)NSAllocateObject(GSUnicodeInlineStringClass,
|
2000-11-03 10:11:56 +00:00
|
|
|
|
length*sizeof(unichar), GSObjCZone(self));
|
|
|
|
|
me->_contents.u = (unichar*)&((GSUnicodeInlineString*)me)[1];
|
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 1;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
me->_flags.free = 1;
|
2004-05-14 10:52:30 +00:00
|
|
|
|
memcpy(me->_contents.u, ((GSStr)string)->_contents.u,
|
2000-11-03 10:11:56 +00:00
|
|
|
|
length*sizeof(unichar));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* For a string with an unknown class, we can initialise by
|
|
|
|
|
* having the string copy its content directly into our buffer.
|
|
|
|
|
*/
|
2004-05-14 10:52:30 +00:00
|
|
|
|
me = (GSStr)NSAllocateObject(GSUnicodeInlineStringClass,
|
2000-11-03 10:11:56 +00:00
|
|
|
|
length*sizeof(unichar), GSObjCZone(self));
|
|
|
|
|
me->_contents.u = (unichar*)&((GSUnicodeInlineString*)me)[1];
|
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 1;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
me->_flags.free = 1;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
[string getCharacters: me->_contents.u];
|
|
|
|
|
}
|
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) length
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"attempt to use uninitialised string"];
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) release
|
|
|
|
|
{
|
|
|
|
|
return; // placeholders never get released.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) retain
|
|
|
|
|
{
|
|
|
|
|
return self; // placeholders never get retained.
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-10-20 10:30:51 +00:00
|
|
|
|
/*
|
|
|
|
|
* The following inline functions are used by the concrete string classes
|
|
|
|
|
* to implement their core functionality.
|
|
|
|
|
* GSCString uses the functions with the _c suffix.
|
2003-11-05 02:11:49 +00:00
|
|
|
|
* GSCSubString, GSCInlineString, GSCBufferString, and ??ConstantString
|
|
|
|
|
* inherit methods from GSCString.
|
2000-11-03 10:11:56 +00:00
|
|
|
|
* GSUnicodeString uses the functions with the _u suffix.
|
2003-11-05 02:11:49 +00:00
|
|
|
|
* GSUnicodeSubString, GSUnicodeInlineString, and GSUnicodeBufferString
|
|
|
|
|
* inherit methods from GSUnicodeString.
|
2000-11-03 10:11:56 +00:00
|
|
|
|
* GSMutableString uses all the functions, selecting the _c or _u versions
|
2000-10-20 10:30:51 +00:00
|
|
|
|
* depending on whether its storage is 8-bit or 16-bit.
|
2000-11-03 10:11:56 +00:00
|
|
|
|
* In addition, GSMutableString uses a few functions without a suffix that are
|
2000-10-20 10:30:51 +00:00
|
|
|
|
* peculiar to its memory management (shrinking, growing, and converting).
|
|
|
|
|
*/
|
|
|
|
|
|
2002-10-05 17:47:54 +00:00
|
|
|
|
static inline char*
|
2004-05-14 10:52:30 +00:00
|
|
|
|
UTF8String_c(GSStr self)
|
2002-10-05 17:47:54 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned char *r;
|
|
|
|
|
|
|
|
|
|
if (self->_count == 0)
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2003-01-31 17:23:15 +00:00
|
|
|
|
if (intEnc == NSASCIIStringEncoding)
|
2002-10-05 17:47:54 +00:00
|
|
|
|
{
|
2003-01-31 17:23:15 +00:00
|
|
|
|
unsigned i = self->_count;
|
2002-10-05 17:47:54 +00:00
|
|
|
|
|
2003-03-02 07:47:18 +00:00
|
|
|
|
r = (unsigned char*)GSAutoreleasedBuffer(self->_count+1);
|
2003-01-31 17:23:15 +00:00
|
|
|
|
while (i-- > 0)
|
2002-10-05 17:47:54 +00:00
|
|
|
|
{
|
2003-01-31 17:23:15 +00:00
|
|
|
|
r[i] = self->_contents.c[i] & 0x7f;
|
2002-10-05 17:47:54 +00:00
|
|
|
|
}
|
|
|
|
|
r[self->_count] = '\0';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned l = 0;
|
|
|
|
|
unsigned s = 0;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We must convert from internal format to unicode and then to
|
|
|
|
|
* UTF8 string encoding.
|
|
|
|
|
*/
|
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count, intEnc,
|
|
|
|
|
NSDefaultMallocZone(), 0) == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to Unicode string."];
|
|
|
|
|
}
|
|
|
|
|
if (GSFromUnicode((unsigned char**)&r, &s, u, l, NSUTF8StringEncoding,
|
|
|
|
|
NSDefaultMallocZone(), GSUniTerminate|GSUniTemporary|GSUniStrict) == NO)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert from Unicode to UTF8."];
|
|
|
|
|
}
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2005-07-08 11:48:37 +00:00
|
|
|
|
return (char*)r;
|
2002-10-05 17:47:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline char*
|
2004-05-14 10:52:30 +00:00
|
|
|
|
UTF8String_u(GSStr self)
|
2002-10-05 17:47:54 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned c = self->_count;
|
|
|
|
|
|
|
|
|
|
if (c == 0)
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned int l = 0;
|
|
|
|
|
unsigned char *r = 0;
|
|
|
|
|
|
|
|
|
|
if (GSFromUnicode(&r, &l, self->_contents.u, c, NSUTF8StringEncoding,
|
|
|
|
|
NSDefaultMallocZone(), GSUniTerminate|GSUniTemporary|GSUniStrict) == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't get UTF8 from Unicode string."];
|
|
|
|
|
}
|
2005-07-08 11:48:37 +00:00
|
|
|
|
return (char*)r;
|
2002-10-05 17:47:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
static inline BOOL
|
2004-05-14 10:52:30 +00:00
|
|
|
|
boolValue_c(GSStr self)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (self->_count == 0)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned len = self->_count < 10 ? self->_count : 9;
|
|
|
|
|
|
|
|
|
|
if (len == 3
|
|
|
|
|
&& (self->_contents.c[0] == 'Y' || self->_contents.c[0] == 'y')
|
|
|
|
|
&& (self->_contents.c[1] == 'E' || self->_contents.c[1] == 'e')
|
|
|
|
|
&& (self->_contents.c[2] == 'S' || self->_contents.c[2] == 's'))
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2002-08-14 07:36:25 +00:00
|
|
|
|
else if (len == 4
|
|
|
|
|
&& (self->_contents.c[0] == 'T' || self->_contents.c[0] == 't')
|
|
|
|
|
&& (self->_contents.c[1] == 'R' || self->_contents.c[1] == 'r')
|
|
|
|
|
&& (self->_contents.c[2] == 'U' || self->_contents.c[2] == 'u')
|
|
|
|
|
&& (self->_contents.c[3] == 'E' || self->_contents.c[3] == 'e'))
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned char buf[len+1];
|
|
|
|
|
|
|
|
|
|
memcpy(buf, self->_contents.c, len);
|
|
|
|
|
buf[len] = '\0';
|
2005-07-08 11:48:37 +00:00
|
|
|
|
return atoi((const char*)buf);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline BOOL
|
2004-05-14 10:52:30 +00:00
|
|
|
|
boolValue_u(GSStr self)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (self->_count == 0)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned int l = self->_count < 10 ? self->_count : 9;
|
|
|
|
|
unsigned char buf[l+1];
|
|
|
|
|
unsigned char *b = buf;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
GSFromUnicode(&b, &l, self->_contents.u, l, intEnc, 0, GSUniTerminate);
|
|
|
|
|
if (l == 3
|
2000-10-09 05:32:50 +00:00
|
|
|
|
&& (buf[0] == 'Y' || buf[0] == 'y')
|
|
|
|
|
&& (buf[1] == 'E' || buf[1] == 'e')
|
|
|
|
|
&& (buf[2] == 'S' || buf[2] == 's'))
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2002-08-14 07:36:25 +00:00
|
|
|
|
else if (l == 4
|
|
|
|
|
&& (buf[0] == 'T' || buf[0] == 't')
|
|
|
|
|
&& (buf[1] == 'R' || buf[1] == 'r')
|
|
|
|
|
&& (buf[2] == 'U' || buf[2] == 'u')
|
|
|
|
|
&& (buf[3] == 'E' || buf[3] == 'e'))
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2005-07-08 11:48:37 +00:00
|
|
|
|
return atoi((const char*)buf);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline BOOL
|
2004-05-14 10:52:30 +00:00
|
|
|
|
canBeConvertedToEncoding_c(GSStr self, NSStringEncoding enc)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2006-01-19 13:28:02 +00:00
|
|
|
|
unsigned c = self->_count;
|
|
|
|
|
BOOL result = YES;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the length is zero, or we are already using the required encoding,
|
|
|
|
|
* or the required encoding is unicode (can hold any character) then we
|
|
|
|
|
* can assume that a conversion would succeed.
|
|
|
|
|
* We also know a conversion must succeed if the internal encoding is
|
|
|
|
|
* ascii and the required encoding has ascii as a subset.
|
|
|
|
|
*/
|
|
|
|
|
if (c > 0
|
|
|
|
|
&& enc != intEnc
|
|
|
|
|
&& enc != NSUTF8StringEncoding
|
|
|
|
|
&& enc != NSUnicodeStringEncoding
|
|
|
|
|
&& ((intEnc != NSASCIIStringEncoding)
|
|
|
|
|
|| ((enc != NSISOLatin1StringEncoding)
|
|
|
|
|
&& (enc != NSISOLatin2StringEncoding)
|
|
|
|
|
&& (enc != NSNEXTSTEPStringEncoding)
|
|
|
|
|
&& (enc != NSNonLossyASCIIStringEncoding))))
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
2006-01-19 13:28:02 +00:00
|
|
|
|
unsigned l = 0;
|
|
|
|
|
unichar *r = 0;
|
2000-10-20 10:30:51 +00:00
|
|
|
|
|
2006-01-19 13:28:02 +00:00
|
|
|
|
/*
|
|
|
|
|
* To check whether conversion is possible, we first convert to
|
|
|
|
|
* unicode and then check to see whether it is possible to convert
|
|
|
|
|
* to the desired encoding.
|
|
|
|
|
*/
|
|
|
|
|
result = GSToUnicode(&r, &l, self->_contents.c, self->_count, intEnc,
|
|
|
|
|
NSDefaultMallocZone(), GSUniStrict);
|
|
|
|
|
if (result == YES)
|
|
|
|
|
{
|
|
|
|
|
if (enc == NSISOLatin1StringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If all the unicode characters are in the 0 to 255 range
|
|
|
|
|
* they are all latin1.
|
|
|
|
|
*/
|
|
|
|
|
for (i = 0; i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
if (r[i] > 255)
|
|
|
|
|
{
|
|
|
|
|
result = NO;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (enc == NSASCIIStringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If all the unicode characters are in the 0 to 127 range
|
|
|
|
|
* they are all ascii.
|
|
|
|
|
*/
|
|
|
|
|
for (i = 0; i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
if (r[i] > 127)
|
|
|
|
|
{
|
|
|
|
|
result = NO;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned dummy = 0; // Hold returned length.
|
|
|
|
|
|
|
|
|
|
result = GSFromUnicode(0, &dummy, r, l, enc, 0, GSUniStrict);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Temporary unicode string no longer needed.
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), r);
|
|
|
|
|
}
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
2006-01-19 13:28:02 +00:00
|
|
|
|
return result;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline BOOL
|
2004-05-14 10:52:30 +00:00
|
|
|
|
canBeConvertedToEncoding_u(GSStr self, NSStringEncoding enc)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2006-01-19 13:28:02 +00:00
|
|
|
|
unsigned c = self->_count;
|
|
|
|
|
BOOL result = YES;
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|
2006-05-12 10:05:52 +00:00
|
|
|
|
if (c > 0)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
{
|
2006-05-12 10:05:52 +00:00
|
|
|
|
if (enc == NSUTF8StringEncoding || enc == NSUnicodeStringEncoding)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
{
|
2006-05-12 10:05:52 +00:00
|
|
|
|
if (GSUnicode(self->_contents.u, c, 0, 0) != c)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
{
|
2006-05-12 10:05:52 +00:00
|
|
|
|
return NO;
|
2004-05-14 10:52:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2006-05-12 10:05:52 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
{
|
2006-05-12 10:05:52 +00:00
|
|
|
|
if (enc == NSISOLatin1StringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
2006-01-19 13:28:02 +00:00
|
|
|
|
|
2006-05-12 10:05:52 +00:00
|
|
|
|
/*
|
|
|
|
|
* If all the unicode characters are in the 0 to 255 range
|
|
|
|
|
* they are all latin1.
|
|
|
|
|
*/
|
|
|
|
|
for (i = 0; i < self->_count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (self->_contents.u[i] > 255)
|
|
|
|
|
{
|
|
|
|
|
result = NO;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (enc == NSASCIIStringEncoding)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
{
|
2006-05-12 10:05:52 +00:00
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If all the unicode characters are in the 0 to 127 range
|
|
|
|
|
* they are all ascii.
|
|
|
|
|
*/
|
|
|
|
|
for (i = 0; i < self->_count; i++)
|
2006-01-19 13:28:02 +00:00
|
|
|
|
{
|
2006-05-12 10:05:52 +00:00
|
|
|
|
if (self->_contents.u[i] > 127)
|
|
|
|
|
{
|
|
|
|
|
result = NO;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-01-19 13:28:02 +00:00
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
}
|
2006-05-12 10:05:52 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned dummy = 0; // Hold returned length.
|
2006-01-19 13:28:02 +00:00
|
|
|
|
|
2006-05-12 10:05:52 +00:00
|
|
|
|
result = GSFromUnicode(0, &dummy, self->_contents.u, c, enc,
|
|
|
|
|
0, GSUniStrict);
|
|
|
|
|
}
|
2006-01-19 13:28:02 +00:00
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
}
|
2000-10-23 06:18:03 +00:00
|
|
|
|
return result;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline unichar
|
2004-05-14 10:52:30 +00:00
|
|
|
|
characterAtIndex_c(GSStr self, unsigned index)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
unichar c;
|
|
|
|
|
|
|
|
|
|
if (index >= self->_count)
|
|
|
|
|
[NSException raise: NSRangeException format: @"Invalid index."];
|
|
|
|
|
c = self->_contents.c[index];
|
|
|
|
|
if (c > 127)
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
c = encode_chartouni(c, intEnc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline unichar
|
2004-05-14 10:52:30 +00:00
|
|
|
|
characterAtIndex_u(GSStr self,unsigned index)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (index >= self->_count)
|
|
|
|
|
[NSException raise: NSRangeException format: @"Invalid index."];
|
|
|
|
|
return self->_contents.u[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline NSComparisonResult
|
2004-05-14 10:52:30 +00:00
|
|
|
|
compare_c(GSStr self, NSString *aString, unsigned mask, NSRange aRange)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
Class c;
|
|
|
|
|
|
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException format: @"compare with nil"];
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsInstance(aString) == NO)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
return strCompCsNs((id)self, aString, mask, aRange);
|
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
|
c = GSObjCClass(aString);
|
2000-11-03 10:11:56 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)aString)->_flags.wide == 1))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return strCompCsUs((id)self, aString, mask, aRange);
|
2000-10-31 16:17:33 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, GSCStringClass) == YES
|
2001-06-06 15:18:28 +00:00
|
|
|
|
|| c == NSConstantStringClass
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)aString)->_flags.wide == 0))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return strCompCsCs((id)self, aString, mask, aRange);
|
|
|
|
|
else
|
|
|
|
|
return strCompCsNs((id)self, aString, mask, aRange);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline NSComparisonResult
|
2004-05-14 10:52:30 +00:00
|
|
|
|
compare_u(GSStr self, NSString *aString, unsigned mask, NSRange aRange)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
Class c;
|
|
|
|
|
|
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException format: @"compare with nil"];
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsInstance(aString) == NO)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
return strCompUsNs((id)self, aString, mask, aRange);
|
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
|
c = GSObjCClass(aString);
|
2000-11-03 10:11:56 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSUnicodeStringClass)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)aString)->_flags.wide == 1))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return strCompUsUs((id)self, aString, mask, aRange);
|
2000-10-31 16:17:33 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, GSCStringClass)
|
2001-06-06 15:18:28 +00:00
|
|
|
|
|| c == NSConstantStringClass
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)aString)->_flags.wide == 0))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return strCompUsCs((id)self, aString, mask, aRange);
|
|
|
|
|
else
|
|
|
|
|
return strCompUsNs((id)self, aString, mask, aRange);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline char*
|
2005-05-08 07:08:28 +00:00
|
|
|
|
cString_c(GSStr self, NSStringEncoding enc)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2002-05-10 17:05:10 +00:00
|
|
|
|
unsigned char *r;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (self->_count == 0)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2005-05-08 15:07:59 +00:00
|
|
|
|
return "\0";
|
2002-03-16 09:54:50 +00:00
|
|
|
|
}
|
2005-06-04 05:48:40 +00:00
|
|
|
|
else if (enc == intEnc)
|
2002-03-16 09:54:50 +00:00
|
|
|
|
{
|
2003-03-02 07:47:18 +00:00
|
|
|
|
r = (unsigned char*)GSAutoreleasedBuffer(self->_count+1);
|
2002-03-16 09:54:50 +00:00
|
|
|
|
|
|
|
|
|
if (self->_count > 0)
|
|
|
|
|
{
|
|
|
|
|
memcpy(r, self->_contents.c, self->_count);
|
|
|
|
|
}
|
|
|
|
|
r[self->_count] = '\0';
|
|
|
|
|
}
|
2005-06-04 05:48:40 +00:00
|
|
|
|
else if (enc == NSUnicodeStringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unsigned l = 0;
|
|
|
|
|
|
|
|
|
|
/*
|
2006-02-27 09:35:19 +00:00
|
|
|
|
* The external C string encoding is unicode ... convert to it.
|
2005-06-04 05:48:40 +00:00
|
|
|
|
*/
|
2005-06-04 07:54:04 +00:00
|
|
|
|
if (GSToUnicode((unichar**)&r, &l, self->_contents.c, self->_count,
|
|
|
|
|
intEnc, NSDefaultMallocZone(),
|
|
|
|
|
GSUniTerminate|GSUniTemporary|GSUniStrict) == NO)
|
2005-06-04 05:48:40 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to Unicode string."];
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned l = 0;
|
|
|
|
|
unsigned s = 0;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The external C string encoding is not compatible with the internal
|
2002-03-18 09:10:05 +00:00
|
|
|
|
* 8-bit character strings ... we must convert from internal format to
|
|
|
|
|
* unicode and then to the external C string encoding.
|
2002-03-16 09:54:50 +00:00
|
|
|
|
*/
|
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count, intEnc,
|
|
|
|
|
NSDefaultMallocZone(), 0) == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
2005-06-04 05:48:40 +00:00
|
|
|
|
format: @"Can't convert to Unicode string."];
|
2002-03-16 09:54:50 +00:00
|
|
|
|
}
|
2005-05-08 07:08:28 +00:00
|
|
|
|
if (GSFromUnicode((unsigned char**)&r, &s, u, l, enc,
|
2002-03-16 09:54:50 +00:00
|
|
|
|
NSDefaultMallocZone(), GSUniTerminate|GSUniTemporary|GSUniStrict) == NO)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
2005-06-04 05:48:40 +00:00
|
|
|
|
format: @"Can't convert from Unicode string."];
|
2002-03-16 09:54:50 +00:00
|
|
|
|
}
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2003-08-20 12:13:34 +00:00
|
|
|
|
|
2005-07-08 11:48:37 +00:00
|
|
|
|
return (char*)r;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline char*
|
2005-05-08 07:08:28 +00:00
|
|
|
|
cString_u(GSStr self, NSStringEncoding enc)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned c = self->_count;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (c == 0)
|
|
|
|
|
{
|
2005-05-08 15:07:59 +00:00
|
|
|
|
return "\0";
|
|
|
|
|
}
|
|
|
|
|
else if (enc == NSUnicodeStringEncoding)
|
|
|
|
|
{
|
2006-05-12 10:05:52 +00:00
|
|
|
|
unichar *tmp;
|
|
|
|
|
unsigned l;
|
2005-05-08 15:07:59 +00:00
|
|
|
|
|
2006-05-12 10:05:52 +00:00
|
|
|
|
if ((l = GSUnicode(self->_contents.u, c, 0, 0)) != c)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"NSString is not legal UTF-16 at %u", l];
|
|
|
|
|
}
|
|
|
|
|
tmp = (unichar*)NSZoneMalloc(NSDefaultMallocZone(), (c + 1)*2);
|
2005-05-08 15:07:59 +00:00
|
|
|
|
memcpy(tmp, self->_contents.u, c*2);
|
|
|
|
|
tmp[c] = 0;
|
|
|
|
|
[NSData dataWithBytesNoCopy: tmp length: (c + 1)*2 freeWhenDone: YES];
|
|
|
|
|
return (char*)tmp;
|
2002-03-16 09:54:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned int l = 0;
|
|
|
|
|
unsigned char *r = 0;
|
|
|
|
|
|
2005-05-08 07:08:28 +00:00
|
|
|
|
if (GSFromUnicode(&r, &l, self->_contents.u, c, enc,
|
2002-03-16 09:54:50 +00:00
|
|
|
|
NSDefaultMallocZone(), GSUniTerminate|GSUniTemporary|GSUniStrict) == NO)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't get cString from Unicode string."];
|
|
|
|
|
}
|
2005-07-08 11:48:37 +00:00
|
|
|
|
return (char*)r;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline unsigned int
|
2005-05-08 07:08:28 +00:00
|
|
|
|
cStringLength_c(GSStr self, NSStringEncoding enc)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2005-05-08 07:08:28 +00:00
|
|
|
|
if (enc == intEnc)
|
2002-03-16 09:54:50 +00:00
|
|
|
|
{
|
|
|
|
|
return self->_count;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* The external C string encoding is not compatible with the internal
|
2002-03-18 09:10:05 +00:00
|
|
|
|
* 8-bit character strings ... we must convert from internal format to
|
|
|
|
|
* unicode and then to the external C string encoding.
|
2002-03-16 09:54:50 +00:00
|
|
|
|
*/
|
|
|
|
|
if (self->_count == 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned l = 0;
|
|
|
|
|
unsigned s = 0;
|
|
|
|
|
|
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count, intEnc,
|
|
|
|
|
NSDefaultMallocZone(), 0) == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to/from Unicode string."];
|
|
|
|
|
}
|
2005-05-08 07:08:28 +00:00
|
|
|
|
if (GSFromUnicode(0, &s, u, l, enc, 0, GSUniStrict) == NO)
|
2002-03-16 09:54:50 +00:00
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't get cStringLength from string."];
|
|
|
|
|
}
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline unsigned int
|
2005-05-08 07:08:28 +00:00
|
|
|
|
cStringLength_u(GSStr self, NSStringEncoding enc)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned c = self->_count;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (c == 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned l = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2006-05-12 10:05:52 +00:00
|
|
|
|
if (GSFromUnicode(0, &l, self->_contents.u, c, enc, 0, GSUniStrict) == NO)
|
2000-10-23 11:44:34 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't get cStringLength from Unicode string."];
|
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
return l;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline NSData*
|
2004-05-14 10:52:30 +00:00
|
|
|
|
dataUsingEncoding_c(GSStr self, NSStringEncoding encoding, BOOL flag)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned len = self->_count;
|
|
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
|
{
|
|
|
|
|
return [NSDataClass data];
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if ((encoding == intEnc)
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|| ((intEnc == NSASCIIStringEncoding)
|
2000-10-23 06:18:03 +00:00
|
|
|
|
&& ((encoding == NSISOLatin1StringEncoding)
|
|
|
|
|
|| (encoding == NSISOLatin2StringEncoding)
|
|
|
|
|
|| (encoding == NSNEXTSTEPStringEncoding)
|
|
|
|
|
|| (encoding == NSNonLossyASCIIStringEncoding))))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2000-10-23 06:18:03 +00:00
|
|
|
|
unsigned char *buff;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
buff = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), len);
|
|
|
|
|
memcpy(buff, self->_contents.c, len);
|
|
|
|
|
return [NSDataClass dataWithBytesNoCopy: buff length: len];
|
|
|
|
|
}
|
|
|
|
|
else if (encoding == NSUnicodeStringEncoding)
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned int l = 0;
|
|
|
|
|
unichar *r = 0;
|
|
|
|
|
unsigned int options = GSUniBOM;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (flag == NO)
|
|
|
|
|
{
|
|
|
|
|
options |= GSUniStrict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GSToUnicode(&r, &l, self->_contents.c, self->_count, intEnc,
|
|
|
|
|
NSDefaultMallocZone(), options) == NO)
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
2002-04-06 06:33:34 +00:00
|
|
|
|
return [NSDataClass dataWithBytesNoCopy: r length: l * sizeof(unichar)];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned l = 0;
|
|
|
|
|
unsigned char *r = 0;
|
|
|
|
|
unsigned s = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count, intEnc,
|
|
|
|
|
NSDefaultMallocZone(), 0) == NO)
|
2001-08-03 12:24:25 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to Unicode string."];
|
2001-08-03 12:24:25 +00:00
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (GSFromUnicode(&r, &s, u, l, 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-09 05:32:50 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
return [NSDataClass dataWithBytesNoCopy: r length: s];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline NSData*
|
2004-05-14 10:52:30 +00:00
|
|
|
|
dataUsingEncoding_u(GSStr self, NSStringEncoding encoding, BOOL flag)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned len = self->_count;
|
|
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
|
{
|
|
|
|
|
return [NSDataClass data];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (encoding == NSUnicodeStringEncoding)
|
|
|
|
|
{
|
2006-03-26 11:34:47 +00:00
|
|
|
|
unichar *buff;
|
|
|
|
|
unsigned l;
|
|
|
|
|
unsigned from = 0;
|
|
|
|
|
unsigned to = 1;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2006-03-26 11:34:47 +00:00
|
|
|
|
if ((l = GSUnicode(self->_contents.u, len, 0, 0)) != len)
|
|
|
|
|
{
|
|
|
|
|
if (flag == NO)
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
buff = (unichar*)NSZoneMalloc(NSDefaultMallocZone(),
|
|
|
|
|
sizeof(unichar)*(len+1));
|
2000-10-09 05:32:50 +00:00
|
|
|
|
buff[0] = 0xFEFF;
|
2006-03-26 11:34:47 +00:00
|
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
|
{
|
|
|
|
|
if (l > 0)
|
|
|
|
|
{
|
|
|
|
|
memcpy(buff + to, self->_contents.u + from, sizeof(unichar)*l);
|
|
|
|
|
from += l;
|
|
|
|
|
to += l;
|
|
|
|
|
len -= l;
|
|
|
|
|
}
|
|
|
|
|
if (len > 0)
|
|
|
|
|
{
|
|
|
|
|
// A bad character in the string ... skip it.
|
|
|
|
|
if (--len > 0)
|
|
|
|
|
{
|
|
|
|
|
// Not at end ... try another batch.
|
|
|
|
|
from++;
|
|
|
|
|
l = GSUnicode(self->_contents.u + from, len, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-10-23 11:44:34 +00:00
|
|
|
|
return [NSData dataWithBytesNoCopy: buff
|
2006-03-26 11:34:47 +00:00
|
|
|
|
length: sizeof(unichar)*to];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned char *r = 0;
|
|
|
|
|
unsigned int l = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
if (GSFromUnicode(&r, &l, self->_contents.u, self->_count, encoding,
|
|
|
|
|
NSDefaultMallocZone(), (flag == NO) ? GSUniStrict : 0) == NO)
|
2001-08-03 12:24:25 +00:00
|
|
|
|
{
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
2002-03-16 09:54:50 +00:00
|
|
|
|
return [NSDataClass dataWithBytesNoCopy: r length: l];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-07 09:05:53 +00:00
|
|
|
|
extern BOOL GSScanDouble(unichar*, unsigned, double*);
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
static inline double
|
2004-05-14 10:52:30 +00:00
|
|
|
|
doubleValue_c(GSStr self)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (self->_count == 0)
|
|
|
|
|
{
|
2003-07-07 09:05:53 +00:00
|
|
|
|
return 0.0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-07-07 09:05:53 +00:00
|
|
|
|
unsigned l = self->_count < 32 ? self->_count : 32;
|
|
|
|
|
unichar buf[l];
|
|
|
|
|
unichar *b = buf;
|
|
|
|
|
double d = 0.0;
|
|
|
|
|
|
|
|
|
|
GSToUnicode(&b, &l, self->_contents.c, l, intEnc, 0, 0);
|
|
|
|
|
GSScanDouble(b, l, &d);
|
|
|
|
|
return d;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline double
|
2004-05-14 10:52:30 +00:00
|
|
|
|
doubleValue_u(GSStr self)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (self->_count == 0)
|
|
|
|
|
{
|
2003-07-07 09:05:53 +00:00
|
|
|
|
return 0.0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-07-07 09:05:53 +00:00
|
|
|
|
double d = 0.0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2003-07-07 09:05:53 +00:00
|
|
|
|
GSScanDouble(self->_contents.u, self->_count, &d);
|
|
|
|
|
return d;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void
|
2004-05-14 10:52:30 +00:00
|
|
|
|
fillHole(GSStr self, unsigned index, unsigned size)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
NSCAssert(size > 0, @"size <= zero");
|
|
|
|
|
NSCAssert(index + size <= self->_count, @"index + size > length");
|
|
|
|
|
|
2000-10-10 20:54:08 +00:00
|
|
|
|
self->_count -= size;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
if (self->_flags.wide == 1)
|
|
|
|
|
{
|
2003-05-05 13:36:25 +00:00
|
|
|
|
memmove(self->_contents.u + index,
|
2003-03-23 07:06:27 +00:00
|
|
|
|
self->_contents.u + index + size,
|
2000-10-09 05:32:50 +00:00
|
|
|
|
sizeof(unichar)*(self->_count - index));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-05-05 13:36:25 +00:00
|
|
|
|
memmove(self->_contents.c + index,
|
2003-03-23 07:06:27 +00:00
|
|
|
|
self->_contents.c + index + size, (self->_count - index));
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
self->_flags.hash = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_c(GSStr self, unichar *buffer, NSRange aRange)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned len = aRange.length;
|
|
|
|
|
|
2003-05-05 13:36:25 +00:00
|
|
|
|
if (!len)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!GSToUnicode(&buffer, &len, self->_contents.c + aRange.location,
|
|
|
|
|
aRange.length, intEnc, 0, 0))
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Can't convert to Unicode."];
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_u(GSStr self, unichar *buffer, NSRange aRange)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
memcpy(buffer, self->_contents.u + aRange.location,
|
|
|
|
|
aRange.length*sizeof(unichar));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_c(GSStr self, char *buffer, unsigned int maxLength,
|
2000-10-09 05:32:50 +00:00
|
|
|
|
NSRange aRange, NSRange *leftoverRange)
|
|
|
|
|
{
|
|
|
|
|
int len;
|
|
|
|
|
|
2000-10-09 06:34:05 +00:00
|
|
|
|
if (maxLength > self->_count)
|
|
|
|
|
{
|
|
|
|
|
maxLength = self->_count;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
if (maxLength < aRange.length)
|
|
|
|
|
{
|
|
|
|
|
len = maxLength;
|
|
|
|
|
if (leftoverRange != 0)
|
|
|
|
|
{
|
2001-06-20 19:12:31 +00:00
|
|
|
|
leftoverRange->location = aRange.location + maxLength;
|
|
|
|
|
leftoverRange->length = aRange.length - maxLength;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
len = aRange.length;
|
|
|
|
|
if (leftoverRange != 0)
|
|
|
|
|
{
|
2001-06-20 19:12:31 +00:00
|
|
|
|
leftoverRange->location = 0;
|
|
|
|
|
leftoverRange->length = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(buffer, &self->_contents.c[aRange.location], len);
|
|
|
|
|
buffer[len] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_u(GSStr self, char *buffer, unsigned int maxLength,
|
2000-10-09 05:32:50 +00:00
|
|
|
|
NSRange aRange, NSRange *leftoverRange)
|
|
|
|
|
{
|
2003-07-09 02:13:58 +00:00
|
|
|
|
/* The primitive we have for converting from unicode, GSFromUnicode,
|
|
|
|
|
can't deal with our leftoverRange case, so we need to use a bit of
|
|
|
|
|
complexity instead. */
|
|
|
|
|
unsigned int len;
|
2004-05-03 20:16:37 +00:00
|
|
|
|
|
2003-07-09 02:13:58 +00:00
|
|
|
|
/* TODO: this is an extremely ugly hack to work around buggy iconvs
|
|
|
|
|
that return -1/E2BIG for buffers larger than 0x40000acf */
|
|
|
|
|
if (maxLength > 0x40000000)
|
|
|
|
|
maxLength = 0x40000000;
|
|
|
|
|
|
|
|
|
|
/* First, try converting the whole thing. */
|
|
|
|
|
len = maxLength;
|
|
|
|
|
if (GSFromUnicode((unsigned char **)&buffer, &len,
|
|
|
|
|
self->_contents.u + aRange.location, aRange.length,
|
|
|
|
|
defEnc, 0, GSUniTerminate | GSUniStrict) == YES)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2003-07-09 02:13:58 +00:00
|
|
|
|
if (leftoverRange)
|
|
|
|
|
leftoverRange->location = leftoverRange->length = 0;
|
|
|
|
|
return;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-09 02:13:58 +00:00
|
|
|
|
/* The conversion failed. Either the buffer is too small for the whole
|
|
|
|
|
range, or there are characters in it we can't convert. Check for
|
|
|
|
|
unconvertable characters first. */
|
|
|
|
|
len = 0;
|
|
|
|
|
if (GSFromUnicode(NULL, &len,
|
|
|
|
|
self->_contents.u + aRange.location, aRange.length,
|
|
|
|
|
defEnc, 0, GSUniTerminate | GSUniStrict) == NO)
|
2000-12-14 09:47:02 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't get cString from Unicode string."];
|
2003-07-09 02:13:58 +00:00
|
|
|
|
return;
|
2000-12-14 09:47:02 +00:00
|
|
|
|
}
|
2003-07-09 02:13:58 +00:00
|
|
|
|
|
|
|
|
|
/* The string can be converted, but not all of it. Do a binary search
|
|
|
|
|
to find the longest subrange that fits in the buffer. */
|
|
|
|
|
{
|
|
|
|
|
unsigned int lo, hi, mid;
|
|
|
|
|
|
|
|
|
|
lo = 0;
|
|
|
|
|
hi = aRange.length;
|
|
|
|
|
while (lo < hi)
|
|
|
|
|
{
|
|
|
|
|
mid = (lo + hi + 1) / 2; /* round up to get edge case right */
|
|
|
|
|
len = maxLength;
|
|
|
|
|
if (GSFromUnicode((unsigned char **)&buffer, &len,
|
|
|
|
|
self->_contents.u + aRange.location, mid,
|
|
|
|
|
defEnc, 0, GSUniTerminate | GSUniStrict) == YES)
|
|
|
|
|
{
|
|
|
|
|
lo = mid;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hi = mid - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* lo==hi characters fit. Do the real conversion. */
|
|
|
|
|
len = maxLength;
|
|
|
|
|
if (lo == 0)
|
|
|
|
|
{
|
|
|
|
|
buffer[0] = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (GSFromUnicode((unsigned char **)&buffer, &len,
|
|
|
|
|
self->_contents.u + aRange.location, lo,
|
|
|
|
|
defEnc, 0, GSUniTerminate | GSUniStrict) == NO)
|
|
|
|
|
{
|
|
|
|
|
NSCAssert(NO, @"binary search gave inconsistent results");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (leftoverRange)
|
|
|
|
|
{
|
|
|
|
|
leftoverRange->location = aRange.location + lo;
|
|
|
|
|
leftoverRange->length = NSMaxRange(aRange) - leftoverRange->location;
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-02-27 09:35:19 +00:00
|
|
|
|
static inline BOOL
|
|
|
|
|
getCStringE_c(GSStr self, char *buffer, unsigned int maxLength,
|
|
|
|
|
NSStringEncoding enc)
|
|
|
|
|
{
|
|
|
|
|
if (enc == NSUnicodeStringEncoding)
|
|
|
|
|
{
|
|
|
|
|
if (maxLength >= sizeof(unichar))
|
|
|
|
|
{
|
|
|
|
|
unsigned bytes = maxLength - sizeof(unichar);
|
|
|
|
|
unichar *u = (unichar*)buffer;
|
|
|
|
|
|
|
|
|
|
if (GSToUnicode(&u, &bytes, self->_contents.c, self->_count, intEnc,
|
|
|
|
|
NSDefaultMallocZone(), GSUniTerminate) == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to Unicode string."];
|
|
|
|
|
}
|
|
|
|
|
if (u == (unichar*)buffer)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (maxLength > sizeof(char))
|
|
|
|
|
{
|
|
|
|
|
unsigned bytes = maxLength - sizeof(char);
|
|
|
|
|
|
|
|
|
|
if (enc == intEnc)
|
|
|
|
|
{
|
|
|
|
|
if (bytes > self->_count)
|
|
|
|
|
{
|
|
|
|
|
bytes = self->_count;
|
|
|
|
|
}
|
|
|
|
|
memcpy(buffer, self->_contents.c, bytes);
|
|
|
|
|
buffer[bytes] = '\0';
|
|
|
|
|
if (bytes < self->_count)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
else if (enc == NSASCIIStringEncoding && GSIsByteEncoding(intEnc))
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
if (bytes > self->_count)
|
|
|
|
|
{
|
|
|
|
|
bytes = self->_count;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < bytes; i++)
|
|
|
|
|
{
|
|
|
|
|
unsigned char c = self->_contents.c[i];
|
|
|
|
|
|
|
|
|
|
if (c > 127)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"unable to convert to encoding"];
|
|
|
|
|
}
|
|
|
|
|
buffer[i] = c;
|
|
|
|
|
}
|
|
|
|
|
buffer[bytes] = '\0';
|
|
|
|
|
if (bytes < self->_count)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned char *c = (unsigned char*)buffer;
|
|
|
|
|
unsigned l = 0;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The specified C string encoding is not compatible with
|
|
|
|
|
* the internal 8-bit character strings ... we must convert
|
|
|
|
|
* from internal format to unicode and then to the specified
|
|
|
|
|
* C string encoding.
|
|
|
|
|
*/
|
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count, intEnc,
|
|
|
|
|
NSDefaultMallocZone(), 0) == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to Unicode string."];
|
|
|
|
|
}
|
|
|
|
|
if (GSFromUnicode((unsigned char**)&c, &bytes, u, l, enc,
|
|
|
|
|
NSDefaultMallocZone(), GSUniTerminate|GSUniStrict) == NO)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert from Unicode string."];
|
|
|
|
|
}
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
if (c == (unsigned char*)buffer)
|
|
|
|
|
{
|
|
|
|
|
return YES; // Fitted in original buffer
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline BOOL
|
|
|
|
|
getCStringE_u(GSStr self, char *buffer, unsigned int maxLength,
|
|
|
|
|
NSStringEncoding enc)
|
|
|
|
|
{
|
|
|
|
|
if (enc == NSUnicodeStringEncoding)
|
|
|
|
|
{
|
|
|
|
|
if (maxLength >= sizeof(unichar))
|
|
|
|
|
{
|
|
|
|
|
unsigned bytes = maxLength - sizeof(unichar);
|
|
|
|
|
|
|
|
|
|
if (bytes/sizeof(unichar) > self->_count)
|
|
|
|
|
{
|
|
|
|
|
bytes = self->_count * sizeof(unichar);
|
|
|
|
|
}
|
|
|
|
|
memcpy(buffer, self->_contents.u, bytes);
|
|
|
|
|
buffer[bytes] = '\0';
|
|
|
|
|
buffer[bytes + 1] = '\0';
|
|
|
|
|
if (bytes/sizeof(unichar) == self->_count)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (maxLength >= 1)
|
|
|
|
|
{
|
|
|
|
|
if (enc == NSISOLatin1StringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unsigned bytes = maxLength - sizeof(char);
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
if (bytes > self->_count)
|
|
|
|
|
{
|
|
|
|
|
bytes = self->_count;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < bytes; i++)
|
|
|
|
|
{
|
|
|
|
|
unichar u = self->_contents.u[i];
|
|
|
|
|
|
|
|
|
|
if (u & 0xff00)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"unable to convert to encoding"];
|
|
|
|
|
}
|
|
|
|
|
buffer[i] = (char)u;
|
|
|
|
|
}
|
|
|
|
|
buffer[i++] = '\0';
|
|
|
|
|
if (bytes == self->_count)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (enc == NSASCIIStringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unsigned bytes = maxLength - sizeof(char);
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
if (bytes > self->_count)
|
|
|
|
|
{
|
|
|
|
|
bytes = self->_count;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < bytes; i++)
|
|
|
|
|
{
|
|
|
|
|
unichar u = self->_contents.u[i];
|
|
|
|
|
|
|
|
|
|
if (u & 0xff80)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"unable to convert to encoding"];
|
|
|
|
|
}
|
|
|
|
|
buffer[i] = (char)u;
|
|
|
|
|
}
|
|
|
|
|
buffer[i++] = '\0';
|
|
|
|
|
if (bytes == self->_count)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned char *c = (unsigned char*)buffer;
|
|
|
|
|
|
|
|
|
|
if (GSFromUnicode((unsigned char**)&c, &maxLength,
|
|
|
|
|
self->_contents.u, self->_count, enc,
|
|
|
|
|
NSDefaultMallocZone(), GSUniTerminate|GSUniStrict) == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to/from Unicode string."];
|
|
|
|
|
}
|
|
|
|
|
if (c == (unsigned char*)buffer)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
static inline int
|
2004-05-14 10:52:30 +00:00
|
|
|
|
intValue_c(GSStr self)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (self->_count == 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned len = self->_count < 32 ? self->_count : 31;
|
|
|
|
|
char buf[len+1];
|
|
|
|
|
|
|
|
|
|
memcpy(buf, self->_contents.c, len);
|
|
|
|
|
buf[len] = '\0';
|
2005-07-08 11:48:37 +00:00
|
|
|
|
return atol((const char*)buf);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int
|
2004-05-14 10:52:30 +00:00
|
|
|
|
intValue_u(GSStr self)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (self->_count == 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-06-20 18:04:38 +00:00
|
|
|
|
unsigned int l = self->_count < 32 ? self->_count : 31;
|
2002-03-16 09:54:50 +00:00
|
|
|
|
unsigned char buf[l+1];
|
|
|
|
|
unsigned char *b = buf;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-16 09:54:50 +00:00
|
|
|
|
GSFromUnicode(&b, &l, self->_contents.u, l, intEnc, 0, GSUniTerminate);
|
2005-07-08 11:48:37 +00:00
|
|
|
|
return atol((const char*)buf);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline BOOL
|
2004-05-14 10:52:30 +00:00
|
|
|
|
isEqual_c(GSStr self, id anObject)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
Class c;
|
|
|
|
|
|
|
|
|
|
if (anObject == (id)self)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
if (anObject == nil)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsInstance(anObject) == NO)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
c = GSObjCClass(anObject);
|
2001-06-06 15:18:28 +00:00
|
|
|
|
if (c == NSConstantStringClass)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr other = (GSStr)anObject;
|
2000-10-20 10:30:51 +00:00
|
|
|
|
NSRange r = {0, self->_count};
|
|
|
|
|
|
|
|
|
|
if (strCompCsCs((id)self, (id)other, 0, r) == NSOrderedSame)
|
|
|
|
|
return YES;
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, GSStringClass) == YES || c == GSMutableStringClass)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr other = (GSStr)anObject;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
NSRange r = {0, self->_count};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* First see if the hash is the same - if not, we can't be equal.
|
|
|
|
|
*/
|
|
|
|
|
if (self->_flags.hash == 0)
|
|
|
|
|
self->_flags.hash = (*hashImp)((id)self, hashSel);
|
|
|
|
|
if (other->_flags.hash == 0)
|
|
|
|
|
other->_flags.hash = (*hashImp)((id)other, hashSel);
|
|
|
|
|
if (self->_flags.hash != other->_flags.hash)
|
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Do a compare depending on the type of the other string.
|
|
|
|
|
*/
|
|
|
|
|
if (other->_flags.wide == 1)
|
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
if (strCompCsUs((id)self, (id)other, 0, r) == NSOrderedSame)
|
|
|
|
|
return YES;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
if (strCompCsCs((id)self, (id)other, 0, r) == NSOrderedSame)
|
|
|
|
|
return YES;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, NSStringClass))
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
|
|
|
|
return (*equalImp)((id)self, equalSel, anObject);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline BOOL
|
2004-05-14 10:52:30 +00:00
|
|
|
|
isEqual_u(GSStr self, id anObject)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
|
|
|
|
Class c;
|
|
|
|
|
|
|
|
|
|
if (anObject == (id)self)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
if (anObject == nil)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsInstance(anObject) == NO)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
c = GSObjCClass(anObject);
|
2001-06-06 15:18:28 +00:00
|
|
|
|
if (c == NSConstantStringClass)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr other = (GSStr)anObject;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
NSRange r = {0, self->_count};
|
|
|
|
|
|
2000-10-20 10:30:51 +00:00
|
|
|
|
if (strCompUsCs((id)self, (id)other, 0, r) == NSOrderedSame)
|
|
|
|
|
return YES;
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, GSStringClass) == YES || c == GSMutableStringClass)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr other = (GSStr)anObject;
|
2000-10-20 10:30:51 +00:00
|
|
|
|
NSRange r = {0, self->_count};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* First see if the hash is the same - if not, we can't be equal.
|
|
|
|
|
*/
|
|
|
|
|
if (self->_flags.hash == 0)
|
|
|
|
|
self->_flags.hash = (*hashImp)((id)self, hashSel);
|
|
|
|
|
if (other->_flags.hash == 0)
|
|
|
|
|
other->_flags.hash = (*hashImp)((id)other, hashSel);
|
|
|
|
|
if (self->_flags.hash != other->_flags.hash)
|
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Do a compare depending on the type of the other string.
|
|
|
|
|
*/
|
|
|
|
|
if (other->_flags.wide == 1)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
if (strCompUsUs((id)self, (id)other, 0, r) == NSOrderedSame)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
if (strCompUsCs((id)self, (id)other, 0, r) == NSOrderedSame)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, NSStringClass))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
return (*equalImp)((id)self, equalSel, anObject);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline const char*
|
2004-05-14 10:52:30 +00:00
|
|
|
|
lossyCString_c(GSStr self)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2002-03-19 07:39:05 +00:00
|
|
|
|
char *r;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-19 07:39:05 +00:00
|
|
|
|
if (self->_count == 0)
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
if (defEnc == intEnc)
|
|
|
|
|
{
|
2003-03-02 07:47:18 +00:00
|
|
|
|
r = (char*)GSAutoreleasedBuffer(self->_count+1);
|
2002-03-19 07:39:05 +00:00
|
|
|
|
|
|
|
|
|
if (self->_count > 0)
|
|
|
|
|
{
|
|
|
|
|
memcpy(r, self->_contents.c, self->_count);
|
|
|
|
|
}
|
|
|
|
|
r[self->_count] = '\0';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned l = 0;
|
|
|
|
|
unsigned s = 0;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The external C string encoding is not compatible with the internal
|
|
|
|
|
* 8-bit character strings ... we must convert from internal format to
|
|
|
|
|
* unicode and then to the external C string encoding.
|
|
|
|
|
*/
|
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count, intEnc,
|
|
|
|
|
NSDefaultMallocZone(), 0) == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to/from Unicode string."];
|
|
|
|
|
}
|
|
|
|
|
if (GSFromUnicode((unsigned char**)&r, &s, u, l, defEnc,
|
|
|
|
|
NSDefaultMallocZone(), GSUniTerminate|GSUniTemporary) == NO)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to/from Unicode string."];
|
|
|
|
|
}
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2002-03-19 07:39:05 +00:00
|
|
|
|
return r;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline const char*
|
2004-05-14 10:52:30 +00:00
|
|
|
|
lossyCString_u(GSStr self)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2002-03-20 06:46:26 +00:00
|
|
|
|
unsigned l = 0;
|
|
|
|
|
unsigned char *r = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-20 22:37:22 +00:00
|
|
|
|
GSFromUnicode(&r, &l, self->_contents.u, self->_count, defEnc,
|
|
|
|
|
NSDefaultMallocZone(), GSUniTemporary|GSUniTerminate);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return (const char*)r;
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-14 10:52:30 +00:00
|
|
|
|
static void GSStrMakeSpace(GSStr s, unsigned size)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned want;
|
|
|
|
|
|
2004-05-14 10:52:30 +00:00
|
|
|
|
want = size + s->_count + 1;
|
|
|
|
|
s->_capacity += s->_capacity/2;
|
|
|
|
|
if (want > s->_capacity)
|
|
|
|
|
{
|
|
|
|
|
s->_capacity = want;
|
|
|
|
|
}
|
|
|
|
|
if (s->_flags.free == 1)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
/*
|
|
|
|
|
* If we own the character buffer, we can simply realloc.
|
|
|
|
|
*/
|
|
|
|
|
if (s->_flags.wide == 1)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
s->_contents.u = NSZoneRealloc(s->_zone,
|
|
|
|
|
s->_contents.u, s->_capacity*sizeof(unichar));
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
else
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
s->_contents.c = NSZoneRealloc(s->_zone,
|
|
|
|
|
s->_contents.c, s->_capacity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* If the initial data was not to be freed, we must allocate new
|
|
|
|
|
* buffer, copy the data, and set up the zone we are using.
|
|
|
|
|
*/
|
|
|
|
|
if (s->_zone == 0)
|
|
|
|
|
{
|
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
s->_zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
|
|
|
|
if (s->isa == 0)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
s->_zone = NSDefaultMallocZone();
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
s->_zone = GSObjCZone((NSString*)s);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
#endif
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (s->_flags.wide == 1)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
unichar *tmp = s->_contents.u;
|
|
|
|
|
|
|
|
|
|
s->_contents.u = NSZoneMalloc(s->_zone,
|
|
|
|
|
s->_capacity*sizeof(unichar));
|
|
|
|
|
if (s->_count > 0)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
memcpy(s->_contents.u, tmp, s->_count*sizeof(unichar));
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned char *tmp = s->_contents.c;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2004-05-14 10:52:30 +00:00
|
|
|
|
s->_contents.c = NSZoneMalloc(s->_zone, s->_capacity);
|
|
|
|
|
if (s->_count > 0)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
memcpy(s->_contents.c, tmp, s->_count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s->_flags.free = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2004-05-14 10:52:30 +00:00
|
|
|
|
static void GSStrWiden(GSStr s)
|
|
|
|
|
{
|
|
|
|
|
unichar *tmp = 0;
|
2005-07-08 11:48:37 +00:00
|
|
|
|
unsigned len = 0;
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|
|
|
|
|
NSCAssert(s->_flags.wide == 0, @"string is not wide");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* As a special case, where we are ascii or latin1 and the buffer size
|
|
|
|
|
* is big enough, we can widen to unicode without having to allocate
|
|
|
|
|
* more memory or call a character conversion function.
|
|
|
|
|
*/
|
|
|
|
|
if (s->_count <= s->_capacity / 2)
|
|
|
|
|
{
|
|
|
|
|
if (intEnc == NSISOLatin1StringEncoding
|
|
|
|
|
|| intEnc == NSASCIIStringEncoding)
|
|
|
|
|
{
|
|
|
|
|
len = s->_count;
|
|
|
|
|
while (len-- > 0)
|
|
|
|
|
{
|
|
|
|
|
s->_contents.u[len] = s->_contents.c[len];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
s->_capacity /= 2;
|
|
|
|
|
s->_flags.wide = 1;
|
|
|
|
|
return;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (!s->_zone)
|
|
|
|
|
{
|
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
s->_zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
|
|
|
|
if (s->isa == 0)
|
|
|
|
|
{
|
|
|
|
|
s->_zone = NSDefaultMallocZone();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s->_zone = GSObjCZone((NSString*)s);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!GSToUnicode(&tmp, &len, s->_contents.c, s->_count, intEnc, s->_zone, 0))
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"widen of string failed"];
|
|
|
|
|
}
|
|
|
|
|
if (s->_flags.free == 1)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(s->_zone, s->_contents.c);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s->_flags.free = 1;
|
|
|
|
|
}
|
|
|
|
|
s->_contents.u = tmp;
|
|
|
|
|
s->_flags.wide = 1;
|
|
|
|
|
s->_count = len;
|
|
|
|
|
s->_capacity = len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
|
makeHole(GSStr self, unsigned int index, unsigned int size)
|
|
|
|
|
{
|
|
|
|
|
NSCAssert(size > 0, @"size < zero");
|
|
|
|
|
NSCAssert(index <= self->_count, @"index > length");
|
|
|
|
|
|
|
|
|
|
if (self->_count + size + 1 >= self->_capacity)
|
|
|
|
|
{
|
|
|
|
|
GSStrMakeSpace((GSStr)self, size);
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
if (index < self->_count)
|
|
|
|
|
{
|
|
|
|
|
if (self->_flags.wide == 1)
|
|
|
|
|
{
|
2003-05-05 13:36:25 +00:00
|
|
|
|
memmove(self->_contents.u + index + size,
|
|
|
|
|
self->_contents.u + index,
|
2000-10-09 05:32:50 +00:00
|
|
|
|
sizeof(unichar)*(self->_count - index));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-05-05 13:36:25 +00:00
|
|
|
|
memmove(self->_contents.c + index + size,
|
|
|
|
|
self->_contents.c + index,
|
2000-10-09 05:32:50 +00:00
|
|
|
|
(self->_count - index));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self->_count += size;
|
|
|
|
|
self->_flags.hash = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline NSRange
|
2004-05-14 10:52:30 +00:00
|
|
|
|
rangeOfSequence_c(GSStr self, unsigned anIndex)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (anIndex >= self->_count)
|
|
|
|
|
[NSException raise: NSRangeException format:@"Invalid location."];
|
|
|
|
|
|
|
|
|
|
return (NSRange){anIndex, 1};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline NSRange
|
2004-05-14 10:52:30 +00:00
|
|
|
|
rangeOfSequence_u(GSStr self, unsigned anIndex)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned start;
|
|
|
|
|
unsigned end;
|
|
|
|
|
|
|
|
|
|
if (anIndex >= self->_count)
|
|
|
|
|
[NSException raise: NSRangeException format:@"Invalid location."];
|
|
|
|
|
|
|
|
|
|
start = anIndex;
|
|
|
|
|
while (uni_isnonsp(self->_contents.u[start]) && start > 0)
|
|
|
|
|
start--;
|
|
|
|
|
end = start + 1;
|
|
|
|
|
if (end < self->_count)
|
2005-02-22 11:22:44 +00:00
|
|
|
|
while ((end < self->_count) && (uni_isnonsp(self->_contents.u[end])))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
end++;
|
|
|
|
|
return (NSRange){start, end-start};
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
static inline NSRange
|
2004-05-14 10:52:30 +00:00
|
|
|
|
rangeOfCharacter_c(GSStr self, NSCharacterSet *aSet, unsigned mask,
|
2002-02-06 13:44:03 +00:00
|
|
|
|
NSRange aRange)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int start;
|
|
|
|
|
int stop;
|
|
|
|
|
int step;
|
|
|
|
|
NSRange range;
|
|
|
|
|
BOOL (*mImp)(id, SEL, unichar);
|
|
|
|
|
|
|
|
|
|
if (aSet == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException format: @"range of nil"];
|
|
|
|
|
i = self->_count;
|
|
|
|
|
|
|
|
|
|
if ((mask & NSBackwardsSearch) == NSBackwardsSearch)
|
|
|
|
|
{
|
|
|
|
|
start = NSMaxRange(aRange)-1; stop = aRange.location-1; step = -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
start = aRange.location; stop = NSMaxRange(aRange); step = 1;
|
|
|
|
|
}
|
|
|
|
|
range.location = NSNotFound;
|
|
|
|
|
range.length = 0;
|
|
|
|
|
|
|
|
|
|
mImp = (BOOL(*)(id,SEL,unichar))
|
|
|
|
|
[aSet methodForSelector: cMemberSel];
|
|
|
|
|
|
|
|
|
|
for (i = start; i != stop; i += step)
|
|
|
|
|
{
|
|
|
|
|
unichar letter = self->_contents.c[i];
|
|
|
|
|
|
|
|
|
|
if (letter > 127)
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
letter = encode_chartouni(letter, intEnc);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
if ((*mImp)(aSet, cMemberSel, letter))
|
|
|
|
|
{
|
|
|
|
|
range = NSMakeRange(i, 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return range;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline NSRange
|
2004-05-14 10:52:30 +00:00
|
|
|
|
rangeOfCharacter_u(GSStr self, NSCharacterSet *aSet, unsigned mask,
|
2002-02-06 13:44:03 +00:00
|
|
|
|
NSRange aRange)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int start;
|
|
|
|
|
int stop;
|
|
|
|
|
int step;
|
|
|
|
|
NSRange range;
|
|
|
|
|
BOOL (*mImp)(id, SEL, unichar);
|
|
|
|
|
|
|
|
|
|
if (aSet == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException format: @"range of nil"];
|
|
|
|
|
i = self->_count;
|
|
|
|
|
|
|
|
|
|
if ((mask & NSBackwardsSearch) == NSBackwardsSearch)
|
|
|
|
|
{
|
|
|
|
|
start = NSMaxRange(aRange)-1; stop = aRange.location-1; step = -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
start = aRange.location; stop = NSMaxRange(aRange); step = 1;
|
|
|
|
|
}
|
|
|
|
|
range.location = NSNotFound;
|
|
|
|
|
range.length = 0;
|
|
|
|
|
|
|
|
|
|
mImp = (BOOL(*)(id,SEL,unichar))
|
|
|
|
|
[aSet methodForSelector: cMemberSel];
|
|
|
|
|
|
|
|
|
|
for (i = start; i != stop; i += step)
|
|
|
|
|
{
|
|
|
|
|
unichar letter = self->_contents.u[i];
|
|
|
|
|
|
|
|
|
|
if ((*mImp)(aSet, cMemberSel, letter))
|
|
|
|
|
{
|
|
|
|
|
range = NSMakeRange(i, 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return range;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
static inline NSRange
|
2004-05-14 10:52:30 +00:00
|
|
|
|
rangeOfString_c(GSStr self, NSString *aString, unsigned mask, NSRange aRange)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
Class c;
|
|
|
|
|
|
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException format: @"range of nil"];
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsInstance(aString) == NO)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
return strRangeCsNs((id)self, aString, mask, aRange);
|
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
|
c = GSObjCClass(aString);
|
2000-11-03 10:11:56 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)aString)->_flags.wide == 1))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return strRangeCsUs((id)self, aString, mask, aRange);
|
2000-10-31 16:17:33 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, GSCStringClass) == YES
|
2001-06-06 15:18:28 +00:00
|
|
|
|
|| c == NSConstantStringClass
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)aString)->_flags.wide == 0))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return strRangeCsCs((id)self, aString, mask, aRange);
|
|
|
|
|
else
|
|
|
|
|
return strRangeCsNs((id)self, aString, mask, aRange);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline NSRange
|
2004-05-14 10:52:30 +00:00
|
|
|
|
rangeOfString_u(GSStr self, NSString *aString, unsigned mask, NSRange aRange)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
Class c;
|
|
|
|
|
|
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException format: @"range of nil"];
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsInstance(aString) == NO)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
return strRangeUsNs((id)self, aString, mask, aRange);
|
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
|
c = GSObjCClass(aString);
|
2000-11-03 10:11:56 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)aString)->_flags.wide == 1))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return strRangeUsUs((id)self, aString, mask, aRange);
|
2000-10-31 16:17:33 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, GSCStringClass) == YES
|
2001-06-06 15:18:28 +00:00
|
|
|
|
|| c == NSConstantStringClass
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)aString)->_flags.wide == 0))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return strRangeUsCs((id)self, aString, mask, aRange);
|
|
|
|
|
else
|
|
|
|
|
return strRangeUsNs((id)self, aString, mask, aRange);
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-24 11:58:25 +00:00
|
|
|
|
static inline NSString*
|
2004-05-14 10:52:30 +00:00
|
|
|
|
substring_c(GSStr self, NSRange aRange)
|
2000-10-24 11:58:25 +00:00
|
|
|
|
{
|
2003-11-03 08:35:44 +00:00
|
|
|
|
id sub;
|
2000-10-24 11:58:25 +00:00
|
|
|
|
|
2003-11-03 08:35:44 +00:00
|
|
|
|
if (self->_flags.free == 1)
|
|
|
|
|
{
|
|
|
|
|
sub = NSAllocateObject(GSCSubStringClass, 0, NSDefaultMallocZone());
|
2005-07-08 11:48:37 +00:00
|
|
|
|
sub = [sub initWithCString: (char*)self->_contents.c + aRange.location
|
2003-11-05 02:11:49 +00:00
|
|
|
|
length: aRange.length
|
|
|
|
|
fromParent: (GSCString *)self];
|
2003-11-03 08:35:44 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2000-10-24 11:58:25 +00:00
|
|
|
|
{
|
2003-11-03 08:35:44 +00:00
|
|
|
|
sub = NSAllocateObject(GSCInlineStringClass,
|
|
|
|
|
aRange.length, NSDefaultMallocZone());
|
2005-07-08 11:48:37 +00:00
|
|
|
|
sub = [sub initWithCString: (char*)self->_contents.c + aRange.location
|
2003-11-03 08:35:44 +00:00
|
|
|
|
length: aRange.length];
|
2000-10-24 11:58:25 +00:00
|
|
|
|
}
|
2003-11-03 08:35:44 +00:00
|
|
|
|
AUTORELEASE(sub);
|
2000-10-24 11:58:25 +00:00
|
|
|
|
return sub;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline NSString*
|
2004-05-14 10:52:30 +00:00
|
|
|
|
substring_u(GSStr self, NSRange aRange)
|
2000-10-24 11:58:25 +00:00
|
|
|
|
{
|
2003-11-03 08:35:44 +00:00
|
|
|
|
id sub;
|
2000-10-24 11:58:25 +00:00
|
|
|
|
|
2003-11-03 08:35:44 +00:00
|
|
|
|
if (self->_flags.free == 1)
|
2000-10-24 11:58:25 +00:00
|
|
|
|
{
|
2003-11-03 08:35:44 +00:00
|
|
|
|
sub = NSAllocateObject(GSUnicodeSubStringClass, 0, NSDefaultMallocZone());
|
2003-11-05 02:11:49 +00:00
|
|
|
|
sub = [sub initWithCharacters: self->_contents.u + aRange.location
|
|
|
|
|
length: aRange.length
|
|
|
|
|
fromParent: (GSUnicodeString *)self];
|
2003-11-03 08:35:44 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sub = NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
aRange.length*sizeof(unichar),
|
|
|
|
|
NSDefaultMallocZone());
|
|
|
|
|
sub = [sub initWithCharacters: self->_contents.u + aRange.location
|
|
|
|
|
length: aRange.length];
|
2000-10-24 11:58:25 +00:00
|
|
|
|
}
|
2003-11-03 08:35:44 +00:00
|
|
|
|
AUTORELEASE(sub);
|
2000-10-24 11:58:25 +00:00
|
|
|
|
return sub;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
/*
|
|
|
|
|
* Function to examine the given string and see if it is one of our concrete
|
|
|
|
|
* string classes. Converts the mutable string (self) from 8-bit to 16-bit
|
|
|
|
|
* representation if necessary in order to contain the data in aString.
|
2004-05-14 10:52:30 +00:00
|
|
|
|
* Returns a pointer to aStrings GSStr if aString is a concrete class
|
2000-10-09 05:32:50 +00:00
|
|
|
|
* from which contents may be copied directly without conversion.
|
|
|
|
|
*/
|
2004-05-14 10:52:30 +00:00
|
|
|
|
static inline GSStr
|
|
|
|
|
transmute(GSStr self, NSString *aString)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr other = (GSStr)aString;
|
|
|
|
|
BOOL transmute = YES;
|
2000-10-31 16:17:33 +00:00
|
|
|
|
Class c = GSObjCClass(aString); // NB aString must not be nil
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
if (self->_flags.wide == 1)
|
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
/*
|
|
|
|
|
* This is already a unicode string, so we don't need to transmute,
|
|
|
|
|
* but we still need to know if the other string is a unicode
|
2004-05-14 10:52:30 +00:00
|
|
|
|
* string whose GSStr we can access directly.
|
2000-10-20 10:30:51 +00:00
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
transmute = NO;
|
2000-11-08 16:02:49 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSUnicodeStringClass) == NO
|
|
|
|
|
&& (c != GSMutableStringClass || other->_flags.wide != 1))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
other = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-04-16 11:18:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* This is a string held in the internal 8-bit encoding.
|
|
|
|
|
*/
|
2001-06-06 15:18:28 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSCStringClass) || c == NSConstantStringClass
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|| (c == GSMutableStringClass && other->_flags.wide == 0))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
/*
|
2002-04-16 11:18:14 +00:00
|
|
|
|
* The other string is also held in the internal 8-bit encoding,
|
2004-05-14 10:52:30 +00:00
|
|
|
|
* so we don't need to transmute, and we can use its GSStr.
|
2000-10-20 10:30:51 +00:00
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
transmute = NO;
|
|
|
|
|
}
|
2002-04-16 13:48:14 +00:00
|
|
|
|
else if (intEnc == defEnc
|
|
|
|
|
&& [aString canBeConvertedToEncoding: intEnc] == YES)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
/*
|
2002-04-16 11:18:14 +00:00
|
|
|
|
* The other string can be converted to the internal 8-bit encoding,
|
2002-04-16 13:48:14 +00:00
|
|
|
|
* via the cString method, so we don't need to transmute, but we
|
2004-05-14 10:52:30 +00:00
|
|
|
|
* can *not* use its GSStr.
|
2002-04-16 13:48:14 +00:00
|
|
|
|
* NB. If 'intEnc != defEnc' the cString method of the other string
|
|
|
|
|
* will not return data in the internal encoding.
|
2000-10-20 10:30:51 +00:00
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
transmute = NO;
|
|
|
|
|
other = 0;
|
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
else if ((c == GSMutableStringClass && other->_flags.wide == 1)
|
2000-11-08 16:02:49 +00:00
|
|
|
|
|| GSObjCIsKindOf(c, GSUnicodeStringClass) == YES)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
/*
|
2002-04-16 11:18:14 +00:00
|
|
|
|
* The other string can not be converted to the internal 8-bit
|
|
|
|
|
* encoding, so we need to transmute, and will then be able to
|
2004-05-14 10:52:30 +00:00
|
|
|
|
* use its GSStr.
|
2000-10-20 10:30:51 +00:00
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
transmute = YES;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
/*
|
2002-04-16 11:18:14 +00:00
|
|
|
|
* The other string can not be converted to the internal 8-bit
|
|
|
|
|
* character string, so we need to transmute, but even then we
|
2004-05-14 10:52:30 +00:00
|
|
|
|
* will not be able to use the other strings GSStr because that
|
2002-04-16 11:18:14 +00:00
|
|
|
|
* string is not a known GSString subclass.
|
2000-10-20 10:30:51 +00:00
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
other = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (transmute == YES)
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStrWiden((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return other;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* The GSString class is actually only provided to provide a common ivar
|
|
|
|
|
* layout for all subclasses, so that they can all share the same code.
|
2003-11-05 02:11:49 +00:00
|
|
|
|
* This class should never be instantiated, and with the exception of
|
|
|
|
|
* -copyWithZone:, none of its memory management related methods
|
|
|
|
|
* (initializers and -dealloc) should ever be called. We guard against
|
|
|
|
|
* this happening.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
*/
|
2003-11-05 02:11:49 +00:00
|
|
|
|
@implementation GSString
|
2000-11-06 19:55:23 +00:00
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
setup();
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2003-11-05 02:11:49 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* Try to initialise a unicode string.
|
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
2003-11-05 02:11:49 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return nil;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* Try to initialise a 'C' string.
|
2003-05-06 06:54:22 +00:00
|
|
|
|
* Only ever called if defEnc==intEnc
|
2000-11-03 10:11:56 +00:00
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (id) initWithCStringNoCopy: (char*)chars
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
2003-11-05 02:11:49 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return nil;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
@implementation GSCString
|
2002-10-05 17:47:54 +00:00
|
|
|
|
- (const char *) UTF8String
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return UTF8String_c((GSStr)self);
|
2002-10-05 17:47:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (BOOL) boolValue
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return boolValue_c((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) canBeConvertedToEncoding: (NSStringEncoding)enc
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return canBeConvertedToEncoding_c((GSStr)self, enc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unichar) characterAtIndex: (unsigned int)index
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return characterAtIndex_c((GSStr)self, index);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSComparisonResult) compare: (NSString*)aString
|
|
|
|
|
options: (unsigned int)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return compare_c((GSStr)self, aString, mask, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cString
|
|
|
|
|
{
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cString_c((GSStr)self, defEnc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cStringUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
return cString_c((GSStr)self, encoding);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) cStringLength
|
|
|
|
|
{
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cStringLength_c((GSStr)self, defEnc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSData*) dataUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
allowLossyConversion: (BOOL)flag
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return dataUsingEncoding_c((GSStr)self, encoding, flag);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (double) doubleValue
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return doubleValue_c((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &_count];
|
|
|
|
|
if (_count > 0)
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(NSStringEncoding) at: &intEnc];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[aCoder encodeArrayOfObjCType: @encode(unsigned char)
|
|
|
|
|
count: _count
|
|
|
|
|
at: _contents.c];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) fastestEncoding
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
return intEnc;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (float) floatValue
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return doubleValue_c((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_c((GSStr)self, buffer, (NSRange){0, _count});
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_c((GSStr)self, buffer, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_c((GSStr)self, buffer, NSMaximumStringLength,
|
2000-10-09 05:32:50 +00:00
|
|
|
|
(NSRange){0, _count}, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_c((GSStr)self, buffer, maxLength, (NSRange){0, _count}, 0);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-02-27 09:35:19 +00:00
|
|
|
|
- (BOOL) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
return getCStringE_c((GSStr)self, buffer, maxLength, encoding);
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
remainingRange: (NSRange*)leftoverRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_c((GSStr)self, buffer, maxLength, aRange, leftoverRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) hash
|
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
if (self->_flags.hash == 0)
|
|
|
|
|
{
|
|
|
|
|
self->_flags.hash = (*hashImp)((id)self, hashSel);
|
|
|
|
|
}
|
|
|
|
|
return self->_flags.hash;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (int) intValue
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return intValue_c((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)anObject
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return isEqual_c((GSStr)self, anObject);
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqualToString: (NSString*)anObject
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return isEqual_c((GSStr)self, anObject);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) length
|
|
|
|
|
{
|
|
|
|
|
return _count;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-08 07:08:28 +00:00
|
|
|
|
- (unsigned int) lengthOfBytesUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
return cStringLength_c((GSStr)self, encoding);
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (const char*) lossyCString
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return lossyCString_c((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) mutableCopy
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSMutableString *obj;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
obj = (GSMutableString*)NSAllocateObject(GSMutableStringClass, 0,
|
|
|
|
|
NSDefaultMallocZone());
|
2005-07-08 11:48:37 +00:00
|
|
|
|
obj = [obj initWithCString: (char*)_contents.c length: _count];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)z
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSMutableString *obj;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
obj = (GSMutableString*)NSAllocateObject(GSMutableStringClass, 0, z);
|
2005-07-08 11:48:37 +00:00
|
|
|
|
obj = [obj initWithCString: (char*)_contents.c length: _count];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (unsigned)anIndex
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfSequence_c((GSStr)self, anIndex);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
|
|
|
|
options: (unsigned)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfCharacter_c((GSStr)self, aSet, mask, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (NSRange) rangeOfString: (NSString*)aString
|
|
|
|
|
options: (unsigned)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfString_c((GSStr)self, aString, mask, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) smallestEncoding
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
return intEnc;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-24 11:58:25 +00:00
|
|
|
|
- (NSString*) substringFromRange: (NSRange)aRange
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return substring_c((GSStr)self, aRange);
|
2000-10-24 11:58:25 +00:00
|
|
|
|
}
|
2000-10-20 10:30:51 +00:00
|
|
|
|
|
2000-10-24 11:58:25 +00:00
|
|
|
|
- (NSString*) substringWithRange: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return substring_c((GSStr)self, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// private method for Unicode level 3 implementation
|
|
|
|
|
- (int) _baseLength
|
|
|
|
|
{
|
|
|
|
|
return _count;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
/*
|
|
|
|
|
Default copy implementation. Retain if we own the buffer and the zones
|
|
|
|
|
agree, create a new GSCInlineString otherwise.
|
|
|
|
|
*/
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
if (!_flags.free || NSShouldRetainWithZone(self, z) == NO)
|
|
|
|
|
{
|
|
|
|
|
NSString *obj;
|
|
|
|
|
|
|
|
|
|
obj = (NSString*)NSAllocateObject(GSCInlineStringClass, _count, z);
|
2005-07-08 11:48:37 +00:00
|
|
|
|
obj = [obj initWithCString: (char*)_contents.c length: _count];
|
2003-11-05 02:11:49 +00:00
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return RETAIN(self);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
@implementation GSCBufferString
|
2000-11-08 17:22:50 +00:00
|
|
|
|
- (id) initWithCStringNoCopy: (char*)chars
|
2003-11-05 02:11:49 +00:00
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
2000-11-08 17:22:50 +00:00
|
|
|
|
{
|
2003-11-05 02:11:49 +00:00
|
|
|
|
if (_contents.c != 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"re-initialisation of string"];
|
|
|
|
|
}
|
|
|
|
|
_count = length;
|
2005-07-08 11:48:37 +00:00
|
|
|
|
_contents.c = (unsigned char*)chars;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
_flags.wide = 0;
|
|
|
|
|
if (flag == YES)
|
|
|
|
|
{
|
|
|
|
|
_flags.free = 1;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
2000-11-08 17:22:50 +00:00
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
if (_flags.free && _contents.c != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(_contents.c), _contents.c);
|
|
|
|
|
_contents.c = 0;
|
|
|
|
|
}
|
|
|
|
|
NSDeallocateObject(self);
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSCInlineString
|
2000-10-29 14:52:33 +00:00
|
|
|
|
- (id) initWithCString: (const char*)chars length: (unsigned)length
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
if (_contents.c != 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"re-initialisation of string"];
|
|
|
|
|
}
|
2000-10-29 14:52:33 +00:00
|
|
|
|
_count = length;
|
|
|
|
|
_contents.c = (unsigned char*)&self[1];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
if (_count > 0)
|
2003-05-06 06:54:22 +00:00
|
|
|
|
memcpy(_contents.c, chars, length);
|
2000-10-29 14:52:33 +00:00
|
|
|
|
_flags.wide = 0;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
_flags.free = 1;
|
2000-10-29 14:52:33 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
2003-11-01 11:11:13 +00:00
|
|
|
|
|
2000-10-29 14:52:33 +00:00
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
NSDeallocateObject(self);
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
2000-10-20 10:30:51 +00:00
|
|
|
|
@implementation GSCSubString
|
2003-11-05 02:11:49 +00:00
|
|
|
|
- (id) initWithCString: (char *)chars length: (unsigned)length
|
|
|
|
|
fromParent: (GSCString *)parent
|
|
|
|
|
{
|
|
|
|
|
if (_contents.c != 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"re-initialisation of string"];
|
|
|
|
|
}
|
|
|
|
|
_count = length;
|
|
|
|
|
_contents.c = (unsigned char *)chars;
|
|
|
|
|
_flags.wide = 0;
|
|
|
|
|
_flags.free = 1;
|
|
|
|
|
ASSIGN(_parent, parent);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2000-12-21 11:55:30 +00:00
|
|
|
|
/*
|
|
|
|
|
* Assume that a copy should be a new string, never just a retained substring.
|
|
|
|
|
*/
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
NSString *obj;
|
|
|
|
|
|
|
|
|
|
obj = (NSString*)NSAllocateObject(GSCInlineStringClass, _count, z);
|
2005-07-08 11:48:37 +00:00
|
|
|
|
obj = [obj initWithCString: (char*)_contents.c length: _count];
|
2000-12-21 11:55:30 +00:00
|
|
|
|
return obj;
|
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
2000-10-20 10:30:51 +00:00
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2003-11-05 02:11:49 +00:00
|
|
|
|
DESTROY(_parent);
|
2000-10-29 14:52:33 +00:00
|
|
|
|
NSDeallocateObject(self);
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
@implementation GSUnicodeString
|
2002-10-05 17:47:54 +00:00
|
|
|
|
- (const char *) UTF8String
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return UTF8String_u((GSStr)self);
|
2002-10-05 17:47:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (BOOL) boolValue
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return boolValue_u((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) canBeConvertedToEncoding: (NSStringEncoding)enc
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return canBeConvertedToEncoding_u((GSStr)self, enc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unichar) characterAtIndex: (unsigned int)index
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return characterAtIndex_u((GSStr)self, index);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSComparisonResult) compare: (NSString*)aString
|
|
|
|
|
options: (unsigned int)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return compare_u((GSStr)self, aString, mask, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cString
|
|
|
|
|
{
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cString_u((GSStr)self, defEnc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cStringUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
return cString_u((GSStr)self, encoding);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) cStringLength
|
|
|
|
|
{
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cStringLength_u((GSStr)self, defEnc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSData*) dataUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
allowLossyConversion: (BOOL)flag
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return dataUsingEncoding_u((GSStr)self, encoding, flag);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (double) doubleValue
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return doubleValue_u((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &_count];
|
|
|
|
|
if (_count > 0)
|
|
|
|
|
{
|
|
|
|
|
NSStringEncoding enc = NSUnicodeStringEncoding;
|
|
|
|
|
|
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(NSStringEncoding) at: &enc];
|
|
|
|
|
[aCoder encodeArrayOfObjCType: @encode(unichar)
|
|
|
|
|
count: _count
|
|
|
|
|
at: _contents.u];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) fastestEncoding
|
|
|
|
|
{
|
|
|
|
|
return NSUnicodeStringEncoding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (float) floatValue
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return doubleValue_u((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_u((GSStr)self, buffer, (NSRange){0, _count});
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_u((GSStr)self, buffer, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_u((GSStr)self, buffer, NSMaximumStringLength,
|
2000-10-09 05:32:50 +00:00
|
|
|
|
(NSRange){0, _count}, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_u((GSStr)self, buffer, maxLength, (NSRange){0, _count}, 0);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-02-27 09:35:19 +00:00
|
|
|
|
- (BOOL) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
return getCStringE_u((GSStr)self, buffer, maxLength, encoding);
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
remainingRange: (NSRange*)leftoverRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
|
|
|
|
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_u((GSStr)self, buffer, maxLength, aRange, leftoverRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) hash
|
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
if (self->_flags.hash == 0)
|
|
|
|
|
{
|
|
|
|
|
self->_flags.hash = (*hashImp)((id)self, hashSel);
|
|
|
|
|
}
|
|
|
|
|
return self->_flags.hash;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (int) intValue
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return intValue_u((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)anObject
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return isEqual_u((GSStr)self, anObject);
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqualToString: (NSString*)anObject
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return isEqual_u((GSStr)self, anObject);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) length
|
|
|
|
|
{
|
|
|
|
|
return _count;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-08 07:08:28 +00:00
|
|
|
|
- (unsigned int) lengthOfBytesUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
return cStringLength_u((GSStr)self, encoding);
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (const char*) lossyCString
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return lossyCString_u((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) mutableCopy
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSMutableString *obj;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
obj = (GSMutableString*)NSAllocateObject(GSMutableStringClass, 0,
|
|
|
|
|
NSDefaultMallocZone());
|
2000-10-09 05:32:50 +00:00
|
|
|
|
obj = [obj initWithCharacters: _contents.u length: _count];
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)z
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSMutableString *obj;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
obj = (GSMutableString*)NSAllocateObject(GSMutableStringClass, 0, z);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
obj = [obj initWithCharacters: _contents.u length: _count];
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (unsigned)anIndex
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfSequence_u((GSStr)self, anIndex);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
|
|
|
|
options: (unsigned)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfCharacter_u((GSStr)self, aSet, mask, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (NSRange) rangeOfString: (NSString*)aString
|
|
|
|
|
options: (unsigned)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfString_u((GSStr)self, aString, mask, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) smallestEncoding
|
|
|
|
|
{
|
|
|
|
|
return NSUnicodeStringEncoding;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-24 11:58:25 +00:00
|
|
|
|
- (NSString*) substringFromRange: (NSRange)aRange
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return substring_u((GSStr)self, aRange);
|
2000-10-24 11:58:25 +00:00
|
|
|
|
}
|
2000-10-20 10:30:51 +00:00
|
|
|
|
|
2000-10-24 11:58:25 +00:00
|
|
|
|
- (NSString*) substringWithRange: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return substring_u((GSStr)self, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// private method for Unicode level 3 implementation
|
|
|
|
|
- (int) _baseLength
|
|
|
|
|
{
|
2003-01-03 20:14:47 +00:00
|
|
|
|
unsigned int count = 0;
|
|
|
|
|
unsigned int blen = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
while (count < _count)
|
|
|
|
|
if (!uni_isnonsp(_contents.u[count++]))
|
|
|
|
|
blen++;
|
|
|
|
|
return blen;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
/*
|
|
|
|
|
Default -copy implementation. Retain if we own the buffer and the zones
|
|
|
|
|
agree, create a new GSUnicodeInlineString otherwise.
|
|
|
|
|
*/
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
if (!_flags.free || NSShouldRetainWithZone(self, z) == NO)
|
|
|
|
|
{
|
|
|
|
|
NSString *obj;
|
|
|
|
|
|
|
|
|
|
obj = (NSString*)NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
_count*sizeof(unichar), z);
|
|
|
|
|
obj = [obj initWithCharacters: _contents.u length: _count];
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
else
|
2003-11-05 02:11:49 +00:00
|
|
|
|
{
|
|
|
|
|
return RETAIN(self);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
@implementation GSUnicodeBufferString
|
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar *)chars
|
|
|
|
|
length: (unsigned int)length
|
2000-11-08 17:22:50 +00:00
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
2006-03-26 10:59:57 +00:00
|
|
|
|
BOOL isASCII;
|
|
|
|
|
BOOL isLatin1;
|
|
|
|
|
|
2006-03-26 11:34:47 +00:00
|
|
|
|
if (GSUnicode(chars, length, &isASCII, &isLatin1) != length)
|
2006-03-26 10:59:57 +00:00
|
|
|
|
{
|
|
|
|
|
RELEASE(self);
|
2006-03-28 06:05:04 +00:00
|
|
|
|
if (flag == YES && chars != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
|
|
|
|
}
|
2006-03-26 10:59:57 +00:00
|
|
|
|
return nil; // Invalid data
|
|
|
|
|
}
|
|
|
|
|
if (isASCII == YES
|
|
|
|
|
|| (intEnc == NSISOLatin1StringEncoding && isLatin1 == YES))
|
|
|
|
|
{
|
|
|
|
|
GSStr me;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* OK ... we can do a more compact version
|
|
|
|
|
*/
|
|
|
|
|
me = (GSStr)NSAllocateObject(GSCInlineStringClass, length,
|
|
|
|
|
GSObjCZone(self));
|
|
|
|
|
me->_contents.c = (unsigned char*)&((GSCInlineString*)me)[1];
|
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 0;
|
|
|
|
|
me->_flags.free = 1;
|
|
|
|
|
while (length-- > 0)
|
|
|
|
|
{
|
|
|
|
|
me->_contents.c[length] = (unsigned char)chars[length];
|
|
|
|
|
}
|
|
|
|
|
RELEASE(self);
|
2006-03-28 06:05:04 +00:00
|
|
|
|
if (flag == YES && chars != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
|
|
|
|
}
|
2006-03-26 10:59:57 +00:00
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
if (_contents.u != 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"re-initialisation of string"];
|
|
|
|
|
}
|
|
|
|
|
_count = length;
|
|
|
|
|
_contents.u = chars;
|
|
|
|
|
_flags.wide = 1;
|
|
|
|
|
if (flag == YES)
|
|
|
|
|
{
|
|
|
|
|
_flags.free = 1;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
2000-11-08 17:22:50 +00:00
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
if (_flags.free && _contents.u != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(_contents.u), _contents.u);
|
|
|
|
|
_contents.u = 0;
|
|
|
|
|
}
|
|
|
|
|
NSDeallocateObject(self);
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSUnicodeInlineString
|
2000-10-29 14:52:33 +00:00
|
|
|
|
- (id) initWithCharacters: (const unichar*)chars length: (unsigned)length
|
|
|
|
|
{
|
2006-03-26 10:59:57 +00:00
|
|
|
|
BOOL isASCII;
|
|
|
|
|
BOOL isLatin1;
|
|
|
|
|
|
2006-03-26 11:34:47 +00:00
|
|
|
|
if (GSUnicode(chars, length, &isASCII, &isLatin1) != length)
|
2006-03-26 10:59:57 +00:00
|
|
|
|
{
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return nil; // Invalid data
|
|
|
|
|
}
|
|
|
|
|
if (isASCII == YES
|
|
|
|
|
|| (intEnc == NSISOLatin1StringEncoding && isLatin1 == YES))
|
|
|
|
|
{
|
|
|
|
|
GSStr me;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* OK ... we can do a more compact version
|
|
|
|
|
*/
|
|
|
|
|
me = (GSStr)NSAllocateObject(GSCInlineStringClass, length,
|
|
|
|
|
GSObjCZone(self));
|
|
|
|
|
me->_contents.c = (unsigned char*)&((GSCInlineString*)me)[1];
|
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 0;
|
|
|
|
|
me->_flags.free = 1;
|
|
|
|
|
while (length-- > 0)
|
|
|
|
|
{
|
|
|
|
|
me->_contents.c[length] = (unsigned char)chars[length];
|
|
|
|
|
}
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
if (_contents.u != 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"re-initialisation of string"];
|
|
|
|
|
}
|
2000-10-29 14:52:33 +00:00
|
|
|
|
_count = length;
|
2000-11-08 17:22:50 +00:00
|
|
|
|
_contents.u = (unichar*)&((GSUnicodeInlineString*)self)[1];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
if (_count > 0)
|
|
|
|
|
memcpy(_contents.u, chars, length*sizeof(unichar));
|
2000-10-29 14:52:33 +00:00
|
|
|
|
_flags.wide = 1;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
_flags.free = 1;
|
2000-10-29 14:52:33 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
2003-11-01 11:11:13 +00:00
|
|
|
|
|
2000-10-29 14:52:33 +00:00
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
NSDeallocateObject(self);
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSUnicodeSubString
|
2003-11-05 02:11:49 +00:00
|
|
|
|
- (id) initWithCharacters: (unichar *)chars length: (unsigned)length
|
|
|
|
|
fromParent: (GSUnicodeString *)parent
|
|
|
|
|
{
|
|
|
|
|
if (_contents.u != 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"re-initialisation of string"];
|
|
|
|
|
}
|
|
|
|
|
_count = length;
|
|
|
|
|
_contents.u = chars;
|
|
|
|
|
_flags.wide = 1;
|
|
|
|
|
_flags.free = 1;
|
|
|
|
|
ASSIGN(_parent, parent);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2000-12-21 11:55:30 +00:00
|
|
|
|
/*
|
|
|
|
|
* Assume that a copy should be a new string, never just a retained substring.
|
|
|
|
|
*/
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
NSString *obj;
|
|
|
|
|
|
|
|
|
|
obj = (NSString*)NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
_count*sizeof(unichar), z);
|
|
|
|
|
obj = [obj initWithCharacters: _contents.u length: _count];
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
2000-10-20 10:30:51 +00:00
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2003-11-05 02:11:49 +00:00
|
|
|
|
DESTROY(_parent);
|
2000-10-29 14:52:33 +00:00
|
|
|
|
NSDeallocateObject(self);
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
2003-05-06 06:54:22 +00:00
|
|
|
|
* The GSMutableString class shares a common initial ivar layout with
|
2000-11-03 10:11:56 +00:00
|
|
|
|
* the GSString class, but adds a few of its own. It uses _flags.wide
|
|
|
|
|
* to determine whether it should use 8-bit or 16-bit characters and
|
2002-02-06 13:44:03 +00:00
|
|
|
|
* is capable of changing that flag (and its underlying storage) to
|
2000-11-03 10:11:56 +00:00
|
|
|
|
* move from an 8-bit to a 16-bit representation is that should be
|
|
|
|
|
* necessary because wide characters have been placed in the string.
|
2003-05-06 06:54:22 +00:00
|
|
|
|
* If defEnc!=intEnc, GSMutableString objects will always have
|
|
|
|
|
* _flags.wide set, since ite cannot have been initialised as a C String.
|
2000-11-03 10:11:56 +00:00
|
|
|
|
*/
|
|
|
|
|
@implementation GSMutableString
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
setup();
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-02 06:14:42 +00:00
|
|
|
|
- (void) appendFormat: (NSString*)format, ...
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
2004-05-14 10:52:30 +00:00
|
|
|
|
unichar buf[1024];
|
|
|
|
|
unichar *fmt = buf;
|
|
|
|
|
size_t len;
|
2001-02-02 06:14:42 +00:00
|
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|
2001-02-02 06:14:42 +00:00
|
|
|
|
/*
|
2004-05-14 10:52:30 +00:00
|
|
|
|
* Make sure we have the format string in a nul terminated array of
|
|
|
|
|
* unichars for passing to GSFormat. Use on-stack memory for performance
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* unless the size of the format string is really big (a rare occurrence).
|
2001-02-02 06:14:42 +00:00
|
|
|
|
*/
|
2004-05-14 10:52:30 +00:00
|
|
|
|
len = [format length];
|
|
|
|
|
if (len >= 1024)
|
2001-02-02 06:14:42 +00:00
|
|
|
|
{
|
|
|
|
|
fmt = objc_malloc((len+1)*sizeof(unichar));
|
2004-05-14 10:52:30 +00:00
|
|
|
|
}
|
|
|
|
|
[format getCharacters: fmt];
|
|
|
|
|
fmt[len] = '\0';
|
2003-05-06 06:54:22 +00:00
|
|
|
|
|
2004-05-14 10:52:30 +00:00
|
|
|
|
/*
|
|
|
|
|
* If no zone is set, make sure we have one so any memory mangement
|
|
|
|
|
* (buffer growth) is done with the correct zone.
|
|
|
|
|
*/
|
|
|
|
|
if (_zone == 0)
|
|
|
|
|
{
|
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
_zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
|
|
|
|
_zone = GSObjCZone(self);
|
|
|
|
|
#endif
|
2001-02-02 06:14:42 +00:00
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSFormat((GSStr)self, fmt, ap, nil);
|
|
|
|
|
_flags.hash = 0; // Invalidate the hash for this string.
|
|
|
|
|
if (fmt != buf)
|
2001-02-02 06:14:42 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
objc_free(fmt);
|
2001-02-02 06:14:42 +00:00
|
|
|
|
}
|
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (BOOL) boolValue
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return boolValue_u((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return boolValue_c((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) canBeConvertedToEncoding: (NSStringEncoding)enc
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return canBeConvertedToEncoding_u((GSStr)self, enc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return canBeConvertedToEncoding_c((GSStr)self, enc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unichar) characterAtIndex: (unsigned int)index
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return characterAtIndex_u((GSStr)self, index);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return characterAtIndex_c((GSStr)self, index);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSComparisonResult) compare: (NSString*)aString
|
|
|
|
|
options: (unsigned int)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return compare_u((GSStr)self, aString, mask, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return compare_c((GSStr)self, aString, mask, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
id copy;
|
|
|
|
|
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
copy = (NSString*)NSAllocateObject(GSUnicodeInlineStringClass,
|
2000-10-29 14:52:33 +00:00
|
|
|
|
_count*sizeof(unichar), z);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
copy = [copy initWithCharacters: _contents.u length: _count];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2000-10-29 14:52:33 +00:00
|
|
|
|
copy = (NSString*)NSAllocateObject(GSCInlineStringClass, _count, z);
|
2005-07-08 11:48:37 +00:00
|
|
|
|
copy = [copy initWithCString: (char*)_contents.c length: _count];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
return copy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cString
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cString_u((GSStr)self, defEnc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cString_c((GSStr)self, defEnc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cStringUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
return cString_u((GSStr)self, encoding);
|
|
|
|
|
else
|
|
|
|
|
return cString_c((GSStr)self, encoding);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) cStringLength
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cStringLength_u((GSStr)self, defEnc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cStringLength_c((GSStr)self, defEnc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSData*) dataUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
allowLossyConversion: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return dataUsingEncoding_u((GSStr)self, encoding, flag);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return dataUsingEncoding_c((GSStr)self, encoding, flag);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
if (_flags.free == 1 && _zone != 0 && _contents.c != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(self->_zone, self->_contents.c);
|
|
|
|
|
self->_contents.c = 0;
|
|
|
|
|
self->_zone = 0;
|
|
|
|
|
}
|
2000-10-29 14:52:33 +00:00
|
|
|
|
NSDeallocateObject(self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) deleteCharactersInRange: (NSRange)range
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(range, _count);
|
2000-10-30 19:51:32 +00:00
|
|
|
|
if (range.length > 0)
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
fillHole((GSStr)self, range.location, range.length);
|
2000-10-30 19:51:32 +00:00
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (double) doubleValue
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return doubleValue_u((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return doubleValue_c((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &_count];
|
|
|
|
|
if (_count > 0)
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
|
|
|
|
NSStringEncoding enc = NSUnicodeStringEncoding;
|
|
|
|
|
|
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(NSStringEncoding) at: &enc];
|
|
|
|
|
[aCoder encodeArrayOfObjCType: @encode(unichar)
|
|
|
|
|
count: _count
|
|
|
|
|
at: _contents.u];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(NSStringEncoding) at: &intEnc];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[aCoder encodeArrayOfObjCType: @encode(unsigned char)
|
|
|
|
|
count: _count
|
|
|
|
|
at: _contents.c];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) fastestEncoding
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
return NSUnicodeStringEncoding;
|
|
|
|
|
else
|
2002-03-16 09:54:50 +00:00
|
|
|
|
return intEnc;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (float) floatValue
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return doubleValue_u((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return doubleValue_c((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_u((GSStr)self, buffer, (NSRange){0, _count});
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_c((GSStr)self, buffer, (NSRange){0, _count});
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_u((GSStr)self, buffer, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_c((GSStr)self, buffer, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_u((GSStr)self, buffer, NSMaximumStringLength,
|
2000-10-09 05:32:50 +00:00
|
|
|
|
(NSRange){0, _count}, 0);
|
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_c((GSStr)self, buffer, NSMaximumStringLength,
|
2000-10-09 05:32:50 +00:00
|
|
|
|
(NSRange){0, _count}, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_u((GSStr)self, buffer, maxLength, (NSRange){0, _count}, 0);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_c((GSStr)self, buffer, maxLength, (NSRange){0, _count}, 0);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-02-27 09:35:19 +00:00
|
|
|
|
- (BOOL) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
return getCStringE_u((GSStr)self, buffer, maxLength, encoding);
|
|
|
|
|
else
|
|
|
|
|
return getCStringE_c((GSStr)self, buffer, maxLength, encoding);
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
remainingRange: (NSRange*)leftoverRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_u((GSStr)self, buffer, maxLength, aRange, leftoverRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCString_c((GSStr)self, buffer, maxLength, aRange, leftoverRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) hash
|
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
if (self->_flags.hash == 0)
|
|
|
|
|
{
|
|
|
|
|
self->_flags.hash = (*hashImp)((id)self, hashSel);
|
|
|
|
|
}
|
|
|
|
|
return self->_flags.hash;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
return [self initWithCapacity: 0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCapacity: (unsigned)capacity
|
|
|
|
|
{
|
|
|
|
|
if (capacity < 2)
|
|
|
|
|
{
|
|
|
|
|
capacity = 2;
|
|
|
|
|
}
|
|
|
|
|
_count = 0;
|
|
|
|
|
_capacity = capacity;
|
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
_zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
2000-10-31 16:17:33 +00:00
|
|
|
|
_zone = GSObjCZone(self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
#endif
|
|
|
|
|
_contents.c = NSZoneMalloc(_zone, capacity + 1);
|
|
|
|
|
_flags.wide = 0;
|
|
|
|
|
_flags.free = 1;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
2006-03-26 10:59:57 +00:00
|
|
|
|
BOOL isASCII;
|
|
|
|
|
BOOL isLatin1;
|
|
|
|
|
|
2006-03-26 11:34:47 +00:00
|
|
|
|
if (GSUnicode(chars, length, &isASCII, &isLatin1) != length)
|
2006-03-26 10:59:57 +00:00
|
|
|
|
{
|
|
|
|
|
RELEASE(self);
|
2006-03-28 06:05:04 +00:00
|
|
|
|
if (flag == YES && chars != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
|
|
|
|
}
|
2006-03-26 10:59:57 +00:00
|
|
|
|
return nil; // Invalid data
|
|
|
|
|
}
|
|
|
|
|
if (isASCII == YES
|
|
|
|
|
|| (intEnc == NSISOLatin1StringEncoding && isLatin1 == YES))
|
|
|
|
|
{
|
2006-03-28 06:05:04 +00:00
|
|
|
|
unsigned char *buf;
|
2006-03-26 10:59:57 +00:00
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
_zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
2006-03-28 06:05:04 +00:00
|
|
|
|
_zone = NSDefaultMallocZone();
|
2000-10-09 05:32:50 +00:00
|
|
|
|
#endif
|
2006-03-28 06:05:04 +00:00
|
|
|
|
buf = NSZoneMalloc(_zone, length);
|
|
|
|
|
_count = length;
|
|
|
|
|
_capacity = length;
|
|
|
|
|
_contents.c = buf;
|
|
|
|
|
_flags.wide = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
_flags.free = 1;
|
2006-03-28 06:05:04 +00:00
|
|
|
|
while (length-- > 0)
|
|
|
|
|
{
|
|
|
|
|
buf[length] = (unsigned char)chars[length];
|
|
|
|
|
}
|
|
|
|
|
if (flag == YES && chars != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2006-03-28 06:05:04 +00:00
|
|
|
|
_count = length;
|
|
|
|
|
_capacity = length;
|
|
|
|
|
_contents.u = chars;
|
|
|
|
|
_flags.wide = 1;
|
|
|
|
|
if (flag == YES && chars != 0)
|
|
|
|
|
{
|
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
_zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
|
|
|
|
_zone = NSZoneFromPointer(chars);
|
|
|
|
|
#endif
|
|
|
|
|
_flags.free = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_zone = 0;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-07 13:15:15 +00:00
|
|
|
|
- (id) initWithCStringNoCopy: (char*)chars
|
2000-10-09 05:32:50 +00:00
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
2003-05-07 13:20:15 +00:00
|
|
|
|
if (defEnc != intEnc)
|
2003-05-07 13:15:15 +00:00
|
|
|
|
{
|
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned l = 0;
|
|
|
|
|
|
2005-07-08 11:48:37 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, (unsigned char*)chars, length, defEnc,
|
|
|
|
|
GSObjCZone(self), 0) == NO)
|
2003-05-07 13:15:15 +00:00
|
|
|
|
{
|
|
|
|
|
DESTROY(self);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithCharactersNoCopy: u length: l freeWhenDone: YES];
|
|
|
|
|
}
|
|
|
|
|
if (flag == YES && chars != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flag == YES && chars != 0)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
_zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
2003-05-07 13:15:15 +00:00
|
|
|
|
_zone = NSZoneFromPointer(chars);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
#endif
|
|
|
|
|
_flags.free = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_zone = 0;
|
|
|
|
|
}
|
2003-05-07 13:15:15 +00:00
|
|
|
|
_count = length;
|
|
|
|
|
_capacity = length;
|
2005-07-08 11:48:37 +00:00
|
|
|
|
_contents.c = (unsigned char*)chars;
|
2003-05-07 13:15:15 +00:00
|
|
|
|
_flags.wide = 0;
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-05 10:06:56 +00:00
|
|
|
|
- (id) initWithFormat: (NSString*)format
|
|
|
|
|
locale: (NSDictionary*)locale
|
|
|
|
|
arguments: (va_list)argList
|
|
|
|
|
{
|
|
|
|
|
unichar fbuf[1024];
|
|
|
|
|
unichar *fmt = fbuf;
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* First we provide an array of unichar characters containing the
|
|
|
|
|
* format string. For performance reasons we try to use an on-stack
|
|
|
|
|
* buffer if the format string is small enough ... it almost always
|
|
|
|
|
* will be.
|
|
|
|
|
*/
|
|
|
|
|
len = [format length];
|
|
|
|
|
if (len >= 1024)
|
|
|
|
|
{
|
|
|
|
|
fmt = objc_malloc((len+1)*sizeof(unichar));
|
|
|
|
|
}
|
|
|
|
|
[format getCharacters: fmt];
|
|
|
|
|
fmt[len] = '\0';
|
|
|
|
|
|
|
|
|
|
GSFormat((GSStr)self, fmt, argList, locale);
|
2004-08-19 14:50:44 +00:00
|
|
|
|
if (fmt != fbuf)
|
|
|
|
|
{
|
|
|
|
|
objc_free(fmt);
|
|
|
|
|
}
|
2004-06-05 10:06:56 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (int) intValue
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return intValue_u((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return intValue_c((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)anObject
|
|
|
|
|
{
|
2000-10-20 10:30:51 +00:00
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return isEqual_u((GSStr)self, anObject);
|
2000-10-20 10:30:51 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return isEqual_c((GSStr)self, anObject);
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqualToString: (NSString*)anObject
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return isEqual_u((GSStr)self, anObject);
|
2000-10-20 10:30:51 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return isEqual_c((GSStr)self, anObject);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) length
|
|
|
|
|
{
|
|
|
|
|
return _count;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-08 07:08:28 +00:00
|
|
|
|
- (unsigned int) lengthOfBytesUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
return cStringLength_u((GSStr)self, encoding);
|
|
|
|
|
else
|
|
|
|
|
return cStringLength_c((GSStr)self, encoding);
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (const char*) lossyCString
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return lossyCString_u((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return lossyCString_c((GSStr)self);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-10-14 08:52:39 +00:00
|
|
|
|
- (id) makeImmutableCopyOnFail: (BOOL)force
|
|
|
|
|
{
|
2002-11-10 13:56:34 +00:00
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
GSDebugAllocationRemove(isa, self);
|
|
|
|
|
#endif
|
2002-11-07 21:07:19 +00:00
|
|
|
|
if (_flags.wide == 1)
|
2002-11-10 13:56:34 +00:00
|
|
|
|
{
|
2003-11-05 02:11:49 +00:00
|
|
|
|
isa = [GSUnicodeBufferString class];
|
2002-11-10 13:56:34 +00:00
|
|
|
|
}
|
2002-11-07 21:07:19 +00:00
|
|
|
|
else
|
2002-11-10 13:56:34 +00:00
|
|
|
|
{
|
2003-11-05 02:11:49 +00:00
|
|
|
|
isa = [GSCBufferString class];
|
2002-11-10 13:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
GSDebugAllocationAdd(isa, self);
|
|
|
|
|
#endif
|
2002-10-14 08:52:39 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (id) mutableCopy
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSMutableString *obj;
|
|
|
|
|
|
|
|
|
|
obj = (GSMutableString*)NSAllocateObject(GSMutableStringClass, 0,
|
|
|
|
|
NSDefaultMallocZone());
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
obj = [obj initWithCharacters: _contents.u length: _count];
|
|
|
|
|
else
|
2005-07-08 11:48:37 +00:00
|
|
|
|
obj = [obj initWithCString: (char*)_contents.c length: _count];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)z
|
|
|
|
|
{
|
2000-11-03 10:11:56 +00:00
|
|
|
|
GSMutableString *obj;
|
|
|
|
|
|
|
|
|
|
obj = (GSMutableString*)NSAllocateObject(GSMutableStringClass, 0, z);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
obj = [obj initWithCharacters: _contents.u length: _count];
|
|
|
|
|
else
|
2005-07-08 11:48:37 +00:00
|
|
|
|
obj = [obj initWithCString: (char*)_contents.c length: _count];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (unsigned)anIndex
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfSequence_u((GSStr)self, anIndex);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfSequence_c((GSStr)self, anIndex);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
|
|
|
|
options: (unsigned)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfCharacter_u((GSStr)self, aSet, mask, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfCharacter_c((GSStr)self, aSet, mask, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (NSRange) rangeOfString: (NSString*)aString
|
|
|
|
|
options: (unsigned)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfString_u((GSStr)self, aString, mask, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfString_c((GSStr)self, aString, mask, aRange);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) replaceCharactersInRange: (NSRange)aRange
|
|
|
|
|
withString: (NSString*)aString
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr other = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
int offset;
|
2001-03-03 14:49:11 +00:00
|
|
|
|
unsigned length = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2001-03-03 14:49:11 +00:00
|
|
|
|
if (aString != nil)
|
2001-02-08 17:12:40 +00:00
|
|
|
|
{
|
2001-03-03 14:49:11 +00:00
|
|
|
|
if (GSObjCIsInstance(aString) == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"replace characters with non-string"];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
length = (aString == nil) ? 0 : [aString length];
|
|
|
|
|
}
|
2001-02-08 17:12:40 +00:00
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
offset = length - aRange.length;
|
|
|
|
|
|
2001-06-05 08:21:09 +00:00
|
|
|
|
/*
|
|
|
|
|
* We must change into a unicode string (if necessary) *before*
|
|
|
|
|
* adjusting length and capacity, so that the transmute doesn't
|
|
|
|
|
* mess up due to any hole in the string etc.
|
|
|
|
|
*/
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
other = transmute((GSStr)self, aString);
|
2001-06-05 08:21:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
if (offset < 0)
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
fillHole((GSStr)self, NSMaxRange(aRange) + offset, -offset);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else if (offset > 0)
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
makeHole((GSStr)self, NSMaxRange(aRange), (unsigned int)offset);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
|
|
|
|
if (other == 0)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Not a cString class - use standard method to get characters.
|
|
|
|
|
*/
|
|
|
|
|
[aString getCharacters: &_contents.u[aRange.location]];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
memcpy(&_contents.u[aRange.location], other->_contents.u,
|
|
|
|
|
length * sizeof(unichar));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (other == 0)
|
|
|
|
|
{
|
|
|
|
|
/*
|
2003-05-05 13:36:25 +00:00
|
|
|
|
* As we got here, intEnc == defEnc, so we can use standard
|
|
|
|
|
* CString methods to get the characters into our buffer,
|
|
|
|
|
* or may even be able to copy from another string directly.
|
|
|
|
|
*
|
2005-02-22 11:22:44 +00:00
|
|
|
|
* Since getCString appends a '\0' terminator, we must handle
|
2002-05-11 07:50:47 +00:00
|
|
|
|
* that problem in copying data into our buffer. Either by
|
|
|
|
|
* saving and restoring the character which would be
|
|
|
|
|
* overwritten by the nul, or by getting a character less,
|
|
|
|
|
* and fetching the last character separately.
|
2000-10-09 05:32:50 +00:00
|
|
|
|
*/
|
2002-05-11 07:50:47 +00:00
|
|
|
|
if (aRange.location + length < _count)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2002-05-11 07:50:47 +00:00
|
|
|
|
unsigned char tmp = _contents.c[aRange.location + length];
|
|
|
|
|
|
2005-07-08 11:48:37 +00:00
|
|
|
|
[aString getCString: (char*)&_contents.c[aRange.location]
|
2002-05-11 07:50:47 +00:00
|
|
|
|
maxLength: length];
|
|
|
|
|
_contents.c[aRange.location + length] = tmp;
|
|
|
|
|
}
|
|
|
|
|
else
|
2005-02-22 11:22:44 +00:00
|
|
|
|
{
|
2002-05-11 07:50:47 +00:00
|
|
|
|
unsigned int l = length - 1;
|
|
|
|
|
unsigned int size = 1;
|
|
|
|
|
unichar u;
|
|
|
|
|
unsigned char *dst = &_contents.c[aRange.location + l];
|
|
|
|
|
|
|
|
|
|
if (l > 0)
|
|
|
|
|
{
|
2005-07-08 11:48:37 +00:00
|
|
|
|
[aString getCString: (char*)&_contents.c[aRange.location]
|
2002-05-11 07:50:47 +00:00
|
|
|
|
maxLength: l];
|
|
|
|
|
}
|
|
|
|
|
u = [aString characterAtIndex: l];
|
|
|
|
|
GSFromUnicode(&dst, &size, &u, 1, intEnc, 0, 0);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Simply copy cString data from other string into self.
|
|
|
|
|
*/
|
|
|
|
|
memcpy(&_contents.c[aRange.location], other->_contents.c, length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_flags.hash = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setString: (NSString*)aString
|
|
|
|
|
{
|
2003-01-03 20:14:47 +00:00
|
|
|
|
unsigned int len = (aString == nil) ? 0 : [aString length];
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr other;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
|
{
|
|
|
|
|
_count = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
other = transmute((GSStr)self, aString);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
if (_count < len)
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
makeHole((GSStr)self, _count, (unsigned int)(len - _count));
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_count = len;
|
|
|
|
|
_flags.hash = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
|
|
|
|
if (other == 0)
|
|
|
|
|
{
|
|
|
|
|
[aString getCharacters: _contents.u];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
memcpy(_contents.u, other->_contents.u, len * sizeof(unichar));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (other == 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned l;
|
|
|
|
|
|
|
|
|
|
/*
|
2003-05-05 13:36:25 +00:00
|
|
|
|
* transmute() will leave us non-wide and return other == 0,
|
|
|
|
|
* only if intEnc == defEnc, so we get use the cstring methods
|
|
|
|
|
* to copy directly to our buffer.
|
|
|
|
|
*
|
2000-10-09 05:32:50 +00:00
|
|
|
|
* Since getCString appends a '\0' terminator, we must ask for
|
|
|
|
|
* one character less than we actually want, then get the last
|
|
|
|
|
* character separately.
|
|
|
|
|
*/
|
|
|
|
|
l = len - 1;
|
|
|
|
|
if (l > 0)
|
|
|
|
|
{
|
2005-07-08 11:48:37 +00:00
|
|
|
|
[aString getCString: (char*)_contents.c maxLength: l];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
_contents.c[l]
|
2002-03-16 09:54:50 +00:00
|
|
|
|
= encode_unitochar([aString characterAtIndex: l], intEnc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
memcpy(_contents.c, other->_contents.c, len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) smallestEncoding
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
2000-10-23 06:18:03 +00:00
|
|
|
|
return NSUnicodeStringEncoding;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2002-03-16 09:54:50 +00:00
|
|
|
|
return intEnc;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-24 11:58:25 +00:00
|
|
|
|
- (NSString*) substringFromRange: (NSRange)aRange
|
|
|
|
|
{
|
2000-11-06 14:15:27 +00:00
|
|
|
|
NSString *sub;
|
|
|
|
|
|
2000-10-24 11:58:25 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2000-11-06 14:15:27 +00:00
|
|
|
|
|
2000-10-24 11:58:25 +00:00
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
2000-11-06 14:15:27 +00:00
|
|
|
|
sub = (NSString*)NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
_count*sizeof(unichar), NSDefaultMallocZone());
|
2000-11-08 17:22:50 +00:00
|
|
|
|
sub = [sub initWithCharacters: self->_contents.u + aRange.location
|
|
|
|
|
length: aRange.length];
|
2000-10-24 11:58:25 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2000-11-06 14:15:27 +00:00
|
|
|
|
sub = (NSString*)NSAllocateObject(GSCInlineStringClass,
|
|
|
|
|
_count, NSDefaultMallocZone());
|
2005-07-08 11:48:37 +00:00
|
|
|
|
sub = [sub initWithCString: (char*)self->_contents.c + aRange.location
|
2000-11-08 17:22:50 +00:00
|
|
|
|
length: aRange.length];
|
2000-10-24 11:58:25 +00:00
|
|
|
|
}
|
2000-11-06 14:15:27 +00:00
|
|
|
|
AUTORELEASE(sub);
|
|
|
|
|
return sub;
|
2000-10-24 11:58:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-23 23:18:44 +00:00
|
|
|
|
- (NSString*) substringWithRange: (NSRange)aRange
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2000-11-06 14:15:27 +00:00
|
|
|
|
NSString *sub;
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2000-11-06 14:15:27 +00:00
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
if (_flags.wide == 1)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
2000-11-06 14:15:27 +00:00
|
|
|
|
sub = (NSString*)NSAllocateObject(GSUnicodeInlineStringClass,
|
2002-02-27 15:15:45 +00:00
|
|
|
|
(aRange.length)*sizeof(unichar),
|
|
|
|
|
NSDefaultMallocZone());
|
2000-11-08 17:22:50 +00:00
|
|
|
|
sub = [sub initWithCharacters: self->_contents.u + aRange.location
|
|
|
|
|
length: aRange.length];
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
2000-11-06 14:15:27 +00:00
|
|
|
|
sub = (NSString*)NSAllocateObject(GSCInlineStringClass,
|
2005-02-22 11:22:44 +00:00
|
|
|
|
aRange.length,
|
2002-02-27 15:15:45 +00:00
|
|
|
|
NSDefaultMallocZone());
|
2005-07-08 11:48:37 +00:00
|
|
|
|
sub = [sub initWithCString: (char*)self->_contents.c + aRange.location
|
2000-11-08 17:22:50 +00:00
|
|
|
|
length: aRange.length];
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
2000-11-06 14:15:27 +00:00
|
|
|
|
AUTORELEASE(sub);
|
|
|
|
|
return sub;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// private method for Unicode level 3 implementation
|
|
|
|
|
- (int) _baseLength
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
2003-01-03 20:14:47 +00:00
|
|
|
|
unsigned int count = 0;
|
|
|
|
|
unsigned int blen = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
while (count < _count)
|
|
|
|
|
if (!uni_isnonsp(_contents.u[count++]))
|
|
|
|
|
blen++;
|
|
|
|
|
return blen;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return _count;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
@interface NSImmutableString: NSString
|
|
|
|
|
{
|
|
|
|
|
id _parent;
|
|
|
|
|
}
|
|
|
|
|
- (id) initWithString: (NSString*)parent;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface GSImmutableString: NSImmutableString
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation NSImmutableString
|
|
|
|
|
|
|
|
|
|
- (BOOL) canBeConvertedToEncoding: (NSStringEncoding)enc
|
|
|
|
|
{
|
|
|
|
|
return [_parent canBeConvertedToEncoding: enc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unichar) characterAtIndex: (unsigned int)index
|
|
|
|
|
{
|
|
|
|
|
return [_parent characterAtIndex: index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSComparisonResult) compare: (NSString*)aString
|
|
|
|
|
options: (unsigned int)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
return [_parent compare: aString options: mask range: aRange];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cString
|
|
|
|
|
{
|
|
|
|
|
return [_parent cString];
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-08 07:08:28 +00:00
|
|
|
|
- (const char *) cStringUsingEncoding
|
|
|
|
|
{
|
|
|
|
|
return [_parent cStringUsingEncoding];
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
- (unsigned int) cStringLength
|
|
|
|
|
{
|
|
|
|
|
return [_parent cStringLength];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSData*) dataUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
allowLossyConversion: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
return [_parent dataUsingEncoding: encoding allowLossyConversion: flag];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
RELEASE(_parent);
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-11 17:16:24 +00:00
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
return [_parent copyWithZone: z];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) mutableCopy
|
|
|
|
|
{
|
|
|
|
|
return [_parent mutableCopy];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
return [_parent mutableCopyWithZone: z];
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
[_parent encodeWithCoder: aCoder];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) fastestEncoding
|
|
|
|
|
{
|
|
|
|
|
return [_parent fastestEncoding];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
|
|
|
|
{
|
2003-08-20 12:13:34 +00:00
|
|
|
|
[_parent getCharacters: buffer];
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer range: (NSRange)aRange
|
|
|
|
|
{
|
2003-08-20 12:13:34 +00:00
|
|
|
|
[_parent getCharacters: buffer range: aRange];
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
{
|
|
|
|
|
[_parent getCString: buffer];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
{
|
|
|
|
|
[_parent getCString: buffer maxLength: maxLength];
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-27 09:35:19 +00:00
|
|
|
|
- (BOOL) getCString: (char*)buffer
|
2005-05-08 07:08:28 +00:00
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
2006-02-27 09:35:19 +00:00
|
|
|
|
return [_parent getCString: buffer maxLength: maxLength encoding: encoding];
|
2005-05-08 07:08:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
remainingRange: (NSRange*)leftoverRange
|
|
|
|
|
{
|
|
|
|
|
[_parent getCString: buffer
|
|
|
|
|
maxLength: maxLength
|
|
|
|
|
range: aRange
|
|
|
|
|
remainingRange: leftoverRange];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) hash
|
|
|
|
|
{
|
|
|
|
|
return [_parent hash];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithString: (NSString*)parent
|
|
|
|
|
{
|
|
|
|
|
_parent = RETAIN(parent);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
return [_parent isEqual: anObject];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqualToString: (NSString*)anObject
|
|
|
|
|
{
|
|
|
|
|
return [_parent isEqualToString: anObject];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) length
|
|
|
|
|
{
|
|
|
|
|
return [_parent length];
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-08 07:08:28 +00:00
|
|
|
|
- (unsigned int) lengthOfBytesUsingEncoding
|
|
|
|
|
{
|
|
|
|
|
return [_parent lengthOfBytesUsingEncoding];
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
- (const char*) lossyCString
|
|
|
|
|
{
|
|
|
|
|
return [_parent lossyCString];
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-08 07:08:28 +00:00
|
|
|
|
- (unsigned int) maximumLengthOfBytesUsingEncoding
|
|
|
|
|
{
|
|
|
|
|
return [_parent maximumLengthOfBytesUsingEncoding];
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (unsigned)anIndex
|
|
|
|
|
{
|
|
|
|
|
return [_parent rangeOfComposedCharacterSequenceAtIndex: anIndex];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
|
|
|
|
options: (unsigned)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
return [_parent rangeOfCharacterFromSet: aSet options: mask range: aRange];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSRange) rangeOfString: (NSString*)aString
|
|
|
|
|
options: (unsigned)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
return [_parent rangeOfString: aString options: mask range: aRange];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) smallestEncoding
|
|
|
|
|
{
|
|
|
|
|
return [_parent smallestEncoding];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSImmutableString
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
setup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) canBeConvertedToEncoding: (NSStringEncoding)enc
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return canBeConvertedToEncoding_u((GSStr)_parent, enc);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return canBeConvertedToEncoding_c((GSStr)_parent, enc);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unichar) characterAtIndex: (unsigned int)index
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return characterAtIndex_u((GSStr)_parent, index);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return characterAtIndex_c((GSStr)_parent, index);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSComparisonResult) compare: (NSString*)aString
|
|
|
|
|
options: (unsigned int)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return compare_u((GSStr)_parent, aString, mask, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return compare_c((GSStr)_parent, aString, mask, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cString
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cString_u((GSStr)_parent, defEnc);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cString_c((GSStr)_parent, defEnc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cStringUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return cString_u((GSStr)_parent, encoding);
|
|
|
|
|
else
|
|
|
|
|
return cString_c((GSStr)_parent, encoding);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) cStringLength
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cStringLength_u((GSStr)_parent, defEnc);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2005-05-08 07:08:28 +00:00
|
|
|
|
return cStringLength_c((GSStr)_parent, defEnc);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSData*) dataUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
allowLossyConversion: (BOOL)flag
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return dataUsingEncoding_u((GSStr)_parent, encoding, flag);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return dataUsingEncoding_c((GSStr)_parent, encoding, flag);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
[_parent encodeWithCoder: aCoder];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) fastestEncoding
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
2002-02-06 13:44:03 +00:00
|
|
|
|
return NSUnicodeStringEncoding;
|
|
|
|
|
else
|
2002-03-16 09:54:50 +00:00
|
|
|
|
return intEnc;
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
2002-02-06 13:44:03 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_u((GSStr)_parent, buffer,
|
|
|
|
|
(NSRange){0, ((GSStr)_parent)->_count});
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_c((GSStr)_parent, buffer,
|
|
|
|
|
(NSRange){0, ((GSStr)_parent)->_count});
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer range: (NSRange)aRange
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, ((GSStr)_parent)->_count);
|
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
2002-02-06 13:44:03 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_u((GSStr)_parent, buffer, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
getCharacters_c((GSStr)_parent, buffer, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) hash
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.hash == 0)
|
2002-02-06 13:44:03 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
((GSStr)_parent)->_flags.hash = (*hashImp)((id)_parent, hashSel);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return ((GSStr)_parent)->_flags.hash;
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)anObject
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return isEqual_u((GSStr)_parent, anObject);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return isEqual_c((GSStr)_parent, anObject);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqualToString: (NSString*)anObject
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return isEqual_u((GSStr)_parent, anObject);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return isEqual_c((GSStr)_parent, anObject);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) length
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return ((GSStr)_parent)->_count;
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-05-08 07:08:28 +00:00
|
|
|
|
- (unsigned int) lengthOfBytesUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return cStringLength_u((GSStr)_parent, encoding);
|
|
|
|
|
else
|
|
|
|
|
return cStringLength_c((GSStr)_parent, encoding);
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
- (const char*) lossyCString
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return lossyCString_u((GSStr)_parent);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return lossyCString_c((GSStr)_parent);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-05-08 07:08:28 +00:00
|
|
|
|
- (unsigned int) maximumLengthOfBytesUsingEncoding
|
|
|
|
|
{
|
|
|
|
|
return [_parent maximumLengthOfBytesUsingEncoding];
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 13:44:03 +00:00
|
|
|
|
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (unsigned)anIndex
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return rangeOfSequence_u((GSStr)_parent, anIndex);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfSequence_c((GSStr)_parent, anIndex);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSRange) rangeOfCharacterFromSet: (NSCharacterSet*)aSet
|
|
|
|
|
options: (unsigned)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, ((GSStr)_parent)->_count);
|
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return rangeOfCharacter_u((GSStr)_parent, aSet, mask, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfCharacter_c((GSStr)_parent, aSet, mask, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSRange) rangeOfString: (NSString*)aString
|
|
|
|
|
options: (unsigned)mask
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
|
|
|
|
return rangeOfString_u((GSStr)_parent, aString, mask, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
else
|
2004-05-14 10:52:30 +00:00
|
|
|
|
return rangeOfString_c((GSStr)_parent, aString, mask, aRange);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) smallestEncoding
|
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
if (((GSStr)_parent)->_flags.wide == 1)
|
2002-02-06 13:44:03 +00:00
|
|
|
|
{
|
|
|
|
|
return NSUnicodeStringEncoding;
|
|
|
|
|
}
|
|
|
|
|
else
|
2002-03-16 09:54:50 +00:00
|
|
|
|
return intEnc;
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-03-13 09:58:43 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>The NXConstantString class is used by the compiler for constant
|
2000-11-03 10:11:56 +00:00
|
|
|
|
* strings, as such its ivar layout is determined by the compiler
|
|
|
|
|
* and consists of a pointer (_contents.c) and a character count
|
|
|
|
|
* (_count). So, while this class inherits GSCString behavior,
|
2004-05-14 10:52:30 +00:00
|
|
|
|
* the code must make sure not to use any other GSCString GSStr
|
2002-03-13 09:58:43 +00:00
|
|
|
|
* when accesssing an NXConstantString.</p>
|
2000-11-03 10:11:56 +00:00
|
|
|
|
*/
|
2000-10-09 05:32:50 +00:00
|
|
|
|
@implementation NXConstantString
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [NXConstantString class])
|
|
|
|
|
{
|
2002-11-27 12:52:29 +00:00
|
|
|
|
GSObjCAddClassBehavior(self, [GSCString class]);
|
2002-10-02 13:29:54 +00:00
|
|
|
|
NSConstantStringClass = self;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
/*
|
2002-05-29 16:32:27 +00:00
|
|
|
|
* Access instance variables of NXConstantString class consistently
|
2002-03-13 13:46:12 +00:00
|
|
|
|
* with other concrete NSString subclasses.
|
|
|
|
|
*/
|
2004-05-14 10:52:30 +00:00
|
|
|
|
#define _self ((GSStr)self)
|
2002-03-13 13:46:12 +00:00
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (id) initWithCharacters: (unichar*)byteString
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException
|
2002-05-29 16:32:27 +00:00
|
|
|
|
format: @"Attempt to init a constant string"];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCStringNoCopy: (char*)byteString
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException
|
2002-05-29 16:32:27 +00:00
|
|
|
|
format: @"Attempt to init a constant string"];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char*) cString
|
|
|
|
|
{
|
2005-07-08 11:48:37 +00:00
|
|
|
|
return (char*)_self->_contents.c;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) retain
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (oneway void) release
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) autorelease
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSZone*) zone
|
|
|
|
|
{
|
|
|
|
|
return NSDefaultMallocZone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) fastestEncoding
|
|
|
|
|
{
|
|
|
|
|
return NSASCIIStringEncoding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) smallestEncoding
|
|
|
|
|
{
|
|
|
|
|
return NSASCIIStringEncoding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Return a 28-bit hash value for the string contents - this
|
|
|
|
|
* MUST match the algorithm used by the NSString base class.
|
|
|
|
|
*/
|
|
|
|
|
- (unsigned) hash
|
|
|
|
|
{
|
2004-03-31 21:41:28 +00:00
|
|
|
|
unsigned ret = 0;
|
|
|
|
|
unsigned len = _self->_count;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2004-03-31 21:41:28 +00:00
|
|
|
|
if (len > 0)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
const unsigned char *p;
|
|
|
|
|
unsigned char_count = 0;
|
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
p = _self->_contents.c;
|
2004-03-31 21:41:28 +00:00
|
|
|
|
while (char_count++ < len)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
unichar c = *p++;
|
|
|
|
|
|
|
|
|
|
if (c > 127)
|
|
|
|
|
{
|
2002-03-16 09:54:50 +00:00
|
|
|
|
c = encode_chartouni(c, intEnc);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
ret = (ret << 5) + ret + c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2004-05-03 20:16:37 +00:00
|
|
|
|
ret &= 0x0fffffff;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
if (ret == 0)
|
2004-03-31 21:41:28 +00:00
|
|
|
|
{
|
|
|
|
|
ret = 0x0fffffff;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret = 0x0ffffffe; /* Hash for an empty string. */
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
Class c;
|
|
|
|
|
|
|
|
|
|
if (anObject == self)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
if (anObject == nil)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsInstance(anObject) == NO)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
c = GSObjCClass(anObject);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSCStringClass) == YES
|
2001-06-06 15:18:28 +00:00
|
|
|
|
|| c == NSConstantStringClass
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)anObject)->_flags.wide == 0))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr other = (GSStr)anObject;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
if (_self->_count != other->_count)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return NO;
|
2002-03-13 13:46:12 +00:00
|
|
|
|
if (memcmp(_self->_contents.c, other->_contents.c, _self->_count) != 0)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return NO;
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
|
|
|
|
|| c == GSMutableStringClass)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2002-03-13 13:46:12 +00:00
|
|
|
|
if (strCompCsUs(self, anObject, 0, (NSRange){0,_self->_count})
|
|
|
|
|
== NSOrderedSame)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, NSStringClass))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
return (*equalImp)(self, equalSel, anObject);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqualToString: (NSString*)anObject
|
|
|
|
|
{
|
|
|
|
|
Class c;
|
|
|
|
|
|
|
|
|
|
if (anObject == self)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
if (anObject == nil)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsInstance(anObject) == NO)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
c = GSObjCClass(anObject);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSCStringClass) == YES
|
2001-06-06 15:18:28 +00:00
|
|
|
|
|| c == NSConstantStringClass
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)anObject)->_flags.wide == 0))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2004-05-14 10:52:30 +00:00
|
|
|
|
GSStr other = (GSStr)anObject;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
if (_self->_count != other->_count)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return NO;
|
2002-03-13 13:46:12 +00:00
|
|
|
|
if (memcmp(_self->_contents.c, other->_contents.c, _self->_count) != 0)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return NO;
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
|
|
|
|
|| c == GSMutableStringClass)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2002-03-13 13:46:12 +00:00
|
|
|
|
if (strCompCsUs(self, anObject, 0, (NSRange){0,_self->_count})
|
|
|
|
|
== NSOrderedSame)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
|
else if (GSObjCIsKindOf(c, NSStringClass))
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
return (*equalImp)(self, equalSel, anObject);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Some classes for backward compatibility with archives.
|
|
|
|
|
*/
|
|
|
|
|
@interface NSGCString : NSString
|
|
|
|
|
@end
|
|
|
|
|
@implementation NSGCString
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
unsigned count;
|
|
|
|
|
|
2000-11-10 03:01:45 +00:00
|
|
|
|
NSLog(@"Warning - decoding archive containing obsolete %@ object - please delete/replace this archive", NSStringFromClass([self class]));
|
2000-10-09 05:32:50 +00:00
|
|
|
|
RELEASE(self);
|
2003-11-05 02:11:49 +00:00
|
|
|
|
self = (id)NSAllocateObject(GSCBufferStringClass, 0, NSDefaultMallocZone());
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(unsigned) at: &count];
|
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned char *chars;
|
|
|
|
|
|
|
|
|
|
chars = NSZoneMalloc(NSDefaultMallocZone(), count+1);
|
|
|
|
|
[aCoder decodeArrayOfObjCType: @encode(unsigned char)
|
|
|
|
|
count: count
|
|
|
|
|
at: chars];
|
2005-07-08 11:48:37 +00:00
|
|
|
|
self = [self initWithCStringNoCopy: (char*)chars
|
2000-10-09 05:32:50 +00:00
|
|
|
|
length: count
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-07-08 11:48:37 +00:00
|
|
|
|
self = [self initWithCStringNoCopy: (char*)0 length: 0 freeWhenDone: NO];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface NSGMutableCString : NSMutableString
|
|
|
|
|
@end
|
|
|
|
|
@implementation NSGMutableCString
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
unsigned count;
|
|
|
|
|
|
2000-11-10 03:01:45 +00:00
|
|
|
|
NSLog(@"Warning - decoding archive containing obsolete %@ object - please delete/replace this archive", NSStringFromClass([self class]));
|
2000-10-09 05:32:50 +00:00
|
|
|
|
RELEASE(self);
|
2000-11-10 03:01:45 +00:00
|
|
|
|
self = (id)NSAllocateObject(GSMutableStringClass, 0, NSDefaultMallocZone());
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(unsigned) at: &count];
|
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned char *chars;
|
|
|
|
|
|
|
|
|
|
chars = NSZoneMalloc(NSDefaultMallocZone(), count+1);
|
|
|
|
|
[aCoder decodeArrayOfObjCType: @encode(unsigned char)
|
|
|
|
|
count: count
|
|
|
|
|
at: chars];
|
2005-07-08 11:48:37 +00:00
|
|
|
|
self = [self initWithCStringNoCopy: (char*)chars
|
2000-10-09 05:32:50 +00:00
|
|
|
|
length: count
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-07-08 11:48:37 +00:00
|
|
|
|
self = [self initWithCStringNoCopy: (char*)0 length: 0 freeWhenDone: NO];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface NSGString : NSString
|
|
|
|
|
@end
|
|
|
|
|
@implementation NSGString
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
unsigned count;
|
|
|
|
|
|
2000-11-10 03:01:45 +00:00
|
|
|
|
NSLog(@"Warning - decoding archive containing obsolete %@ object - please delete/replace this archive", NSStringFromClass([self class]));
|
2000-10-09 05:32:50 +00:00
|
|
|
|
RELEASE(self);
|
2003-11-05 02:11:49 +00:00
|
|
|
|
self = (id)NSAllocateObject(GSUnicodeBufferStringClass, 0, NSDefaultMallocZone());
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(unsigned) at: &count];
|
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
unichar *chars;
|
|
|
|
|
|
|
|
|
|
chars = NSZoneMalloc(NSDefaultMallocZone(), count*sizeof(unichar));
|
|
|
|
|
[aCoder decodeArrayOfObjCType: @encode(unichar)
|
|
|
|
|
count: count
|
|
|
|
|
at: chars];
|
|
|
|
|
self = [self initWithCharactersNoCopy: chars
|
|
|
|
|
length: count
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithCharactersNoCopy: 0 length: 0 freeWhenDone: NO];
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface NSGMutableString : NSMutableString
|
|
|
|
|
@end
|
|
|
|
|
@implementation NSGMutableString
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
unsigned count;
|
|
|
|
|
|
2000-11-10 03:01:45 +00:00
|
|
|
|
NSLog(@"Warning - decoding archive containing obsolete %@ object - please delete/replace this archive", NSStringFromClass([self class]));
|
2000-10-09 05:32:50 +00:00
|
|
|
|
RELEASE(self);
|
2000-11-10 03:01:45 +00:00
|
|
|
|
self = (id)NSAllocateObject(GSMutableStringClass, 0, NSDefaultMallocZone());
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(unsigned) at: &count];
|
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
unichar *chars;
|
|
|
|
|
|
|
|
|
|
chars = NSZoneMalloc(NSDefaultMallocZone(), count*sizeof(unichar));
|
|
|
|
|
[aCoder decodeArrayOfObjCType: @encode(unichar)
|
|
|
|
|
count: count
|
|
|
|
|
at: chars];
|
|
|
|
|
self = [self initWithCharactersNoCopy: chars
|
|
|
|
|
length: count
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithCharactersNoCopy: 0 length: 0 freeWhenDone: NO];
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2004-05-14 10:52:30 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Append characters to a string.
|
|
|
|
|
*/
|
|
|
|
|
void GSStrAppendUnichars(GSStr s, const unichar *u, unsigned l)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Make the string wide if necessary.
|
|
|
|
|
*/
|
|
|
|
|
if (s->_flags.wide == 0)
|
|
|
|
|
{
|
|
|
|
|
BOOL widen = NO;
|
|
|
|
|
|
|
|
|
|
if (intEnc == NSISOLatin1StringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
if (u[i] > 255)
|
|
|
|
|
{
|
|
|
|
|
widen = YES;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
if (u[i] > 127)
|
|
|
|
|
{
|
|
|
|
|
widen = YES;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (widen == YES)
|
|
|
|
|
{
|
|
|
|
|
GSStrWiden(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Make room for the characters we are appending.
|
|
|
|
|
*/
|
|
|
|
|
if (s->_count + l + 1 >= s->_capacity)
|
|
|
|
|
{
|
|
|
|
|
GSStrMakeSpace(s, l);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copy the characters into place.
|
|
|
|
|
*/
|
|
|
|
|
if (s->_flags.wide == 1)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
s->_contents.u[s->_count++] = u[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
s->_contents.c[s->_count++] = u[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GSStrAppendUnichar(GSStr s, unichar u)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Make the string wide if necessary.
|
|
|
|
|
*/
|
|
|
|
|
if (s->_flags.wide == 0)
|
|
|
|
|
{
|
|
|
|
|
if (u > 255 || (u > 127 && intEnc != NSISOLatin1StringEncoding))
|
|
|
|
|
{
|
|
|
|
|
GSStrWiden(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Make room for the characters we are appending.
|
|
|
|
|
*/
|
|
|
|
|
if (s->_count + 2 >= s->_capacity)
|
|
|
|
|
{
|
|
|
|
|
GSStrMakeSpace(s, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copy the characters into place.
|
|
|
|
|
*/
|
|
|
|
|
if (s->_flags.wide == 1)
|
|
|
|
|
{
|
|
|
|
|
s->_contents.u[s->_count++] = u;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s->_contents.c[s->_count++] = u;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-05 09:34:41 +00:00
|
|
|
|
/*
|
|
|
|
|
* Make the content of this string into unicode if it is not in
|
|
|
|
|
* the external defaults C string encoding.
|
|
|
|
|
*/
|
|
|
|
|
void GSStrExternalize(GSStr s)
|
|
|
|
|
{
|
|
|
|
|
if (s->_flags.wide == 0 && intEnc != defEnc)
|
|
|
|
|
{
|
|
|
|
|
GSStrWiden(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|