Skip to content

Instantly share code, notes, and snippets.

@greggirwin
Created July 7, 2016 23:38
Show Gist options
  • Save greggirwin/cf03798e0a09f89907f943379e0bb704 to your computer and use it in GitHub Desktop.
Save greggirwin/cf03798e0a09f89907f943379e0bb704 to your computer and use it in GitHub Desktop.
Red invalid-utf8? function
invalid-utf?: function [
; https://robots.thoughtbot.com/fight-back-utf-8-invalid-byte-sequences
"Checks UTF encoding; if correct, returns none else position of error."
binary [binary!]
][
bad: [
#{C0} | #{C1} | #{F5} | #{F6} | #{F7} | #{F8} | #{F9} | #{FA}
| #{FB} | #{FC} | #{FD} | #{FE} | #{FF}
]
if parse binary [any [[mark: bad (return mark)] | skip]] [none]
]
invalid-utf? #{00000000}
invalid-utf? #{C100FF00}
invalid-utf? #{000000FF}
invalid-utf? #{00FF00F8}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment