<commit_before>
module Jellyfish
  class URLMap
    def initialize mapped
      string = mapped.keys.sort_by{ |k| -k.size }.
        map{ |k| build_regexp(k) }.
        join('|')

      @mapped = mapped
      @routes = Regexp.new("\\A(?:#{string})(?:/|\\z)", 'i', 'n')
    end

    def call env
      path_info = env['PATH_INFO']
      host      = env['HTTP_HOST'].to_s
      if m   = @routes.match("#{host}/#{path_info}")
        cut_path = m.to_s[host.size + 1 .. -1].chomp('/')
        script_name = cut_path.squeeze('/')

        search =
          if m[1]
            "http://#{m.to_s.squeeze('/').chomp('/')}"
          else
            script_name
          end
      end

      if app = @mapped[search]
        app.call(env.merge('PATH_INFO' => path_info[cut_path.size..-1],
                           'SCRIPT_NAME' => env['SCRIPT_NAME'] + script_name))
      else
        [404, {}, []]
      end
    end

    private

    def build_regexp path
      if matched = path.match(%r{\Ahttps?://([^/]+)(/.*)})
        "()#{matched[1]}/#{regexp_path(matched[2])}"
      else
        "[^/]*/#{regexp_path(path)}"
      end
    end

    def regexp_path path
      Regexp.escape(path).gsub('/', '/+')
    end
  end
end
<commit_msg>Optimize when there's no host<commit_after>
module Jellyfish
  class URLMap
    def initialize mapped
      keys = mapped.keys
      @no_host = !keys.any?{ |k| k.match?(%r{\Ahttps?://}) }

      string = keys.sort_by{ |k| -k.size }.
        map{ |k| build_regexp(k) }.
        join('|')

      @mapped = mapped
      @routes = Regexp.new("\\A(?:#{string})(?:/|\\z)", 'i', 'n')
    end

    def call env
      path_info = env['PATH_INFO']

      if @no_host
        if matched = @routes.match(path_info)
          cut_path = matched.to_s.chomp('/')
          script_name = path = cut_path.squeeze('/')
        end
      else
        host = env['HTTP_HOST'].to_s
        if matched = @routes.match("#{host}/#{path_info}")
          cut_path = matched.to_s[host.size + 1..-1].chomp('/')
          script_name = cut_path.squeeze('/')

          path =
            if matched[1]
              "http://#{matched.to_s.chomp('/').squeeze('/')}"
            else
              script_name
            end
        end
      end

      if app = @mapped[path]
        app.call(env.merge('PATH_INFO' => path_info[cut_path.size..-1],
                           'SCRIPT_NAME' => env['SCRIPT_NAME'] + script_name))
      else
        [404, {}, []]
      end
    end

    private

    def build_regexp path
      if @no_host
        regexp_path(path)
      elsif matched = path.match(%r{\Ahttps?://([^/]+)(/.*)})
        "()#{matched[1]}/#{regexp_path(matched[2])}"
      else
        "[^/]*/#{regexp_path(path)}"
      end
    end

    def regexp_path path
      Regexp.escape(path).gsub('/', '/+')
    end
  end
end

{'fraction_non_alphanumeric': 0.1823321554770318, 'fraction_numerical': 0.0063604240282685515, 'mean_word_length': 1.770058708414873, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4191416', 'n_tokens_mistral': 1015, 'n_tokens_neox': 1007, 'n_words': 181}