Pickcharparser now can handle non-default checkbox formats
This commit is contained in:
parent
2d5974982e
commit
24c8d8efc8
|
@ -14,8 +14,6 @@
|
||||||
@property (readonly) BOOL convertToDownArrows;
|
@property (readonly) BOOL convertToDownArrows;
|
||||||
@property (readonly) NSUInteger pickCount; // count to pick - 0 if unlimted
|
@property (readonly) NSUInteger pickCount; // count to pick - 0 if unlimted
|
||||||
@property (readonly) NSUInteger checkboxOffset;
|
@property (readonly) NSUInteger checkboxOffset;
|
||||||
@property (readonly, copy) NSString *checkboxFormat;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Initializes the parser with the given option string.
|
Initializes the parser with the given option string.
|
||||||
|
|
|
@ -15,7 +15,8 @@
|
||||||
|
|
||||||
typedef NS_ENUM(NSInteger, MPPickCharOffsetType) {
|
typedef NS_ENUM(NSInteger, MPPickCharOffsetType) {
|
||||||
MPPickCharOffsetTypeNone,
|
MPPickCharOffsetTypeNone,
|
||||||
MPPickCharOffsetTypeCharacter,
|
MPPickCharOffsetTypeLowerCaseCharacter,
|
||||||
|
MPPickCharOffsetTypeUpperCaseCharacter,
|
||||||
MPPickCharOffsetTypeNumber,
|
MPPickCharOffsetTypeNumber,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -25,12 +26,18 @@ struct MPPickCharOffset {
|
||||||
};
|
};
|
||||||
typedef struct MPPickCharOffset MPPickCharOffset;
|
typedef struct MPPickCharOffset MPPickCharOffset;
|
||||||
|
|
||||||
MPPickCharOffset MPMakePickCharCharacterOffset(NSUInteger offset) {
|
MPPickCharOffset MPMakePickCharUpperCaseCharacterOffset(unichar c) {
|
||||||
MPPickCharOffset offsetStruct = {MPPickCharOffsetTypeCharacter, offset};
|
MPPickCharOffset offsetStruct = {MPPickCharOffsetTypeUpperCaseCharacter, c - 'A'};
|
||||||
return offsetStruct;
|
return offsetStruct;
|
||||||
}
|
}
|
||||||
MPPickCharOffset MPMakePickCharNumberOffset(NSUInteger offset) {
|
|
||||||
MPPickCharOffset offsetStruct = {MPPickCharOffsetTypeNumber, offset};
|
MPPickCharOffset MPMakePickCharLowerCaseCharacterOffset(unichar c) {
|
||||||
|
MPPickCharOffset offsetStruct = {MPPickCharOffsetTypeLowerCaseCharacter, c - 'a'};
|
||||||
|
return offsetStruct;
|
||||||
|
}
|
||||||
|
|
||||||
|
MPPickCharOffset MPMakePickCharNumberOffset(unichar c) {
|
||||||
|
MPPickCharOffset offsetStruct = {MPPickCharOffsetTypeNumber, c - '0'};
|
||||||
return offsetStruct;
|
return offsetStruct;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,6 +46,19 @@ MPPickCharOffset MPMakeInvalidPickCharOffset(void) {
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MPPickCharOffset MPMakePickCharOffset(unichar character) {
|
||||||
|
if(character >= '0' && character <= '9') {
|
||||||
|
return MPMakePickCharNumberOffset(character);
|
||||||
|
}
|
||||||
|
else if(character >= 'a' && character <= 'z') {
|
||||||
|
return MPMakePickCharLowerCaseCharacterOffset(character);
|
||||||
|
}
|
||||||
|
else if(character >= 'A' && character <= 'Z') {
|
||||||
|
return MPMakePickCharUpperCaseCharacterOffset(character);
|
||||||
|
}
|
||||||
|
return MPMakeInvalidPickCharOffset();
|
||||||
|
}
|
||||||
|
|
||||||
BOOL MPIsValidPickCharOffset(MPPickCharOffset offset) {
|
BOOL MPIsValidPickCharOffset(MPPickCharOffset offset) {
|
||||||
return (offset.type != MPPickCharOffsetTypeNone);
|
return (offset.type != MPPickCharOffsetTypeNone);
|
||||||
}
|
}
|
||||||
|
@ -48,9 +68,22 @@ NSInteger numberOffset(MPPickCharOffset offset) {
|
||||||
}
|
}
|
||||||
|
|
||||||
NSInteger characterOffset(MPPickCharOffset offset) {
|
NSInteger characterOffset(MPPickCharOffset offset) {
|
||||||
return (offset.type == MPPickCharOffsetTypeCharacter ? offset.offset : 0);
|
switch(offset.type) {
|
||||||
|
case MPPickCharOffsetTypeUpperCaseCharacter:
|
||||||
|
case MPPickCharOffsetTypeLowerCaseCharacter:
|
||||||
|
return offset.offset;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef NSUInteger (^MPPickcharOffsetConverter)(NSInteger offset);
|
||||||
|
|
||||||
|
@interface MPPickcharsParser () {
|
||||||
|
NSDictionary <NSValue *, MPPickcharOffsetConverter> *_offsetConverter;
|
||||||
|
}
|
||||||
|
@end
|
||||||
|
|
||||||
@implementation MPPickcharsParser
|
@implementation MPPickcharsParser
|
||||||
|
|
||||||
- (instancetype)init {
|
- (instancetype)init {
|
||||||
|
@ -65,6 +98,7 @@ NSInteger characterOffset(MPPickCharOffset offset) {
|
||||||
_checkboxOffset = 0;
|
_checkboxOffset = 0;
|
||||||
_convertToDownArrows = NO;
|
_convertToDownArrows = NO;
|
||||||
_hideCharacters = YES;
|
_hideCharacters = YES;
|
||||||
|
_offsetConverter = nil;
|
||||||
[self _parseOptions:options];
|
[self _parseOptions:options];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
|
@ -75,23 +109,13 @@ NSInteger characterOffset(MPPickCharOffset offset) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
NSMutableString *mutableString = [[NSMutableString alloc] init];
|
NSMutableString *mutableString = [[NSMutableString alloc] init];
|
||||||
BOOL isFirst = NO;
|
|
||||||
for(NSString *substring in string.composedCharacters) {
|
for(NSString *substring in string.composedCharacters) {
|
||||||
if(substring.length != 1) {
|
if(substring.length != 1) {
|
||||||
NSLog(@"Pickchars: Unsupported character %@ for conversion to down arrows, skipping!", substring);
|
NSLog(@"Pickchars: Unsupported character %@ for conversion to down arrows, skipping!", substring);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
MPPickCharOffset offset = MPMakeInvalidPickCharOffset();
|
|
||||||
unichar character = [substring characterAtIndex:0];
|
unichar character = [substring characterAtIndex:0];
|
||||||
if(character >= '0' && character <= '9') {
|
MPPickCharOffset offset = MPMakePickCharOffset(character);
|
||||||
offset = MPMakePickCharNumberOffset(character - '0');
|
|
||||||
}
|
|
||||||
else if(character >= 'a' && character <= 'z') {
|
|
||||||
offset = MPMakePickCharCharacterOffset(character - 'a');
|
|
||||||
}
|
|
||||||
else if(character >= 'A' && character <= 'Z') {
|
|
||||||
offset = MPMakePickCharCharacterOffset(character - 'A');
|
|
||||||
}
|
|
||||||
[self _appendKeyCommandsForOffset:offset toString:mutableString];
|
[self _appendKeyCommandsForOffset:offset toString:mutableString];
|
||||||
}
|
}
|
||||||
return [mutableString copy];
|
return [mutableString copy];
|
||||||
|
@ -102,18 +126,13 @@ NSInteger characterOffset(MPPickCharOffset offset) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NSUInteger actualOffset = self.checkboxOffset;
|
NSUInteger actualOffset = self.checkboxOffset;
|
||||||
switch(offset.type) {
|
MPPickcharOffsetConverter convertBlock = _offsetConverter[@(offset.type)];
|
||||||
case MPPickCharOffsetTypeNumber:
|
if(convertBlock) {
|
||||||
actualOffset += offset.offset;
|
actualOffset += convertBlock(offset.offset);
|
||||||
break;
|
}
|
||||||
case MPPickCharOffsetTypeCharacter:
|
else {
|
||||||
actualOffset += offset.offset;
|
actualOffset += offset.offset;
|
||||||
break;
|
|
||||||
case MPPickCharOffsetTypeNone:
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
/* todo respect format definition */
|
|
||||||
while (actualOffset--) {
|
while (actualOffset--) {
|
||||||
[string appendString:kKPKAutotypeDown];
|
[string appendString:kKPKAutotypeDown];
|
||||||
}
|
}
|
||||||
|
@ -201,7 +220,68 @@ NSInteger characterOffset(MPPickCharOffset offset) {
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
if(NSOrderedSame == [key compare:kKPKPlaceholderPickCharsOptionConvertFormat options:NSCaseInsensitiveSearch]) {
|
if(NSOrderedSame == [key compare:kKPKPlaceholderPickCharsOptionConvertFormat options:NSCaseInsensitiveSearch]) {
|
||||||
self.checkboxFormat = option;
|
if(option.length == 0) {
|
||||||
|
/* interpret no optoins as default too*/
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
/* parse range format */
|
||||||
|
NSMutableDictionary *tmpOffsetMap = [[NSMutableDictionary alloc] init];
|
||||||
|
NSUInteger index = 0;
|
||||||
|
NSUInteger collectedOffset = 0;
|
||||||
|
while(index < option.length) {
|
||||||
|
NSString *formatOption = [option substringWithRange:NSMakeRange(index, 1)];
|
||||||
|
if([formatOption isEqualToString:@"0"]) {
|
||||||
|
if(tmpOffsetMap[@(MPPickCharOffsetTypeNumber)]) {
|
||||||
|
return NO; // double definition!
|
||||||
|
}
|
||||||
|
tmpOffsetMap[@(MPPickCharOffsetTypeNumber)] = ^NSInteger(NSUInteger offset) {
|
||||||
|
return offset + collectedOffset;
|
||||||
|
};
|
||||||
|
collectedOffset += 10;
|
||||||
|
}
|
||||||
|
else if([formatOption isEqualToString:@"1"]) {
|
||||||
|
if(tmpOffsetMap[@(MPPickCharOffsetTypeNumber)]) {
|
||||||
|
return NO; // double definition!
|
||||||
|
}
|
||||||
|
tmpOffsetMap[@(MPPickCharOffsetTypeNumber)] = ^NSInteger(NSUInteger offset) {
|
||||||
|
NSInteger tmpOffset = offset - 1;
|
||||||
|
if(tmpOffset < 0) {
|
||||||
|
tmpOffset += 10;
|
||||||
|
}
|
||||||
|
return tmpOffset + collectedOffset;
|
||||||
|
};
|
||||||
|
collectedOffset += 10;
|
||||||
|
}
|
||||||
|
else if([formatOption isEqualToString:@"a"]) {
|
||||||
|
if(tmpOffsetMap[@(MPPickCharOffsetTypeLowerCaseCharacter)]) {
|
||||||
|
return NO; // double definition!
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpOffsetMap[@(MPPickCharOffsetTypeLowerCaseCharacter)] = ^NSInteger(NSUInteger offset) {
|
||||||
|
return offset + collectedOffset;
|
||||||
|
};
|
||||||
|
collectedOffset += 26;
|
||||||
|
}
|
||||||
|
else if([formatOption isEqualToString:@"A"]) {
|
||||||
|
if(tmpOffsetMap[@(MPPickCharOffsetTypeUpperCaseCharacter)]) {
|
||||||
|
return NO; // double definition!
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpOffsetMap[@(MPPickCharOffsetTypeUpperCaseCharacter)] = ^NSInteger(NSUInteger offset) {
|
||||||
|
return offset + collectedOffset;
|
||||||
|
};
|
||||||
|
collectedOffset += 26;
|
||||||
|
}
|
||||||
|
else if([formatOption isEqualToString:@"?"]) {
|
||||||
|
/* just collect skips */
|
||||||
|
collectedOffset++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
_offsetConverter = [tmpOffsetMap copy];
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
return NO;
|
return NO;
|
||||||
|
|
|
@ -14,6 +14,5 @@
|
||||||
@property NSUInteger checkboxOffset;
|
@property NSUInteger checkboxOffset;
|
||||||
@property BOOL convertToDownArrows;
|
@property BOOL convertToDownArrows;
|
||||||
@property BOOL hideCharacters;
|
@property BOOL hideCharacters;
|
||||||
@property (copy) NSString *checkboxFormat;
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -23,7 +23,9 @@
|
||||||
XCTAssertEqual(NO, parser.hideCharacters);
|
XCTAssertEqual(NO, parser.hideCharacters);
|
||||||
XCTAssertEqual(YES, parser.convertToDownArrows);
|
XCTAssertEqual(YES, parser.convertToDownArrows);
|
||||||
XCTAssertEqual(11, parser.checkboxOffset);
|
XCTAssertEqual(11, parser.checkboxOffset);
|
||||||
XCTAssertEqualObjects(@"0?aA", parser.checkboxFormat);
|
|
||||||
|
NSString *result = [parser processPickedString:@"1B0f"];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,7 +35,6 @@
|
||||||
XCTAssertEqual(YES, parser.hideCharacters); // option invalid, default is YES
|
XCTAssertEqual(YES, parser.hideCharacters); // option invalid, default is YES
|
||||||
XCTAssertEqual(NO, parser.convertToDownArrows); // option was invalid, default is NO
|
XCTAssertEqual(NO, parser.convertToDownArrows); // option was invalid, default is NO
|
||||||
XCTAssertEqual(20, parser.checkboxOffset);
|
XCTAssertEqual(20, parser.checkboxOffset);
|
||||||
XCTAssertEqualObjects(@"0A", parser.checkboxFormat);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)testConvertToDownArrows {
|
- (void)testConvertToDownArrows {
|
||||||
|
|
Loading…
Reference in New Issue