2001-12-17 14:31:42 +00:00
|
|
|
|
/** NSCharacterSet - Character set holder
|
1998-11-10 20:16:33 +00:00
|
|
|
|
Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
|
1995-05-05 21:03:04 +00:00
|
|
|
|
|
|
|
|
|
Written by: Adam Fedor <fedor@boulder.colorado.edu>
|
|
|
|
|
Date: Apr 1995
|
2007-02-21 10:02:26 +00:00
|
|
|
|
Updates by: Richard Frith-Macdonald <rfm@gnu.org>
|
1995-05-05 21:03:04 +00:00
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
|
This file is part of the GNUstep Base Library.
|
1995-05-05 21:03:04 +00:00
|
|
|
|
|
|
|
|
|
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
|
1995-05-05 21:03:04 +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
|
|
|
|
|
1995-05-05 21:03:04 +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
|
1995-05-05 21:03:04 +00:00
|
|
|
|
License along with this library; if not, write to the Free
|
2006-04-01 06:53:24 +00:00
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
|
|
<title>NSCharacterSet class reference</title>
|
|
|
|
|
$Date$ $Revision$
|
1995-05-05 21:03:04 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
|
#import "common.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
|
#import "GNUstepBase/GSLock.h"
|
|
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
|
#import "Foundation/NSCoder.h"
|
|
|
|
|
#import "Foundation/NSException.h"
|
|
|
|
|
#import "Foundation/NSData.h"
|
|
|
|
|
#import "Foundation/NSLock.h"
|
|
|
|
|
#import "Foundation/NSDictionary.h"
|
|
|
|
|
#import "Foundation/NSIndexSet.h"
|
|
|
|
|
#import "Foundation/NSThread.h"
|
|
|
|
|
#import "Foundation/NSNotification.h"
|
|
|
|
|
#import "Foundation/NSCharacterSet.h"
|
|
|
|
|
#import "Foundation/NSData.h"
|
|
|
|
|
#import "GNUstepBase/NSObject+GNUstepBase.h"
|
1995-05-05 21:03:04 +00:00
|
|
|
|
|
2010-09-24 11:05:11 +00:00
|
|
|
|
/* Using and index set to hold a characterset is more space efficient but
|
|
|
|
|
* on the intel core-2 system I benchmarked on, it made my applications
|
|
|
|
|
* about 20% slower.
|
|
|
|
|
* It only makes sense to build base to use index charactersets on machines
|
|
|
|
|
* with a very low memory (eg PDAs), and possibly not even there.
|
|
|
|
|
*/
|
|
|
|
|
//#define GNUSTEP_INDEX_CHARSET 1
|
|
|
|
|
#undef GNUSTEP_INDEX_CHARSET
|
2007-02-21 10:02:26 +00:00
|
|
|
|
|
2010-02-17 11:47:06 +00:00
|
|
|
|
#import "NSCharacterSetData.h"
|
1995-05-05 21:03:04 +00:00
|
|
|
|
|
2010-09-24 11:05:11 +00:00
|
|
|
|
#define GSUNICODE_MAX 1114112
|
|
|
|
|
#define GSBITMAP_SIZE 8192
|
|
|
|
|
#define GSBITMAP_MAX 139264
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
2010-09-24 11:05:11 +00:00
|
|
|
|
#ifndef GSSETBIT
|
|
|
|
|
#define GSSETBIT(a,i) ((a) |= 1<<(i))
|
|
|
|
|
#define GSCLRBIT(a,i) ((a) &= ~(1<<(i)))
|
|
|
|
|
#define GSISSET(a,i) ((a) & (1<<(i)))
|
2005-03-07 10:47:10 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2007-02-21 10:02:26 +00:00
|
|
|
|
@interface _GSIndexCharSet : NSCharacterSet
|
|
|
|
|
{
|
|
|
|
|
NSMutableIndexSet *indexes;
|
|
|
|
|
}
|
|
|
|
|
- (NSIndexSet*) _indexes;
|
|
|
|
|
- (id) initWithBitmap: (NSData*)d;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface _GSMutableIndexCharSet : NSMutableCharacterSet
|
|
|
|
|
{
|
|
|
|
|
NSMutableIndexSet *indexes;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2005-07-08 11:48:37 +00:00
|
|
|
|
@interface NSDataStatic : NSObject // Help the compiler
|
|
|
|
|
@end
|
2005-03-08 11:32:24 +00:00
|
|
|
|
|
2007-02-21 10:02:26 +00:00
|
|
|
|
/* Private class from NSIndexSet.m
|
|
|
|
|
*/
|
|
|
|
|
@interface _GSStaticIndexSet : NSIndexSet
|
2010-09-15 22:16:18 +00:00
|
|
|
|
- (id) _initWithBytes: (const void*)bytes length: (NSUInteger)length;
|
2007-02-21 10:02:26 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2005-03-07 10:47:10 +00:00
|
|
|
|
@interface NSBitmapCharSet : NSCharacterSet
|
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
const unsigned char *_data;
|
|
|
|
|
unsigned _length;
|
|
|
|
|
NSData *_obj;
|
|
|
|
|
unsigned _known;
|
|
|
|
|
unsigned _present;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
- (id) initWithBitmap: (NSData*)bitmap;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface NSMutableBitmapCharSet : NSMutableCharacterSet
|
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
unsigned char *_data;
|
|
|
|
|
unsigned _length;
|
|
|
|
|
NSMutableData *_obj;
|
|
|
|
|
unsigned _known;
|
|
|
|
|
unsigned _present;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
- (id) initWithBitmap: (NSData*)bitmap;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation NSBitmapCharSet
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (NSData*) bitmapRepresentation
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
2006-05-12 15:13:12 +00:00
|
|
|
|
unsigned i = 17;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
|
|
|
|
|
while (i > 0 && [self hasMemberInPlane: i-1] == NO)
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
i--;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
2010-09-24 11:05:11 +00:00
|
|
|
|
i *= GSBITMAP_SIZE;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
if (i < _length)
|
|
|
|
|
{
|
|
|
|
|
return [NSData dataWithBytes: _data length: i];
|
|
|
|
|
}
|
|
|
|
|
return _obj;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) characterIsMember: (unichar)aCharacter
|
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
unsigned byte = aCharacter/8;
|
|
|
|
|
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if (byte < _length && GSISSET(_data[byte], aCharacter % 8))
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (Class) classForCoder
|
|
|
|
|
{
|
|
|
|
|
return [self class];
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
DESTROY(_obj);
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-07 10:47:10 +00:00
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
[aCoder encodeObject: [self bitmapRepresentation]];
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (BOOL) hasMemberInPlane: (uint8_t)aPlane
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
unsigned bit;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
2006-05-12 15:13:12 +00:00
|
|
|
|
if (aPlane > 16)
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
bit = (1 << aPlane);
|
|
|
|
|
if (_known & bit)
|
|
|
|
|
{
|
|
|
|
|
if (_present & bit)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if (aPlane * GSBITMAP_SIZE < _length)
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
2010-09-24 11:05:11 +00:00
|
|
|
|
unsigned i = GSBITMAP_SIZE * aPlane;
|
|
|
|
|
unsigned e = GSBITMAP_SIZE * (aPlane + 1);
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
while (i < e)
|
|
|
|
|
{
|
|
|
|
|
if (_data[i] != 0)
|
|
|
|
|
{
|
|
|
|
|
_present |= bit;
|
|
|
|
|
_known |= bit;
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_present &= ~bit;
|
|
|
|
|
_known |= bit;
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self initWithBitmap: nil];
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithBitmap: (NSData*)bitmap
|
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
unsigned length = [bitmap length];
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if ((length % GSBITMAP_SIZE) != 0 || length > GSBITMAP_MAX)
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"attempt to initialize character set with invalid bitmap");
|
|
|
|
|
[self dealloc];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (bitmap == nil)
|
|
|
|
|
{
|
|
|
|
|
bitmap = [NSData data];
|
|
|
|
|
}
|
|
|
|
|
ASSIGNCOPY(_obj, bitmap);
|
|
|
|
|
_length = length;
|
|
|
|
|
_data = [_obj bytes];
|
|
|
|
|
return self;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
NSData *rep;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
|
|
|
|
rep = [aCoder decodeObject];
|
|
|
|
|
self = [self initWithBitmap: rep];
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (BOOL) longCharacterIsMember: (UTF32Char)aCharacter
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
unsigned byte = aCharacter/8;
|
|
|
|
|
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if (aCharacter >= GSUNICODE_MAX)
|
2006-05-12 15:13:12 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@-%@] argument (0x%08x) is too large",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd),
|
|
|
|
|
aCharacter];
|
|
|
|
|
}
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if (byte < _length && GSISSET(_data[byte], aCharacter % 8))
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
2005-03-08 11:32:24 +00:00
|
|
|
|
@end
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
@implementation NSMutableBitmapCharSet
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
if (self == [NSMutableBitmapCharSet class])
|
|
|
|
|
{
|
|
|
|
|
[self setVersion: 1];
|
|
|
|
|
GSObjCAddClassBehavior(self, [NSBitmapCharSet class]);
|
|
|
|
|
}
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) addCharactersInRange: (NSRange)aRange
|
|
|
|
|
{
|
2010-03-16 15:03:41 +00:00
|
|
|
|
NSUInteger i;
|
|
|
|
|
NSUInteger m;
|
|
|
|
|
NSUInteger b;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
2010-03-16 15:03:41 +00:00
|
|
|
|
m = NSMaxRange(aRange);
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if (m > GSUNICODE_MAX)
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
|
format:@"Specified range exceeds character set"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
2010-03-16 15:03:41 +00:00
|
|
|
|
else if (m < 1)
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
2010-03-16 15:03:41 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2005-03-08 11:32:24 +00:00
|
|
|
|
|
2010-03-16 15:03:41 +00:00
|
|
|
|
/* Make space if needed.
|
|
|
|
|
*/
|
|
|
|
|
b = (m - 1) / 8;
|
|
|
|
|
if (b >= _length)
|
|
|
|
|
{
|
|
|
|
|
while (b >= _length)
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
2010-09-24 11:05:11 +00:00
|
|
|
|
_length += GSBITMAP_SIZE;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
}
|
2010-03-16 15:03:41 +00:00
|
|
|
|
[_obj setLength: _length];
|
|
|
|
|
_data = [_obj mutableBytes];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Fill the first byte in the range.
|
|
|
|
|
*/
|
|
|
|
|
i = aRange.location;
|
|
|
|
|
b = i / 8;
|
|
|
|
|
while (i % 8 != 0 && i < m)
|
|
|
|
|
{
|
2010-09-24 11:05:11 +00:00
|
|
|
|
GSSETBIT(_data[b], i % 8);
|
2010-03-16 15:03:41 +00:00
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set any complete bytes in the range.
|
|
|
|
|
*/
|
|
|
|
|
b = (m - i) / 8;
|
|
|
|
|
if (b > 0)
|
|
|
|
|
{
|
|
|
|
|
memset(&_data[i / 8], 0xff, b);
|
|
|
|
|
i += b * 8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Partial set of any bits needed in the last byte.
|
|
|
|
|
*/
|
|
|
|
|
b = i / 8;
|
|
|
|
|
while (i < m)
|
|
|
|
|
{
|
2010-09-24 11:05:11 +00:00
|
|
|
|
GSSETBIT(_data[b], i % 8);
|
2010-03-16 15:03:41 +00:00
|
|
|
|
i++;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
2005-03-08 11:32:24 +00:00
|
|
|
|
_known = 0; // Invalidate cache
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) addCharactersInString: (NSString*)aString
|
|
|
|
|
{
|
|
|
|
|
unsigned length;
|
|
|
|
|
|
|
|
|
|
if (!aString)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
|
format:@"Adding characters from nil string"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
length = [aString length];
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
unichar (*get)(id, SEL, unsigned);
|
|
|
|
|
|
|
|
|
|
get = (unichar (*)(id, SEL, unsigned))
|
|
|
|
|
[aString methodForSelector: @selector(characterAtIndex:)];
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
unichar letter;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
unichar second;
|
|
|
|
|
unsigned byte;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
|
|
|
|
letter = (*get)(aString, @selector(characterAtIndex:), i);
|
2005-03-08 11:32:24 +00:00
|
|
|
|
// Convert a surrogate pair if necessary
|
|
|
|
|
if (letter >= 0xd800 && letter <= 0xdbff && i < length-1
|
|
|
|
|
&& (second = (*get)(aString, @selector(characterAtIndex:), i+1))
|
|
|
|
|
>= 0xdc00 && second <= 0xdfff)
|
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
letter = ((letter - 0xd800) << 10)
|
|
|
|
|
+ (second - 0xdc00) + 0x0010000;
|
|
|
|
|
}
|
|
|
|
|
byte = letter/8;
|
2010-03-16 15:03:41 +00:00
|
|
|
|
if (byte >= _length)
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
2010-03-16 15:03:41 +00:00
|
|
|
|
while (byte >= _length)
|
|
|
|
|
{
|
2010-09-24 11:05:11 +00:00
|
|
|
|
_length += GSBITMAP_SIZE;
|
2010-03-16 15:03:41 +00:00
|
|
|
|
}
|
|
|
|
|
[_obj setLength: _length];
|
2005-03-08 11:32:24 +00:00
|
|
|
|
_data = [_obj mutableBytes];
|
|
|
|
|
}
|
2010-09-24 11:05:11 +00:00
|
|
|
|
GSSETBIT(_data[byte], letter % 8);
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-03-08 11:32:24 +00:00
|
|
|
|
_known = 0; // Invalidate cache
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSData*) bitmapRepresentation
|
|
|
|
|
{
|
2006-05-12 15:13:12 +00:00
|
|
|
|
unsigned i = 17;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
|
|
|
|
|
while (i > 0 && [self hasMemberInPlane: i-1] == NO)
|
|
|
|
|
{
|
|
|
|
|
i--;
|
|
|
|
|
}
|
2010-09-24 11:05:11 +00:00
|
|
|
|
i *= GSBITMAP_SIZE;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [NSData dataWithBytes: _data length: i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) formIntersectionWithCharacterSet: (NSCharacterSet *)otherSet
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
NSData *otherData = [otherSet bitmapRepresentation];
|
|
|
|
|
unsigned other_length = [otherData length];
|
|
|
|
|
const unsigned char *other_bytes = [otherData bytes];
|
|
|
|
|
|
|
|
|
|
if (_length > other_length)
|
|
|
|
|
{
|
|
|
|
|
[_obj setLength: other_length];
|
|
|
|
|
_length = other_length;
|
|
|
|
|
_data = [_obj mutableBytes];
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < _length; i++)
|
|
|
|
|
{
|
|
|
|
|
_data[i] = (_data[i] & other_bytes[i]);
|
|
|
|
|
}
|
|
|
|
|
_known = 0; // Invalidate cache
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) formUnionWithCharacterSet: (NSCharacterSet*)otherSet
|
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
unsigned i;
|
|
|
|
|
NSData *otherData = [otherSet bitmapRepresentation];
|
|
|
|
|
unsigned other_length = [otherData length];
|
|
|
|
|
const unsigned char *other_bytes = [otherData bytes];
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
if (other_length > _length)
|
|
|
|
|
{
|
|
|
|
|
[_obj setLength: other_length];
|
|
|
|
|
_length = other_length;
|
|
|
|
|
_data = [_obj mutableBytes];
|
|
|
|
|
}
|
2005-03-09 17:05:36 +00:00
|
|
|
|
for (i = 0; i < other_length; i++)
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
|
|
|
|
_data[i] = (_data[i] | other_bytes[i]);
|
|
|
|
|
}
|
2005-03-08 11:32:24 +00:00
|
|
|
|
_known = 0; // Invalidate cache
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (id) initWithBitmap: (NSData*)bitmap
|
|
|
|
|
{
|
|
|
|
|
unsigned length = [bitmap length];
|
|
|
|
|
id tmp;
|
|
|
|
|
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if ((length % GSBITMAP_SIZE) != 0 || length > GSBITMAP_MAX)
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"attempt to initialize character set with invalid bitmap");
|
|
|
|
|
[self dealloc];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (bitmap == nil)
|
|
|
|
|
{
|
|
|
|
|
tmp = [NSMutableData new];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tmp = [bitmap mutableCopy];
|
|
|
|
|
}
|
|
|
|
|
DESTROY(_obj);
|
|
|
|
|
_obj = tmp;
|
|
|
|
|
_length = length;
|
|
|
|
|
_data = [_obj mutableBytes];
|
|
|
|
|
_known = 0; // Invalidate cache
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) invert
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if (_length < GSBITMAP_MAX)
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
2010-09-24 11:05:11 +00:00
|
|
|
|
[_obj setLength: GSBITMAP_MAX];
|
|
|
|
|
_length = GSBITMAP_MAX;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
_data = [_obj mutableBytes];
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
2005-03-08 11:32:24 +00:00
|
|
|
|
for (i = 0; i < _length; i++)
|
|
|
|
|
{
|
|
|
|
|
_data[i] = ~_data[i];
|
|
|
|
|
}
|
|
|
|
|
_known = 0; // Invalidate cache
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) removeCharactersInRange: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
unsigned limit = NSMaxRange(aRange);
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if (NSMaxRange(aRange) > GSUNICODE_MAX)
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
|
format:@"Specified range exceeds character set"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
if (limit > _length * 8)
|
|
|
|
|
{
|
|
|
|
|
limit = _length * 8;
|
|
|
|
|
}
|
|
|
|
|
for (i = aRange.location; i < limit; i++)
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
2010-09-24 11:05:11 +00:00
|
|
|
|
GSCLRBIT(_data[i/8], i % 8);
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
2005-03-08 11:32:24 +00:00
|
|
|
|
_known = 0; // Invalidate cache
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) removeCharactersInString: (NSString*)aString
|
|
|
|
|
{
|
|
|
|
|
unsigned length;
|
|
|
|
|
|
|
|
|
|
if (!aString)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
|
format:@"Removing characters from nil string"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
length = [aString length];
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
unichar (*get)(id, SEL, unsigned);
|
|
|
|
|
|
|
|
|
|
get = (unichar (*)(id, SEL, unsigned))
|
|
|
|
|
[aString methodForSelector: @selector(characterAtIndex:)];
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
unichar letter;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
unichar second;
|
|
|
|
|
unsigned byte;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
|
|
|
|
|
letter = (*get)(aString, @selector(characterAtIndex:), i);
|
2005-03-08 11:32:24 +00:00
|
|
|
|
// Convert a surrogate pair if necessary
|
|
|
|
|
if (letter >= 0xd800 && letter <= 0xdbff && i < length-1
|
|
|
|
|
&& (second = (*get)(aString, @selector(characterAtIndex:), i+1))
|
|
|
|
|
>= 0xdc00 && second <= 0xdfff)
|
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
letter = ((letter - 0xd800) << 10)
|
|
|
|
|
+ (second - 0xdc00) + 0x0010000;
|
|
|
|
|
}
|
|
|
|
|
byte = letter/8;
|
|
|
|
|
if (byte < _length)
|
|
|
|
|
{
|
2010-09-24 11:05:11 +00:00
|
|
|
|
GSCLRBIT(_data[byte], letter % 8);
|
2005-03-08 11:32:24 +00:00
|
|
|
|
}
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-03-08 11:32:24 +00:00
|
|
|
|
_known = 0; // Invalidate cache
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
2005-03-06 10:39:10 +00:00
|
|
|
|
|
1996-09-02 13:53:26 +00:00
|
|
|
|
/* A simple array for caching standard bitmap sets */
|
1999-04-09 15:34:49 +00:00
|
|
|
|
#define MAX_STANDARD_SETS 15
|
2003-12-23 23:19:00 +00:00
|
|
|
|
static NSCharacterSet *cache_set[MAX_STANDARD_SETS];
|
|
|
|
|
static NSLock *cache_lock = nil;
|
|
|
|
|
static Class abstractClass = nil;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
static Class abstractMutableClass = nil;
|
2007-02-21 10:02:26 +00:00
|
|
|
|
static Class concreteClass = nil;
|
|
|
|
|
static Class concreteMutableClass = nil;
|
|
|
|
|
|
|
|
|
|
#if defined(GNUSTEP_INDEX_CHARSET)
|
|
|
|
|
@interface _GSStaticCharSet : _GSIndexCharSet
|
|
|
|
|
{
|
|
|
|
|
int _index;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation _GSStaticCharSet
|
1996-09-02 13:53:26 +00:00
|
|
|
|
|
2007-02-21 10:02:26 +00:00
|
|
|
|
- (Class) classForCoder
|
|
|
|
|
{
|
|
|
|
|
return abstractClass;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(int) at: &_index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
DESTROY(self);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithBitmap: (NSData*)bitmap number: (int)number
|
|
|
|
|
{
|
|
|
|
|
_index = number;
|
|
|
|
|
indexes = [[_GSStaticIndexSet alloc] _initWithBytes: [bitmap bytes]
|
|
|
|
|
length: [bitmap length]];
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#else /* GNUSTEP_INDEX_CHARSET */
|
|
|
|
|
|
|
|
|
|
@interface _GSStaticCharSet : NSCharacterSet
|
2005-02-27 10:46:19 +00:00
|
|
|
|
{
|
|
|
|
|
const unsigned char *_data;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
unsigned _length;
|
|
|
|
|
NSData *_obj;
|
|
|
|
|
unsigned _known;
|
|
|
|
|
unsigned _present;
|
2005-02-27 10:46:19 +00:00
|
|
|
|
int _index;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2007-02-21 10:02:26 +00:00
|
|
|
|
@implementation _GSStaticCharSet
|
2005-02-27 10:46:19 +00:00
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
+ (void) initialize
|
2005-02-27 10:46:19 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
GSObjCAddClassBehavior(self, [NSBitmapCharSet class]);
|
2005-02-27 10:46:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (Class) classForCoder
|
2005-02-27 10:46:19 +00:00
|
|
|
|
{
|
2007-02-21 10:02:26 +00:00
|
|
|
|
return abstractClass;
|
2005-02-27 10:46:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
2005-02-27 10:46:19 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(int) at: &_index];
|
2005-02-27 10:46:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (id) init
|
2005-02-27 10:46:19 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
DESTROY(self);
|
|
|
|
|
return nil;
|
2005-02-27 10:46:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (id) initWithBitmap: (NSData*)bitmap number: (int)number
|
2005-02-27 10:46:19 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
if ((self = [(NSBitmapCharSet*)self initWithBitmap: bitmap]) != nil)
|
|
|
|
|
{
|
|
|
|
|
_index = number;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
2005-02-27 10:46:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2007-02-21 10:02:26 +00:00
|
|
|
|
#endif /* GNUSTEP_INDEX_CHARSET */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1995-05-05 21:03:04 +00:00
|
|
|
|
@implementation NSCharacterSet
|
|
|
|
|
|
1996-09-02 13:53:26 +00:00
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
static BOOL one_time = NO;
|
|
|
|
|
|
|
|
|
|
if (one_time == NO)
|
|
|
|
|
{
|
2003-12-23 23:19:00 +00:00
|
|
|
|
abstractClass = [NSCharacterSet class];
|
2005-03-08 11:32:24 +00:00
|
|
|
|
abstractMutableClass = [NSMutableCharacterSet class];
|
2007-02-21 10:02:26 +00:00
|
|
|
|
#if defined(GNUSTEP_INDEX_CHARSET)
|
|
|
|
|
concreteClass = [_GSIndexCharSet class];
|
|
|
|
|
concreteMutableClass = [_GSMutableIndexCharSet class];
|
|
|
|
|
#else
|
|
|
|
|
concreteClass = [NSBitmapCharSet class];
|
|
|
|
|
concreteMutableClass = [NSMutableBitmapCharSet class];
|
|
|
|
|
#endif
|
1996-09-02 13:53:26 +00:00
|
|
|
|
one_time = YES;
|
|
|
|
|
}
|
2004-02-08 09:42:38 +00:00
|
|
|
|
cache_lock = [GSLazyLock new];
|
1996-09-02 13:53:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-02-27 10:46:19 +00:00
|
|
|
|
/**
|
|
|
|
|
* Creat and cache (or retrieve from cache) a characterset
|
|
|
|
|
* using static bitmap data.
|
|
|
|
|
* Return nil if no data is supplied and the cache is empty.
|
|
|
|
|
*/
|
2007-02-21 10:02:26 +00:00
|
|
|
|
+ (NSCharacterSet*) _staticSet: (const void*)bytes
|
2005-03-08 11:32:24 +00:00
|
|
|
|
length: (unsigned)length
|
|
|
|
|
number: (int)number
|
1995-08-30 21:31:29 +00:00
|
|
|
|
{
|
1996-09-02 13:53:26 +00:00
|
|
|
|
[cache_lock lock];
|
2005-02-27 10:46:19 +00:00
|
|
|
|
if (cache_set[number] == nil && bytes != 0)
|
1997-05-03 17:16:10 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
NSData *bitmap;
|
|
|
|
|
|
|
|
|
|
bitmap = [[NSDataStatic alloc] initWithBytesNoCopy: (void*)bytes
|
|
|
|
|
length: length
|
|
|
|
|
freeWhenDone: NO];
|
2005-02-27 10:46:19 +00:00
|
|
|
|
cache_set[number]
|
2007-02-21 10:02:26 +00:00
|
|
|
|
= [[_GSStaticCharSet alloc] initWithBitmap: bitmap number: number];
|
2005-03-08 11:32:24 +00:00
|
|
|
|
RELEASE(bitmap);
|
1995-08-30 21:31:29 +00:00
|
|
|
|
}
|
1996-09-02 13:53:26 +00:00
|
|
|
|
[cache_lock unlock];
|
2005-02-27 10:46:19 +00:00
|
|
|
|
return cache_set[number];
|
1995-08-30 21:31:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) alphanumericCharacterSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: alphanumericCharSet
|
|
|
|
|
length: sizeof(alphanumericCharSet)
|
|
|
|
|
number: 0];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-06 09:15:08 +00:00
|
|
|
|
+ (NSCharacterSet*) capitalizedLetterCharacterSet
|
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: titlecaseLetterCharSet
|
|
|
|
|
length: sizeof(titlecaseLetterCharSet)
|
|
|
|
|
number: 13];
|
2005-03-06 09:15:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) controlCharacterSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: controlCharSet
|
|
|
|
|
length: sizeof(controlCharSet)
|
|
|
|
|
number: 1];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) decimalDigitCharacterSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: decimalDigitCharSet
|
|
|
|
|
length: sizeof(decimalDigitCharSet)
|
|
|
|
|
number: 2];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) decomposableCharacterSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: decomposableCharSet
|
|
|
|
|
length: sizeof(decomposableCharSet)
|
|
|
|
|
number: 3];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) illegalCharacterSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: illegalCharSet
|
|
|
|
|
length: sizeof(illegalCharSet)
|
|
|
|
|
number: 4];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) letterCharacterSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: letterCharSet
|
|
|
|
|
length: sizeof(letterCharSet)
|
|
|
|
|
number: 5];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) lowercaseLetterCharacterSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: lowercaseLetterCharSet
|
|
|
|
|
length: sizeof(lowercaseLetterCharSet)
|
|
|
|
|
number: 6];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-05 22:28:12 +00:00
|
|
|
|
+ (NSCharacterSet*) newlineCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return [self _staticSet: newlineCharSet
|
|
|
|
|
length: sizeof(newlineCharSet)
|
|
|
|
|
number: 14];
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) nonBaseCharacterSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: nonBaseCharSet
|
|
|
|
|
length: sizeof(nonBaseCharSet)
|
|
|
|
|
number: 7];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-12-17 14:31:42 +00:00
|
|
|
|
+ (NSCharacterSet*) punctuationCharacterSet
|
1998-11-16 19:36:51 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: punctuationCharSet
|
|
|
|
|
length: sizeof(punctuationCharSet)
|
|
|
|
|
number: 8];
|
1998-11-16 19:36:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-06 10:39:10 +00:00
|
|
|
|
+ (NSCharacterSet*) symbolCharacterSet
|
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: symbolAndOperatorCharSet
|
|
|
|
|
length: sizeof(symbolAndOperatorCharSet)
|
|
|
|
|
number: 9];
|
2005-03-06 10:39:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME ... deprecated ... remove after next release.
|
2001-12-17 14:31:42 +00:00
|
|
|
|
+ (NSCharacterSet*) symbolAndOperatorCharacterSet
|
1999-04-09 15:34:49 +00:00
|
|
|
|
{
|
2005-03-06 10:39:10 +00:00
|
|
|
|
GSOnceMLog(@"symbolAndOperatorCharacterSet is deprecated ... use symbolCharacterSet");
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: symbolAndOperatorCharSet
|
|
|
|
|
length: sizeof(symbolAndOperatorCharSet)
|
|
|
|
|
number: 9];
|
1999-04-09 15:34:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) uppercaseLetterCharacterSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: uppercaseLetterCharSet
|
|
|
|
|
length: sizeof(uppercaseLetterCharSet)
|
|
|
|
|
number: 10];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) whitespaceAndNewlineCharacterSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: whitespaceAndNlCharSet
|
|
|
|
|
length: sizeof(whitespaceAndNlCharSet)
|
|
|
|
|
number: 11];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) whitespaceCharacterSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
return [self _staticSet: whitespaceCharSet
|
|
|
|
|
length: sizeof(whitespaceCharSet)
|
|
|
|
|
number: 12];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) characterSetWithBitmapRepresentation: (NSData*)data
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2007-02-21 10:02:26 +00:00
|
|
|
|
return AUTORELEASE([[concreteClass alloc] initWithBitmap: data]);
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) characterSetWithCharactersInString: (NSString*)aString
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
NSMutableCharacterSet *ms;
|
|
|
|
|
NSCharacterSet *cs;
|
2001-11-07 16:24:55 +00:00
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
ms = [NSMutableCharacterSet new];
|
|
|
|
|
[ms addCharactersInString: aString];
|
|
|
|
|
cs = [ms copy];
|
|
|
|
|
RELEASE(ms);
|
|
|
|
|
return AUTORELEASE(cs);
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-07 10:47:10 +00:00
|
|
|
|
+ (NSCharacterSet*) characterSetWithRange: (NSRange)aRange
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
NSMutableCharacterSet *ms;
|
|
|
|
|
NSCharacterSet *cs;
|
1995-08-30 21:31:29 +00:00
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
ms = [NSMutableCharacterSet new];
|
|
|
|
|
[ms addCharactersInRange: aRange];
|
|
|
|
|
cs = [ms copy];
|
|
|
|
|
RELEASE(ms);
|
|
|
|
|
return AUTORELEASE(cs);
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) characterSetWithContentsOfFile: (NSString*)aFile
|
1998-11-10 20:16:33 +00:00
|
|
|
|
{
|
|
|
|
|
if ([@"bitmap" isEqual: [aFile pathExtension]])
|
|
|
|
|
{
|
|
|
|
|
NSData *bitmap = [NSData dataWithContentsOfFile: aFile];
|
|
|
|
|
return [self characterSetWithBitmapRepresentation: bitmap];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
- (NSData*) bitmapRepresentation
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-07 10:47:10 +00:00
|
|
|
|
BOOL (*imp)(id, SEL, unichar);
|
|
|
|
|
NSMutableData *m = [NSMutableData dataWithLength: 8192];
|
|
|
|
|
unsigned char *p = (unsigned char*)[m mutableBytes];
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
imp = (BOOL (*)(id,SEL,unichar))
|
|
|
|
|
[self methodForSelector: @selector(characterIsMember:)];
|
|
|
|
|
for (i = 0; i <= 0xffff; i++)
|
|
|
|
|
{
|
|
|
|
|
if (imp(self, @selector(characterIsMember:), i) == YES)
|
|
|
|
|
{
|
2010-09-24 11:05:11 +00:00
|
|
|
|
GSSETBIT(p[i/8], i % 8);
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return m;
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
- (BOOL) characterIsMember: (unichar)aCharacter
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
1999-04-07 11:57:53 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-07 10:47:10 +00:00
|
|
|
|
- (id) copyWithZone: (NSZone*)zone
|
|
|
|
|
{
|
|
|
|
|
if (NSShouldRetainWithZone(self, zone))
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
|
|
|
|
return RETAIN(self);
|
|
|
|
|
}
|
2005-03-07 10:47:10 +00:00
|
|
|
|
else
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
|
|
|
|
id obj;
|
|
|
|
|
|
2007-02-21 10:02:26 +00:00
|
|
|
|
obj = [concreteClass allocWithZone: zone];
|
2005-03-08 11:32:24 +00:00
|
|
|
|
obj = [obj initWithBitmap: [self bitmapRepresentation]];
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
2005-03-07 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-07-29 09:22:18 +00:00
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-07 10:47:10 +00:00
|
|
|
|
- (BOOL) hasMemberInPlane: (uint8_t)aPlane
|
|
|
|
|
{
|
|
|
|
|
if (aPlane == 0)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (id) init
|
|
|
|
|
{
|
2010-02-22 10:13:20 +00:00
|
|
|
|
if (object_getClass(self) == abstractClass)
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
|
|
|
|
id obj;
|
|
|
|
|
|
2007-02-21 10:02:26 +00:00
|
|
|
|
obj = [concreteClass allocWithZone: [self zone]];
|
2005-03-08 11:32:24 +00:00
|
|
|
|
obj = [obj initWithBitmap: nil];
|
2010-02-25 18:49:31 +00:00
|
|
|
|
DESTROY(self);
|
2005-03-08 11:32:24 +00:00
|
|
|
|
self = obj;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1998-07-29 09:22:18 +00:00
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
2005-02-28 17:18:54 +00:00
|
|
|
|
if ([self class] == abstractClass)
|
2005-02-27 10:46:19 +00:00
|
|
|
|
{
|
|
|
|
|
int index;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Abstract class returns characterset from cache.
|
|
|
|
|
*/
|
|
|
|
|
DESTROY(self);
|
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(int) at: &index];
|
2005-03-08 11:32:24 +00:00
|
|
|
|
self = RETAIN([abstractClass _staticSet: 0 length: 0 number: index]);
|
2005-02-28 17:18:54 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-02-27 10:46:19 +00:00
|
|
|
|
}
|
|
|
|
|
return self;
|
1998-07-29 09:22:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-07 10:47:10 +00:00
|
|
|
|
- (NSCharacterSet*) invertedSet
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
unsigned length;
|
|
|
|
|
unsigned char *bytes;
|
|
|
|
|
NSMutableData *bitmap;
|
|
|
|
|
|
|
|
|
|
bitmap = AUTORELEASE([[self bitmapRepresentation] mutableCopy]);
|
|
|
|
|
length = [bitmap length];
|
|
|
|
|
bytes = [bitmap mutableBytes];
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
bytes[i] = ~bytes[i];
|
|
|
|
|
}
|
|
|
|
|
return [[self class] characterSetWithBitmapRepresentation: bitmap];
|
|
|
|
|
}
|
|
|
|
|
|
1998-07-29 14:46:16 +00:00
|
|
|
|
- (BOOL) isEqual: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
if (anObject == self)
|
|
|
|
|
return YES;
|
2005-02-28 17:18:54 +00:00
|
|
|
|
if ([anObject isKindOfClass: abstractClass])
|
1998-07-29 14:46:16 +00:00
|
|
|
|
{
|
2001-11-07 16:24:55 +00:00
|
|
|
|
unsigned i;
|
2005-03-08 11:32:24 +00:00
|
|
|
|
unsigned p;
|
2005-03-07 10:47:10 +00:00
|
|
|
|
BOOL (*rImp)(id, SEL, unichar);
|
|
|
|
|
BOOL (*oImp)(id, SEL, unichar);
|
|
|
|
|
|
|
|
|
|
rImp = (BOOL (*)(id,SEL,unichar))
|
|
|
|
|
[self methodForSelector: @selector(characterIsMember:)];
|
|
|
|
|
oImp = (BOOL (*)(id,SEL,unichar))
|
|
|
|
|
[anObject methodForSelector: @selector(characterIsMember:)];
|
1998-07-29 14:46:16 +00:00
|
|
|
|
|
2006-05-12 15:13:12 +00:00
|
|
|
|
for (p = 0; p <= 16; p++)
|
2001-11-07 16:24:55 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
if ([self hasMemberInPlane: p] == YES)
|
2001-11-07 16:24:55 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
if ([anObject hasMemberInPlane: p] == YES)
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; i <= 0xffff; i++)
|
|
|
|
|
{
|
|
|
|
|
if (rImp(self, @selector(characterIsMember:), i)
|
|
|
|
|
!= oImp(anObject, @selector(characterIsMember:), i))
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ([anObject hasMemberInPlane: p] == YES)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
2001-11-07 16:24:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1998-07-29 14:46:16 +00:00
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-07 10:47:10 +00:00
|
|
|
|
- (BOOL) isSupersetOfSet: (NSCharacterSet*)aSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-07 10:47:10 +00:00
|
|
|
|
NSMutableCharacterSet *m = [self mutableCopy];
|
|
|
|
|
BOOL superset;
|
1995-05-05 21:03:04 +00:00
|
|
|
|
|
2005-03-07 10:47:10 +00:00
|
|
|
|
[m formUnionWithCharacterSet: aSet];
|
|
|
|
|
superset = [self isEqual: m];
|
|
|
|
|
RELEASE(m);
|
|
|
|
|
return superset;
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-07 10:47:10 +00:00
|
|
|
|
- (BOOL) longCharacterIsMember: (UTF32Char)aCharacter
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-07 10:47:10 +00:00
|
|
|
|
int plane = (aCharacter >> 16);
|
|
|
|
|
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if (aCharacter >= GSUNICODE_MAX)
|
2006-05-12 15:13:12 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@-%@] argument (0x%08x) is too large",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd),
|
|
|
|
|
aCharacter];
|
|
|
|
|
}
|
2005-03-07 10:47:10 +00:00
|
|
|
|
if (plane == 0)
|
|
|
|
|
{
|
|
|
|
|
unichar u = (unichar)(aCharacter & 0xffff);
|
|
|
|
|
|
|
|
|
|
return [self characterIsMember: u];
|
|
|
|
|
}
|
1995-05-05 21:03:04 +00:00
|
|
|
|
else
|
2005-03-07 10:47:10 +00:00
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)zone
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
|
NSData *bitmap;
|
|
|
|
|
bitmap = [self bitmapRepresentation];
|
2007-02-21 10:02:26 +00:00
|
|
|
|
return [[concreteMutableClass allocWithZone: zone] initWithBitmap: bitmap];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation NSMutableCharacterSet
|
|
|
|
|
|
1995-08-30 21:31:29 +00:00
|
|
|
|
/* Override this from NSCharacterSet to create the correct class */
|
2001-11-07 16:24:55 +00:00
|
|
|
|
+ (NSCharacterSet*) characterSetWithBitmapRepresentation: (NSData*)data
|
1995-08-30 21:31:29 +00:00
|
|
|
|
{
|
2007-02-21 10:02:26 +00:00
|
|
|
|
return AUTORELEASE([[concreteMutableClass alloc] initWithBitmap: data]);
|
1995-08-30 21:31:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-02-28 17:18:54 +00:00
|
|
|
|
+ (NSCharacterSet*) alphanumericCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-06 09:15:08 +00:00
|
|
|
|
+ (NSCharacterSet*) capitalizedLetterCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-28 17:18:54 +00:00
|
|
|
|
+ (NSCharacterSet*) controlCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSCharacterSet*) decimalDigitCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSCharacterSet*) decomposableCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSCharacterSet*) illegalCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSCharacterSet*) letterCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSCharacterSet*) lowercaseLetterCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-05 22:28:12 +00:00
|
|
|
|
+ (NSCharacterSet*) newlineCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-28 17:18:54 +00:00
|
|
|
|
+ (NSCharacterSet*) nonBaseCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSCharacterSet*) punctuationCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-06 10:39:10 +00:00
|
|
|
|
+ (NSCharacterSet*) symbolCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME ... deprecated ... remove after next release.
|
2005-02-28 17:18:54 +00:00
|
|
|
|
+ (NSCharacterSet*) symbolAndOperatorCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSCharacterSet*) uppercaseLetterCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSCharacterSet*) whitespaceAndNewlineCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSCharacterSet*) whitespaceCharacterSet
|
|
|
|
|
{
|
|
|
|
|
return AUTORELEASE([[abstractClass performSelector: _cmd] mutableCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
+ (NSCharacterSet*) characterSetWithCharactersInString: (NSString*)aString
|
|
|
|
|
{
|
|
|
|
|
NSMutableCharacterSet *ms;
|
|
|
|
|
|
|
|
|
|
ms = [abstractMutableClass new];
|
|
|
|
|
[ms addCharactersInString: aString];
|
|
|
|
|
return AUTORELEASE(ms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSCharacterSet*) characterSetWithRange: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
NSMutableCharacterSet *ms;
|
|
|
|
|
|
|
|
|
|
ms = [abstractMutableClass new];
|
|
|
|
|
[ms addCharactersInRange: aRange];
|
|
|
|
|
return AUTORELEASE(ms);
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
- (void) addCharactersInRange: (NSRange)aRange
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
1999-04-07 11:57:53 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
- (void) addCharactersInString: (NSString*)aString
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
1999-04-07 11:57:53 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (id) copyWithZone: (NSZone*)zone
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
NSData *bitmap;
|
|
|
|
|
|
|
|
|
|
bitmap = [self bitmapRepresentation];
|
2007-02-21 10:02:26 +00:00
|
|
|
|
return [[concreteClass allocWithZone: zone] initWithBitmap: bitmap];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
- (void) formIntersectionWithCharacterSet: (NSCharacterSet*)otherSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
1999-04-07 11:57:53 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (void) formUnionWithCharacterSet: (NSCharacterSet*)otherSet
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
1999-04-07 11:57:53 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (id) init
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2010-02-22 10:13:20 +00:00
|
|
|
|
if (object_getClass(self) == abstractMutableClass)
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
|
|
|
|
id obj;
|
|
|
|
|
|
2007-02-21 10:02:26 +00:00
|
|
|
|
obj = [concreteMutableClass allocWithZone: [self zone]];
|
2005-03-08 11:32:24 +00:00
|
|
|
|
obj = [obj initWithBitmap: nil];
|
2010-02-25 18:49:31 +00:00
|
|
|
|
DESTROY(self);
|
2005-03-08 11:32:24 +00:00
|
|
|
|
self = obj;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithBitmap: (NSData*)bitmap
|
|
|
|
|
{
|
2010-02-22 10:13:20 +00:00
|
|
|
|
if (object_getClass(self) == abstractMutableClass)
|
2005-03-08 11:32:24 +00:00
|
|
|
|
{
|
|
|
|
|
id obj;
|
|
|
|
|
|
2007-02-21 10:02:26 +00:00
|
|
|
|
obj = [concreteMutableClass allocWithZone: [self zone]];
|
2005-03-08 11:32:24 +00:00
|
|
|
|
obj = [obj initWithBitmap: bitmap];
|
2010-02-25 18:49:31 +00:00
|
|
|
|
DESTROY(self);
|
2005-03-08 11:32:24 +00:00
|
|
|
|
self = obj;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 16:24:55 +00:00
|
|
|
|
- (void) invert
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
1999-04-07 11:57:53 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (void) removeCharactersInRange: (NSRange)aRange
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 11:32:24 +00:00
|
|
|
|
- (void) removeCharactersInString: (NSString*)aString
|
1995-05-05 21:03:04 +00:00
|
|
|
|
{
|
2005-03-08 11:32:24 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
2007-02-21 10:02:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Below is an experimental implementation of a mutable character set
|
|
|
|
|
* implemented in terms of an NSMutableIndexSet. This should be much
|
|
|
|
|
* smaller than a bitmap representation for normal charactersets.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
@interface NSIndexSet (NSCharacterSet)
|
2010-09-15 22:16:18 +00:00
|
|
|
|
- (NSUInteger) _gapGreaterThanIndex: (NSUInteger)anIndex;
|
2007-02-21 10:02:26 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation _GSIndexCharSet
|
|
|
|
|
|
|
|
|
|
- (NSData*) bitmapRepresentation
|
|
|
|
|
{
|
|
|
|
|
NSMutableBitmapCharSet *tmp;
|
|
|
|
|
NSData *result;
|
|
|
|
|
unsigned index = 0;
|
|
|
|
|
|
|
|
|
|
tmp = [NSMutableBitmapCharSet new];
|
|
|
|
|
while ((index = [indexes indexGreaterThanOrEqualToIndex: index])
|
|
|
|
|
!= NSNotFound)
|
|
|
|
|
{
|
|
|
|
|
NSRange r;
|
|
|
|
|
|
|
|
|
|
r.location = index;
|
|
|
|
|
index = [indexes _gapGreaterThanIndex: index];
|
|
|
|
|
if (index == NSNotFound)
|
|
|
|
|
{
|
|
|
|
|
r.length = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
r.length = index - r.location;
|
|
|
|
|
}
|
|
|
|
|
[tmp addCharactersInRange: r];
|
|
|
|
|
index = NSMaxRange(r);
|
|
|
|
|
}
|
|
|
|
|
result = AUTORELEASE(RETAIN([tmp bitmapRepresentation]));
|
|
|
|
|
RELEASE(tmp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) characterIsMember: (unichar)aCharacter
|
|
|
|
|
{
|
|
|
|
|
return [indexes containsIndex: (int)aCharacter];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (Class) classForCoder
|
|
|
|
|
{
|
|
|
|
|
return [NSBitmapCharSet class];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
DESTROY(indexes);
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
[aCoder encodeObject: [self bitmapRepresentation]];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) hasMemberInPlane: (uint8_t)aPlane
|
|
|
|
|
{
|
|
|
|
|
unsigned found;
|
|
|
|
|
|
|
|
|
|
found = [indexes indexGreaterThanOrEqualToIndex: 0x10000 * aPlane];
|
|
|
|
|
if (found != NSNotFound && found < 0x10000 * (aPlane + 1))
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSIndexSet*) _indexes
|
|
|
|
|
{
|
|
|
|
|
return indexes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
return [self initWithBitmap: nil];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithBitmap: (NSData*)bitmap
|
|
|
|
|
{
|
|
|
|
|
const unsigned char *bytes = [bitmap bytes];
|
|
|
|
|
unsigned length = [bitmap length];
|
|
|
|
|
unsigned index = 0;
|
|
|
|
|
unsigned i;
|
|
|
|
|
NSRange r;
|
|
|
|
|
BOOL findingLocation = YES;
|
|
|
|
|
|
2008-11-17 13:45:32 +00:00
|
|
|
|
r.location = 0;
|
2007-02-21 10:02:26 +00:00
|
|
|
|
indexes = [NSMutableIndexSet new];
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
unsigned char byte = bytes[i];
|
|
|
|
|
|
|
|
|
|
if (byte == 0)
|
|
|
|
|
{
|
|
|
|
|
if (findingLocation == NO)
|
|
|
|
|
{
|
|
|
|
|
r.length = index - r.location;
|
|
|
|
|
[indexes addIndexesInRange: r];
|
|
|
|
|
findingLocation = YES;
|
|
|
|
|
}
|
|
|
|
|
index += 8;
|
|
|
|
|
}
|
|
|
|
|
else if (byte == 0xff)
|
|
|
|
|
{
|
|
|
|
|
if (findingLocation == YES)
|
|
|
|
|
{
|
|
|
|
|
r.location = index;
|
|
|
|
|
findingLocation = NO;
|
|
|
|
|
}
|
|
|
|
|
index += 8;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned int bit;
|
|
|
|
|
|
|
|
|
|
for (bit = 1; bit & 0xff; bit <<= 1)
|
|
|
|
|
{
|
|
|
|
|
if ((byte & bit) == 0)
|
|
|
|
|
{
|
|
|
|
|
if (findingLocation == NO)
|
|
|
|
|
{
|
|
|
|
|
r.length = index - r.location;
|
|
|
|
|
[indexes addIndexesInRange: r];
|
|
|
|
|
findingLocation = YES;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (findingLocation == YES)
|
|
|
|
|
{
|
|
|
|
|
r.location = index;
|
|
|
|
|
findingLocation = NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (findingLocation == NO)
|
|
|
|
|
{
|
|
|
|
|
r.length = index - r.location;
|
|
|
|
|
[indexes addIndexesInRange: r];
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
NSData *rep;
|
|
|
|
|
|
|
|
|
|
rep = [aCoder decodeObject];
|
|
|
|
|
self = [self initWithBitmap: rep];
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) longCharacterIsMember: (UTF32Char)aCharacter
|
|
|
|
|
{
|
|
|
|
|
return [indexes containsIndex: (int)aCharacter];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation _GSMutableIndexCharSet
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [_GSMutableIndexCharSet class])
|
|
|
|
|
{
|
|
|
|
|
[self setVersion: 1];
|
|
|
|
|
GSObjCAddClassBehavior(self, [_GSIndexCharSet class]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) addCharactersInRange: (NSRange)aRange
|
|
|
|
|
{
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if (NSMaxRange(aRange) > GSUNICODE_MAX)
|
2007-02-21 10:02:26 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
|
format:@"Specified range exceeds character set"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
[indexes addIndexesInRange: aRange];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) addCharactersInString: (NSString*)aString
|
|
|
|
|
{
|
|
|
|
|
unsigned length;
|
|
|
|
|
|
|
|
|
|
if (!aString)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
|
format:@"Adding characters from nil string"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
length = [aString length];
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
unichar (*get)(id, SEL, unsigned);
|
|
|
|
|
|
|
|
|
|
get = (unichar (*)(id, SEL, unsigned))
|
|
|
|
|
[aString methodForSelector: @selector(characterAtIndex:)];
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
unichar letter;
|
|
|
|
|
unichar second;
|
|
|
|
|
|
|
|
|
|
letter = (*get)(aString, @selector(characterAtIndex:), i);
|
|
|
|
|
// Convert a surrogate pair if necessary
|
|
|
|
|
if (letter >= 0xd800 && letter <= 0xdbff && i < length-1
|
|
|
|
|
&& (second = (*get)(aString, @selector(characterAtIndex:), i+1))
|
|
|
|
|
>= 0xdc00 && second <= 0xdfff)
|
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
letter = ((letter - 0xd800) << 10)
|
|
|
|
|
+ (second - 0xdc00) + 0x0010000;
|
|
|
|
|
}
|
|
|
|
|
[indexes addIndexesInRange: NSMakeRange(letter, 1)];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (Class) classForCoder
|
|
|
|
|
{
|
|
|
|
|
return [NSMutableBitmapCharSet class];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) formIntersectionWithCharacterSet: (NSCharacterSet *)otherSet
|
|
|
|
|
{
|
|
|
|
|
NSIndexSet *otherIndexes;
|
|
|
|
|
unsigned index = 0;
|
|
|
|
|
unsigned i0;
|
|
|
|
|
unsigned i1;
|
|
|
|
|
|
|
|
|
|
if ([otherSet isKindOfClass: [_GSIndexCharSet class]] == YES)
|
|
|
|
|
{
|
|
|
|
|
otherIndexes = [(_GSIndexCharSet*)otherSet _indexes];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_GSIndexCharSet *tmp;
|
|
|
|
|
|
|
|
|
|
tmp = [[_GSIndexCharSet alloc] initWithBitmap:
|
|
|
|
|
[otherSet bitmapRepresentation]];
|
|
|
|
|
otherIndexes = AUTORELEASE(RETAIN([tmp _indexes]));
|
|
|
|
|
RELEASE(tmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find first index in each set.
|
|
|
|
|
*/
|
|
|
|
|
i0 = [indexes indexGreaterThanOrEqualToIndex: 0];
|
|
|
|
|
i1 = [otherIndexes indexGreaterThanOrEqualToIndex: 0];
|
|
|
|
|
|
|
|
|
|
/* Loop until there are no more indexes to process in the set and
|
|
|
|
|
* the intersection operation has therefore completed.
|
|
|
|
|
*/
|
|
|
|
|
while (i0 != NSNotFound)
|
|
|
|
|
{
|
|
|
|
|
if (i1 == NSNotFound)
|
|
|
|
|
{
|
|
|
|
|
/* No more indexes in other set ... remove everything from the
|
|
|
|
|
* last gap onwards, and finish.
|
|
|
|
|
*/
|
|
|
|
|
[indexes removeIndexesInRange: NSMakeRange(index, NSNotFound-index)];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (i1 > i0)
|
|
|
|
|
{
|
|
|
|
|
/* Indexes in other set start after this set ... so remove any
|
|
|
|
|
* from the last gap to the index in the other set.
|
|
|
|
|
*/
|
|
|
|
|
[indexes removeIndexesInRange: NSMakeRange(index, i1 - index)];
|
|
|
|
|
index = i1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
index = i0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find the next gap in each set, and set our gap index to the
|
|
|
|
|
* lower of the two.
|
|
|
|
|
*/
|
|
|
|
|
i0 = [indexes _gapGreaterThanIndex: index];
|
|
|
|
|
i1 = [otherIndexes _gapGreaterThanIndex: index];
|
|
|
|
|
index = i0;
|
|
|
|
|
if (i1 < i0)
|
|
|
|
|
{
|
|
|
|
|
index = i1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find the next index in each set so wer can loop round and
|
|
|
|
|
* do it all again.
|
|
|
|
|
*/
|
|
|
|
|
i0 = [indexes indexGreaterThanIndex: i0];
|
|
|
|
|
i1 = [otherIndexes indexGreaterThanIndex: i1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) formUnionWithCharacterSet: (NSCharacterSet*)otherSet
|
|
|
|
|
{
|
|
|
|
|
NSIndexSet *otherIndexes;
|
|
|
|
|
unsigned index;
|
|
|
|
|
|
|
|
|
|
if ([otherSet isKindOfClass: [_GSIndexCharSet class]] == YES)
|
|
|
|
|
{
|
|
|
|
|
otherIndexes = [(_GSIndexCharSet*)otherSet _indexes];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_GSIndexCharSet *tmp;
|
|
|
|
|
|
|
|
|
|
tmp = [[_GSIndexCharSet alloc] initWithBitmap:
|
|
|
|
|
[otherSet bitmapRepresentation]];
|
|
|
|
|
otherIndexes = AUTORELEASE(RETAIN([tmp _indexes]));
|
|
|
|
|
RELEASE(tmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index = [otherIndexes indexGreaterThanOrEqualToIndex: 0];
|
|
|
|
|
while (index != NSNotFound)
|
|
|
|
|
{
|
|
|
|
|
NSRange r;
|
|
|
|
|
|
|
|
|
|
r.location = index;
|
|
|
|
|
index = [otherIndexes _gapGreaterThanIndex: index];
|
|
|
|
|
r.length = index - r.location;
|
|
|
|
|
[indexes addIndexesInRange: r];
|
|
|
|
|
index = [otherIndexes indexGreaterThanOrEqualToIndex: index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) invert
|
|
|
|
|
{
|
|
|
|
|
NSMutableIndexSet *tmp;
|
|
|
|
|
unsigned index;
|
|
|
|
|
|
|
|
|
|
tmp = [NSMutableIndexSet new];
|
|
|
|
|
|
|
|
|
|
/* Locate the start of the first gap
|
|
|
|
|
*/
|
|
|
|
|
if ([indexes containsIndex: 0] == YES)
|
|
|
|
|
{
|
|
|
|
|
index = [indexes _gapGreaterThanIndex: 0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
index = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (index != NSNotFound)
|
|
|
|
|
{
|
|
|
|
|
NSRange r;
|
|
|
|
|
|
|
|
|
|
r.location = index;
|
|
|
|
|
index = [indexes indexGreaterThanIndex: index];
|
|
|
|
|
if (index == NSNotFound)
|
|
|
|
|
{
|
|
|
|
|
/* No more indexes, so we have a gap to the end of all
|
|
|
|
|
* unicode characters which we can invert.
|
|
|
|
|
*/
|
2010-09-24 11:05:11 +00:00
|
|
|
|
index = GSUNICODE_MAX;
|
2007-02-21 10:02:26 +00:00
|
|
|
|
}
|
|
|
|
|
r.length = index - r.location;
|
|
|
|
|
[tmp addIndexesInRange: r];
|
|
|
|
|
index = [indexes _gapGreaterThanIndex: NSMaxRange(r) - 1];
|
|
|
|
|
}
|
|
|
|
|
ASSIGN(indexes, tmp);
|
|
|
|
|
RELEASE(tmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) removeCharactersInRange: (NSRange)aRange
|
|
|
|
|
{
|
2010-09-24 11:05:11 +00:00
|
|
|
|
if (NSMaxRange(aRange) > GSUNICODE_MAX)
|
2007-02-21 10:02:26 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
|
format:@"Specified range exceeds character set"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
[indexes removeIndexesInRange: aRange];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) removeCharactersInString: (NSString*)aString
|
|
|
|
|
{
|
|
|
|
|
unsigned length;
|
|
|
|
|
|
|
|
|
|
if (!aString)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
|
format:@"Removing characters from nil string"];
|
|
|
|
|
/* NOT REACHED */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
length = [aString length];
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
unichar (*get)(id, SEL, unsigned);
|
|
|
|
|
|
|
|
|
|
get = (unichar (*)(id, SEL, unsigned))
|
|
|
|
|
[aString methodForSelector: @selector(characterAtIndex:)];
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
unichar letter;
|
|
|
|
|
unichar second;
|
|
|
|
|
|
|
|
|
|
letter = (*get)(aString, @selector(characterAtIndex:), i);
|
|
|
|
|
// Convert a surrogate pair if necessary
|
|
|
|
|
if (letter >= 0xd800 && letter <= 0xdbff && i < length-1
|
|
|
|
|
&& (second = (*get)(aString, @selector(characterAtIndex:), i+1))
|
|
|
|
|
>= 0xdc00 && second <= 0xdfff)
|
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
letter = ((letter - 0xd800) << 10)
|
|
|
|
|
+ (second - 0xdc00) + 0x0010000;
|
|
|
|
|
}
|
|
|
|
|
[indexes removeIndexesInRange: NSMakeRange(letter, 1)];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|