File: sanitize-url.rb

package info (click to toggle)
jekyll 4.3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,356 kB
  • sloc: ruby: 16,765; javascript: 1,455; sh: 214; xml: 29; makefile: 9
file content (26 lines) | stat: -rwxr-xr-x 643 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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