Archived
1

Fixed reference frame conversion error

Added Documentation
This commit is contained in:
Kim Wittenburg
2014-11-25 23:58:59 +01:00
parent 8b0f8109c8
commit 5bac2f0bbe

View File

@@ -22,7 +22,7 @@
NSString *const MPIllegalElementException = @"MPIllegalElementException"; NSString *const MPIllegalElementException = @"Illegal Element Exception";
NSString *const MPIllegalElementExceptionElementKey = @"MPIllegalElementExceptionElementKey"; NSString *const MPIllegalElementExceptionElementKey = @"MPIllegalElementExceptionElementKey";
@@ -30,15 +30,87 @@ NSString *const MPIllegalElementExceptionElementKey = @"MPIllegalElementExceptio
NSMutableArray * _elements; NSMutableArray * _elements;
} }
/*!
@method tokens
@brief Private method. Returns an array containing all tokens from the
receiver.
@return All items in the @c MPTokenReferenceFrame.
*/
- (NSArray *)tokens; - (NSArray *)tokens;
/*!
@method validateElements:
@brief Private method. Checks whether all objects in the specified array
are valid expression elements.
@discussion If an object is not valid a @c MPIllegalElementException is
raised.
@param elements
The array of objects to be validated.
*/
- (void)validateElements:(NSArray *)elements; - (void)validateElements:(NSArray *)elements;
/*!
@method fixElements
@brief Private method. Restores consistency in the receiver after a
change was made.
*/
- (void)fixElements; - (void)fixElements;
/*!
@method _replaceSymbolsInRange:withElements:
@brief Private method. Replaces the symbols in the specified range with
the elements from the @c elements array.
@discussion This is the most primitive mutation method of the @c MPExpression
class.
@param range
The range of symbols to be replaced. The range is specified in
the symbol reference frame. If the range exceeds the receiver's
bounds a @c NSRangeException is raised.
@param elements
The elements that should replace the symbols in the specified
range.
*/
- (void)_replaceSymbolsInRange:(NSRange)range - (void)_replaceSymbolsInRange:(NSRange)range
withElements:(NSArray *)elements; withElements:(NSArray *)elements;
/*!
@method _splitElementsAtLocation:insertionIndex:
@brief Splits the receiver's elements at the specified @c location.
@discussion The split location can be inside a string element. In that case
the string is replaced with two other smaller strings.
Splitting the elements destroys the receiver's integrity. The
receiver of this message must also receive a @c -fixElements
message soon afterwards.
@param location
The index where to split the elements. Specified in the symbol
reference frame.
@param insertionIndex
Splitting elements may shift the indexes of the following
elements. This parameter is set to the index (specified in the
element reference frame) that should be used to insert a new
element at the location where the elements were previously
splitted.
@return @c YES if a string element was split into two smaller strings, @c
NO if the split @c location corresponds to a location between two
elements.
*/
- (BOOL)_splitElementsAtLocation:(NSUInteger)location - (BOOL)_splitElementsAtLocation:(NSUInteger)location
insertionIndex:(out NSUInteger *)insertionIndex; insertionIndex:(NSUInteger *)insertionIndex;
@end @end
@@ -301,7 +373,7 @@ NSString *const MPIllegalElementExceptionElementKey = @"MPIllegalElementExceptio
- (NSUInteger)convertIndex:(NSUInteger)anIndex - (NSUInteger)convertIndex:(NSUInteger)anIndex
fromReferenceFrame:(MPReferenceFrame)fromReferenceFrame fromReferenceFrame:(MPReferenceFrame)fromReferenceFrame
toReferenceFrame:(MPReferenceFrame)toReferenceFrame toReferenceFrame:(MPReferenceFrame)toReferenceFrame
offset:(out NSUInteger *)offset offset:(NSUInteger *)offset
{ {
if (fromReferenceFrame == toReferenceFrame || anIndex == 0) { if (fromReferenceFrame == toReferenceFrame || anIndex == 0) {
if (offset) { if (offset) {
@@ -328,7 +400,6 @@ NSString *const MPIllegalElementExceptionElementKey = @"MPIllegalElementExceptio
symbolIndex += obj.range.length; symbolIndex += obj.range.length;
*stop = idx >= anIndex - 1; *stop = idx >= anIndex - 1;
}]; }];
// symbolIndex = [self.tokens[anIndex] range].location;
break; break;
} }
@@ -362,13 +433,15 @@ NSString *const MPIllegalElementExceptionElementKey = @"MPIllegalElementExceptio
case MPTokenReferenceFrame: case MPTokenReferenceFrame:
{ {
NSUInteger totalLength = 0;
NSUInteger tokenIndex = 0; NSUInteger tokenIndex = 0;
while (true) { id<MPToken> token;
id<MPToken> token = self.tokens[tokenIndex++]; while (totalLength < symbolIndex) {
if (NSMaxRange(token.range) < symbolIndex || token.range.location > symbolIndex) { token = self.tokens[tokenIndex++];
continue; totalLength += token.range.length;
} }
NSUInteger offsetInToken = anIndex - token.range.location; --tokenIndex;
NSUInteger offsetInToken = token.range.length - totalLength + symbolIndex;
if (offsetInToken == token.range.length) { if (offsetInToken == token.range.length) {
offsetInToken = 0; offsetInToken = 0;
tokenIndex++; tokenIndex++;
@@ -378,9 +451,6 @@ NSString *const MPIllegalElementExceptionElementKey = @"MPIllegalElementExceptio
} }
return tokenIndex; return tokenIndex;
} }
// Should never get here
return 0;
}
} }
} }
@@ -497,7 +567,7 @@ NSString *const MPIllegalElementExceptionElementKey = @"MPIllegalElementExceptio
- (BOOL)_splitElementsAtLocation:(NSUInteger)location - (BOOL)_splitElementsAtLocation:(NSUInteger)location
insertionIndex:(out NSUInteger *)insertionIndex insertionIndex:(NSUInteger *)insertionIndex
{ {
if (location == 0) { if (location == 0) {
*insertionIndex = 0; *insertionIndex = 0;
@@ -562,7 +632,7 @@ NSString *const MPIllegalElementExceptionElementKey = @"MPIllegalElementExceptio
MPParsedExpression *parsedExpression = [self parse:&parsingErrors]; MPParsedExpression *parsedExpression = [self parse:&parsingErrors];
NSError *evaluationError; NSError *evaluationError;
NSDecimalNumber *result = parsedExpression ? [parsedExpression evaluate:&evaluationError] : nil; NSDecimalNumber *result = parsedExpression ? [parsedExpression evaluate:&evaluationError] : nil;
if (errors) { if (errors && (parsedExpression || evaluationError)) {
NSMutableArray *localErrors = [[NSMutableArray alloc] initWithCapacity:parsingErrors.count+1]; NSMutableArray *localErrors = [[NSMutableArray alloc] initWithCapacity:parsingErrors.count+1];
if (parsingErrors) { if (parsingErrors) {
[localErrors addObjectsFromArray:parsingErrors]; [localErrors addObjectsFromArray:parsingErrors];