Module: ToRegex::StringMixin

Included in:
String
Defined in:
lib/wayback_machine_downloader/to_regex.rb

Constant Summary collapse

INLINE_OPTIONS =
/[imxnesu]*/
REGEXP_DELIMITERS =
{
  '%r{' => '}',
  '/' => '/',
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.literal?(str) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/wayback_machine_downloader/to_regex.rb', line 4

def literal?(str)
  REGEXP_DELIMITERS.none? { |s, e| str.start_with?(s) and str =~ /#{e}#{INLINE_OPTIONS}\z/ }
end

Instance Method Details

#as_regexp(options = {}) ⇒ Object

Return arguments that can be passed to ‘Regexp.new`

See Also:

  • to_regexp


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wayback_machine_downloader/to_regex.rb', line 34

def as_regexp(options = {})
  unless options.is_a?(::Hash)
    raise ::ArgumentError, "[to_regexp] Options must be a Hash"
  end
  str = self

  return if options[:detect] and str == ''

  if options[:literal] or (options[:detect] and ToRegexp::String.literal?(str))
    content = ::Regexp.escape str
  elsif delim_set = REGEXP_DELIMITERS.detect { |k, _| str.start_with?(k) }
    delim_start, delim_end = delim_set
    /\A#{delim_start}(.*)#{delim_end}(#{INLINE_OPTIONS})\z/u =~ str
    content = $1
    inline_options = $2
    return unless content.is_a?(::String)
    content.gsub! '\\/', '/'
    if inline_options
      options[:ignore_case] = true if inline_options.include?('i')
      options[:multiline] = true if inline_options.include?('m')
      options[:extended] = true if inline_options.include?('x')
      # 'n', 'N' = none, 'e', 'E' = EUC, 's', 'S' = SJIS, 'u', 'U' = UTF-8
      options[:lang] = inline_options.scan(/[nesu]/i).join.downcase
    end
  else
    return
  end

  ignore_case = options[:ignore_case] ? ::Regexp::IGNORECASE : 0
  multiline = options[:multiline] ? ::Regexp::MULTILINE : 0
  extended = options[:extended] ? ::Regexp::EXTENDED : 0
  lang = options[:lang] || ''
  if ::RUBY_VERSION > '1.9' and lang.include?('u')
    lang = lang.delete 'u'
  end

  if lang.empty?
    [ content, (ignore_case|multiline|extended) ]
  else
    [ content, (ignore_case|multiline|extended), lang ]
  end
end

#to_regex(options = {}) ⇒ Object

Get a regex back

Without :literal or :detect, ‘“foo”.to_regex` will return nil.

Parameters:

  • options (optional, Hash) (defaults to: {})

Options Hash (options):

  • :literal (true, false)

    Treat meta characters and other regexp codes as just text; always return a regexp

  • :detect (true, false)

    If string starts and ends with valid regexp delimiters, treat it as a regexp; otherwise, interpret it literally

  • :ignore_case (true, false)

    /foo/i

  • :multiline (true, false)

    /foo/m

  • :extended (true, false)

    /foo/x

  • :lang (true, false)

    /foo/



26
27
28
29
30
# File 'lib/wayback_machine_downloader/to_regex.rb', line 26

def to_regex(options = {})
  if args = as_regexp(options)
    ::Regexp.new(*args)
  end
end