More progress on Autotype command parsing

This commit is contained in:
michael starke
2014-02-20 00:35:32 +01:00
parent ae544ae810
commit a046555aee
5 changed files with 48 additions and 29 deletions

View File

@@ -20,38 +20,31 @@
@implementation MPAutotypeCommand @implementation MPAutotypeCommand
+ (NSArray *)commandsForContext:(MPAutotypeContext *)context { + (NSArray *)commandsForContext:(MPAutotypeContext *)context {
if([context isValid]) { if(![context isValid]) {
return nil; return nil;
} }
NSMutableArray *commands = [[NSMutableArray alloc] initWithCapacity:10]; NSUInteger reserverd = [context.normalizedCommand length] / 4;
BOOL outsideCommand = YES; NSMutableArray *commands = [[NSMutableArray alloc] initWithCapacity:reserverd];
NSString *unparsedCommand = context.normalizedCommand; NSMutableArray __block *commandRanges = [[NSMutableArray alloc] initWithCapacity:reserverd];
while(YES) { NSRegularExpression *commandRegExp = [[NSRegularExpression alloc] initWithPattern:@"\\{[^\\}]+\\}" options:NSRegularExpressionCaseInsensitive error:0];
/* Outside Command */ NSAssert(commandRegExp, @"RegExp is constant. Has to work all the time");
if(outsideCommand) { [commandRegExp enumerateMatchesInString:context.normalizedCommand options:0 range:NSMakeRange(0, [context.normalizedCommand length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange openingBracketRange = [unparsedCommand rangeOfString:@"{"]; @autoreleasepool {
if(openingBracketRange.location != NSNotFound && openingBracketRange.length == 1) { [commandRanges addObject:[NSValue valueWithRange:result.range]];
outsideCommand = NO;
NSString *skipped = [unparsedCommand substringToIndex:openingBracketRange.location];
unparsedCommand = [unparsedCommand substringFromIndex:openingBracketRange.location + 1];
} }
else { }];
/* No more opeing brackets, stop - or none at all */ NSUInteger skipped = 0;
[self appendPasteCommandForContent:unparsedCommand toCommands:commands]; for(NSValue *rangeValue in commandRanges) {
break; NSRange range = [rangeValue rangeValue];
/* All non-commands will get translated into paste commands */
if(range.location > skipped) {
NSString *pasteValue = [context.normalizedCommand substringWithRange:NSMakeRange(skipped, range.location - skipped)];
[self appendPasteCommandForContent:pasteValue toCommands:commands];
skipped = range.location;
} }
} }
/* Inside Command */
else {
NSRange closingBracketRange = [unparsedCommand rangeOfString:@"}"];
if(closingBracketRange.location == NSNotFound || closingBracketRange.length != 1) {
return nil; return nil;
} }
outsideCommand = NO;
}
}
return commands;
}
+ (MPAutotypeCommand *)appendPasteCommandForContent:(NSString *)pasteContent toCommands:(NSMutableArray *)commands { + (MPAutotypeCommand *)appendPasteCommandForContent:(NSString *)pasteContent toCommands:(NSMutableArray *)commands {
if(pasteContent) { if(pasteContent) {

View File

@@ -24,8 +24,8 @@
/** /**
* The Autotype command as it's supplied by the entry * The Autotype command as it's supplied by the entry
*/ */
@property (nonatomic, copy) NSString *command; @property (nonatomic, readonly, copy) NSString *command;
@property (nonatomic, copy) NSString *normalizedCommand; @property (nonatomic, readonly, copy) NSString *normalizedCommand;
/** /**
* Designated initializer * Designated initializer
@@ -45,4 +45,6 @@
*/ */
- (BOOL)isValid; - (BOOL)isValid;
- (NSString *)evaluatedCommand;
@end @end

View File

@@ -40,8 +40,18 @@
return copy; return copy;
} }
- (BOOL)isValid { - (BOOL)isValid {
return (self.normalizedCommand != nil); return (self.normalizedCommand != nil);
} }
- (NSString *)evaluatedCommand {
static NSString *evaluated;
if(!evaluated) {
NSString *placeholderFilled = [self.normalizedCommand evaluatePlaceholderWithEntry:self.entry];
evaluated = [placeholderFilled resolveReferencesWithTree:self.entry.tree];
}
return evaluated;
}
@end @end

View File

@@ -8,6 +8,10 @@
#import <XCTest/XCTest.h> #import <XCTest/XCTest.h>
#import "NSString+Commands.h" #import "NSString+Commands.h"
#import "MPAutotypeCommand.h"
#import "MPAutotypeContext.h"
#import "KPKEntry.h"
@interface KPKTestAutotypeNormalization : XCTestCase @interface KPKTestAutotypeNormalization : XCTestCase
@@ -34,4 +38,14 @@
XCTAssertTrue([@"{}{}{}{}{}{ }ThisIsValid{}{STOP}" validateCommmand]); XCTAssertTrue([@"{}{}{}{}{}{ }ThisIsValid{}{STOP}" validateCommmand]);
} }
- (void)testCommandCreation {
KPKEntry *entry = [[KPKEntry alloc] init];
entry.title = @"Title";
entry.url = @"www.myurl.com";
entry.username = @"Username";
entry.password = @"Password";
MPAutotypeContext *context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"{USERNAME}{TAB}{PASSWORD}{ENTER}"];
NSArray *commands = [MPAutotypeCommand commandsForContext:context];
}
@end @end