Skip to content

Commit

Permalink
Fix label validation formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
zorgiepoo committed May 6, 2024
1 parent 0ccc9cb commit f43e98c
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions Bit Slicer/ZGEditLabelWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ @interface ZGLabelTextFormatter : NSFormatter

@implementation ZGLabelTextFormatter
{
NSCharacterSet *_validCharacterSet;
NSCharacterSet *_invalidCharacterSet;
}

Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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
Expand Down

0 comments on commit f43e98c

Please sign in to comment.