From 08bbfccb167815d048afda9d90ebea81c424e738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20C=C5=93ur?= Date: Fri, 8 Mar 2019 02:11:08 +0800 Subject: [PATCH] This regex can't produce any error/exception because we've hardcoded its pattern --- .../Classes/UILabel+FlickerNumber.m | 19 +++++++------------ .../UILabel+FlickerNumber.swift | 14 +++++--------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/Objective-C/FlickerNumber/Classes/UILabel+FlickerNumber.m b/Objective-C/FlickerNumber/Classes/UILabel+FlickerNumber.m index ee68d56..3ee0981 100644 --- a/Objective-C/FlickerNumber/Classes/UILabel+FlickerNumber.m +++ b/Objective-C/FlickerNumber/Classes/UILabel+FlickerNumber.m @@ -399,21 +399,16 @@ - (NSNumberFormatter *)defaultFormatter { * @return The string-format String. */ - (NSString *)regexNumberFormat:(NSString *)formatString { - NSError *regexError = nil; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^%((\\d+.\\d+)|(\\d+).|(.\\d+))f$" options:NSRegularExpressionCaseInsensitive - error:®exError]; - if (!regexError) { - NSTextCheckingResult *match = [regex firstMatchInString:formatString - options:0 - range:NSMakeRange(0, [formatString length])]; - if (match) { - NSString *result = [formatString substringWithRange:match.range]; - return result; - } - } else { - NSLog(@"error - %@", regexError); + error:nil]; + NSTextCheckingResult *match = [regex firstMatchInString:formatString + options:0 + range:NSMakeRange(0, [formatString length])]; + if (match) { + NSString *result = [formatString substringWithRange:match.range]; + return result; } return @"%f"; } diff --git a/Swift/FlickerNumber-Swift/UILabel+FlickerNumber.swift b/Swift/FlickerNumber-Swift/UILabel+FlickerNumber.swift index 795c9e5..0b4c1ec 100644 --- a/Swift/FlickerNumber-Swift/UILabel+FlickerNumber.swift +++ b/Swift/FlickerNumber-Swift/UILabel+FlickerNumber.swift @@ -438,15 +438,11 @@ extension UILabel { */ fileprivate func regexNumberFormat(_ format: String) -> String { let patternStr = "^%((\\d+.\\d+)|(\\d+).|(.\\d+))f$" - do { - let regex : NSRegularExpression = try NSRegularExpression(pattern: patternStr, options: .caseInsensitive) - let fRange = NSRange(location: 0, length: format.count) - if let match = regex.firstMatch(in: format, options: .reportProgress, range: fRange) { - let result = (format as NSString).substring(with: match.range) - return result - } - } catch { - print("Exception") + let regex = try! NSRegularExpression(pattern: patternStr, options: .caseInsensitive) + let fRange = NSRange(location: 0, length: format.count) + if let match = regex.firstMatch(in: format, options: .reportProgress, range: fRange) { + let result = (format as NSString).substring(with: match.range) + return result } return "%f" }