Skip to content

Commit

Permalink
Avoid regex backtracking in Inflector.underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed Jan 17, 2023
1 parent 90e8a90 commit a7cda7e
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions activesupport/lib/active_support/inflector/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ def underscore(camel_cased_word)
return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
word = camel_cased_word.to_s.gsub("::", "/")
word.gsub!(inflections.acronyms_underscore_regex) { "#{$1 && '_' }#{$2.downcase}" }
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
word.gsub!(/([A-Z])(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { ($1 || $2) << "_" }
word.tr!("-", "_")
word.downcase!
word
Expand Down

0 comments on commit a7cda7e

Please sign in to comment.