Skip to content

Instantly share code, notes, and snippets.

@peterc
Created February 3, 2013 16:22
Show Gist options
  • Select an option

  • Save peterc/41cc5c231fe280120034 to your computer and use it in GitHub Desktop.

Select an option

Save peterc/41cc5c231fe280120034 to your computer and use it in GitHub Desktop.
# encoding: utf-8
# clean filenames for OS X, Windows and Linux
class Filename
CHARACTER_FILTER = /[\x00-\x1F\/\\:\*\?\"<>\|]/u
UNICODE_WHITESPACE = /[[:space:]]+/u
def initialize(filename)
@raw = filename.to_s.freeze
end
# strip whitespace on beginning and end
# collapse intra-string whitespace into single spaces
def normalize
@normalized ||= @raw.strip.gsub(UNICODE_WHITESPACE,' ')
end
# remove characters that aren't allowed cross-OS
def sanitize
@sanitized ||= normalize.gsub(CHARACTER_FILTER,'')
end
# normalize unicode string and cut off at 255 characters
def truncate
@truncated ||= sanitize.chars.to_a.slice(0..254).join
end
# convert back from multibyte string
def to_s
truncate
end
end
x = Filename.new(" ühere\\ x\tis a totally:unsafeöø.name!!ø ").to_s
puts "YAY!" if x == "ühere x is a totallyunsafeöø.name!!ø"
# rvm 1.8.7,1.9.2,1.9.3,2.0.0 do ruby fn.rb
# YAY!
# YAY!
# YAY!
# YAY!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment