diff --git a/MathPad/MPRangePath.m b/MathPad/MPRangePath.m index 801c6b7..9e4dcd0 100644 --- a/MathPad/MPRangePath.m +++ b/MathPad/MPRangePath.m @@ -116,6 +116,20 @@ return [self.location isEqual:aRangePath.location] && self.length == aRangePath.length; } +- (NSString *)description +{ + NSMutableString *description = [[NSMutableString alloc] initWithString:@"MPRangePath 0) { + [description appendFormat:@"%ld", [self.location indexAtPosition:0]]; + } + for (NSUInteger position = 1; position < self.location.length; position ++) { + [description appendFormat:@",%ld", [self.location indexAtPosition:position]]; + } + [description appendFormat:@" length=%ld", self.length]; + [description appendString:@">"]; + return description.copy; +} + #pragma mark - NSCopying - (id)copyWithZone:(NSZone *)zone diff --git a/MathPad/NSIndexPath+MPAdditions.m b/MathPad/NSIndexPath+MPAdditions.m index ee58bce..43a975f 100644 --- a/MathPad/NSIndexPath+MPAdditions.m +++ b/MathPad/NSIndexPath+MPAdditions.m @@ -58,4 +58,29 @@ return [[self indexPathByRemovingLastIndex] indexPathByAddingIndex:lastIndex]; } +- (NSIndexPath *)indexPathWithLength:(NSUInteger)length +{ + NSIndexPath *indexPath = [[NSIndexPath alloc] init]; + for (NSUInteger position = 0; position < length; position++) { + indexPath = [indexPath indexPathByAddingIndex:[self indexAtPosition:position]]; + } + return indexPath; +} + +- (NSIndexPath *)commonIndexPathWith:(NSIndexPath *)indexPath +{ + NSIndexPath *commonPath = [[NSIndexPath alloc] init]; + NSUInteger length = MIN(self.length, indexPath.length); + for (NSUInteger position = 0; position < length; position++) { + NSUInteger selfIndex = [self indexAtPosition:position]; + NSUInteger otherIndex = [indexPath indexAtPosition:position]; + if (selfIndex == otherIndex) { + commonPath = [commonPath indexPathByAddingIndex:selfIndex]; + } else { + break; + } + } + return commonPath; +} + @end