From 7de7033b3d0c4632c9586d678d55b0d3d5257d90 Mon Sep 17 00:00:00 2001 From: Adam Renberg Tamm Date: Thu, 24 Oct 2024 13:42:30 +0200 Subject: [PATCH] Memoize regexp inside PhoneAnalyzer#country_code_candidates_for --- lib/phonelib/phone_analyzer.rb | 2 +- lib/phonelib/phone_analyzer_helper.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/phonelib/phone_analyzer.rb b/lib/phonelib/phone_analyzer.rb index ded95ff..ff14624 100644 --- a/lib/phonelib/phone_analyzer.rb +++ b/lib/phonelib/phone_analyzer.rb @@ -139,7 +139,7 @@ def detect_and_parse(phone, country) end def country_code_candidates_for(phone) - stripped_phone = phone.gsub(/^(#{Phonelib.phone_data_int_prefixes})/, '') + stripped_phone = phone.gsub(cr("Phonelib.phone_data_int_prefixes") { /^(#{Phonelib.phone_data_int_prefixes})/ }, '') ((1..3).map { |length| phone[0, length] } + (1..3).map { |length| stripped_phone[0, length] }).uniq end diff --git a/lib/phonelib/phone_analyzer_helper.rb b/lib/phonelib/phone_analyzer_helper.rb index 2affe29..0eceb60 100644 --- a/lib/phonelib/phone_analyzer_helper.rb +++ b/lib/phonelib/phone_analyzer_helper.rb @@ -52,7 +52,7 @@ def country_prefix(country) # caches regular expression, reusing it for later lookups def cr(regexp) - Phonelib.phone_regexp_cache[regexp] ||= Regexp.new(regexp).freeze + Phonelib.phone_regexp_cache[regexp] ||= Regexp.new(block_given? ? yield(regexp) : regexp).freeze end # defines whether country can have double country prefix in number