Replace Instances of `NSRegularExpression` with static instances (creating an instance of NSRegularExpression requires a lot of resources)

pull/3830/head
Krypto Pank 3 years ago
parent 8b77287411
commit d1a2664055
  1. 5
      AlphaWallet/Browser/Types/BrowserURLParser.swift
  2. 5
      AlphaWallet/TokenScriptClient/Models/GeneralisedTime.swift
  3. 4
      AlphaWallet/TokenScriptClient/Models/TokenScriptFilterParser.swift
  4. 6
      AlphaWallet/TokenScriptClient/Models/XMLHandler.swift
  5. 3
      AlphaWallet/UI/Form/PrivateKeyValidator.swift

@ -3,7 +3,7 @@
import Foundation
final class BrowserURLParser {
private let urlRegEx = try! NSRegularExpression(pattern: "^(http(s)?://)?[a-z0-9-_]+(\\.[a-z0-9-_]+)+(/)?", options: .caseInsensitive)
private static let urlRegEx = try? NSRegularExpression(pattern: "^(http(s)?://)?[a-z0-9-_]+(\\.[a-z0-9-_]+)+(/)?", options: .caseInsensitive)
private let validSchemes = ["http", "https"]
let engine: SearchEngine
@ -15,8 +15,9 @@ final class BrowserURLParser {
/// Determines if a string is an address or a search query and returns the appropriate URL.
func url(from string: String) -> URL? {
guard let regex = BrowserURLParser.urlRegEx else { return nil }
let range = NSRange(string.startIndex ..< string.endIndex, in: string)
if urlRegEx.firstMatch(in: string, options: .anchored, range: range) != nil {
if regex.firstMatch(in: string, options: .anchored, range: range) != nil {
if !validSchemes.contains(where: { string.hasPrefix("\($0)://") }) {
return URL(string: "http://" + string)
} else {

@ -47,11 +47,10 @@ struct GeneralisedTime: Codable {
self.date = date
self.timeZone = timeZone
}
private static let regex = try? NSRegularExpression(pattern: "([+-])(\\d\\d)(\\d\\d)$", options: [])
/// Given "20180619210000+0300", extract "+0300" and convert to seconds
private static func extractTimeZoneSecondsFromGMT(string: String) -> Int? {
let pattern = "([+-])(\\d\\d)(\\d\\d)$"
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { return nil }
guard let regex = GeneralisedTime.regex else { return nil }
let matches = regex.matches(in: string, options: [], range: NSRange(location: 0, length: string.count))
guard matches.count == 1 else { return nil }
guard matches[0].numberOfRanges == 4 else { return nil }

@ -452,12 +452,12 @@ struct TokenScriptFilterParser {
guard let interpolatedValue = interpolate(value: value) else { return nil }
return op.isTrueFor(attributeValue: attributeValue, value: interpolatedValue)
}
private static let regex = try? NSRegularExpression(pattern: "\\$\\{(?<attribute>[a-zA-Z][a-zA-Z0-9]*)\\}", options: [])
//TODO replace the very dumb regex for now. And also recursively interpolates (not good). Should involve parser
private func interpolate(value: String) -> String? {
var value = value
repeat {
guard let regex = try? NSRegularExpression(pattern: "\\$\\{(?<attribute>[a-zA-Z][a-zA-Z0-9]*)\\}", options: []) else { return nil }
guard let regex = Parser.regex else { return nil }
let range = NSRange(value.startIndex ..< value.endIndex, in: value)
let matches = regex.matches(in: value, options: [], range: range)
guard matches.count >= 1 else { return value }

@ -655,11 +655,11 @@ private class PrivateXMLHandler {
]
return .init(namespacePrefix: rootNamespacePrefix, namespaces: namespaces, lang: lang)
}
private static let regex = try? NSRegularExpression(pattern: "<\\!ENTITY\\s+(.*)\\s+SYSTEM\\s+\"(.*)\">", options: [])
fileprivate static func getEntities(inXml xml: String) -> [TokenScriptFileIndices.Entity] {
var entities = [TokenScriptFileIndices.Entity]()
let pattern = "<\\!ENTITY\\s+(.*)\\s+SYSTEM\\s+\"(.*)\">"
if let regex = try? NSRegularExpression(pattern: pattern, options: []) {
if let regex = Self.regex {
regex.enumerateMatches(in: xml, options: [], range: .init(xml.startIndex..<xml.endIndex, in: xml)) { match, _, _ in
guard let match = match else { return }
guard match.numberOfRanges == 3 else { return }

@ -8,6 +8,7 @@ struct ValidationError: Error {
struct PrivateKeyValidator {
private let validationError: ValidationError
private static let regex = try? NSRegularExpression(pattern: "^[0-9a-fA-F]{64}$")
init(msg: String = R.string.localizable.importWalletImportInvalidPrivateKey()) {
validationError = ValidationError(msg: msg)
@ -16,7 +17,7 @@ struct PrivateKeyValidator {
func isValid(value: String) -> ValidationError? {
//allows for private key import to have 0x or not
let drop0xKey = value.drop0x
let regex = try! NSRegularExpression(pattern: "^[0-9a-fA-F]{64}$")
guard let regex = PrivateKeyValidator.regex else { return nil }
let range = NSRange(location: 0, length: drop0xKey.utf16.count)
let result = regex.matches(in: drop0xKey, range: range)
let matched = !result.isEmpty

Loading…
Cancel
Save