Last active
June 28, 2016 09:04
Star
You must be signed in to star a gist
unicode-friendly ruby whitespace removal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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