#import <RCString.h>
/*
    Copyright (C) 1992. Bruce Ediger.

      This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Library General Public License.
*/

//
// implementation of "comparison" methods
//

// Is it better to use strcmp() or bcmp()?  bcmp()
// may be faster since it can block out the comparison
// looping.  Or is a custom routine better?

@implementation RCString (Comparison)

// returns
//  < 0 if this object is "before" anotherObject
//    0 if this object is "same as" anotherObject
//  > 0 if this object is "in front of" anotherObject
//
// should the comparison be case-insensitive if _either_
// object is marked as case-insensitive?
- (int)compareWithObject: (RCString *)anotherObject
{
	int irRetval;

	if (p == NULL || p->s == NULL || anotherObject->p == NULL
		|| anotherObject->p->s == NULL)
			return 0;

	// one quick special case: are references to the same internal rep?
	if (p->n > 1 && p == anotherObject->p)
		return 0;

	if (!yCaseSensitive) {
		RCString *oThisObject, *oOtherObject;
		oThisObject  = [RCString newFromObject:self];
		oOtherObject = [RCString newFromObject:anotherObject];
		[oThisObject  toUpper];
		[oOtherObject toUpper];
		irRetval = strcmp([oThisObject data], [oOtherObject data]);
		[oThisObject  free];
		[oOtherObject free];
	} else {
		irRetval = strcmp([self data], [anotherObject data]);
	}
	return irRetval;
}

// returns
//  < 0 if this object is "before" anAsciiString
//    0 if this object is "same as" anAsciiString
//  > 0 if this object is "in front of" anAsciiString
- (int)compareWithString: (char *)anAsciiString
{
	int irRetval;
	RCString *oTmp = [RCString newFromString:anAsciiString];
	irRetval = [self compareWithObject:oTmp];
	[oTmp free];
	return irRetval;
}

// set whether or not lexicographic comparisons
// are case sensitive.
- caseSensitive: (BOOL)isOrNot
{
	yCaseSensitive = isOrNot;
	return self;
}

@end
