From 02d7398ee219710f2d4f0d98dd23c7e98eb66a97 Mon Sep 17 00:00:00 2001 From: Kim Wittenburg Date: Sun, 7 Sep 2014 16:46:50 +0200 Subject: [PATCH] Added Some Helpers --- MathPad/MPRangePath.m | 14 ++++++++++++++ MathPad/NSIndexPath+MPAdditions.m | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+) 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