diff --git a/benchmark/sanitize-url.rb b/benchmark/sanitize-url.rb new file mode 100755 index 00000000..14ad2786 --- /dev/null +++ b/benchmark/sanitize-url.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby + +require "benchmark/ips" + +PATH = "/../../..../...//.....//lorem/ipsum//dolor///sit.xyz" + +def sanitize_with_regex + "/" + PATH.gsub(%r!/{2,}!, "/").gsub(%r!\.+/|\A/+!, "") +end + +def sanitize_with_builtin + "/#{PATH}".gsub("..", "/").gsub("./", "").squeeze("/") +end + +if sanitize_with_regex == sanitize_with_builtin + Benchmark.ips do |x| + x.report("sanitize w/ regexes") { sanitize_with_regex } + x.report("sanitize w/ builtin") { sanitize_with_builtin } + x.compare! + end +else + puts "w/ regexes: #{sanitize_with_regex}" + puts "w/ builtin: #{sanitize_with_builtin}" + puts "" + puts "Thank you. Do try again :(" +end diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index 4697a146..eb0e17ee 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -417,7 +417,7 @@ module Jekyll def merge_categories!(other) if other.key?("categories") && !other["categories"].nil? if other["categories"].is_a?(String) - other["categories"] = other["categories"].split(%r!\s+!).map(&:strip) + other["categories"] = other["categories"].split end other["categories"] = (data["categories"] || []) | other["categories"] end diff --git a/lib/jekyll/static_file.rb b/lib/jekyll/static_file.rb index cdc78565..ea89b6d2 100644 --- a/lib/jekyll/static_file.rb +++ b/lib/jekyll/static_file.rb @@ -137,7 +137,7 @@ module Jekyll :template => @collection.url_template, :placeholders => placeholders, }) - end.to_s.gsub(%r!/$!, "") + end.to_s.chomp("/") end # Returns the type of the collection if present, nil otherwise. diff --git a/lib/jekyll/theme_builder.rb b/lib/jekyll/theme_builder.rb index 9b43e0a3..36ef7161 100644 --- a/lib/jekyll/theme_builder.rb +++ b/lib/jekyll/theme_builder.rb @@ -8,7 +8,7 @@ class Jekyll::ThemeBuilder attr_reader :name, :path, :code_of_conduct def initialize(theme_name, opts) - @name = theme_name.to_s.tr(" ", "_").gsub(%r!_+!, "_") + @name = theme_name.to_s.tr(" ", "_").squeeze("_") @path = Pathname.new(File.expand_path(name, Dir.pwd)) @code_of_conduct = !!opts["code_of_conduct"] end diff --git a/lib/jekyll/url.rb b/lib/jekyll/url.rb index f9157850..12f4ea45 100644 --- a/lib/jekyll/url.rb +++ b/lib/jekyll/url.rb @@ -72,9 +72,9 @@ module Jekyll break result if result.index(":").nil? if token.last.nil? # Remove leading "/" to avoid generating urls with `//` - result.gsub(%r!/:#{token.first}!, "") + result.gsub("/:#{token.first}", "") else - result.gsub(%r!:#{token.first}!, self.class.escape_path(token.last)) + result.gsub(":#{token.first}", self.class.escape_path(token.last)) end end end @@ -109,14 +109,14 @@ module Jekyll replacement = self.class.escape_path(value) match.sub(":#{winner}", replacement) - end.gsub(%r!//!, "/".freeze) + end.squeeze("/") end # Returns a sanitized String URL, stripping "../../" and multiples of "/", # as well as the beginning "/" so we can enforce and ensure it. def sanitize_url(str) - "/" + str.gsub(%r!/{2,}!, "/").gsub(%r!\.+/|\A/+!, "") + "/#{str}".gsub("..", "/").gsub("./", "").squeeze("/") end # Escapes a path to be a valid URL path segment