Corrected Some Formatting Issues
This commit is contained in:
+10
-5
@@ -78,16 +78,21 @@ extern NSString *MPDivisionOperator;
|
|||||||
|
|
||||||
@interface MPMutableExpression : MPExpression
|
@interface MPMutableExpression : MPExpression
|
||||||
|
|
||||||
- (void)replaceSymbolsInRange:(NSRange)range withSymbols:(NSArray *)symbols;
|
- (void)replaceSymbolsInRange:(NSRange)range
|
||||||
|
withSymbols:(NSArray *)symbols;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@interface MPMutableExpression (MPMutableExpressionExtensionMethods)
|
@interface MPMutableExpression (MPMutableExpressionExtensionMethods)
|
||||||
|
|
||||||
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;
|
- (void)insertString:(NSString *)aString
|
||||||
- (void)insertFunction:(MPFunction *)aFunction atIndex:(NSUInteger)loc;
|
atIndex:(NSUInteger)loc;
|
||||||
- (void)insertExpression:(MPExpression *)anExpression atIndex:(NSUInteger)loc;
|
- (void)insertFunction:(MPFunction *)aFunction
|
||||||
- (void)insertSymbols:(NSArray *)symbols atIndex:(NSUInteger)loc;
|
atIndex:(NSUInteger)loc;
|
||||||
|
- (void)insertExpression:(MPExpression *)anExpression
|
||||||
|
atIndex:(NSUInteger)loc;
|
||||||
|
- (void)insertSymbols:(NSArray *)symbols
|
||||||
|
atIndex:(NSUInteger)loc;
|
||||||
|
|
||||||
- (void)deleteSymbolsInRange:(NSRange)range;
|
- (void)deleteSymbolsInRange:(NSRange)range;
|
||||||
|
|
||||||
|
|||||||
+75
-37
@@ -10,6 +10,8 @@
|
|||||||
#import "MPFunction.h"
|
#import "MPFunction.h"
|
||||||
#import "MPException.h"
|
#import "MPException.h"
|
||||||
|
|
||||||
|
#import "NSObject+MPStringTest.h"
|
||||||
|
|
||||||
NSString *MPAdditionOperator = @"+";
|
NSString *MPAdditionOperator = @"+";
|
||||||
NSString *MPSubtractionOperator = @"-";
|
NSString *MPSubtractionOperator = @"-";
|
||||||
NSString *MPMultiplicationOperator = @"*";
|
NSString *MPMultiplicationOperator = @"*";
|
||||||
@@ -21,7 +23,9 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
- (void)validateSymbols:(NSArray *)symbols;
|
- (void)validateSymbols:(NSArray *)symbols;
|
||||||
- (NSArray *)repairedSymbols:(NSArray *)symbols; // Merges subsequent strings
|
- (NSArray *)repairedSymbols:(NSArray *)symbols; // Merges subsequent strings
|
||||||
- (void)repairSymbols:(NSMutableArray *)symbols;
|
- (void)repairSymbols:(NSMutableArray *)symbols;
|
||||||
- (void)getSplitOffset:(out NSUInteger *)offset inSymbolAtIndex:(out NSUInteger *)symbolIndex forSplitLocation:(NSUInteger)loc;
|
- (void)getSplitOffset:(out NSUInteger *)offset
|
||||||
|
inSymbolAtIndex:(out NSUInteger *)symbolIndex
|
||||||
|
forSplitLocation:(NSUInteger)loc;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@@ -98,7 +102,7 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
|
|
||||||
- (NSInteger)lengthOfSymbol:(id)symbol
|
- (NSInteger)lengthOfSymbol:(id)symbol
|
||||||
{
|
{
|
||||||
if ([symbol isKindOfClass:[NSString class]]) {
|
if ([symbol isString]) {
|
||||||
return [symbol length];
|
return [symbol length];
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -108,7 +112,7 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
- (void)validateSymbols:(NSArray *)symbols
|
- (void)validateSymbols:(NSArray *)symbols
|
||||||
{
|
{
|
||||||
for (id symbol in symbols) {
|
for (id symbol in symbols) {
|
||||||
if (!([symbol isKindOfClass:[NSString class]]
|
if (!([symbol isString]
|
||||||
|| [symbol isKindOfClass:[MPFunction class]])) {
|
|| [symbol isKindOfClass:[MPFunction class]])) {
|
||||||
@throw [NSException exceptionWithName:MPIllegalSymbolException
|
@throw [NSException exceptionWithName:MPIllegalSymbolException
|
||||||
reason:@"Only NSString and MPFunction objects are valid symbols."
|
reason:@"Only NSString and MPFunction objects are valid symbols."
|
||||||
@@ -127,19 +131,26 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
|
|
||||||
- (void)repairSymbols:(NSMutableArray *)symbols
|
- (void)repairSymbols:(NSMutableArray *)symbols
|
||||||
{
|
{
|
||||||
for (NSInteger index = 1; index < symbols.count; index++) {
|
for (NSInteger index = 0; index < symbols.count; index++) {
|
||||||
id last = symbols[index-1];
|
id next = index+1 < symbols.count ? symbols[index+1] : nil;
|
||||||
id current = symbols[index];
|
id current = symbols[index];
|
||||||
if ([last isKindOfClass:[NSString class]] && [current isKindOfClass:[NSString class]]) {
|
if ([current isString]) {
|
||||||
NSString *new = [NSString stringWithFormat:@"%@%@", last, current];
|
if ([current length] == 0) {
|
||||||
[symbols replaceObjectAtIndex:index-1 withObject:new];
|
[symbols removeObjectAtIndex:index];
|
||||||
[symbols removeObjectAtIndex:index];
|
index--;
|
||||||
index--;
|
} else if ([next isString]) {
|
||||||
|
NSString *new = [NSString stringWithFormat:@"%@%@", current, next];
|
||||||
|
[symbols replaceObjectAtIndex:index withObject:new];
|
||||||
|
[symbols removeObjectAtIndex:index+1];
|
||||||
|
index--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)getSplitOffset:(out NSUInteger *)offset inSymbolAtIndex:(out NSUInteger *)symbolIndex forSplitLocation:(NSUInteger)loc
|
- (void)getSplitOffset:(out NSUInteger *)offset
|
||||||
|
inSymbolAtIndex:(out NSUInteger *)symbolIndex
|
||||||
|
forSplitLocation:(NSUInteger)loc
|
||||||
{
|
{
|
||||||
NSUInteger length = 0;
|
NSUInteger length = 0;
|
||||||
NSUInteger index = 0;
|
NSUInteger index = 0;
|
||||||
@@ -227,7 +238,9 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
- (MPExpression *)subexpressionWithRange:(NSRange)range
|
- (MPExpression *)subexpressionWithRange:(NSRange)range
|
||||||
{
|
{
|
||||||
if (NSMaxRange(range) > self.length) {
|
if (NSMaxRange(range) > self.length) {
|
||||||
@throw [NSException exceptionWithName:NSRangeException reason:@"Range outside bounds of expression." userInfo:nil];
|
@throw [NSException exceptionWithName:NSRangeException
|
||||||
|
reason:@"Range outside bounds of expression."
|
||||||
|
userInfo:nil];
|
||||||
}
|
}
|
||||||
if (range.location == self.length || NSMaxRange(range) == 0 || range.length == 0) {
|
if (range.location == self.length || NSMaxRange(range) == 0 || range.length == 0) {
|
||||||
// Speed this up
|
// Speed this up
|
||||||
@@ -235,20 +248,24 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
}
|
}
|
||||||
NSUInteger startOffset;
|
NSUInteger startOffset;
|
||||||
NSUInteger startSymbolIndex;
|
NSUInteger startSymbolIndex;
|
||||||
[self getSplitOffset:&startOffset inSymbolAtIndex:&startSymbolIndex forSplitLocation:range.location];
|
[self getSplitOffset:&startOffset
|
||||||
|
inSymbolAtIndex:&startSymbolIndex
|
||||||
|
forSplitLocation:range.location];
|
||||||
id startSymbol = _symbols[startSymbolIndex];
|
id startSymbol = _symbols[startSymbolIndex];
|
||||||
if (startOffset == [self lengthOfSymbol:startSymbol]) {
|
if (startOffset == [self lengthOfSymbol:startSymbol]) {
|
||||||
startOffset = 0;
|
startOffset = 0;
|
||||||
startSymbolIndex++;
|
startSymbolIndex++;
|
||||||
startSymbol = _symbols[startSymbolIndex];
|
startSymbol = _symbols[startSymbolIndex];
|
||||||
} else if ([startSymbol isKindOfClass:[NSString class]]) {
|
} else if ([startSymbol isString]) {
|
||||||
startSymbol = [startSymbol substringFromIndex:startOffset];
|
startSymbol = [startSymbol substringFromIndex:startOffset];
|
||||||
}
|
}
|
||||||
NSUInteger endOffset;
|
NSUInteger endOffset;
|
||||||
NSUInteger endSymbolIndex;
|
NSUInteger endSymbolIndex;
|
||||||
[self getSplitOffset:&endOffset inSymbolAtIndex:&endSymbolIndex forSplitLocation:NSMaxRange(range)];
|
[self getSplitOffset:&endOffset
|
||||||
|
inSymbolAtIndex:&endSymbolIndex
|
||||||
|
forSplitLocation:NSMaxRange(range)];
|
||||||
id endSymbol = _symbols[endSymbolIndex];
|
id endSymbol = _symbols[endSymbolIndex];
|
||||||
if ([endSymbol isKindOfClass:[NSString class]]) {
|
if ([endSymbol isString]) {
|
||||||
endSymbol = [endSymbol substringToIndex:endOffset];
|
endSymbol = [endSymbol substringToIndex:endOffset];
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -261,9 +278,10 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
}
|
}
|
||||||
if (endSymbolIndex > startSymbolIndex) {
|
if (endSymbolIndex > startSymbolIndex) {
|
||||||
[symbols addObject:endSymbol];
|
[symbols addObject:endSymbol];
|
||||||
} else if (endSymbolIndex == startSymbolIndex && [startSymbol isKindOfClass:[NSString class]]) {
|
} else if (endSymbolIndex == startSymbolIndex && [startSymbol isString]) {
|
||||||
NSString *result = [_symbols[startSymbolIndex] substringWithRange:NSMakeRange(startOffset, endOffset-startOffset)];
|
NSString *result = [_symbols[startSymbolIndex] substringWithRange:NSMakeRange(startOffset, endOffset-startOffset)];
|
||||||
[symbols replaceObjectAtIndex:0 withObject:result];
|
[symbols replaceObjectAtIndex:0
|
||||||
|
withObject:result];
|
||||||
}
|
}
|
||||||
return [[MPExpression alloc] initWithSymbols:symbols];
|
return [[MPExpression alloc] initWithSymbols:symbols];
|
||||||
}
|
}
|
||||||
@@ -343,13 +361,14 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
NSMutableString *description = [[NSMutableString alloc] init];
|
NSMutableString *description = [[NSMutableString alloc] init];
|
||||||
NSUInteger index = 0;
|
NSUInteger index = 0;
|
||||||
for (id symbol in _symbols) {
|
for (id symbol in _symbols) {
|
||||||
if ([symbol isKindOfClass:[NSString class]]) {
|
if ([symbol isString]) {
|
||||||
NSMutableString *correctedSymbol = [[symbol stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
|
NSMutableString *correctedSymbol = [[symbol stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
|
||||||
// Prefix operator
|
// Prefix operator
|
||||||
if (symbol != _symbols[0]) {
|
if (symbol != _symbols[0]) {
|
||||||
unichar prefix = [correctedSymbol characterAtIndex:0];
|
unichar prefix = [correctedSymbol characterAtIndex:0];
|
||||||
if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:prefix]) {
|
if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:prefix]) {
|
||||||
[correctedSymbol insertString:@"*" atIndex:0];
|
[correctedSymbol insertString:@"*"
|
||||||
|
atIndex:0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Suffix operator
|
// Suffix operator
|
||||||
@@ -385,7 +404,8 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
self = [super initWithSymbols:nil];
|
self = [super initWithSymbols:nil];
|
||||||
if (self) {
|
if (self) {
|
||||||
symbols = [self repairedSymbols:symbols];
|
symbols = [self repairedSymbols:symbols];
|
||||||
_symbols = [[NSMutableArray alloc] initWithArray:symbols copyItems:YES];
|
_symbols = [[NSMutableArray alloc] initWithArray:symbols
|
||||||
|
copyItems:YES];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
@@ -396,21 +416,26 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
return [_symbols copy];
|
return [_symbols copy];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)replaceSymbolsInRange:(NSRange)range withSymbols:(NSArray *)symbols
|
- (void)replaceSymbolsInRange:(NSRange)range
|
||||||
|
withSymbols:(NSArray *)symbols
|
||||||
{
|
{
|
||||||
if (NSMaxRange(range) > self.length) {
|
if (NSMaxRange(range) > self.length) {
|
||||||
@throw [NSException exceptionWithName:NSRangeException reason:@"Range out of bounds of expression." userInfo:nil];
|
@throw [NSException exceptionWithName:NSRangeException
|
||||||
|
reason:@"Range out of bounds of expression."
|
||||||
|
userInfo:nil];
|
||||||
}
|
}
|
||||||
[self validateSymbols:symbols];
|
[self validateSymbols:symbols];
|
||||||
|
|
||||||
// Locate the position, split the symbols
|
// Locate the position, split the symbols
|
||||||
NSUInteger startIndex;
|
NSUInteger startIndex;
|
||||||
[self splitSymbolsAtLocation:range.location insertionIndex:&startIndex];
|
[self splitSymbolsAtLocation:range.location
|
||||||
|
insertionIndex:&startIndex];
|
||||||
|
|
||||||
// Perform the deletion
|
// Perform the deletion
|
||||||
if (range.length > 0) {
|
if (range.length > 0) {
|
||||||
NSUInteger endIndex;
|
NSUInteger endIndex;
|
||||||
[self splitSymbolsAtLocation:NSMaxRange(range) insertionIndex:&endIndex];
|
[self splitSymbolsAtLocation:NSMaxRange(range)
|
||||||
|
insertionIndex:&endIndex];
|
||||||
NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];
|
NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];
|
||||||
for (NSUInteger index = startIndex; index < endIndex; index++) {
|
for (NSUInteger index = startIndex; index < endIndex; index++) {
|
||||||
[indexes addIndex:index];
|
[indexes addIndex:index];
|
||||||
@@ -421,7 +446,8 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
// Perform the insertion
|
// Perform the insertion
|
||||||
if (symbols.count > 0) {
|
if (symbols.count > 0) {
|
||||||
NSArray *newSymbols = [[NSArray alloc] initWithArray:symbols copyItems:YES];
|
NSArray *newSymbols = [[NSArray alloc] initWithArray:symbols copyItems:YES];
|
||||||
[(NSMutableArray *)_symbols replaceObjectsInRange:NSMakeRange(startIndex, 0) withObjectsFromArray:newSymbols];
|
[(NSMutableArray *)_symbols replaceObjectsInRange:NSMakeRange(startIndex, 0)
|
||||||
|
withObjectsFromArray:newSymbols];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Revalidate structure and invalidate length
|
// Revalidate structure and invalidate length
|
||||||
@@ -429,11 +455,14 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
_length = 0;
|
_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)splitSymbolsAtLocation:(NSUInteger)loc insertionIndex:(out NSUInteger *)insertionIndex
|
- (void)splitSymbolsAtLocation:(NSUInteger)loc
|
||||||
|
insertionIndex:(out NSUInteger *)insertionIndex
|
||||||
{
|
{
|
||||||
NSUInteger splitSymbolIndex;
|
NSUInteger splitSymbolIndex;
|
||||||
NSUInteger splitOffset;
|
NSUInteger splitOffset;
|
||||||
[self getSplitOffset:&splitOffset inSymbolAtIndex:&splitSymbolIndex forSplitLocation:loc];
|
[self getSplitOffset:&splitOffset
|
||||||
|
inSymbolAtIndex:&splitSymbolIndex
|
||||||
|
forSplitLocation:loc];
|
||||||
id splitSymbol = _symbols[splitSymbolIndex];
|
id splitSymbol = _symbols[splitSymbolIndex];
|
||||||
NSInteger splitSymbolLength = [self lengthOfSymbol:splitSymbol];
|
NSInteger splitSymbolLength = [self lengthOfSymbol:splitSymbol];
|
||||||
if (splitOffset == splitSymbolLength) {
|
if (splitOffset == splitSymbolLength) {
|
||||||
@@ -443,9 +472,11 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
if (splitOffset != 0) {
|
if (splitOffset != 0) {
|
||||||
NSString *leftPart = [splitSymbol substringToIndex:splitOffset];
|
NSString *leftPart = [splitSymbol substringToIndex:splitOffset];
|
||||||
NSString *rightPart = [splitSymbol substringFromIndex:splitOffset];
|
NSString *rightPart = [splitSymbol substringFromIndex:splitOffset];
|
||||||
[(NSMutableArray *)_symbols replaceObjectAtIndex:splitSymbolIndex withObject:leftPart];
|
[(NSMutableArray *)_symbols replaceObjectAtIndex:splitSymbolIndex
|
||||||
|
withObject:leftPart];
|
||||||
splitSymbolIndex++;
|
splitSymbolIndex++;
|
||||||
[(NSMutableArray *)_symbols insertObject:rightPart atIndex:splitSymbolIndex];
|
[(NSMutableArray *)_symbols insertObject:rightPart
|
||||||
|
atIndex:splitSymbolIndex];
|
||||||
}
|
}
|
||||||
*insertionIndex = splitSymbolIndex;
|
*insertionIndex = splitSymbolIndex;
|
||||||
}
|
}
|
||||||
@@ -454,22 +485,29 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
|
|
||||||
@implementation MPMutableExpression (MPMutableExpressionExtensionMethods)
|
@implementation MPMutableExpression (MPMutableExpressionExtensionMethods)
|
||||||
|
|
||||||
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc
|
- (void)insertString:(NSString *)aString
|
||||||
|
atIndex:(NSUInteger)loc
|
||||||
{
|
{
|
||||||
[self insertSymbols:@[aString] atIndex:loc];
|
[self insertSymbols:@[aString]
|
||||||
|
atIndex:loc];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)insertFunction:(MPFunction *)aFunction atIndex:(NSUInteger)loc
|
- (void)insertFunction:(MPFunction *)aFunction
|
||||||
|
atIndex:(NSUInteger)loc
|
||||||
{
|
{
|
||||||
[self insertSymbols:@[aFunction] atIndex:loc];
|
[self insertSymbols:@[aFunction]
|
||||||
|
atIndex:loc];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)insertExpression:(MPExpression *)anExpression atIndex:(NSUInteger)loc
|
- (void)insertExpression:(MPExpression *)anExpression
|
||||||
|
atIndex:(NSUInteger)loc
|
||||||
{
|
{
|
||||||
[self insertSymbols:anExpression.symbols atIndex:loc];
|
[self insertSymbols:anExpression.symbols
|
||||||
|
atIndex:loc];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)insertSymbols:(NSArray *)symbols atIndex:(NSUInteger)loc
|
- (void)insertSymbols:(NSArray *)symbols
|
||||||
|
atIndex:(NSUInteger)loc
|
||||||
{
|
{
|
||||||
[self replaceSymbolsInRange:NSMakeRange(loc, 0)
|
[self replaceSymbolsInRange:NSMakeRange(loc, 0)
|
||||||
withSymbols:symbols];
|
withSymbols:symbols];
|
||||||
|
|||||||
@@ -18,7 +18,8 @@
|
|||||||
|
|
||||||
- (NSUInteger)numberOfChildren;
|
- (NSUInteger)numberOfChildren;
|
||||||
- (MPExpression *)childAtIndex:(NSUInteger)index;
|
- (MPExpression *)childAtIndex:(NSUInteger)index;
|
||||||
- (void)setChild:(MPExpression *)child atIndex:(NSUInteger)index;
|
- (void)setChild:(MPExpression *)child
|
||||||
|
atIndex:(NSUInteger)index;
|
||||||
|
|
||||||
#pragma mark Evaluating Functions
|
#pragma mark Evaluating Functions
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,8 @@
|
|||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setChild:(MPExpression *)child atIndex:(NSUInteger)index
|
- (void)setChild:(MPExpression *)child
|
||||||
|
atIndex:(NSUInteger)index
|
||||||
{}
|
{}
|
||||||
|
|
||||||
#pragma mark Evaluating Functions
|
#pragma mark Evaluating Functions
|
||||||
|
|||||||
Reference in New Issue
Block a user