Created
August 23, 2008 12:31
-
-
Save jptix/6912 to your computer and use it in GitHub Desktop.
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
UTF8_REGEXP = / \A (?: [\x00-\x7F] | [\xC2-\xDF] [\x80-\xBF] | [\xE1-\xEF] [\x80-\xBF]{2} | | |
[\xF1-\xF7] [\x80-\xBF]{3} | [\xF9-\xFB] [\x80-\xBF]{4} | | |
[\xFD-\xFD] [\x80-\xBF]{5} ) \Z /x | |
# Return true if string is valid UTF-8 | |
def utf8?(string) | |
string.split(//u).all? { |c| c =~ UTF8_REGEXP} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment