Ensure that input is bona fide utf-8.
This commit is contained in:
parent
8300133c8d
commit
d74116dc67
3 changed files with 38 additions and 9 deletions
18
lib/string_utils.rb
Normal file
18
lib/string_utils.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
# Some useful additions to the String class
|
||||
|
||||
class String
|
||||
|
||||
def is_utf8?
|
||||
self =~ /^(
|
||||
[\x09\x0A\x0D\x20-\x7E] # ASCII
|
||||
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
|
||||
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
|
||||
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
|
||||
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
|
||||
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
|
||||
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
|
||||
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
|
||||
)*$/x;
|
||||
end
|
||||
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue