From d69d832de45c3a12a0900dc8453f890cd215d893 Mon Sep 17 00:00:00 2001 From: michael starke Date: Thu, 23 Nov 2017 19:11:59 +0100 Subject: [PATCH] Inializing regualr expressions only once for autotype --- MacPass/MPAutotypeCommand.m | 23 ++++++++++++--------- MacPass/MPAutotypePickchars.m | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 MacPass/MPAutotypePickchars.m diff --git a/MacPass/MPAutotypeCommand.m b/MacPass/MPAutotypeCommand.m index 216b58e4..afdf9b8c 100644 --- a/MacPass/MPAutotypeCommand.m +++ b/MacPass/MPAutotypeCommand.m @@ -334,7 +334,20 @@ static CGKeyCode kMPNumpadKeyCodes[] = { } /* F1-16 */ - NSRegularExpression *functionKeyRegExp = [[NSRegularExpression alloc] initWithPattern:kKPKAutotypeFunctionMaskRegularExpression options:NSRegularExpressionCaseInsensitive error:0]; + static NSRegularExpression *functionKeyRegExp; + static NSRegularExpression *numpadKeyRegExp; + static NSRegularExpression *delayRegExp; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + NSString *delayPattern = [[NSString alloc] initWithFormat:@"\\{(%@|%@)[ |=]+([0-9]+)\\}", + kKPKAutotypeDelay, + kKPKAutotypeVirtualKey/*, + kKPKAutotypeVirtualExtendedKey, + kKPKAutotypeVirtualNonExtendedKey*/]; + functionKeyRegExp = [[NSRegularExpression alloc] initWithPattern:kKPKAutotypeFunctionMaskRegularExpression options:NSRegularExpressionCaseInsensitive error:0]; + numpadKeyRegExp = [[NSRegularExpression alloc] initWithPattern:kKPKAutotypeKeypaddNumberMaskRegularExpression options:NSRegularExpressionCaseInsensitive error:0]; + delayRegExp = [[NSRegularExpression alloc] initWithPattern:delayPattern options:NSRegularExpressionCaseInsensitive error:0]; + }); NSTextCheckingResult *functionResult = [functionKeyRegExp firstMatchInString:commandString options:0 range:NSMakeRange(0, commandString.length)]; if(functionResult && functionResult.numberOfRanges == 2) { NSString *numberString = [commandString substringWithRange:[functionResult rangeAtIndex:1]]; @@ -350,7 +363,6 @@ static CGKeyCode kMPNumpadKeyCodes[] = { } /* Numpad0-9 */ - NSRegularExpression *numpadKeyRegExp = [[NSRegularExpression alloc] initWithPattern:kKPKAutotypeKeypaddNumberMaskRegularExpression options:NSRegularExpressionCaseInsensitive error:0]; NSTextCheckingResult *numpadResult = [numpadKeyRegExp firstMatchInString:commandString options:0 range:NSMakeRange(0, commandString.length)]; if(numpadResult && numpadResult.numberOfRanges == 2) { NSString *numberString = [commandString substringWithRange:[numpadResult rangeAtIndex:1]]; @@ -372,13 +384,6 @@ static CGKeyCode kMPNumpadKeyCodes[] = { } // TODO: add {APPLICATION } /* Delay */ - NSString *delayPattern = [[NSString alloc] initWithFormat:@"\\{(%@|%@)[ |=]+([0-9]+)\\}", - kKPKAutotypeDelay, - kKPKAutotypeVirtualKey/*, - kKPKAutotypeVirtualExtendedKey, - kKPKAutotypeVirtualNonExtendedKey*/]; - NSRegularExpression *delayRegExp = [[NSRegularExpression alloc] initWithPattern:delayPattern options:NSRegularExpressionCaseInsensitive error:0]; - NSAssert(delayRegExp, @"Regex for delay should work!"); NSTextCheckingResult *result = [delayRegExp firstMatchInString:commandString options:0 range:NSMakeRange(0, commandString.length)]; if(result && (result.numberOfRanges == 3)) { NSString *uppercaseCommand = [[commandString substringWithRange:[result rangeAtIndex:1]] uppercaseString]; diff --git a/MacPass/MPAutotypePickchars.m b/MacPass/MPAutotypePickchars.m new file mode 100644 index 00000000..164f4bb3 --- /dev/null +++ b/MacPass/MPAutotypePickchars.m @@ -0,0 +1,38 @@ +// +// MPAutotypePickchars.m +// MacPass +// +// Created by Michael Starke on 23.11.17. +// Copyright © 2017 HicknHack Software GmbH. All rights reserved. +// + +#import "MPAutotypePickchars.h" +#import "MPPickcharViewController.h" + +@implementation MPAutotypePickchars + +- (void)execute { + dispatch_sync(dispatch_get_main_queue(), ^{ + + + MPPickcharViewController *vc = [[MPPickcharViewController alloc] init]; + vc.sourceValue = @"ThisIsANicePassword"; + vc.countToPick = 10; + + + NSPanel *panel = [[NSPanel alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100) + styleMask:NSWindowStyleMaskNonactivatingPanel|NSWindowStyleMaskTitled + backing:NSBackingStoreRetained + defer:YES]; + panel.level = NSScreenSaverWindowLevel; + panel.contentViewController = vc; + [panel center]; + [panel makeKeyAndOrderFront:self]; + + if(NSModalResponseOK == [NSApp runModalForWindow:panel]) { + // Do some stuff! + } + }); +} + +@end