Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoize regexp inside PhoneAnalyzer#country_code_candidates_for #317

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/phonelib/phone_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/phonelib/phone_analyzer_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated so that cr can take a block that returns the string to use for the regexp. That way we don't have to compute create new objects just to create the key for the regexp cache.

end

# defines whether country can have double country prefix in number
Expand Down