From f43e98c39303efc378280d9891afef4165ff0be2 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 5 May 2024 18:40:33 -0700 Subject: [PATCH] Fix label validation formatter --- Bit Slicer/ZGEditLabelWindowController.m | 28 +++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Bit Slicer/ZGEditLabelWindowController.m b/Bit Slicer/ZGEditLabelWindowController.m index d24b8638..196f9cb3 100644 --- a/Bit Slicer/ZGEditLabelWindowController.m +++ b/Bit Slicer/ZGEditLabelWindowController.m @@ -45,6 +45,7 @@ @interface ZGLabelTextFormatter : NSFormatter @implementation ZGLabelTextFormatter { + NSCharacterSet *_validCharacterSet; NSCharacterSet *_invalidCharacterSet; } @@ -53,9 +54,9 @@ - (instancetype)init self = [super init]; if (self != nil) { - NSCharacterSet *validCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_$"]; + _validCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_$"]; - _invalidCharacterSet = [validCharacterSet invertedSet]; + _invalidCharacterSet = [_validCharacterSet invertedSet]; } return self; } @@ -72,6 +73,22 @@ - (NSString *)stringForObjectValue:(id)anObject - (BOOL)getObjectValue:(out id _Nullable __autoreleasing *)obj forString:(NSString *)string errorDescription:(out NSString * _Nullable __autoreleasing *)error { + if (obj != NULL) + { + NSUInteger stringLength = string.length; + NSMutableString *newString = [[NSMutableString alloc] initWithCapacity:stringLength]; + + for (NSUInteger characterIndex = 0; characterIndex < stringLength; characterIndex++) + { + unichar character = [string characterAtIndex:characterIndex]; + if ([_validCharacterSet characterIsMember:character]) + { + [newString appendFormat:@"%C", character]; + } + } + + *obj = [newString copy]; + } return YES; } @@ -87,7 +104,12 @@ - (BOOL)isPartialStringValid:(NSString * __autoreleasing *)partialStringPtr } NSRange invalidRange = [*partialStringPtr rangeOfCharacterFromSet:_invalidCharacterSet]; - return (invalidRange.location == NSNotFound); + if (invalidRange.location != NSNotFound) + { + return NO; + } + + return YES; } @end