Inializing regualr expressions only once for autotype

This commit is contained in:
michael starke
2017-11-23 19:11:59 +01:00
parent fa78e446c3
commit d69d832de4
2 changed files with 52 additions and 9 deletions

View File

@@ -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 <appname>}
/* 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];

View File

@@ -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