Last active
June 28, 2016 09:04
-
-
Save mahemoff/c877eb1e955b1160dcdf6f4d4c0ba043 to your computer and use it in GitHub Desktop.
unicode-friendly ruby whitespace removal
This file contains hidden or 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