module FakeFS::Globber

def regexp(pattern, find_flags = 0, gave_char_class = false)

def regexp(pattern, find_flags = 0, gave_char_class = false)
  pattern = pattern.to_s
  # Escape .+?*()$ characters unless already escaped.
  regex_body =
    pattern
    .gsub(/(?<!\\)\./, '\.')
    .gsub(/(?<!\\)\+/) { '\+' }
    .gsub(/(?<!\\)\?/, '.')
    .gsub(/(?<!\\)\*/, '.*')
    .gsub(/(?<!\\)\(/, '\(')
    .gsub(/(?<!\\)\)/, '\)')
    .gsub(/(?<!\\)\$/, '\$')
  # Unless we're expecting character class contructs in regexes, escape all brackets
  # since if we're expecting them, the string should already be properly escaped.
  unless gave_char_class
    regex_body = regex_body.gsub(/(?<!\\)([\[\]])/, '\\\\\1')
  end
  # This matches nested braces and attempts to do something correct most of the time
  # There are known issues (i.e. {,*,*/*}) that cannot be resolved with out a total
  # refactoring
  loop do
    break unless regex_body.gsub!(/(?<re>\{(?:(?>[^{}]+)|\g<re>)*\})/) do
      "(#{Regexp.last_match[1][1..-2].gsub(',', '|')})"
    end
  end
  # if we are matching dot files/directories, add that to the regex
  if find_flags == File::FNM_DOTMATCH
    regex_body = "(\.)?" + regex_body
  end
  regex_body = regex_body.gsub(/\A\./, '(?!\.).')
  /\A#{regex_body}\Z/
end