Class: Slack::Messenger::Util::UntrustedRegexp
- Inherits:
-
Object
- Object
- Slack::Messenger::Util::UntrustedRegexp
- Defined in:
- lib/slack-messenger/util/untrusted_regexp.rb
Instance Method Summary collapse
-
#initialize(pattern, multiline: false) ⇒ UntrustedRegexp
constructor
A new instance of UntrustedRegexp.
- #match(text) ⇒ Object
-
#replace_gsub(text) ⇒ Object
There is no built-in replace with block support (like ‘gsub`).
Constructor Details
#initialize(pattern, multiline: false) ⇒ UntrustedRegexp
Returns a new instance of UntrustedRegexp.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/slack-messenger/util/untrusted_regexp.rb', line 23 def initialize(pattern, multiline: false) if multiline pattern = "(?m)#{pattern}" end @regexp = RE2::Regexp.new(pattern, log_errors: false) @scan_regexp = initialize_scan_regexp raise RegexpError, regexp.error unless regexp.ok? end |
Instance Method Details
#match(text) ⇒ Object
55 56 57 |
# File 'lib/slack-messenger/util/untrusted_regexp.rb', line 55 def match(text) scan_regexp.match(text) end |
#replace_gsub(text) ⇒ Object
There is no built-in replace with block support (like ‘gsub`). We can accomplish the same thing by parsing and rebuilding the string with the substitutions.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/slack-messenger/util/untrusted_regexp.rb', line 36 def replace_gsub(text) new_text = +'' remainder = text matched = match(remainder) until matched.nil? || matched.to_a.compact.empty? partitioned = remainder.partition(matched.to_s) new_text << partitioned.first remainder = partitioned.last new_text << yield(matched) matched = match(remainder) end new_text << remainder end |