#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 "substring retrieval" methods
//

@implementation RCString (Retrieval)

- (char *)subStringAt:(int)iIndex extent:(int)iLength
{
	char *bpReturn = NULL;

	if (iIndex >= 0 && iLength >= 0) {
		if (iIndex + iLength >= p->l)
			iLength = p->l - 1 - iIndex;
		if (iLength > 0) {
			bpReturn = malloc(iLength + 1);
			if (bpReturn) {
				bcopy(p->s + iIndex, bpReturn, iLength);
				bpReturn[iLength] = '\0';
			}
		}
	}

	return bpReturn;
}

- subObjectAt:(int)index extent:(int)length
{
	RCString *oNew;
	char *bpNewString = [self subStringAt:index extent:length];

	if (bpNewString) {
		oNew = [[RCString alloc] init];
		if (oNew) {
			oNew->p->s = bpNewString;
			oNew->p->l = strlen(bpNewString) + 1;
		} else {
			[oNew free];
			oNew = NULL;
		}
	} else {
		oNew = [RCString new];
	}
	return oNew;
}
@end
