Skip to content

Commit

Permalink
Makes RegexManager thread-safe (#529)
Browse files Browse the repository at this point in the history
* Fix #528

* Apply suggestions from code review

Co-authored-by: Michael Williams <[email protected]>

Co-authored-by: Michael Williams <[email protected]>
  • Loading branch information
bguidolim and michaeldwilliams authored May 20, 2022
1 parent 5a1ff64 commit 7161b7e
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions PhoneNumberKit/RegexManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@
import Foundation

final class RegexManager {

public init() {
var characterSet = CharacterSet(charactersIn: PhoneNumberConstants.nonBreakingSpace)
characterSet.formUnion(.whitespacesAndNewlines)
self.spaceCharacterSet = characterSet
spaceCharacterSet = characterSet
}

// MARK: Regular expression pool

var regularExpresionPool = [String: NSRegularExpression]()
var regularExpressionPool = [String: NSRegularExpression]()

private let regularExpressionPoolQueue = DispatchQueue(label: "com.phonenumberkit.regexpool", attributes: .concurrent)
private let regularExpressionPoolQueue = DispatchQueue(label: "com.phonenumberkit.regexpool", target: .global())

var spaceCharacterSet: CharacterSet

// MARK: Regular expression

func regexWithPattern(_ pattern: String) throws -> NSRegularExpression {
var cached: NSRegularExpression?

self.regularExpressionPoolQueue.sync {
cached = self.regularExpresionPool[pattern]
cached = regularExpressionPoolQueue.sync {
regularExpressionPool[pattern]
}

if let cached = cached {
Expand All @@ -40,8 +38,8 @@ final class RegexManager {
do {
let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)

regularExpressionPoolQueue.async(flags: .barrier) {
self.regularExpresionPool[pattern] = regex
regularExpressionPoolQueue.sync {
regularExpressionPool[pattern] = regex
}

return regex
Expand Down Expand Up @@ -113,7 +111,7 @@ final class RegexManager {
return false
}
pattern = "^(\(pattern))$"
return self.matchesExist(pattern, string: string)
return matchesExist(pattern, string: string)
}

func matchedStringByRegex(_ pattern: String, string: String) throws -> [String] {
Expand Down Expand Up @@ -166,7 +164,7 @@ final class RegexManager {

func stringByReplacingOccurrences(_ string: String, map: [String: String], keepUnmapped: Bool = false) -> String {
var targetString = String()
for i in 0..<string.count {
for i in 0 ..< string.count {
let oneChar = string[string.index(string.startIndex, offsetBy: i)]
let keyString = String(oneChar).uppercased()
if let mappedValue = map[keyString] {
Expand All @@ -182,7 +180,7 @@ final class RegexManager {

func hasValue(_ value: String?) -> Bool {
if let valueString = value {
if valueString.trimmingCharacters(in: self.spaceCharacterSet).count == 0 {
if valueString.trimmingCharacters(in: spaceCharacterSet).count == 0 {
return false
}
return true
Expand All @@ -192,7 +190,7 @@ final class RegexManager {
}

func testStringLengthAgainstPattern(_ pattern: String, string: String) -> Bool {
if self.matchesEntirely(pattern, string: string) {
if matchesEntirely(pattern, string: string) {
return true
} else {
return false
Expand Down

0 comments on commit 7161b7e

Please sign in to comment.