Skip to content

Instantly share code, notes, and snippets.

@jptix
Created August 23, 2008 12:31
Show Gist options
  • Save jptix/6912 to your computer and use it in GitHub Desktop.
Save jptix/6912 to your computer and use it in GitHub Desktop.
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