Created
February 4, 2015 10:24
-
-
Save guillaume-martin/4b359d1cbee1b50a3669 to your computer and use it in GitHub Desktop.
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
# modified from http://stackoverflow.com/a/2728344 | |
# iterate through all characters of a string | |
# returns true if all characters are chinese and false otherwise | |
def is_chinese(str) | |
result = nil | |
unless str.nil? | |
# put utf of each character in array | |
list_of_chars = str.unpack("U*") | |
# control if each character is a chinese character | |
# breaks if find a non chinese character | |
list_of_chars.each do |char| | |
case char | |
when 0x4E00..0x9FFF # main blocks | |
result = true | |
when 0x3400..0x4DBF # extended block A | |
result = true | |
when 0x20000..0x2A6DF # extended block B | |
result = true | |
when 0x2A700..0x2B73F # extended block C | |
result = true | |
when 0x2B740..0x2B81F # extended block D | |
result = true | |
else | |
result = false | |
break | |
end | |
end | |
return result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment