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
|
2007-09-14 11:36:11 +00:00
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2000-10-09 05:32:50 +00:00
|
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
|
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.
|
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2000-10-09 05:32:50 +00:00
|
|
|
|
License along with this library; if not, write to the Free
|
2006-05-22 10:09:34 +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
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
|
#import "common.h"
|
2007-11-29 20:53:26 +00:00
|
|
|
|
#import "Foundation/NSCoder.h"
|
|
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
|
#import "Foundation/NSData.h"
|
|
|
|
|
#import "Foundation/NSDictionary.h"
|
|
|
|
|
#import "Foundation/NSCharacterSet.h"
|
|
|
|
|
#import "Foundation/NSRange.h"
|
|
|
|
|
#import "Foundation/NSException.h"
|
|
|
|
|
#import "Foundation/NSValue.h"
|
|
|
|
|
#import "Foundation/NSKeyedArchiver.h"
|
|
|
|
|
#import "GNUstepBase/GSObjCRuntime.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
|
#import "GNUstepBase/NSObject+GNUstepBase.h"
|
2002-03-13 09:58:43 +00:00
|
|
|
|
|
2007-11-29 20:53:26 +00:00
|
|
|
|
#import "GSPrivate.h"
|
2002-03-13 09:58:43 +00:00
|
|
|
|
|
2010-03-16 06:11:00 +00:00
|
|
|
|
#ifdef HAVE_MALLOC_H
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
#endif
|
2010-02-25 19:05:21 +00:00
|
|
|
|
#ifdef HAVE_ALLOCA_H
|
|
|
|
|
#include <alloca.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
/* memcpy(), strlen(), strcmp() are gcc builtin's */
|
|
|
|
|
|
2007-11-29 20:53:26 +00:00
|
|
|
|
#import "GNUstepBase/Unicode.h"
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2006-10-09 14:00:01 +00:00
|
|
|
|
static BOOL isByteEncoding(NSStringEncoding enc)
|
|
|
|
|
{
|
2006-10-20 10:56:27 +00:00
|
|
|
|
return GSPrivateIsByteEncoding(enc);
|
2006-10-09 14:00:01 +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.
|
|
|
|
|
|
|
|
|
|
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
|
2009-04-10 08:25:03 +00:00
|
|
|
|
by the instance; the 'owned' flag indicates which. If it is set,
|
|
|
|
|
we may need to free the buffer when we are deallocated.
|
2003-11-05 02:11:49 +00:00
|
|
|
|
*/
|
|
|
|
|
@interface GSCBufferString : GSCString
|
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
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
@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
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface GSUnicodeInlineString : GSUnicodeString
|
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
|
2006-08-15 04:51:18 +00:00
|
|
|
|
GSString *_parent;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
@interface GSUnicodeSubString : GSUnicodeString
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
|
|
|
|
@public
|
2006-08-15 04:51:18 +00:00
|
|
|
|
GSString *_parent;
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
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
|
|
|
|
|
2008-07-15 06:57:01 +00:00
|
|
|
|
#define GSEQ_STRRANGE strRangeNsNs
|
|
|
|
|
#define GSEQ_O GSEQ_NS
|
|
|
|
|
#define GSEQ_S GSEQ_NS
|
|
|
|
|
#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;
|
2010-09-12 17:05:30 +00:00
|
|
|
|
static NSUInteger (*hashImp)(id, SEL);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
static NSStringEncoding externalEncoding = 0;
|
|
|
|
|
static NSStringEncoding internalEncoding = 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
|
2010-07-19 17:10:46 +00:00
|
|
|
|
setup(BOOL rerun)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
static BOOL beenHere = NO;
|
|
|
|
|
|
2010-07-19 17:10:46 +00:00
|
|
|
|
if (!beenHere || rerun)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
beenHere = YES;
|
|
|
|
|
|
2008-07-15 06:57:01 +00:00
|
|
|
|
caiSel = @selector(characterAtIndex:);
|
|
|
|
|
gcrSel = @selector(getCharacters:range:);
|
|
|
|
|
ranSel = @selector(rangeOfComposedCharacterSequenceAtIndex:);
|
|
|
|
|
|
2006-10-09 14:00:01 +00:00
|
|
|
|
/*
|
|
|
|
|
* Cache the default string encoding, and set the internal encoding
|
|
|
|
|
* used by 8-bit character strings to match if possible.
|
|
|
|
|
*/
|
2006-10-20 10:56:27 +00:00
|
|
|
|
externalEncoding = GSPrivateDefaultCStringEncoding();
|
2006-10-09 14:00:01 +00:00
|
|
|
|
if (isByteEncoding(externalEncoding) == YES)
|
|
|
|
|
{
|
|
|
|
|
internalEncoding = externalEncoding;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2010-10-01 11:24:19 +00:00
|
|
|
|
hashImp = (NSUInteger (*)(id, SEL))
|
2009-12-07 05:02:55 +00:00
|
|
|
|
[GSStringClass instanceMethodForSelector: hashSel];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2001-05-23 14:32:56 +00:00
|
|
|
|
caiSel = @selector(characterAtIndex:);
|
|
|
|
|
gcrSel = @selector(getCharacters:range:);
|
|
|
|
|
ranSel = @selector(rangeOfComposedCharacterSequenceAtIndex:);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-12 04:55:36 +00:00
|
|
|
|
/* Predeclare a few functions
|
|
|
|
|
*/
|
|
|
|
|
static void GSStrWiden(GSStr s);
|
|
|
|
|
static void getCString_u(GSStr self, char *buffer, unsigned int maxLength,
|
|
|
|
|
NSRange aRange, NSRange *leftoverRange);
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2010-07-19 17:10:46 +00:00
|
|
|
|
setup(NO);
|
2000-11-03 10:11:56 +00:00
|
|
|
|
}
|
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.
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (unichar) characterAtIndex: (NSUInteger)index
|
2000-11-03 10:11:56 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"attempt to use uninitialised string"];
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2009-10-10 08:16:17 +00:00
|
|
|
|
NSLog(@"Warning ... attempt to deallocate instance of %@ in zone %p",
|
|
|
|
|
NSStringFromClass([self class]), [self zone]);
|
2006-06-04 06:42:10 +00:00
|
|
|
|
GSNOSUPERDEALLOC; // 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
|
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return [self initWithBytes: 0 length: 0 encoding: internalEncoding];
|
2004-09-09 10:39:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
/*
|
2006-05-14 18:01:13 +00:00
|
|
|
|
* Remove any BOM and perform byte swapping if required.
|
2000-11-03 10:11:56 +00:00
|
|
|
|
*/
|
2006-05-14 18:01:13 +00:00
|
|
|
|
static void
|
2009-04-10 08:25:03 +00:00
|
|
|
|
fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
|
2006-05-14 18:01:13 +00:00
|
|
|
|
NSStringEncoding encoding)
|
2000-11-03 10:11:56 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
unsigned char *b = *bytes;
|
|
|
|
|
unsigned len = *length;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (encoding == NSUnicodeStringEncoding && *length >= 2
|
|
|
|
|
&& ((b[0] == 0xFE && b[1] == 0xFF) || (b[0] == 0xFF && b[1] == 0xFE)))
|
2006-03-26 10:59:57 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
// Got a byte order marker ... remove it.
|
|
|
|
|
if (len == sizeof(unichar))
|
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
if (*owned)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(b), b);
|
2009-04-10 08:25:03 +00:00
|
|
|
|
*owned = NO;
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
|
|
|
|
*length = 0;
|
|
|
|
|
*bytes = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned char *from = b;
|
|
|
|
|
unsigned char *to;
|
|
|
|
|
unichar u;
|
|
|
|
|
|
|
|
|
|
// Got a byte order marker ... remove it.
|
|
|
|
|
len -= sizeof(unichar);
|
|
|
|
|
memcpy(&u, from, sizeof(unichar));
|
|
|
|
|
from += sizeof(unichar);
|
2009-04-10 08:25:03 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
to = NSAllocateCollectable(len, 0);
|
|
|
|
|
#else
|
|
|
|
|
to = NSZoneMalloc(NSDefaultMallocZone(), len);
|
|
|
|
|
#endif
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (u == 0xFEFF)
|
|
|
|
|
{
|
|
|
|
|
// Native byte order
|
|
|
|
|
memcpy(to, from, len);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; i += 2)
|
|
|
|
|
{
|
|
|
|
|
to[i] = from[i+1];
|
|
|
|
|
to[i+1] = from[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-04-10 08:25:03 +00:00
|
|
|
|
if (*owned == YES)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(b), b);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*owned = YES;
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
|
|
|
|
*length = len;
|
|
|
|
|
*bytes = to;
|
|
|
|
|
}
|
2006-03-26 10:59:57 +00:00
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
else if (encoding == NSUTF8StringEncoding && len >= 3
|
|
|
|
|
&& b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF)
|
2006-03-26 10:59:57 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (len == 3)
|
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
if (*owned)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(b), b);
|
2009-04-10 08:25:03 +00:00
|
|
|
|
*owned = NO;
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
|
|
|
|
*length = 0;
|
|
|
|
|
*bytes = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned char *from = b;
|
|
|
|
|
unsigned char *to;
|
|
|
|
|
|
|
|
|
|
// Got a byte order marker ... remove it.
|
|
|
|
|
len -= 3;
|
|
|
|
|
from += 3;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
to = NSAllocateCollectable(len, 0);
|
|
|
|
|
#else
|
|
|
|
|
to = NSZoneMalloc(NSDefaultMallocZone(), len);
|
|
|
|
|
#endif
|
2006-05-14 18:01:13 +00:00
|
|
|
|
memcpy(to, from, len);
|
2009-04-10 08:25:03 +00:00
|
|
|
|
if (*owned == YES)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(b), b);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*owned = YES;
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
|
|
|
|
*length = len;
|
|
|
|
|
*bytes = to;
|
2006-03-26 10:59:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithBytes: (const void*)bytes
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
void *chars = 0;
|
|
|
|
|
BOOL flag = NO;
|
|
|
|
|
|
2006-10-20 10:56:27 +00:00
|
|
|
|
if (GSPrivateIsEncodingSupported(encoding) == NO)
|
2006-06-02 05:25:21 +00:00
|
|
|
|
{
|
|
|
|
|
return nil; // Invalid encoding
|
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (length > 0)
|
2006-03-26 10:59:57 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
const void *original = bytes;
|
|
|
|
|
|
|
|
|
|
fixBOM((unsigned char**)&bytes, &length, &flag, encoding);
|
|
|
|
|
/*
|
|
|
|
|
* We need to copy the data if there is any, unless fixBOM()
|
|
|
|
|
* has already done it for us.
|
|
|
|
|
*/
|
|
|
|
|
if (original == bytes)
|
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
chars = NSAllocateCollectable(length, 0);
|
|
|
|
|
#else
|
2010-03-05 09:30:18 +00:00
|
|
|
|
chars = NSZoneMalloc([self zone], length);
|
2009-04-10 08:25:03 +00:00
|
|
|
|
#endif
|
2006-05-14 18:01:13 +00:00
|
|
|
|
memcpy(chars, bytes, length);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2006-06-02 05:25:21 +00:00
|
|
|
|
/*
|
|
|
|
|
* The fixBOM() function has already copied the data and allocated
|
|
|
|
|
* new memory, so we can just pass that to the designated initialiser
|
|
|
|
|
*/
|
2006-05-14 18:01:13 +00:00
|
|
|
|
chars = (void*)bytes;
|
|
|
|
|
}
|
2006-03-26 10:59:57 +00:00
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return [self initWithBytesNoCopy: chars
|
|
|
|
|
length: length
|
|
|
|
|
encoding: encoding
|
|
|
|
|
freeWhenDone: YES];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
- (id) initWithBytesNoCopy: (void*)bytes
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
2000-11-03 10:11:56 +00:00
|
|
|
|
{
|
2006-05-30 11:19:44 +00:00
|
|
|
|
GSCharPtr chars = { .u = 0 };
|
2006-05-14 18:01:13 +00:00
|
|
|
|
BOOL isASCII = NO;
|
|
|
|
|
BOOL isLatin1 = NO;
|
|
|
|
|
GSStr me;
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
2006-10-20 10:56:27 +00:00
|
|
|
|
if (GSPrivateIsEncodingSupported(encoding) == NO)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
|
|
|
|
if (flag == YES && bytes != 0)
|
|
|
|
|
{
|
2006-05-20 14:52:38 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(bytes), bytes);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
|
|
|
|
return nil; // Invalid encoding
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
|
|
|
|
fixBOM((unsigned char**)&bytes, &length, &flag, encoding);
|
2006-05-30 11:19:44 +00:00
|
|
|
|
if (encoding == NSUnicodeStringEncoding)
|
|
|
|
|
{
|
|
|
|
|
chars.u = bytes;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
chars.c = bytes;
|
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-06-22 22:36:46 +00:00
|
|
|
|
if (encoding == NSUTF8StringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
if ((chars.c)[i] > 127)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (i == length)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* This is actually ASCII data ... so we can just store it as if
|
|
|
|
|
* in the internal 8bit encoding scheme.
|
|
|
|
|
*/
|
|
|
|
|
encoding = internalEncoding;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-10-09 14:00:01 +00:00
|
|
|
|
else if (encoding != internalEncoding && isByteEncoding(encoding) == YES)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
2006-05-30 11:19:44 +00:00
|
|
|
|
if ((chars.c)[i] > 127)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
|
|
|
|
if (encoding == NSASCIIStringEncoding)
|
|
|
|
|
{
|
2006-05-30 11:19:44 +00:00
|
|
|
|
if (flag == YES && chars.c != 0)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
2006-05-30 11:19:44 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars.c), chars.c);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
|
|
|
|
return nil; // Invalid data
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (i == length)
|
|
|
|
|
{
|
|
|
|
|
/*
|
2006-05-30 11:19:44 +00:00
|
|
|
|
* This is actually ASCII data ... so we can just store it as if
|
2006-05-14 18:01:13 +00:00
|
|
|
|
* in the internal 8bit encoding scheme.
|
|
|
|
|
*/
|
|
|
|
|
encoding = internalEncoding;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-15 08:03:19 +00:00
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (encoding == internalEncoding)
|
|
|
|
|
{
|
2009-04-15 08:03:19 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
/* If we are using GC, copy and free any non-collectable buffer so
|
|
|
|
|
* we don't leak memory.
|
|
|
|
|
*/
|
|
|
|
|
if (GSPrivateIsCollectable(chars.c) == NO)
|
|
|
|
|
{
|
|
|
|
|
me = (GSStr)NSAllocateObject(GSCInlineStringClass, length,
|
2010-03-05 09:30:18 +00:00
|
|
|
|
[self zone]);
|
2009-04-15 08:03:19 +00:00
|
|
|
|
me->_contents.c = (unsigned char*)&((GSCInlineString*)me)[1];
|
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 0;
|
|
|
|
|
me->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
|
|
|
|
memcpy(me->_contents.c, chars.c, length);
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars.c), chars.c);
|
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2010-03-05 09:30:18 +00:00
|
|
|
|
me = (GSStr)NSAllocateObject(GSCBufferStringClass, 0, [self zone]);
|
2006-05-30 11:19:44 +00:00
|
|
|
|
me->_contents.c = chars.c;
|
2006-05-14 18:01:13 +00:00
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 0;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
me->_flags.owned = flag;
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Any remaining encoding needs to be converted to UTF-16.
|
|
|
|
|
*/
|
|
|
|
|
if (encoding != NSUnicodeStringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned l = 0;
|
|
|
|
|
|
2006-05-30 11:19:44 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, chars.c, length, encoding,
|
2010-03-05 09:30:18 +00:00
|
|
|
|
[self zone], 0) == NO)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
2006-05-30 11:19:44 +00:00
|
|
|
|
if (flag == YES && chars.c != 0)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
2006-05-30 11:19:44 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars.c), chars.c);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
|
|
|
|
return nil; // Invalid data
|
|
|
|
|
}
|
2006-05-30 11:19:44 +00:00
|
|
|
|
if (flag == YES && chars.c != 0)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
2006-05-30 11:19:44 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars.c), chars.c);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
2006-05-30 11:19:44 +00:00
|
|
|
|
chars.u = u;
|
2006-05-14 18:01:13 +00:00
|
|
|
|
length = l * sizeof(unichar);
|
|
|
|
|
flag = YES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
length /= sizeof(unichar);
|
2006-05-30 11:19:44 +00:00
|
|
|
|
if (GSUnicode(chars.u, length, &isASCII, &isLatin1) != length)
|
2006-03-26 10:59:57 +00:00
|
|
|
|
{
|
2006-05-30 11:19:44 +00:00
|
|
|
|
if (flag == YES && chars.u != 0)
|
2006-03-28 06:05:04 +00:00
|
|
|
|
{
|
2006-05-30 11:19:44 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars.u), chars.u);
|
2006-03-28 06:05:04 +00:00
|
|
|
|
}
|
2006-03-26 10:59:57 +00:00
|
|
|
|
return nil; // Invalid data
|
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
|
2006-03-26 10:59:57 +00:00
|
|
|
|
if (isASCII == YES
|
2006-05-14 18:01:13 +00:00
|
|
|
|
|| (internalEncoding == NSISOLatin1StringEncoding && isLatin1 == YES))
|
2002-04-07 16:54:35 +00:00
|
|
|
|
{
|
2006-03-26 10:59:57 +00:00
|
|
|
|
me = (GSStr)NSAllocateObject(GSCInlineStringClass, length,
|
2010-03-05 09:30:18 +00:00
|
|
|
|
[self zone]);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
me->_contents.c = (unsigned char*)
|
|
|
|
|
(((void*)me)+class_getInstanceSize(GSCInlineStringClass));
|
2006-03-26 10:59:57 +00:00
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 0;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
me->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2006-03-26 10:59:57 +00:00
|
|
|
|
while (length-- > 0)
|
|
|
|
|
{
|
2006-05-30 11:19:44 +00:00
|
|
|
|
me->_contents.c[length] = chars.u[length];
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
2006-05-30 11:19:44 +00:00
|
|
|
|
if (flag == YES && chars.u != 0)
|
2006-03-28 06:05:04 +00:00
|
|
|
|
{
|
2006-05-30 11:19:44 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars.u), chars.u);
|
2006-03-28 06:05:04 +00:00
|
|
|
|
}
|
2006-03-26 10:59:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-04-15 08:03:19 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
/* If we are using GC, copy and free any non-collectable buffer so
|
|
|
|
|
* we don't leak memory.
|
|
|
|
|
*/
|
|
|
|
|
if (GSPrivateIsCollectable(chars.u) == NO)
|
|
|
|
|
{
|
|
|
|
|
me = (GSStr)NSAllocateObject(GSUnicodeInlineStringClass, length,
|
2010-03-05 09:30:18 +00:00
|
|
|
|
[self zone]);
|
2009-04-15 08:03:19 +00:00
|
|
|
|
me->_contents.u = (unichar*)&((GSUnicodeInlineString*)me)[1];
|
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 1;
|
|
|
|
|
me->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
|
|
|
|
memcpy(me->_contents.u, chars.u, length * sizeof(unichar));
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars.u), chars.u);
|
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2006-03-26 10:59:57 +00:00
|
|
|
|
me = (GSStr)NSAllocateObject(GSUnicodeBufferStringClass,
|
2010-03-05 09:30:18 +00:00
|
|
|
|
0, [self zone]);
|
2006-05-30 11:19:44 +00:00
|
|
|
|
me->_contents.u = chars.u;
|
2006-03-26 10:59:57 +00:00
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 1;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
me->_flags.owned = flag;
|
2002-04-07 16:54:35 +00:00
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
- (id) initWithCharacters: (const unichar*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2000-11-03 10:11:56 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return [self initWithBytes: (const void*)chars
|
|
|
|
|
length: length * sizeof(unichar)
|
|
|
|
|
encoding: NSUnicodeStringEncoding];
|
|
|
|
|
}
|
2002-04-07 16:54:35 +00:00
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return [self initWithBytesNoCopy: (void*)chars
|
2006-05-14 18:01:13 +00:00
|
|
|
|
length: length * sizeof(unichar)
|
|
|
|
|
encoding: NSUnicodeStringEncoding
|
|
|
|
|
freeWhenDone: flag];
|
|
|
|
|
}
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
- (id) initWithCString: (const char*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
|
|
|
|
return [self initWithBytes: (const void*)chars
|
|
|
|
|
length: length
|
|
|
|
|
encoding: externalEncoding];
|
2000-11-03 10:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCStringNoCopy: (char*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2000-11-03 10:11:56 +00:00
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return [self initWithBytesNoCopy: (void*)chars
|
2006-05-14 18:01:13 +00:00
|
|
|
|
length: length
|
|
|
|
|
encoding: externalEncoding
|
|
|
|
|
freeWhenDone: flag];
|
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
|
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSStr f;
|
2004-06-05 10:06:56 +00:00
|
|
|
|
unsigned char buf[2048];
|
|
|
|
|
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)
|
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
fmt = NSZoneMalloc(NSDefaultMallocZone(), (len+1)*sizeof(unichar));
|
2004-06-05 10:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
[format getCharacters: fmt];
|
|
|
|
|
fmt[len] = '\0';
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Now set up 'f' as a GSMutableString object whose initial buffer is
|
2006-10-20 10:56:27 +00:00
|
|
|
|
* allocated on the stack. The GSPrivateFormat function can write into it.
|
2004-06-05 10:06:56 +00:00
|
|
|
|
*/
|
2010-02-14 10:48:10 +00:00
|
|
|
|
f = (GSStr)alloca(class_getInstanceSize(GSMutableStringClass));
|
|
|
|
|
object_setClass(f, GSMutableStringClass);
|
|
|
|
|
f->_zone = NSDefaultMallocZone();
|
|
|
|
|
f->_contents.c = buf;
|
|
|
|
|
f->_capacity = sizeof(buf);
|
|
|
|
|
f->_count = 0;
|
|
|
|
|
f->_flags.wide = 0;
|
|
|
|
|
f->_flags.owned = 0;
|
|
|
|
|
GSPrivateFormat(f, fmt, argList, locale);
|
2004-06-05 10:06:56 +00:00
|
|
|
|
if (fmt != fbuf)
|
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), fmt);
|
2004-06-05 10:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2010-02-14 10:48:10 +00:00
|
|
|
|
* 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
|
2004-06-05 10:06:56 +00:00
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2010-02-14 10:48:10 +00:00
|
|
|
|
if (f->_flags.wide == 1)
|
2004-06-05 10:06:56 +00:00
|
|
|
|
{
|
|
|
|
|
me = (GSStr)NSAllocateObject(GSUnicodeInlineStringClass,
|
2010-03-05 09:30:18 +00:00
|
|
|
|
f->_count*sizeof(unichar), [self zone]);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
me->_contents.u = (unichar*)
|
|
|
|
|
(((void*)me)+class_getInstanceSize(GSUnicodeInlineStringClass));
|
|
|
|
|
me->_count = f->_count;
|
2004-06-05 10:06:56 +00:00
|
|
|
|
me->_flags.wide = 1;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
me->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2010-02-14 10:48:10 +00:00
|
|
|
|
memcpy(me->_contents.u, f->_contents.u, f->_count*sizeof(unichar));
|
2004-06-05 10:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
me = (GSStr)NSAllocateObject(GSCInlineStringClass, f->_count,
|
2010-03-05 09:30:18 +00:00
|
|
|
|
[self zone]);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
me->_contents.c = (unsigned char*)
|
|
|
|
|
(((void*)me)+class_getInstanceSize(GSCInlineStringClass));
|
|
|
|
|
me->_count = f->_count;
|
2004-06-05 10:06:56 +00:00
|
|
|
|
me->_flags.wide = 0;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
me->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2010-02-14 10:48:10 +00:00
|
|
|
|
memcpy(me->_contents.c, f->_contents.c, f->_count);
|
2004-06-05 10:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the string had to grow beyond the initial buffer size, we must
|
|
|
|
|
* release any allocated memory.
|
|
|
|
|
*/
|
2010-02-14 10:48:10 +00:00
|
|
|
|
if (f->_flags.owned == 1)
|
2004-06-05 10:06:56 +00:00
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
NSZoneFree(f->_zone, f->_contents.c);
|
2004-06-05 10:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
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"];
|
2010-02-08 17:52:36 +00:00
|
|
|
|
if (NO == [string isKindOfClass: NSStringClass]) // may be proxy
|
2000-11-03 10:11:56 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"-initWithString: given non-string object"];
|
|
|
|
|
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(string);
|
2000-11-03 10:11:56 +00:00
|
|
|
|
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,
|
2010-03-05 09:30:18 +00:00
|
|
|
|
length, [self zone]);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
me->_contents.c = (unsigned char*)
|
|
|
|
|
(((void*)me)+class_getInstanceSize(GSCInlineStringClass));
|
2000-11-03 10:11:56 +00:00
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 0;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
me->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
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,
|
2010-03-05 09:30:18 +00:00
|
|
|
|
length*sizeof(unichar), [self zone]);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
me->_contents.u = (unichar*)
|
|
|
|
|
(((void*)me)+class_getInstanceSize(GSUnicodeInlineStringClass));
|
2000-11-03 10:11:56 +00:00
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 1;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
me->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
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,
|
2010-03-05 09:30:18 +00:00
|
|
|
|
length*sizeof(unichar), [self zone]);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
me->_contents.u = (unichar*)
|
|
|
|
|
(((void*)me)+class_getInstanceSize(GSUnicodeInlineStringClass));
|
2000-11-03 10:11:56 +00:00
|
|
|
|
me->_count = length;
|
|
|
|
|
me->_flags.wide = 1;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
me->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2000-11-03 10:11:56 +00:00
|
|
|
|
[string getCharacters: me->_contents.u];
|
|
|
|
|
}
|
|
|
|
|
return (id)me;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSUInteger) length
|
2000-11-03 10:11:56 +00:00
|
|
|
|
{
|
|
|
|
|
[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).
|
|
|
|
|
*/
|
|
|
|
|
|
2008-11-17 13:45:32 +00:00
|
|
|
|
static inline const 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 "";
|
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (internalEncoding == 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.
|
|
|
|
|
*/
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count, internalEncoding,
|
2002-10-05 17:47:54 +00:00
|
|
|
|
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
|
|
|
|
|
2008-11-17 13:45:32 +00:00
|
|
|
|
return (const char*)r;
|
2002-10-05 17:47:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-11-17 13:45:32 +00:00
|
|
|
|
static inline const 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."];
|
|
|
|
|
}
|
2008-11-17 13:45:32 +00:00
|
|
|
|
return (const 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
|
|
|
|
{
|
2008-02-20 09:56:25 +00:00
|
|
|
|
unsigned c = self->_count;
|
|
|
|
|
unsigned i;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2008-02-20 09:56:25 +00:00
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
|
{
|
2009-08-24 07:07:36 +00:00
|
|
|
|
char c = self->_contents.c[i];
|
|
|
|
|
|
|
|
|
|
if (strchr("123456789yYtT", c) != 0)
|
2008-02-20 09:56:25 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2009-08-24 07:07:36 +00:00
|
|
|
|
if (!isspace(c) && c != '0' && c != '-' && c != '+')
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2008-02-20 09:56:25 +00:00
|
|
|
|
return NO;
|
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
|
|
|
|
{
|
2008-02-20 09:56:25 +00:00
|
|
|
|
unsigned c = self->_count;
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < c; i++)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2009-08-24 07:07:36 +00:00
|
|
|
|
unichar c = self->_contents.u[i];
|
|
|
|
|
|
|
|
|
|
if (c > 'y')
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (strchr("123456789yYtT", c) != 0)
|
2008-02-20 09:56:25 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2009-08-24 07:07:36 +00:00
|
|
|
|
if (!isspace(c) && c != '0' && c != '-' && c != '+')
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2008-02-20 09:56:25 +00:00
|
|
|
|
return NO;
|
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
|
2006-05-14 18:01:13 +00:00
|
|
|
|
&& enc != internalEncoding
|
2006-01-19 13:28:02 +00:00
|
|
|
|
&& enc != NSUTF8StringEncoding
|
|
|
|
|
&& enc != NSUnicodeStringEncoding
|
2006-10-09 14:00:01 +00:00
|
|
|
|
&& ((internalEncoding != NSASCIIStringEncoding) || !isByteEncoding(enc)))
|
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.
|
|
|
|
|
*/
|
2006-05-14 18:01:13 +00:00
|
|
|
|
result = GSToUnicode(&r, &l, self->_contents.c, self->_count,
|
|
|
|
|
internalEncoding, NSDefaultMallocZone(), GSUniStrict);
|
2006-01-19 13:28:02 +00:00
|
|
|
|
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
|
|
|
|
{
|
2006-08-12 15:09:23 +00:00
|
|
|
|
unichar u;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
if (index >= self->_count)
|
|
|
|
|
[NSException raise: NSRangeException format: @"Invalid index."];
|
2006-08-12 15:09:23 +00:00
|
|
|
|
u = self->_contents.c[index];
|
|
|
|
|
if (u > 127)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2006-08-12 15:09:23 +00:00
|
|
|
|
unsigned char c = (unsigned char)u;
|
|
|
|
|
unsigned int s = 1;
|
|
|
|
|
unichar *d = &u;
|
|
|
|
|
|
|
|
|
|
GSToUnicode(&d, &s, &c, 1, internalEncoding, 0, 0);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2006-08-12 15:09:23 +00:00
|
|
|
|
return u;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(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;
|
|
|
|
|
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(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);
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-17 13:45:32 +00:00
|
|
|
|
static inline const 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
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
else if (enc == internalEncoding)
|
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,
|
2006-05-14 18:01:13 +00:00
|
|
|
|
internalEncoding, NSDefaultMallocZone(),
|
2005-06-04 07:54:04 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count, internalEncoding,
|
2002-03-16 09:54:50 +00:00
|
|
|
|
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
|
|
|
|
|
2008-11-17 13:45:32 +00:00
|
|
|
|
return (const char*)r;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-11-17 13:45:32 +00:00
|
|
|
|
static inline const 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;
|
2006-08-15 04:51:18 +00:00
|
|
|
|
[NSDataClass dataWithBytesNoCopy: tmp
|
|
|
|
|
length: (c + 1)*2
|
|
|
|
|
freeWhenDone: YES];
|
2008-11-17 13:45:32 +00:00
|
|
|
|
return (const 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."];
|
|
|
|
|
}
|
2008-11-17 13:45:32 +00:00
|
|
|
|
return (const 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
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (enc == internalEncoding)
|
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;
|
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count,
|
|
|
|
|
internalEncoding, NSDefaultMallocZone(), 0) == NO)
|
2002-03-16 09:54:50 +00:00
|
|
|
|
{
|
|
|
|
|
[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*
|
2006-05-24 11:28:04 +00:00
|
|
|
|
dataUsingEncoding_c(GSStr self, NSStringEncoding encoding, BOOL lossy)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned len = self->_count;
|
|
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
|
{
|
|
|
|
|
return [NSDataClass data];
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if ((encoding == internalEncoding)
|
|
|
|
|
|| ((internalEncoding == NSASCIIStringEncoding)
|
2006-10-09 14:00:01 +00:00
|
|
|
|
&& (encoding == NSUTF8StringEncoding || isByteEncoding(encoding))))
|
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
|
|
|
|
|
2006-05-24 11:28:04 +00:00
|
|
|
|
if (lossy == NO)
|
2002-03-16 09:54:50 +00:00
|
|
|
|
{
|
|
|
|
|
options |= GSUniStrict;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (GSToUnicode(&r, &l, self->_contents.c, self->_count, internalEncoding,
|
2002-03-16 09:54:50 +00:00
|
|
|
|
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
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count, internalEncoding,
|
2002-03-16 09:54:50 +00:00
|
|
|
|
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(),
|
2006-05-24 11:28:04 +00:00
|
|
|
|
(lossy == 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*
|
2006-05-24 11:28:04 +00:00
|
|
|
|
dataUsingEncoding_u(GSStr self, NSStringEncoding encoding, BOOL lossy)
|
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)
|
|
|
|
|
{
|
2006-05-24 11:28:04 +00:00
|
|
|
|
if (lossy == NO)
|
2006-03-26 11:34:47 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-15 04:51:18 +00:00
|
|
|
|
return [NSDataClass dataWithBytesNoCopy: buff
|
|
|
|
|
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,
|
2006-05-24 11:28:04 +00:00
|
|
|
|
NSDefaultMallocZone(), (lossy == 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
|
|
|
|
{
|
2008-10-28 18:39:20 +00:00
|
|
|
|
const char *ptr = (const char*)self->_contents.c;
|
|
|
|
|
const char *end = ptr + self->_count;
|
|
|
|
|
|
2008-11-19 12:18:28 +00:00
|
|
|
|
while (ptr < end && isspace(*ptr))
|
2008-10-28 18:39:20 +00:00
|
|
|
|
{
|
|
|
|
|
ptr++;
|
|
|
|
|
}
|
|
|
|
|
if (ptr == end)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2003-07-07 09:05:53 +00:00
|
|
|
|
return 0.0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-10-29 09:16:17 +00:00
|
|
|
|
unsigned s = 99;
|
|
|
|
|
unichar b[100];
|
|
|
|
|
unichar *u = b;
|
2003-07-07 09:05:53 +00:00
|
|
|
|
double d = 0.0;
|
|
|
|
|
|
2008-10-29 09:16:17 +00:00
|
|
|
|
/* use static buffer unless string is really long, in which case
|
|
|
|
|
* we use the stack to allocate a bigger one.
|
|
|
|
|
*/
|
|
|
|
|
if (GSToUnicode(&u, &s, (const uint8_t*)ptr, end - ptr,
|
|
|
|
|
internalEncoding, NSDefaultMallocZone(), GSUniTerminate) == NO)
|
|
|
|
|
{
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
if (GSScanDouble(u, end - ptr, &d) == NO)
|
|
|
|
|
{
|
|
|
|
|
d = 0.0;
|
|
|
|
|
}
|
|
|
|
|
if (u != b)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
}
|
2003-07-07 09:05:53 +00:00
|
|
|
|
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,
|
2006-05-14 18:01:13 +00:00
|
|
|
|
aRange.length, internalEncoding, 0, 0))
|
2003-05-05 13:36:25 +00:00
|
|
|
|
{
|
|
|
|
|
[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));
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-12 04:55:36 +00:00
|
|
|
|
static 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)
|
|
|
|
|
{
|
2010-02-21 11:12:17 +00:00
|
|
|
|
GSMutableString *o;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-21 11:12:17 +00:00
|
|
|
|
if (externalEncoding == internalEncoding)
|
|
|
|
|
{
|
|
|
|
|
memcpy(buffer, &self->_contents.c[aRange.location], len);
|
|
|
|
|
buffer[len] = '\0';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isByteEncoding(internalEncoding))
|
|
|
|
|
{
|
|
|
|
|
if (externalEncoding == NSUTF8StringEncoding
|
|
|
|
|
|| isByteEncoding(externalEncoding))
|
|
|
|
|
{
|
|
|
|
|
const unsigned char *ptr = self->_contents.c + aRange.location;
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Maybe we actually contain ascii data, which can be
|
|
|
|
|
* copied out directly.
|
|
|
|
|
*/
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
{
|
|
|
|
|
unsigned char c = ptr[i];
|
|
|
|
|
|
|
|
|
|
if (c > 127)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
buffer[i] = c;
|
|
|
|
|
}
|
|
|
|
|
if (i == len)
|
|
|
|
|
{
|
|
|
|
|
buffer[i] = '\0';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Fall through to perform conversion to unicode and back
|
|
|
|
|
if ([(id)self class] == NSConstantStringClass)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Warning: non-ASCII character in string literal");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* As the internal and external encodings don't match, the simplest
|
|
|
|
|
* thing to do is widen the internal data to unicode and use the
|
|
|
|
|
* unicode function to get the cString.
|
|
|
|
|
*/
|
|
|
|
|
o = (GSMutableString*)alloca(class_getInstanceSize(GSMutableStringClass));
|
|
|
|
|
object_setClass(o, GSMutableStringClass);
|
|
|
|
|
o->_count = self->_count;
|
|
|
|
|
o->_flags.wide = 0;
|
|
|
|
|
o->_flags.owned = 0;
|
|
|
|
|
o->_flags.unused = 0;
|
|
|
|
|
o->_flags.hash = 0;
|
|
|
|
|
o->_capacity = self->_count;
|
|
|
|
|
o->_contents.c = self->_contents.c;
|
|
|
|
|
o->_zone = NSDefaultMallocZone();
|
|
|
|
|
GSStrWiden(o);
|
|
|
|
|
getCString_u(o, buffer, maxLength, aRange, leftoverRange);
|
|
|
|
|
if (o->_flags.owned == 1)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(o->_zone, o->_contents.u);
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-08-12 04:55:36 +00:00
|
|
|
|
static 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,
|
2006-05-14 18:01:13 +00:00
|
|
|
|
self->_contents.u + aRange.location, aRange.length,
|
|
|
|
|
externalEncoding, 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,
|
2006-05-14 18:01:13 +00:00
|
|
|
|
self->_contents.u + aRange.location, aRange.length,
|
|
|
|
|
externalEncoding, 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,
|
2006-05-14 18:01:13 +00:00
|
|
|
|
self->_contents.u + aRange.location, mid,
|
|
|
|
|
externalEncoding, 0, GSUniTerminate | GSUniStrict) == YES)
|
2003-07-09 02:13:58 +00:00
|
|
|
|
{
|
|
|
|
|
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,
|
2006-05-14 18:01:13 +00:00
|
|
|
|
self->_contents.u + aRange.location, lo,
|
|
|
|
|
externalEncoding, 0, GSUniTerminate | GSUniStrict) == NO)
|
2003-07-09 02:13:58 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2006-06-29 13:32:56 +00:00
|
|
|
|
if (buffer == 0)
|
|
|
|
|
{
|
|
|
|
|
return NO; // Can't fit in here
|
|
|
|
|
}
|
2006-02-27 09:35:19 +00:00
|
|
|
|
if (enc == NSUnicodeStringEncoding)
|
|
|
|
|
{
|
|
|
|
|
if (maxLength >= sizeof(unichar))
|
|
|
|
|
{
|
|
|
|
|
unsigned bytes = maxLength - sizeof(unichar);
|
2011-02-19 15:34:21 +00:00
|
|
|
|
unichar *u = (unichar*)(void*)buffer;
|
2006-02-27 09:35:19 +00:00
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (GSToUnicode(&u, &bytes, self->_contents.c, self->_count,
|
|
|
|
|
internalEncoding, NSDefaultMallocZone(), GSUniTerminate) == NO)
|
2006-02-27 09:35:19 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to Unicode string."];
|
|
|
|
|
}
|
2011-02-19 15:34:21 +00:00
|
|
|
|
if (u == (unichar*)(void*)buffer)
|
2006-02-27 09:35:19 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (maxLength > sizeof(char))
|
|
|
|
|
{
|
|
|
|
|
unsigned bytes = maxLength - sizeof(char);
|
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (enc == internalEncoding)
|
2006-02-27 09:35:19 +00:00
|
|
|
|
{
|
|
|
|
|
if (bytes > self->_count)
|
|
|
|
|
{
|
|
|
|
|
bytes = self->_count;
|
|
|
|
|
}
|
|
|
|
|
memcpy(buffer, self->_contents.c, bytes);
|
|
|
|
|
buffer[bytes] = '\0';
|
|
|
|
|
if (bytes < self->_count)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
2006-06-29 13:32:56 +00:00
|
|
|
|
|
|
|
|
|
if (enc == NSUTF8StringEncoding
|
2006-10-09 14:00:01 +00:00
|
|
|
|
&& isByteEncoding(internalEncoding))
|
2006-06-29 13:32:56 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Maybe we actually contain ascii data, which can be
|
|
|
|
|
* copied out directly as a utf-8 string.
|
|
|
|
|
*/
|
|
|
|
|
if (bytes > self->_count)
|
|
|
|
|
{
|
|
|
|
|
bytes = self->_count;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < bytes; i++)
|
|
|
|
|
{
|
|
|
|
|
unsigned char c = self->_contents.c[i];
|
|
|
|
|
|
|
|
|
|
if (c > 127)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
buffer[i] = c;
|
|
|
|
|
}
|
|
|
|
|
if (i == bytes)
|
|
|
|
|
{
|
|
|
|
|
buffer[bytes] = '\0';
|
|
|
|
|
if (bytes < self->_count)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
// Fall through to perform conversion to unicode and back
|
2007-06-23 08:52:39 +00:00
|
|
|
|
if ([(id)self class] == NSConstantStringClass)
|
2007-06-08 06:41:16 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"Warning: non-ASCII character in string literal");
|
|
|
|
|
}
|
2006-06-29 13:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (enc == NSASCIIStringEncoding
|
2006-10-09 14:00:01 +00:00
|
|
|
|
&& isByteEncoding(internalEncoding))
|
2006-02-27 09:35:19 +00:00
|
|
|
|
{
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2007-06-08 06:00:11 +00:00
|
|
|
|
bytes = maxLength - sizeof(char);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count,
|
|
|
|
|
internalEncoding, NSDefaultMallocZone(), 0) == NO)
|
2006-02-27 09:35:19 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to Unicode string."];
|
|
|
|
|
}
|
|
|
|
|
if (GSFromUnicode((unsigned char**)&c, &bytes, u, l, enc,
|
2006-06-29 13:32:56 +00:00
|
|
|
|
0, GSUniTerminate|GSUniStrict) == NO)
|
2006-02-27 09:35:19 +00:00
|
|
|
|
{
|
2006-06-29 13:32:56 +00:00
|
|
|
|
c = 0; // Unable to convert
|
2006-02-27 09:35:19 +00:00
|
|
|
|
}
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), u);
|
|
|
|
|
if (c == (unsigned char*)buffer)
|
|
|
|
|
{
|
|
|
|
|
return YES; // Fitted in original buffer
|
|
|
|
|
}
|
2006-06-29 13:32:56 +00:00
|
|
|
|
else if (c != 0)
|
2006-02-27 09:35:19 +00:00
|
|
|
|
{
|
|
|
|
|
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,
|
2006-06-29 13:32:56 +00:00
|
|
|
|
0, GSUniTerminate|GSUniStrict) == NO)
|
2006-02-27 09:35:19 +00:00
|
|
|
|
{
|
2006-06-29 13:32:56 +00:00
|
|
|
|
return NO;
|
2006-02-27 09:35:19 +00:00
|
|
|
|
}
|
2006-06-29 13:32:56 +00:00
|
|
|
|
return YES;
|
2006-02-27 09:35:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2008-10-28 18:39:20 +00:00
|
|
|
|
const char *ptr = (const char*)self->_contents.c;
|
|
|
|
|
const char *end = ptr + self->_count;
|
|
|
|
|
|
2008-11-19 12:18:28 +00:00
|
|
|
|
while (ptr < end && isspace(*ptr))
|
2008-10-28 18:39:20 +00:00
|
|
|
|
{
|
|
|
|
|
ptr++;
|
|
|
|
|
}
|
|
|
|
|
if (ptr == end)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-10-28 18:39:20 +00:00
|
|
|
|
unsigned len = (end - ptr) < 32 ? (end - ptr) : 31;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
char buf[len+1];
|
|
|
|
|
|
2008-10-28 18:39:20 +00:00
|
|
|
|
memcpy(buf, ptr, len);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
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
|
|
|
|
{
|
2008-10-28 18:39:20 +00:00
|
|
|
|
const unichar *ptr = self->_contents.u;
|
|
|
|
|
const unichar *end = ptr + self->_count;
|
|
|
|
|
|
2008-11-19 12:18:28 +00:00
|
|
|
|
while (ptr < end && isspace(*ptr))
|
2008-10-28 18:39:20 +00:00
|
|
|
|
{
|
|
|
|
|
ptr++;
|
|
|
|
|
}
|
|
|
|
|
if (ptr == end)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-10-28 18:39:20 +00:00
|
|
|
|
unsigned int l = (end - ptr) < 32 ? (end - ptr) : 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
|
|
|
|
|
2008-10-28 18:39:20 +00:00
|
|
|
|
GSFromUnicode(&b, &l, ptr, l, internalEncoding, 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;
|
|
|
|
|
}
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(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
|
|
|
|
|
2009-06-08 16:03:54 +00:00
|
|
|
|
if (other->_count == self->_count
|
|
|
|
|
&& memcmp(other->_contents.c, self->_contents.c, self->_count) == 0)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
return YES;
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2009-12-07 05:02:55 +00:00
|
|
|
|
else if (c == GSMutableStringClass || GSObjCIsKindOf(c, GSStringClass) == YES)
|
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
|
|
|
|
|
{
|
2009-06-08 16:03:54 +00:00
|
|
|
|
if (other->_count == self->_count
|
|
|
|
|
&& memcmp(other->_contents.c, self->_contents.c, self->_count) == 0)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
return YES;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2010-02-08 17:52:36 +00:00
|
|
|
|
else if (YES == [anObject isKindOfClass: NSStringClass]) // may be proxy
|
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;
|
|
|
|
|
}
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(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;
|
|
|
|
|
}
|
2009-12-07 05:02:55 +00:00
|
|
|
|
else if (c == GSMutableStringClass || GSObjCIsKindOf(c, GSStringClass) == YES)
|
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;
|
|
|
|
|
}
|
2010-02-08 17:52:36 +00:00
|
|
|
|
else if (YES == [anObject isKindOfClass: NSStringClass]) // may be proxy
|
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 "";
|
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (externalEncoding == internalEncoding)
|
2002-03-19 07:39:05 +00:00
|
|
|
|
{
|
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.
|
|
|
|
|
*/
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, self->_contents.c, self->_count,
|
|
|
|
|
internalEncoding, NSDefaultMallocZone(), 0) == NO)
|
2002-03-19 07:39:05 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSCharacterConversionException
|
|
|
|
|
format: @"Can't convert to/from Unicode string."];
|
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (GSFromUnicode((unsigned char**)&r, &s, u, l, externalEncoding,
|
2002-03-19 07:39:05 +00:00
|
|
|
|
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
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
GSFromUnicode(&r, &l, self->_contents.u, self->_count, externalEncoding,
|
2002-03-20 22:37:22 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2009-04-10 08:25:03 +00:00
|
|
|
|
if (s->_flags.owned == 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
|
2010-03-05 09:30:18 +00:00
|
|
|
|
s->_zone = [(NSString*)s zone];
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-04-10 08:25:03 +00:00
|
|
|
|
s->_flags.owned = 1;
|
2004-05-14 10:52:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (internalEncoding == NSISOLatin1StringEncoding
|
|
|
|
|
|| internalEncoding == NSASCIIStringEncoding)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
2010-03-05 09:30:18 +00:00
|
|
|
|
s->_zone = [(NSString*)s zone];
|
2004-05-14 10:52:30 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (!GSToUnicode(&tmp, &len, s->_contents.c, s->_count,
|
|
|
|
|
internalEncoding, s->_zone, 0))
|
2004-05-14 10:52:30 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"widen of string failed"];
|
|
|
|
|
}
|
2009-04-10 08:25:03 +00:00
|
|
|
|
if (s->_flags.owned == 1)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
{
|
|
|
|
|
NSZoneFree(s->_zone, s->_contents.c);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
s->_flags.owned = 1;
|
2004-05-14 10:52:30 +00:00
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
2006-08-12 15:09:23 +00:00
|
|
|
|
unichar u = self->_contents.c[i];
|
2002-02-06 13:44:03 +00:00
|
|
|
|
|
2008-07-15 07:31:55 +00:00
|
|
|
|
if (u > 127 && internalEncoding != NSISOLatin1StringEncoding)
|
2002-02-06 13:44:03 +00:00
|
|
|
|
{
|
2006-08-12 15:09:23 +00:00
|
|
|
|
unsigned char c = (unsigned char)u;
|
|
|
|
|
unsigned int s = 1;
|
|
|
|
|
unichar *d = &u;
|
|
|
|
|
|
|
|
|
|
GSToUnicode(&d, &s, &c, 1, internalEncoding, 0, 0);
|
2002-02-06 13:44:03 +00:00
|
|
|
|
}
|
2008-07-15 07:31:55 +00:00
|
|
|
|
/* FIXME ... what about UTF-16 sequences of more than one 16bit value
|
|
|
|
|
* corresponding to a single UCS-32 codepoint?
|
|
|
|
|
*/
|
2006-08-12 15:09:23 +00:00
|
|
|
|
if ((*mImp)(aSet, cMemberSel, u))
|
2002-02-06 13:44:03 +00:00
|
|
|
|
{
|
|
|
|
|
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];
|
|
|
|
|
|
2008-07-15 07:31:55 +00:00
|
|
|
|
/* FIXME ... what about UTF-16 sequences of more than one 16bit value
|
|
|
|
|
* corresponding to a single UCS-32 codepoint?
|
|
|
|
|
*/
|
2002-02-06 13:44:03 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-15 06:57:01 +00:00
|
|
|
|
GSRSFunc
|
|
|
|
|
GSPrivateRangeOfString(NSString *receiver, NSString *target)
|
|
|
|
|
{
|
|
|
|
|
Class c;
|
|
|
|
|
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(receiver);
|
2008-07-15 06:57:01 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)receiver)->_flags.wide == 1))
|
|
|
|
|
{
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(target);
|
2008-07-15 06:57:01 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)target)->_flags.wide == 1))
|
|
|
|
|
return (GSRSFunc)strRangeUsUs;
|
|
|
|
|
else if (GSObjCIsKindOf(c, GSCStringClass) == YES
|
|
|
|
|
|| c == NSConstantStringClass
|
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)target)->_flags.wide == 0))
|
|
|
|
|
return (GSRSFunc)strRangeUsCs;
|
|
|
|
|
else
|
|
|
|
|
return (GSRSFunc)strRangeUsNs;
|
|
|
|
|
}
|
|
|
|
|
else if (GSObjCIsKindOf(c, GSCStringClass) == YES
|
|
|
|
|
|| c == NSConstantStringClass
|
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)target)->_flags.wide == 0))
|
|
|
|
|
{
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(target);
|
2008-07-15 06:57:01 +00:00
|
|
|
|
if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
|
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)target)->_flags.wide == 1))
|
|
|
|
|
return (GSRSFunc)strRangeCsUs;
|
|
|
|
|
else if (GSObjCIsKindOf(c, GSCStringClass) == YES
|
|
|
|
|
|| c == NSConstantStringClass
|
|
|
|
|
|| (c == GSMutableStringClass && ((GSStr)target)->_flags.wide == 0))
|
|
|
|
|
return (GSRSFunc)strRangeCsCs;
|
|
|
|
|
else
|
|
|
|
|
return (GSRSFunc)strRangeCsNs;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (GSRSFunc)strRangeNsNs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(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;
|
|
|
|
|
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(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
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSCSubString *o;
|
2000-10-24 11:58:25 +00:00
|
|
|
|
|
2006-05-23 19:29:52 +00:00
|
|
|
|
if (aRange.length == 0)
|
|
|
|
|
{
|
|
|
|
|
return @"";
|
|
|
|
|
}
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o = (typeof(o))NSAllocateObject(GSCSubStringClass,
|
|
|
|
|
0, NSDefaultMallocZone());
|
|
|
|
|
o->_contents.c = self->_contents.c + aRange.location;
|
|
|
|
|
o->_count = aRange.length;
|
|
|
|
|
o->_flags.wide = 0;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 0;
|
2009-01-12 12:48:46 +00:00
|
|
|
|
ASSIGN(o->_parent, (id)self);
|
|
|
|
|
return AUTORELEASE((id)o);
|
2000-10-24 11:58:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSCSubString *o;
|
2000-10-24 11:58:25 +00:00
|
|
|
|
|
2006-05-23 19:29:52 +00:00
|
|
|
|
if (aRange.length == 0)
|
|
|
|
|
{
|
|
|
|
|
return @"";
|
|
|
|
|
}
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o = (typeof(o))NSAllocateObject(GSUnicodeSubStringClass,
|
|
|
|
|
0, NSDefaultMallocZone());
|
|
|
|
|
o->_contents.u = self->_contents.u + aRange.location;
|
|
|
|
|
o->_count = aRange.length;
|
|
|
|
|
o->_flags.wide = 1;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 0;
|
2009-01-12 12:48:46 +00:00
|
|
|
|
ASSIGN(o->_parent, (id)self);
|
|
|
|
|
return AUTORELEASE((id)o);
|
2000-10-24 11:58:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
2010-02-22 10:13:20 +00:00
|
|
|
|
Class c = object_getClass(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;
|
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
else if (internalEncoding == externalEncoding
|
|
|
|
|
&& [aString canBeConvertedToEncoding: internalEncoding] == 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.
|
2006-05-14 18:01:13 +00:00
|
|
|
|
* NB. If 'internalEncoding != externalEncoding' 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
|
|
|
|
|
{
|
2010-07-19 17:10:46 +00:00
|
|
|
|
setup(NO);
|
|
|
|
|
}
|
|
|
|
|
+ (void) reinitialize
|
|
|
|
|
{
|
|
|
|
|
setup(YES);
|
2000-11-06 19:55:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-28 13:23:53 +00:00
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-07 05:02:55 +00:00
|
|
|
|
/*
|
|
|
|
|
* Return a 28-bit hash value for the string contents - this
|
|
|
|
|
* MUST match the algorithm used by the NSString base class.
|
|
|
|
|
*/
|
2009-12-04 09:52:14 +00:00
|
|
|
|
- (NSUInteger) hash
|
|
|
|
|
{
|
|
|
|
|
if (self->_flags.hash == 0)
|
|
|
|
|
{
|
2009-12-07 05:02:55 +00:00
|
|
|
|
unsigned ret = 0;
|
|
|
|
|
unsigned len = self->_count;
|
|
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
|
{
|
|
|
|
|
register unsigned index = 0;
|
|
|
|
|
|
|
|
|
|
if (self->_flags.wide)
|
|
|
|
|
{
|
2009-12-07 05:10:50 +00:00
|
|
|
|
register const unichar *p = self->_contents.u;
|
2009-12-07 05:02:55 +00:00
|
|
|
|
|
|
|
|
|
while (index < len)
|
|
|
|
|
{
|
|
|
|
|
ret = (ret << 5) + ret + p[index++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
register const unsigned char *p = self->_contents.c;
|
|
|
|
|
|
|
|
|
|
if (internalEncoding == NSISOLatin1StringEncoding)
|
|
|
|
|
{
|
|
|
|
|
while (index < len)
|
|
|
|
|
{
|
|
|
|
|
ret = (ret << 5) + ret + p[index++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
while (index < len)
|
|
|
|
|
{
|
|
|
|
|
unichar u = p[index++];
|
|
|
|
|
|
|
|
|
|
if (u > 127)
|
|
|
|
|
{
|
|
|
|
|
unsigned char c = (unsigned char)u;
|
|
|
|
|
unsigned int s = 1;
|
|
|
|
|
unichar *d = &u;
|
|
|
|
|
|
|
|
|
|
GSToUnicode(&d, &s, &c, 1, internalEncoding, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
ret = (ret << 5) + ret + u;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
ret &= 0x0fffffff;
|
|
|
|
|
if (ret == 0)
|
|
|
|
|
{
|
|
|
|
|
ret = 0x0fffffff;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret = 0x0ffffffe; /* Hash for an empty string. */
|
|
|
|
|
}
|
|
|
|
|
self->_flags.hash = ret;
|
2009-12-04 09:52:14 +00:00
|
|
|
|
}
|
2009-12-07 05:02:55 +00:00
|
|
|
|
|
2009-12-04 09:52:14 +00:00
|
|
|
|
return self->_flags.hash;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
- (id) initWithBytes: (const void*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
2010-03-05 09:30:18 +00:00
|
|
|
|
void *tmp = NSZoneMalloc([self zone], length);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
|
|
|
|
|
memcpy(tmp, chars, length);
|
|
|
|
|
chars = tmp;
|
|
|
|
|
}
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return [self initWithBytesNoCopy: (void*)chars
|
2006-05-14 18:01:13 +00:00
|
|
|
|
length: length
|
|
|
|
|
encoding: encoding
|
|
|
|
|
freeWhenDone: YES];
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
- (id) initWithBytesNoCopy: (void*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
NSString *c = NSStringFromClass([self class]);
|
|
|
|
|
NSString *s = NSStringFromSelector(_cmd);
|
|
|
|
|
|
2010-02-25 18:49:31 +00:00
|
|
|
|
DESTROY(self);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"[%@-%@] called on string already initialised", c, s];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
- (id) initWithCharacters: (const unichar*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
|
|
|
|
return [self initWithBytes: chars
|
|
|
|
|
length: length * sizeof(unichar)
|
|
|
|
|
encoding: NSUnicodeStringEncoding];
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2000-10-09 05:32:50 +00:00
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return [self initWithBytesNoCopy: chars
|
|
|
|
|
length: length * sizeof(unichar)
|
|
|
|
|
encoding: NSUnicodeStringEncoding
|
|
|
|
|
freeWhenDone: flag];
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
- (id) initWithCString: (const char*)chars
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
|
|
|
|
return [self initWithBytes: chars
|
|
|
|
|
length: strlen(chars)
|
|
|
|
|
encoding: externalEncoding];
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
- (id) initWithCString: (const char*)chars
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
return [self initWithBytes: chars
|
|
|
|
|
length: strlen(chars)
|
|
|
|
|
encoding: encoding];
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
- (id) initWithCString: (const char*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
|
|
|
|
return [self initWithBytes: chars
|
|
|
|
|
length: length
|
|
|
|
|
encoding: externalEncoding];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCStringNoCopy: (char*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2000-10-09 05:32:50 +00:00
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return [self initWithBytesNoCopy: chars
|
|
|
|
|
length: length
|
|
|
|
|
encoding: externalEncoding
|
|
|
|
|
freeWhenDone: flag];
|
2003-11-05 02:11:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-28 13:23:53 +00:00
|
|
|
|
- (id) lowercaseString
|
2003-11-05 02:11:49 +00:00
|
|
|
|
{
|
2010-09-28 13:23:53 +00:00
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
|
|
|
|
GSUnicodeInlineString *o;
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
o = (typeof(o))NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
_count * sizeof(unichar), NSDefaultMallocZone());
|
|
|
|
|
o->_contents.u = (unichar*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSUnicodeInlineStringClass));
|
|
|
|
|
i = o->_count = _count;
|
|
|
|
|
while (i-- > 0)
|
|
|
|
|
{
|
|
|
|
|
o->_contents.u[i] = uni_tolower(_contents.u[i]);
|
|
|
|
|
}
|
|
|
|
|
o->_flags.wide = 1;
|
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
|
|
|
|
return [(id)o autorelease];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GSCInlineString *o;
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
o = (typeof(o))NSAllocateObject(GSCInlineStringClass,
|
|
|
|
|
_count, NSDefaultMallocZone());
|
|
|
|
|
o->_contents.c = (unsigned char*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSCInlineStringClass));
|
|
|
|
|
i = o->_count = _count;
|
|
|
|
|
while (i-- > 0)
|
|
|
|
|
{
|
|
|
|
|
o->_contents.c[i] = tolower(_contents.c[i]);
|
|
|
|
|
}
|
|
|
|
|
o->_flags.wide = 0;
|
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
|
|
|
|
return [(id)o autorelease];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) uppercaseString
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
|
|
|
|
GSUnicodeInlineString *o;
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
o = (typeof(o))NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
_count * sizeof(unichar), NSDefaultMallocZone());
|
|
|
|
|
o->_contents.u = (unichar*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSUnicodeInlineStringClass));
|
|
|
|
|
i = o->_count = _count;
|
|
|
|
|
while (i-- > 0)
|
|
|
|
|
{
|
|
|
|
|
o->_contents.u[i] = uni_toupper(_contents.u[i]);
|
|
|
|
|
}
|
|
|
|
|
o->_flags.wide = 1;
|
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
|
|
|
|
return [(id)o autorelease];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GSCInlineString *o;
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
o = (typeof(o))NSAllocateObject(GSCInlineStringClass,
|
|
|
|
|
_count, NSDefaultMallocZone());
|
|
|
|
|
o->_contents.c = (unsigned char*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSCInlineStringClass));
|
|
|
|
|
i = o->_count = _count;
|
|
|
|
|
while (i-- > 0)
|
|
|
|
|
{
|
|
|
|
|
o->_contents.c[i] = toupper(_contents.c[i]);
|
|
|
|
|
}
|
|
|
|
|
o->_flags.wide = 0;
|
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
|
|
|
|
return [(id)o autorelease];
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (unichar) characterAtIndex: (NSUInteger)index
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
options: (NSUInteger)mask
|
2000-10-09 05:32:50 +00:00
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2008-07-15 08:05:33 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] nil string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
if (GSObjCIsInstance(aString) == NO)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] not a string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
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
|
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return cString_c((GSStr)self, externalEncoding);
|
2005-05-08 07:08:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cStringUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
return cString_c((GSStr)self, encoding);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSUInteger) cStringLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return cStringLength_c((GSStr)self, externalEncoding);
|
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
|
|
|
|
|
{
|
2006-12-27 15:30:27 +00:00
|
|
|
|
if ([aCoder allowsKeyedCoding])
|
|
|
|
|
{
|
|
|
|
|
[(NSKeyedArchiver*)aCoder _encodePropertyList: self forKey: @"NS.string"];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &_count];
|
|
|
|
|
if (_count > 0)
|
|
|
|
|
{
|
2010-12-27 11:28:01 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(int)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
at: &internalEncoding];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[aCoder encodeArrayOfObjCType: @encode(unsigned char)
|
|
|
|
|
count: _count
|
|
|
|
|
at: _contents.c];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) fastestEncoding
|
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return internalEncoding;
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
maxLength: (NSUInteger)maxLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
maxLength: (NSUInteger)maxLength
|
2006-02-27 09:35:19 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
return getCStringE_c((GSStr)self, buffer, maxLength, encoding);
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (void) getCString: (char*)buffer
|
2009-02-23 20:42:32 +00:00
|
|
|
|
maxLength: (NSUInteger)maxLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 17:05:30 +00:00
|
|
|
|
- (int) intValue
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSUInteger) length
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
return _count;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSUInteger) lengthOfBytesUsingEncoding: (NSStringEncoding)encoding
|
2005-05-08 07:08:28 +00:00
|
|
|
|
{
|
|
|
|
|
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());
|
2006-05-12 17:46:32 +00:00
|
|
|
|
obj = [obj initWithBytes: (char*)_contents.c
|
|
|
|
|
length: _count
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: internalEncoding];
|
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);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
obj = [obj initWithBytes: (char*)_contents.c
|
|
|
|
|
length: _count
|
|
|
|
|
encoding: internalEncoding];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (NSUInteger)anIndex
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
options: (NSUInteger)mask
|
2002-02-06 13:44:03 +00:00
|
|
|
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
options: (NSUInteger)mask
|
2000-10-09 05:32:50 +00:00
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2008-07-15 07:58:49 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] nil string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
if (GSObjCIsInstance(aString) == NO)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] not a string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
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
|
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return internalEncoding;
|
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
|
2010-09-12 17:05:30 +00:00
|
|
|
|
- (int) _baseLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
if (!_flags.owned || NSShouldRetainWithZone(self, z) == NO)
|
2003-11-05 02:11:49 +00:00
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSCInlineString *o;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o = (typeof(o))NSAllocateObject(GSCInlineStringClass, _count, z);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
o->_contents.c = (unsigned char*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSCInlineStringClass));
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o->_count = _count;
|
|
|
|
|
memcpy(o->_contents.c, _contents.c, _count);
|
|
|
|
|
o->_flags.wide = 0;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return (id)o;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return RETAIN(self);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-11-05 02:11:49 +00:00
|
|
|
|
@implementation GSCBufferString
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2009-10-10 08:16:17 +00:00
|
|
|
|
if (_contents.c != 0)
|
2003-11-05 02:11:49 +00:00
|
|
|
|
{
|
2009-10-10 08:16:17 +00:00
|
|
|
|
if (_flags.owned)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(_contents.c), _contents.c);
|
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
_contents.c = 0;
|
|
|
|
|
}
|
2009-10-10 08:16:17 +00:00
|
|
|
|
[super dealloc];
|
2003-11-05 02:11:49 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSCInlineString
|
2000-10-29 14:52:33 +00:00
|
|
|
|
@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
|
|
|
|
|
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
|
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSCInlineString *o;
|
2000-12-21 11:55:30 +00:00
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o = (typeof(o))NSAllocateObject(GSCInlineStringClass, _count, z);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
o->_contents.c = (unsigned char*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSCInlineStringClass));
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o->_count = _count;
|
|
|
|
|
memcpy(o->_contents.c, _contents.c, _count);
|
|
|
|
|
o->_flags.wide = 0;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return (id)o;
|
2000-12-21 11:55:30 +00:00
|
|
|
|
}
|
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);
|
2009-10-10 08:16:17 +00:00
|
|
|
|
[super dealloc];
|
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
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (unichar) characterAtIndex: (NSUInteger)index
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
options: (NSUInteger)mask
|
2000-10-09 05:32:50 +00:00
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2008-07-15 08:05:33 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] nil string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
if (GSObjCIsInstance(aString) == NO)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] not a string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
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
|
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return cString_u((GSStr)self, externalEncoding);
|
2005-05-08 07:08:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cStringUsingEncoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
return cString_u((GSStr)self, encoding);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSUInteger) cStringLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return cStringLength_u((GSStr)self, externalEncoding);
|
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
|
|
|
|
|
{
|
2006-12-27 15:30:27 +00:00
|
|
|
|
if ([aCoder allowsKeyedCoding])
|
|
|
|
|
{
|
|
|
|
|
[(NSKeyedArchiver*)aCoder _encodePropertyList: self forKey: @"NS.string"];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &_count];
|
|
|
|
|
if (_count > 0)
|
|
|
|
|
{
|
|
|
|
|
NSStringEncoding enc = NSUnicodeStringEncoding;
|
|
|
|
|
|
2010-12-27 11:28:01 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(int) at: &enc];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
maxLength: (NSUInteger)maxLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
maxLength: (NSUInteger)maxLength
|
2006-02-27 09:35:19 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
{
|
|
|
|
|
return getCStringE_u((GSStr)self, buffer, maxLength, encoding);
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
- (void) getCString: (char*)buffer
|
2009-02-23 20:42:32 +00:00
|
|
|
|
maxLength: (NSUInteger)maxLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 17:05:30 +00:00
|
|
|
|
- (int) intValue
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSUInteger) length
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
return _count;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSUInteger) lengthOfBytesUsingEncoding: (NSStringEncoding)encoding
|
2005-05-08 07:08:28 +00:00
|
|
|
|
{
|
|
|
|
|
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());
|
2006-05-20 14:52:38 +00:00
|
|
|
|
obj = [obj initWithBytes: (const void*)_contents.u
|
|
|
|
|
length: _count * sizeof(unichar)
|
|
|
|
|
encoding: NSUnicodeStringEncoding];
|
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);
|
2006-05-20 14:52:38 +00:00
|
|
|
|
obj = [obj initWithBytes: (const void*)_contents.u
|
|
|
|
|
length: _count * sizeof(unichar)
|
|
|
|
|
encoding: NSUnicodeStringEncoding];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (NSUInteger)anIndex
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
options: (NSUInteger)mask
|
2002-02-06 13:44:03 +00:00
|
|
|
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
options: (NSUInteger)mask
|
2000-10-09 05:32:50 +00:00
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2008-07-15 07:58:49 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] nil string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
if (GSObjCIsInstance(aString) == NO)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] not a string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
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
|
2010-09-12 17:05:30 +00:00
|
|
|
|
- (int) _baseLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
if (!_flags.owned || NSShouldRetainWithZone(self, z) == NO)
|
2003-11-05 02:11:49 +00:00
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSUnicodeInlineString *o;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o = (typeof(o))NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
_count * sizeof(unichar), z);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
o->_contents.u = (unichar*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSUnicodeInlineStringClass));
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o->_count = _count;
|
|
|
|
|
memcpy(o->_contents.u, _contents.u, _count * sizeof(unichar));
|
|
|
|
|
o->_flags.wide = 1;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return (id)o;
|
2003-11-05 02:11:49 +00:00
|
|
|
|
}
|
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
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2009-10-10 08:16:17 +00:00
|
|
|
|
if (_contents.u != 0)
|
2003-11-05 02:11:49 +00:00
|
|
|
|
{
|
2009-10-10 08:16:17 +00:00
|
|
|
|
if (_flags.owned)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(_contents.u), _contents.u);
|
|
|
|
|
}
|
2003-11-05 02:11:49 +00:00
|
|
|
|
_contents.u = 0;
|
|
|
|
|
}
|
2009-10-10 08:16:17 +00:00
|
|
|
|
[super dealloc];
|
2003-11-05 02:11:49 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSUnicodeInlineString
|
2000-10-29 14:52:33 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2000-11-03 10:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSUnicodeSubString
|
2003-11-05 02:11:49 +00:00
|
|
|
|
|
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
|
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSUnicodeInlineString *o;
|
2000-12-21 11:55:30 +00:00
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o = (typeof(o))NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
_count * sizeof(unichar), z);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
o->_contents.u = (unichar*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSUnicodeInlineStringClass));
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o->_count = _count;
|
|
|
|
|
memcpy(o->_contents.u, _contents.u, _count * sizeof(unichar));
|
|
|
|
|
o->_flags.wide = 1;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return (id)o;
|
2000-12-21 11:55:30 +00:00
|
|
|
|
}
|
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);
|
2009-10-10 08:16:17 +00:00
|
|
|
|
[super dealloc];
|
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.
|
|
|
|
|
*/
|
|
|
|
|
@implementation GSMutableString
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
2010-07-19 17:10:46 +00:00
|
|
|
|
setup(NO);
|
2009-12-07 05:10:50 +00:00
|
|
|
|
GSObjCAddClassBehavior(self, [GSString class]);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
2006-10-20 10:56:27 +00:00
|
|
|
|
* unichars for passing to GSPrivateFormat. Use on-stack memory for
|
|
|
|
|
* performance 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
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
fmt = NSZoneMalloc(NSDefaultMallocZone(), (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
|
2010-03-05 09:30:18 +00:00
|
|
|
|
_zone = [self zone];
|
2004-05-14 10:52:30 +00:00
|
|
|
|
#endif
|
2001-02-02 06:14:42 +00:00
|
|
|
|
}
|
2006-10-20 10:56:27 +00:00
|
|
|
|
GSPrivateFormat((GSStr)self, fmt, ap, nil);
|
2004-05-14 10:52:30 +00:00
|
|
|
|
_flags.hash = 0; // Invalidate the hash for this string.
|
|
|
|
|
if (fmt != buf)
|
2001-02-02 06:14:42 +00:00
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), 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
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (unichar) characterAtIndex: (NSUInteger)index
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
options: (NSUInteger)mask
|
2000-10-09 05:32:50 +00:00
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2008-07-15 08:05:33 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] nil string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
if (GSObjCIsInstance(aString) == NO)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] not a string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSUnicodeInlineString *o;
|
2006-05-20 14:52:38 +00:00
|
|
|
|
|
|
|
|
|
o = (typeof(o))NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
_count * sizeof(unichar), z);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
o->_contents.u = (unichar*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSUnicodeInlineStringClass));
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o->_count = _count;
|
|
|
|
|
memcpy(o->_contents.u, _contents.u, _count * sizeof(unichar));
|
|
|
|
|
o->_flags.wide = 1;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return (id)o;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSCInlineString *o;
|
2006-05-20 14:52:38 +00:00
|
|
|
|
|
|
|
|
|
o = (typeof(o))NSAllocateObject(GSCInlineStringClass, _count, z);
|
2010-02-14 10:48:10 +00:00
|
|
|
|
o->_contents.c = (unsigned char*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSCInlineStringClass));
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o->_count = _count;
|
|
|
|
|
memcpy(o->_contents.c, _contents.c, _count);
|
|
|
|
|
o->_flags.wide = 0;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return (id)o;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cString
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return cString_u((GSStr)self, externalEncoding);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return cString_c((GSStr)self, externalEncoding);
|
2005-05-08 07:08:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (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
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSUInteger) cStringLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return cStringLength_u((GSStr)self, externalEncoding);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return cStringLength_c((GSStr)self, externalEncoding);
|
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
|
|
|
|
|
{
|
2006-06-17 06:51:53 +00:00
|
|
|
|
if (_contents.c != 0)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2010-03-08 12:11:03 +00:00
|
|
|
|
NSAssert(_flags.owned == 1 && _zone != 0, NSInternalInconsistencyException);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
NSZoneFree(self->_zone, self->_contents.c);
|
|
|
|
|
self->_contents.c = 0;
|
|
|
|
|
self->_zone = 0;
|
|
|
|
|
}
|
2009-10-10 08:16:17 +00:00
|
|
|
|
[super dealloc];
|
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
|
|
|
|
|
{
|
2006-12-27 15:30:27 +00:00
|
|
|
|
if ([aCoder allowsKeyedCoding])
|
|
|
|
|
{
|
|
|
|
|
[(NSKeyedArchiver*)aCoder _encodePropertyList: self forKey: @"NS.string"];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &_count];
|
|
|
|
|
if (_count > 0)
|
|
|
|
|
{
|
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
|
|
|
|
NSStringEncoding enc = NSUnicodeStringEncoding;
|
|
|
|
|
|
2010-12-27 11:28:01 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(int) at: &enc];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
[aCoder encodeArrayOfObjCType: @encode(unichar)
|
|
|
|
|
count: _count
|
|
|
|
|
at: _contents.u];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2010-12-27 11:28:01 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(int)
|
2006-05-14 18:01:13 +00:00
|
|
|
|
at: &internalEncoding];
|
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
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return internalEncoding;
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
maxLength: (NSUInteger)maxLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
maxLength: (NSUInteger)maxLength
|
2006-02-27 09:35:19 +00:00
|
|
|
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
maxLength: (NSUInteger)maxLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
return [self initWithCapacity: 0];
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-12 17:46:32 +00:00
|
|
|
|
- (id) initWithBytes: (const void*)bytes
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-12 17:46:32 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
unsigned char *chars = 0;
|
|
|
|
|
BOOL isASCII = NO;
|
|
|
|
|
BOOL isLatin1 = NO;
|
|
|
|
|
BOOL shouldFree = NO;
|
2006-05-12 17:46:32 +00:00
|
|
|
|
|
2009-04-10 08:25:03 +00:00
|
|
|
|
_flags.owned = YES;
|
2006-06-17 06:51:53 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
_zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
2010-03-05 09:30:18 +00:00
|
|
|
|
_zone = [self zone];
|
2006-06-17 06:51:53 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
|
|
|
|
fixBOM((unsigned char**)&bytes, &length, &shouldFree, encoding);
|
|
|
|
|
chars = (unsigned char*)bytes;
|
|
|
|
|
}
|
2006-05-12 17:46:32 +00:00
|
|
|
|
|
2006-06-22 22:36:46 +00:00
|
|
|
|
if (encoding == NSUTF8StringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (chars[i] > 127)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (i == length)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* This is actually ASCII data ... so we can just store it as if
|
|
|
|
|
* in the internal 8bit encoding scheme.
|
|
|
|
|
*/
|
|
|
|
|
encoding = internalEncoding;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-10-09 14:00:01 +00:00
|
|
|
|
else if (encoding != internalEncoding && isByteEncoding(encoding) == YES)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2006-05-12 17:46:32 +00:00
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (((unsigned char*)chars)[i] > 127)
|
2006-05-12 17:46:32 +00:00
|
|
|
|
{
|
|
|
|
|
if (encoding == NSASCIIStringEncoding)
|
|
|
|
|
{
|
2010-02-25 18:49:31 +00:00
|
|
|
|
DESTROY(self);
|
2006-06-17 06:51:53 +00:00
|
|
|
|
if (shouldFree == YES)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
|
|
|
|
}
|
2006-05-12 17:46:32 +00:00
|
|
|
|
return nil; // Invalid data
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (i == length)
|
|
|
|
|
{
|
|
|
|
|
/*
|
2006-05-14 18:01:13 +00:00
|
|
|
|
* This is actually ASCII data ... so we can just store it as if
|
2006-05-12 17:46:32 +00:00
|
|
|
|
* in the internal 8bit encoding scheme.
|
|
|
|
|
*/
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding = internalEncoding;
|
2006-05-12 17:46:32 +00:00
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2006-05-12 17:46:32 +00:00
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (encoding == internalEncoding)
|
2006-05-12 17:46:32 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (shouldFree == YES)
|
|
|
|
|
{
|
2006-06-17 06:51:53 +00:00
|
|
|
|
_zone = NSZoneFromPointer(chars);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
_contents.c = chars;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2006-06-17 06:51:53 +00:00
|
|
|
|
_contents.c = NSZoneMalloc(_zone, length);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
memcpy(_contents.c, chars, length);
|
|
|
|
|
}
|
2006-05-12 17:46:32 +00:00
|
|
|
|
_count = length;
|
|
|
|
|
_flags.wide = 0;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2006-05-12 17:46:32 +00:00
|
|
|
|
/*
|
|
|
|
|
* Any remaining encoding needs to be converted to UTF-16.
|
|
|
|
|
*/
|
|
|
|
|
if (encoding != NSUnicodeStringEncoding)
|
|
|
|
|
{
|
|
|
|
|
unichar *u = 0;
|
|
|
|
|
unsigned l = 0;
|
2006-03-26 10:59:57 +00:00
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (GSToUnicode(&u, &l, (unsigned char*)chars, length, encoding,
|
2006-06-17 06:51:53 +00:00
|
|
|
|
_zone, 0) == NO)
|
2006-05-12 17:46:32 +00:00
|
|
|
|
{
|
2010-02-25 18:49:31 +00:00
|
|
|
|
DESTROY(self);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (shouldFree == YES)
|
2006-05-12 17:46:32 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
2006-05-12 17:46:32 +00:00
|
|
|
|
}
|
|
|
|
|
return nil; // Invalid data
|
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
chars = (unsigned char*)u;
|
2006-05-12 17:46:32 +00:00
|
|
|
|
length = l * sizeof(unichar);
|
2006-05-14 18:01:13 +00:00
|
|
|
|
shouldFree = YES;
|
2006-05-12 17:46:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
length /= sizeof(unichar);
|
2011-02-19 15:34:21 +00:00
|
|
|
|
if (GSUnicode((unichar*)(void*)chars, length, &isASCII, &isLatin1) != length)
|
2006-03-26 10:59:57 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (shouldFree == YES && chars != 0)
|
2006-03-28 06:05:04 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
2006-03-28 06:05:04 +00:00
|
|
|
|
}
|
2006-03-26 10:59:57 +00:00
|
|
|
|
return nil; // Invalid data
|
|
|
|
|
}
|
2006-05-12 17:46:32 +00:00
|
|
|
|
|
2006-03-26 10:59:57 +00:00
|
|
|
|
if (isASCII == YES
|
2006-05-14 18:01:13 +00:00
|
|
|
|
|| (internalEncoding == NSISOLatin1StringEncoding && isLatin1 == YES))
|
2006-03-26 10:59:57 +00:00
|
|
|
|
{
|
2006-06-17 06:51:53 +00:00
|
|
|
|
_contents.c = NSZoneMalloc(_zone, length);
|
2006-03-28 06:05:04 +00:00
|
|
|
|
_count = length;
|
|
|
|
|
_flags.wide = 0;
|
|
|
|
|
while (length-- > 0)
|
|
|
|
|
{
|
2011-02-19 15:34:21 +00:00
|
|
|
|
_contents.c[length] = ((unichar*)(void*)chars)[length];
|
2006-03-28 06:05:04 +00:00
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (shouldFree == YES && chars != 0)
|
2006-03-28 06:05:04 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(chars), chars);
|
2006-03-28 06:05:04 +00:00
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
return self;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (shouldFree == YES)
|
|
|
|
|
{
|
2006-06-17 06:51:53 +00:00
|
|
|
|
_zone = NSZoneFromPointer(chars);
|
2011-02-19 15:34:21 +00:00
|
|
|
|
_contents.u = (unichar*)(void*)chars;
|
2006-05-14 18:01:13 +00:00
|
|
|
|
}
|
2006-03-28 06:05:04 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2006-06-17 06:51:53 +00:00
|
|
|
|
_contents.u = NSZoneMalloc(_zone, length * sizeof(unichar));
|
2006-05-14 18:01:13 +00:00
|
|
|
|
memcpy(_contents.u, chars, length * sizeof(unichar));
|
2006-03-28 06:05:04 +00:00
|
|
|
|
}
|
2006-05-14 18:01:13 +00:00
|
|
|
|
_count = length;
|
|
|
|
|
_flags.wide = 1;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
- (id) initWithBytesNoCopy: (void*)bytes
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithBytes: bytes
|
|
|
|
|
length: length
|
|
|
|
|
encoding: encoding];
|
|
|
|
|
if (flag == YES && bytes != 0)
|
|
|
|
|
{
|
2006-05-20 14:52:38 +00:00
|
|
|
|
NSZoneFree(NSZoneFromPointer(bytes), bytes);
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (id) initWithCapacity: (NSUInteger)capacity
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2006-05-12 17:46:32 +00:00
|
|
|
|
if (capacity < 2)
|
2003-05-07 13:15:15 +00:00
|
|
|
|
{
|
2006-05-12 17:46:32 +00:00
|
|
|
|
capacity = 2;
|
2003-05-07 13:15:15 +00:00
|
|
|
|
}
|
2006-05-12 17:46:32 +00:00
|
|
|
|
_count = 0;
|
|
|
|
|
_capacity = capacity;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
#if GS_WITH_GC
|
2006-05-12 17:46:32 +00:00
|
|
|
|
_zone = GSAtomicMallocZone();
|
2000-10-09 05:32:50 +00:00
|
|
|
|
#else
|
2010-03-05 09:30:18 +00:00
|
|
|
|
_zone = [self zone];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
#endif
|
2006-05-12 17:46:32 +00:00
|
|
|
|
_contents.c = NSZoneMalloc(_zone, capacity + 1);
|
2003-05-07 13:15:15 +00:00
|
|
|
|
_flags.wide = 0;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
_flags.owned = 1;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-12 17:46:32 +00:00
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-12 17:46:32 +00:00
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
return [self initWithBytesNoCopy: (void*)chars
|
|
|
|
|
length: length*sizeof(unichar)
|
|
|
|
|
encoding: NSUnicodeStringEncoding
|
|
|
|
|
freeWhenDone: flag];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCStringNoCopy: (char*)chars
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-12 17:46:32 +00:00
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
return [self initWithBytesNoCopy: (void*)chars
|
|
|
|
|
length: length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: externalEncoding
|
2006-05-12 17:46:32 +00:00
|
|
|
|
freeWhenDone: flag];
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
fmt = NSZoneMalloc(NSDefaultMallocZone(), (len+1)*sizeof(unichar));
|
2004-06-05 10:06:56 +00:00
|
|
|
|
}
|
|
|
|
|
[format getCharacters: fmt];
|
|
|
|
|
fmt[len] = '\0';
|
|
|
|
|
|
2006-10-20 10:56:27 +00:00
|
|
|
|
GSPrivateFormat((GSStr)self, fmt, argList, locale);
|
2004-08-19 14:50:44 +00:00
|
|
|
|
if (fmt != fbuf)
|
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), fmt);
|
2004-08-19 14:50:44 +00:00
|
|
|
|
}
|
2004-06-05 10:06:56 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 17:05:30 +00:00
|
|
|
|
- (int) intValue
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSUInteger) length
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
return _count;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSUInteger) lengthOfBytesUsingEncoding: (NSStringEncoding)encoding
|
2005-05-08 07:08:28 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
{
|
2009-04-10 08:25:03 +00:00
|
|
|
|
NSAssert(_flags.owned == 1 && _zone != 0, NSInternalInconsistencyException);
|
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)
|
2006-05-20 14:52:38 +00:00
|
|
|
|
obj = [obj initWithBytes: (void*)_contents.u
|
|
|
|
|
length: _count * sizeof(unichar)
|
|
|
|
|
encoding: NSUnicodeStringEncoding];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2006-05-20 14:52:38 +00:00
|
|
|
|
obj = [obj initWithBytes: (void*)_contents.c
|
2006-05-12 17:46:32 +00:00
|
|
|
|
length: _count
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: internalEncoding];
|
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)
|
2006-05-20 14:52:38 +00:00
|
|
|
|
obj = [obj initWithBytes: (void*)_contents.u
|
|
|
|
|
length: _count * sizeof(unichar)
|
|
|
|
|
encoding: NSUnicodeStringEncoding];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
else
|
2006-05-12 17:46:32 +00:00
|
|
|
|
obj = [obj initWithBytes: (char*)_contents.c
|
|
|
|
|
length: _count
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: internalEncoding];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSRange) rangeOfComposedCharacterSequenceAtIndex: (NSUInteger)anIndex
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
options: (NSUInteger)mask
|
2002-02-06 13:44:03 +00:00
|
|
|
|
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
|
2009-02-23 20:42:32 +00:00
|
|
|
|
options: (NSUInteger)mask
|
2000-10-09 05:32:50 +00:00
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
2008-07-15 07:58:49 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
|
|
|
|
if (aString == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] nil string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
if (GSObjCIsInstance(aString) == NO)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@ -%@] not a string argument",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2009-02-23 20:42:32 +00:00
|
|
|
|
makeHole((GSStr)self, NSMaxRange(aRange), (NSUInteger)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)
|
|
|
|
|
{
|
|
|
|
|
/*
|
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]
|
2006-06-02 05:25:21 +00:00
|
|
|
|
maxLength: length+1
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: internalEncoding];
|
2002-05-11 07:50:47 +00:00
|
|
|
|
_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]
|
2006-06-02 05:25:21 +00:00
|
|
|
|
maxLength: l+1
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: internalEncoding];
|
2002-05-11 07:50:47 +00:00
|
|
|
|
}
|
|
|
|
|
u = [aString characterAtIndex: l];
|
2006-08-12 16:33:39 +00:00
|
|
|
|
GSFromUnicode(&dst, &size, &u, 1,
|
|
|
|
|
internalEncoding, 0, GSUniStrict);
|
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)
|
|
|
|
|
{
|
2009-02-23 20:42:32 +00:00
|
|
|
|
makeHole((GSStr)self, _count, (NSUInteger)(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;
|
2006-08-12 15:09:23 +00:00
|
|
|
|
unsigned s = 1;
|
|
|
|
|
unichar u;
|
|
|
|
|
unsigned char *d;
|
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)
|
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
[aString getCString: (char*)_contents.c
|
2006-06-02 05:25:21 +00:00
|
|
|
|
maxLength: l+1
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: internalEncoding];
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
2006-08-12 15:09:23 +00:00
|
|
|
|
u = [aString characterAtIndex: l];
|
|
|
|
|
d = _contents.c + l;
|
2006-08-12 16:33:39 +00:00
|
|
|
|
GSFromUnicode(&d, &s, &u, 1, internalEncoding, 0, GSUniStrict);
|
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
|
2006-05-14 18:01:13 +00:00
|
|
|
|
{
|
|
|
|
|
return internalEncoding;
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-24 11:58:25 +00:00
|
|
|
|
- (NSString*) substringFromRange: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2000-11-06 14:15:27 +00:00
|
|
|
|
|
2006-05-23 19:29:52 +00:00
|
|
|
|
if (aRange.length == 0)
|
|
|
|
|
{
|
|
|
|
|
return @"";
|
|
|
|
|
}
|
2000-10-24 11:58:25 +00:00
|
|
|
|
if (_flags.wide == 1)
|
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSUnicodeInlineString *o;
|
2006-05-20 14:52:38 +00:00
|
|
|
|
|
|
|
|
|
o = (typeof(o))NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
aRange.length * sizeof(unichar), NSDefaultMallocZone());
|
2010-02-14 10:48:10 +00:00
|
|
|
|
o->_contents.u = (unichar*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSUnicodeInlineStringClass));
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o->_count = aRange.length;
|
|
|
|
|
memcpy(o->_contents.u, _contents.u + aRange.location,
|
|
|
|
|
aRange.length * sizeof(unichar));
|
|
|
|
|
o->_flags.wide = 1;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return AUTORELEASE((id)o);
|
2000-10-24 11:58:25 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSCInlineString *o;
|
2006-05-20 14:52:38 +00:00
|
|
|
|
|
|
|
|
|
o = (typeof(o))NSAllocateObject(GSCInlineStringClass,
|
|
|
|
|
aRange.length, NSDefaultMallocZone());
|
2010-02-14 10:48:10 +00:00
|
|
|
|
o->_contents.c = (unsigned char*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSCInlineStringClass));
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o->_count = aRange.length;
|
|
|
|
|
memcpy(o->_contents.c, _contents.c + aRange.location, aRange.length);
|
|
|
|
|
o->_flags.wide = 0;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return AUTORELEASE((id)o);
|
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
|
|
|
|
{
|
|
|
|
|
GS_RANGE_CHECK(aRange, _count);
|
2000-11-06 14:15:27 +00:00
|
|
|
|
|
2006-05-23 19:29:52 +00:00
|
|
|
|
if (aRange.length == 0)
|
|
|
|
|
{
|
|
|
|
|
return @"";
|
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
if (_flags.wide == 1)
|
2000-10-20 10:30:51 +00:00
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSUnicodeInlineString *o;
|
2006-05-20 14:52:38 +00:00
|
|
|
|
|
|
|
|
|
o = (typeof(o))NSAllocateObject(GSUnicodeInlineStringClass,
|
|
|
|
|
aRange.length * sizeof(unichar), NSDefaultMallocZone());
|
2010-02-14 10:48:10 +00:00
|
|
|
|
o->_contents.u = (unichar*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSUnicodeInlineStringClass));
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o->_count = aRange.length;
|
|
|
|
|
memcpy(o->_contents.u, _contents.u + aRange.location,
|
|
|
|
|
aRange.length * sizeof(unichar));
|
|
|
|
|
o->_flags.wide = 1;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return AUTORELEASE((id)o);
|
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
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
|
GSCInlineString *o;
|
2006-05-20 14:52:38 +00:00
|
|
|
|
|
|
|
|
|
o = (typeof(o))NSAllocateObject(GSCInlineStringClass,
|
|
|
|
|
aRange.length, NSDefaultMallocZone());
|
2010-02-14 10:48:10 +00:00
|
|
|
|
o->_contents.c = (unsigned char*)
|
|
|
|
|
(((void*)o)+class_getInstanceSize(GSCInlineStringClass));
|
2006-05-20 14:52:38 +00:00
|
|
|
|
o->_count = aRange.length;
|
|
|
|
|
memcpy(o->_contents.c, _contents.c + aRange.location, aRange.length);
|
|
|
|
|
o->_flags.wide = 0;
|
2009-04-10 08:25:03 +00:00
|
|
|
|
o->_flags.owned = 1; // Ignored on dealloc, but means we own buffer
|
2006-05-20 14:52:38 +00:00
|
|
|
|
return AUTORELEASE((id)o);
|
2000-10-20 10:30:51 +00:00
|
|
|
|
}
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// private method for Unicode level 3 implementation
|
2010-09-12 17:05:30 +00:00
|
|
|
|
- (int) _baseLength
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
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-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
|
|
|
|
|
2006-05-20 14:52:38 +00:00
|
|
|
|
- (id) initWithBytes: (const void*)bytes
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
[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;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
- (id) initWithBytesNoCopy: (void*)bytes
|
2009-02-23 20:42:32 +00:00
|
|
|
|
length: (NSUInteger)length
|
2006-05-14 18:01:13 +00:00
|
|
|
|
encoding: (NSStringEncoding)encoding
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
[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
|
|
|
|
|
{
|
2006-06-04 06:42:10 +00:00
|
|
|
|
GSNOSUPERDEALLOC;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (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.
|
|
|
|
|
*/
|
2009-02-23 20:42:32 +00:00
|
|
|
|
- (NSUInteger) hash
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
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
|
|
|
|
{
|
2009-06-06 16:04:32 +00:00
|
|
|
|
register const unsigned char *p;
|
|
|
|
|
register unsigned index = 0;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
|
2002-03-13 13:46:12 +00:00
|
|
|
|
p = _self->_contents.c;
|
2009-06-06 16:04:32 +00:00
|
|
|
|
if (internalEncoding == NSISOLatin1StringEncoding)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2009-06-06 16:04:32 +00:00
|
|
|
|
while (index < len)
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
2009-06-06 16:04:32 +00:00
|
|
|
|
ret = (ret << 5) + ret + p[index++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
while (index < len)
|
|
|
|
|
{
|
|
|
|
|
unichar u = p[index++];
|
|
|
|
|
|
|
|
|
|
if (u > 127)
|
|
|
|
|
{
|
|
|
|
|
unsigned char c = (unsigned char)u;
|
|
|
|
|
unsigned int s = 1;
|
|
|
|
|
unichar *d = &u;
|
2006-08-12 15:09:23 +00:00
|
|
|
|
|
2009-06-06 16:04:32 +00:00
|
|
|
|
GSToUnicode(&d, &s, &c, 1, internalEncoding, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
ret = (ret << 5) + ret + u;
|
2000-10-09 05:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(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;
|
|
|
|
|
}
|
2010-02-08 17:52:36 +00:00
|
|
|
|
else if (YES == [anObject isKindOfClass: NSStringClass]) // may be proxy
|
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;
|
|
|
|
|
}
|
2010-02-22 10:13:20 +00:00
|
|
|
|
c = object_getClass(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;
|
|
|
|
|
}
|
2010-02-08 17:52:36 +00:00
|
|
|
|
else if (YES == [anObject isKindOfClass: NSStringClass]) // may be proxy
|
2000-10-09 05:32:50 +00:00
|
|
|
|
{
|
|
|
|
|
return (*equalImp)(self, equalSel, anObject);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
2004-05-14 10:52:30 +00:00
|
|
|
|
/**
|
|
|
|
|
* Append characters to a string.
|
|
|
|
|
*/
|
2006-10-20 12:57:59 +00:00
|
|
|
|
void
|
|
|
|
|
GSPrivateStrAppendUnichars(GSStr s, const unichar *u, unsigned l)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Make the string wide if necessary.
|
|
|
|
|
*/
|
|
|
|
|
if (s->_flags.wide == 0)
|
|
|
|
|
{
|
|
|
|
|
BOOL widen = NO;
|
|
|
|
|
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (internalEncoding == NSISOLatin1StringEncoding)
|
2004-05-14 10:52:30 +00:00
|
|
|
|
{
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-20 12:57:59 +00:00
|
|
|
|
void
|
|
|
|
|
GSPrivateStrExternalize(GSStr s)
|
2004-06-05 09:34:41 +00:00
|
|
|
|
{
|
2006-05-14 18:01:13 +00:00
|
|
|
|
if (s->_flags.wide == 0 && internalEncoding != externalEncoding)
|
2004-06-05 09:34:41 +00:00
|
|
|
|
{
|
|
|
|
|
GSStrWiden(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|