From 7c3656ba112c728287ffcf82bc9d571453ad47dd Mon Sep 17 00:00:00 2001 From: NachoSoto Date: Wed, 2 Aug 2023 11:30:59 -0700 Subject: [PATCH] `Paywalls`: avoid recomputing variable `Regex` (#2944) See #2811. It's unclear from the docs whether this is a performance optimization, but at least it's a nice refactor. --- RevenueCatUI/Data/Variables.swift | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/RevenueCatUI/Data/Variables.swift b/RevenueCatUI/Data/Variables.swift index 5c03043566..364312894f 100644 --- a/RevenueCatUI/Data/Variables.swift +++ b/RevenueCatUI/Data/Variables.swift @@ -48,17 +48,7 @@ enum VariableHandler { } private static func extractVariables(from expression: String) -> [VariableMatch] { - let variablePattern = Regex { - OneOrMore { - "{{ " - Capture { - OneOrMore(.word) - } - " }}" - } - } - - return expression.matches(of: variablePattern).map { match in + return expression.matches(of: Self.regex).map { match in let (_, variable) = match.output return VariableMatch(variable: String(variable), range: match.range) } @@ -71,6 +61,16 @@ enum VariableHandler { } + private static let regex = Regex { + OneOrMore { + "{{ " + Capture { + OneOrMore(.word) + } + " }}" + } + } + } @available(iOS 16.0, macOS 13.0, tvOS 16.0, *)