Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Last active June 28, 2016 09:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mahemoff/c877eb1e955b1160dcdf6f4d4c0ba043 to your computer and use it in GitHub Desktop.
unicode-friendly ruby whitespace removal
class String
# performance hack - memoize these regexps
SPACE_8_RE = Regexp.new('[[:space:]]'.encode('UTF-8'))
SPACE_16_RE = Regexp.new('[[:space:]]'.encode('UTF-16LE'))
def safe_strip
if self.encoding=='UTF-8'
self.gsub SPACE_8_RE, ''
elsif self.encoding=='UTF-16'
self.gsub SPACE_16_RE, ''
else
self.strip
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment