ftp.nice.ch/Attic/openStep/implementation/gnustep/sources/libFoundation.0.7.tgz#/libFoundation-0.7/libFoundation/Foundation/NSConcreteCharacterSet.m

This is NSConcreteCharacterSet.m in view mode; [Download] [Up]

/* 
   NSConcreteCharacterSet.m

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <Foundation/common.h>
#include <Foundation/NSCharacterSet.h>
#include <Foundation/NSString.h>
#include <Foundation/NSData.h>
#include "NSConcreteCharacterSet.h"

/*
 * NSRangeCharacterSet
 */

@implementation NSRangeCharacterSet

- init
{
    NSRange aRange = {0,0};
    return [self initWithRange:aRange inverted:NO];
}

- initWithRange:(NSRange)aRange
{
    return [self initWithRange:aRange inverted:NO];
}

- initWithRange:(NSRange)aRange inverted:(BOOL)inv
{
    [super init];
    range = aRange;
    inverted = inv;
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (NSData *)bitmapRepresentation
{
    char* bytes = Calloc(1, BITMAPDATABYTES);
    int i;
    
    for (i=MIN(range.location+range.length-1, BITMAPDATABYTES);
		i >= range.location; i--)
	SETBIT(bytes, i);
    
    return [NSData dataWithBytesNoCopy:bytes 
	    length:BITMAPDATABYTES];
}

- (BOOL)characterIsMember:(unichar)aCharacter
{
    return inverted ^ (range.location<=aCharacter &&
	    aCharacter<range.location+range.length);
}

- (NSCharacterSet *)invertedSet
{
    return [[[NSRangeCharacterSet alloc]
		initWithRange:range inverted:!inverted]
		autorelease];
}

// NSCopying

- copyWithZone:(NSZone*)zone
{
    if ([self zone] == zone)
	return [self retain];
    else {
	return [[NSRangeCharacterSet allocWithZone:zone]
		initWithRange:range inverted:inverted];
    }
}

@end /* NSRangeCharacterSet */

/*
 * NSStringCharacterSet
 */

@implementation NSStringCharacterSet

- init
{
    return [self initWithString:@"" inverted:NO];
}

- initWithString:(NSString*)aString
{
    return [self initWithString:aString inverted:NO];
}

- initWithString:(NSString*)aString inverted:(BOOL)inv
{
    [super init];
    string = [aString copy];
    inverted = inv;
    return self;
}

- (void)dealloc
{
    [string release];
    [super dealloc];
}

- (NSData *)bitmapRepresentation
{
    char*	bytes = Calloc(1, BITMAPDATABYTES);
    int		i;
    unichar	c;
    
    for (i=[string length]; i>=0; i--) {
	    c = [string characterAtIndex:i];
	SETBIT(bytes, c);
    }
    
    return [NSData dataWithBytesNoCopy:bytes 
	    length:BITMAPDATABYTES];
}

- (BOOL)characterIsMember:(unichar)aCharacter
{
    int i;
    
    for (i=[string length]; i>=0; i++)
	if (aCharacter == [string characterAtIndex:i])
	    return !inverted;
    return inverted;
}

- (NSCharacterSet *)invertedSet
{
    return [[[NSStringCharacterSet alloc]
	    initWithString:string inverted:!inverted]
	    autorelease];
}

// NSCopying

- copyWithZone:(NSZone*)zone
{
    if ([self zone] == zone)
	return [self retain];
    else {
	return [[NSStringCharacterSet allocWithZone:zone] 
		initWithString:string];
    }
}

@end /* NSStringCharacterSet */

/*
 * NSBitmapCharacterSet
 */

@implementation NSBitmapCharacterSet

- init
{
    return [self initWithBitmapRepresentation:
	    [[NSCharacterSet emptyCharacterSet] bitmapRepresentation] 
	    inverted:NO];
}

- initWithBitmapRepresentation:(id)aData
{
    return [self initWithBitmapRepresentation:aData inverted:NO];
}

- initWithBitmapRepresentation:(id)aData inverted:(BOOL)inv
{
    [super init];
    data = [aData copy];
    bytes = (char*)[data bytes];
    inverted = inv;
    return self;
}

- (void)dealloc
{
    [data release];
    [super dealloc];
}

- (NSData *)bitmapRepresentation
{
    if (inverted) {
	char* theBytes = Calloc(1, BITMAPDATABYTES);
	int i;
	
	for (i = 0; i < BITMAPDATABYTES; i++)
	    theBytes[i] = ~bytes[i];
		
	return [NSData dataWithBytesNoCopy:theBytes length:BITMAPDATABYTES];
    }
    return data;
}

- (BOOL)characterIsMember:(unichar)aCharacter
{
    return inverted ^ ISBITSET(bytes, aCharacter);
}

- (NSCharacterSet *)invertedSet
{
    return [[[NSBitmapCharacterSet alloc]
	    initWithBitmapRepresentation:data inverted:!inverted]
	    autorelease];
}

// NSCopying

- copyWithZone:(NSZone*)zone
{
    if ([self zone] == zone)
	return [self retain];
    else {
	id aData = [self bitmapRepresentation];
	return [[NSBitmapCharacterSet allocWithZone:zone]
		initWithBitmapRepresentation:aData];
    }
}

@end /* NSBitmapCharacterSet */

/*
* NSMutableBitmapCharacterSet
*/

@implementation NSMutableBitmapCharacterSet

- init
{
    [super init];
    data = [NSMutableData dataWithCapacity:BITMAPDATABYTES];
    bytes = (char*)[data bytes];
    return self;
}

- initWithBitmapRepresentation:(id)aData
{
    return [self initWithBitmapRepresentation:aData inverted:NO];
}

- initWithBitmapRepresentation:(id)aData inverted:(BOOL)inv
{
    if (!aData)
	return [self init];
    else {
	data = [aData mutableCopyWithZone:[self zone]];
	bytes = (char*)[data bytes];
	if (inv)
	    [self invert];
	return self;
    }
}

- (void)dealloc
{
    [data release];
    [super dealloc];
}

- (NSData *)bitmapRepresentation
{
    if (inverted) {
	char* theBytes = Calloc(1, BITMAPDATABYTES);
	int i;
	
	for (i = 0; i < BITMAPDATABYTES; i++)
	    theBytes[i] = ~bytes[i];
		
	return [NSData dataWithBytesNoCopy:theBytes length:BITMAPDATABYTES];
    }
    return data;
}

- (BOOL)characterIsMember:(unichar)aCharacter
{
    return inverted ^ ISBITSET(bytes, aCharacter);
}

- (NSCharacterSet *)invertedSet
{
    return [[[NSBitmapCharacterSet alloc]
	    initWithBitmapRepresentation:data inverted:!inverted]
	    autorelease];
}

- (void)addCharactersInRange:(NSRange)aRange
{
    int i;

    for (i=MIN(aRange.location+aRange.length-1, BITMAPDATABYTES);
		i >= aRange.location; i--)
	if (inverted)
	    RESETBIT(bytes, i);
	else
	    SETBIT(bytes, i);
}

- (void)addCharactersInString:(NSString *)aString
{
    int i;
    unichar c;
    
    for (i=[aString length]; i >= 0; i--) {
	c = [aString characterAtIndex:i];
	if (inverted)
	    RESETBIT(bytes, c);
	else
	    SETBIT(bytes, c);
    }
}

- (void)removeCharactersInRange:(NSRange)aRange
{
    int i;

    for (i=MIN(aRange.location+aRange.length-1, BITMAPDATABYTES);
		    i >= aRange.location; i--)
	if (inverted)
	    SETBIT(bytes, i);
	else
	    RESETBIT(bytes, i);
}

- (void)removeCharactersInString:(NSString *)aString
{
    int	i;
    unichar c;
    
    for (i=[aString length]; i >= 0; i--) {
	c = [aString characterAtIndex:i];
	if (inverted)
	    SETBIT(bytes, c);
	else
	    RESETBIT(bytes, c);
    }
}

- (void)formIntersectionWithCharacterSet:(NSCharacterSet *)otherSet
{
    id otherdata = [otherSet bitmapRepresentation];
    char* otherbytes = (char*)[otherdata bytes];
    int i;
    
    if (inverted)
	for (i=0; i < BITMAPDATABYTES; i++)
	    bytes[i] |= ~otherbytes[i];
    else
	for (i=0; i < BITMAPDATABYTES; i++)
	    bytes[i] &= otherbytes[i];
}

- (void)formUnionWithCharacterSet:(NSCharacterSet *)otherSet
{
    id otherdata = [otherSet bitmapRepresentation];
    char* otherbytes = (char*)[otherdata bytes];
    int i;
    
    if (inverted)
	for (i=0; i<BITMAPDATABYTES; i++)
	    bytes[i] &= ~otherbytes[i];
    else
	for (i=0; i<BITMAPDATABYTES; i++)
	    bytes[i] |= otherbytes[i];
}

- (void)invert
{
    inverted = !inverted;
}

@end /* NSMutableBitmapCharacterSet */

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.