1995-05-05 21:03:04 +00:00
|
|
|
/* NSCharacterSet - Character set holder
|
|
|
|
Copyright (C) 1993,1994 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Adam Fedor <fedor@boulder.colorado.edu>
|
|
|
|
Date: Apr 1995
|
|
|
|
|
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
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
1995-08-30 21:31:29 +00:00
|
|
|
#include <Foundation/NSBitmapCharSet.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSBundle.h>
|
|
|
|
#include <Foundation/NSData.h>
|
1995-05-05 21:03:04 +00:00
|
|
|
|
1995-08-30 21:31:29 +00:00
|
|
|
/* FIXME: Where should bitmaps go? Maybe should be defined with configure */
|
|
|
|
#ifndef BITMAP_PATH
|
|
|
|
#define BITMAP_PATH @"/usr/local/share/objects"
|
|
|
|
#endif
|
1995-05-05 21:03:04 +00:00
|
|
|
|
|
|
|
@implementation NSCharacterSet
|
|
|
|
|
1995-08-30 21:31:29 +00:00
|
|
|
/* Provide a default object for allocation */
|
|
|
|
+ allocWithZone:(NSZone *)zone
|
|
|
|
{
|
|
|
|
return NSAllocateObject([NSBitmapCharSet self], 0, zone);
|
|
|
|
}
|
|
|
|
|
1995-05-05 21:03:04 +00:00
|
|
|
// Creating standard character sets
|
|
|
|
|
1995-08-30 21:31:29 +00:00
|
|
|
+ (NSData *)_bitmapForSet:(NSString *)setname
|
|
|
|
{
|
|
|
|
NSString *path;
|
|
|
|
|
|
|
|
path = [NSBundle pathForResource:setname
|
|
|
|
ofType:@".dat"
|
|
|
|
inDirectory:BITMAP_PATH
|
|
|
|
withVersion:0];
|
|
|
|
/* This is for testing purposes */
|
|
|
|
if (path == nil || [path length] == 0)
|
|
|
|
{
|
|
|
|
path = [NSBundle pathForResource:setname
|
|
|
|
ofType:@".dat"
|
|
|
|
inDirectory:@"../share"
|
|
|
|
withVersion:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path == nil || [path length] == 0)
|
|
|
|
{
|
|
|
|
[NSException raise:NSGenericException
|
|
|
|
format:@"Could not find bitmap file %s", [setname cString]];
|
|
|
|
/* NOT REACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
return [NSData dataWithContentsOfFile:path];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-05-05 21:03:04 +00:00
|
|
|
+ (NSCharacterSet *)alphanumericCharacterSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *data = [self _bitmapForSet:@"alphanumCharSet"];
|
|
|
|
|
|
|
|
return [self characterSetWithBitmapRepresentation:data];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)controlCharacterSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *data = [self _bitmapForSet:@"controlCharSet"];
|
|
|
|
|
|
|
|
return [self characterSetWithBitmapRepresentation:data];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)decimalDigitCharacterSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *data = [self _bitmapForSet:@"decimalCharSet"];
|
|
|
|
|
|
|
|
return [self characterSetWithBitmapRepresentation:data];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)decomposableCharacterSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *data = [self _bitmapForSet:@"decomposableCharSet"];
|
|
|
|
|
|
|
|
fprintf(stderr, "Warning: Decomposable set not yet fully specified\n");
|
|
|
|
return [self characterSetWithBitmapRepresentation:data];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)illegalCharacterSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *data = [self _bitmapForSet:@"illegalCharSet"];
|
|
|
|
|
|
|
|
fprintf(stderr, "Warning: Illegal set not yet fully specified\n");
|
|
|
|
return [self characterSetWithBitmapRepresentation:data];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)letterCharacterSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *data = [self _bitmapForSet:@"lettercharCharSet"];
|
|
|
|
|
|
|
|
return [self characterSetWithBitmapRepresentation:data];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)lowercaseLetterCharacterSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *data = [self _bitmapForSet:@"lowercaseCharSet"];
|
|
|
|
|
|
|
|
return [self characterSetWithBitmapRepresentation:data];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)nonBaseCharacterSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *data = [self _bitmapForSet:@"nonbaseCharSet"];
|
|
|
|
|
|
|
|
return [self characterSetWithBitmapRepresentation:data];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)uppercaseLetterCharacterSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *data = [self _bitmapForSet:@"uppercaseCharSet"];
|
|
|
|
|
|
|
|
return [self characterSetWithBitmapRepresentation:data];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)whitespaceAndNewlineCharacterSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *data = [self _bitmapForSet:@"whitespaceandnlCharSet"];
|
|
|
|
|
|
|
|
return [self characterSetWithBitmapRepresentation:data];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)whitespaceCharacterSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *data = [self _bitmapForSet:@"whitespaceCharSet"];
|
|
|
|
|
|
|
|
return [self characterSetWithBitmapRepresentation:data];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creating custom character sets
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)characterSetWithBitmapRepresentation:(NSData *)data
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
return [[[NSBitmapCharSet alloc] initWithBitmap:data] autorelease];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)characterSetWithCharactersInString:(NSString *)aString
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
int i, length;
|
|
|
|
char *bytes;
|
|
|
|
NSMutableData *bitmap = [NSMutableData dataWithLength:BITMAP_SIZE];
|
|
|
|
|
|
|
|
if (!aString)
|
|
|
|
{
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
format:@"Creating character set with nil string"];
|
|
|
|
/* NOT REACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
length = [aString length];
|
|
|
|
bytes = [bitmap mutableBytes];
|
|
|
|
for (i=0; i < length; i++)
|
|
|
|
{
|
|
|
|
unichar letter = [aString characterAtIndex:i];
|
|
|
|
SETBIT(bytes[letter/8], letter % 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [self characterSetWithBitmapRepresentation:bitmap];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSCharacterSet *)characterSetWithRange:(NSRange)aRange
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
int i;
|
|
|
|
char *bytes;
|
|
|
|
NSMutableData *bitmap = [NSMutableData dataWithLength:BITMAP_SIZE];
|
|
|
|
|
|
|
|
if (NSMaxRange(aRange) > UNICODE_SIZE)
|
|
|
|
{
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
format:@"Specified range exceeds character set"];
|
|
|
|
/* NOT REACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = (char *)[bitmap mutableBytes];
|
|
|
|
for (i=aRange.location; i < NSMaxRange(aRange); i++)
|
|
|
|
SETBIT(bytes[i/8], i % 8);
|
|
|
|
|
|
|
|
return [self characterSetWithBitmapRepresentation:bitmap];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData *)bitmapRepresentation
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)characterIsMember:(unichar)aCharacter
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSCharacterSet *)invertedSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
int i, length;
|
|
|
|
char *bytes;
|
|
|
|
NSMutableData *bitmap = [[self bitmapRepresentation] mutableCopy];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
1995-08-30 21:31:29 +00:00
|
|
|
length = [bitmap length];
|
|
|
|
bytes = [bitmap mutableBytes];
|
|
|
|
for (i=0; i < length; i++)
|
|
|
|
bytes[i] = ~bytes[i];
|
1995-05-05 21:03:04 +00:00
|
|
|
|
1995-08-30 21:31:29 +00:00
|
|
|
return [[self class] characterSetWithBitmapRepresentation:bitmap];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
1995-08-30 21:31:29 +00:00
|
|
|
|
|
|
|
// NSCopying, NSMutableCopying
|
1995-05-05 21:03:04 +00:00
|
|
|
- (id)copyWithZone:(NSZone *)zone
|
|
|
|
{
|
|
|
|
if (NSShouldRetainWithZone(self, zone))
|
|
|
|
return [self retain];
|
|
|
|
else
|
1995-08-30 21:31:29 +00:00
|
|
|
return [super copyWithZone:zone];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)mutableCopyWithZone:(NSZone *)zone
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *bitmap;
|
|
|
|
bitmap = [self bitmapRepresentation];
|
|
|
|
return [[NSMutableBitmapCharSet allocWithZone:zone] initWithBitmap:bitmap];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSMutableCharacterSet
|
|
|
|
|
1995-08-30 21:31:29 +00:00
|
|
|
/* Provide a default object for allocation */
|
|
|
|
+ allocWithZone:(NSZone *)zone
|
|
|
|
{
|
|
|
|
return NSAllocateObject([NSMutableBitmapCharSet self], 0, zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Override this from NSCharacterSet to create the correct class */
|
|
|
|
+ (NSCharacterSet *)characterSetWithBitmapRepresentation:(NSData *)data
|
|
|
|
{
|
|
|
|
return [[[NSMutableBitmapCharSet alloc] initWithBitmap:data] autorelease];
|
|
|
|
}
|
|
|
|
|
1995-05-05 21:03:04 +00:00
|
|
|
/* Mutable subclasses must implement ALL of these methods. */
|
|
|
|
- (void)addCharactersInRange:(NSRange)aRange
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addCharactersInString:(NSString *)aString
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)formUnionWithCharacterSet:(NSCharacterSet *)otherSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)formIntersectionWithCharacterSet:(NSCharacterSet *)otherSet
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeCharactersInRange:(NSRange)aRange
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeCharactersInString:(NSString *)aString
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invert
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NSCopying, NSMutableCopying
|
|
|
|
- (id)copyWithZone:(NSZone *)zone
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
NSData *bitmap;
|
|
|
|
bitmap = [self bitmapRepresentation];
|
|
|
|
return [[NSBitmapCharSet allocWithZone:zone] initWithBitmap:bitmap];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)mutableCopyWithZone:(NSZone *)zone
|
|
|
|
{
|
1995-08-30 21:31:29 +00:00
|
|
|
return [super mutableCopyWithZone:zone];
|
1995-05-05 21:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|