Skip to content

Instantly share code, notes, and snippets.

@guillaume-martin
Created February 4, 2015 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guillaume-martin/4b359d1cbee1b50a3669 to your computer and use it in GitHub Desktop.
Save guillaume-martin/4b359d1cbee1b50a3669 to your computer and use it in GitHub Desktop.
# 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