Skip to content

Instantly share code, notes, and snippets.

@meijeru
Created October 25, 2011 15:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save meijeru/1313122 to your computer and use it in GitHub Desktop.
Save meijeru/1313122 to your computer and use it in GitHub Desktop.
Encoding/decoding between Unicode and UTF-8: Red/System version
Red/System []
utf8-to-codepoint: func [ ; yields an integer >= 0 and < 1114112, or -1
u [c-string!] ; should have length 1 to 4
/local b1 b2 b3 b4
][
either 1 = length? u
[
b1: as-integer u/1
either b1 < 128 [
b1
][
-1
]
][
either 2 = length? u
[
b1: as-integer u/1 - 192 b2: as-integer u/2 - 128
either all [
b1 >= 0 b1 < 32
b2 >= 0 b2 < 64
][
(b1 << 6) or b2
][
-1
]
][
either 3 = length? u
[
b1: as-integer u/1 - 224 b2: as-integer u/2 - 128
b3: as-integer u/3 - 128
either all [
b1 >= 0 b1 < 16
b2 >= 0 b2 < 64
b3 >= 0 b3 < 64
][
(b1 << 12) or (b2 << 6) or b3
][
-1
]
][
either 4 = length? u
[
b1: as-integer u/1 - 240 b2: as-integer u/2 - 128
b3: as-integer u/3 - 128 b4: as-integer u/4 - 128
either all [
b1 >= 0 b1 < 8
b2 >= 0 b2 < 64
b3 >= 0 b3 < 64
b4 >= 0 b4 < 64
][
(b1 << 18) or (b2 << 12) or (b3 << 6) or b4
][
-1
]
][
-1
]
]
]
]
]
codepoint-to-utf8: func [ ; yields a c-string value of length 1 to 4, or empty
cp [integer!] ; should be >= 0 and < 1114112
/local b [c-string!]
][
either cp < 0
[
b: ""
][
either cp < 128
[
b: " "
b/1: as-byte cp
][
either cp < 2048
[
b: " "
b/1: as-byte cp >> 6 or 192
b/2: as-byte cp and 63 or 128
][
either cp < 66536
[
b: " "
b/1: as-byte cp >> 12 or 224
b/2: as-byte cp and 4095 >> 6 or 128
b/3: as-byte cp and 63 or 128
][
either cp < 1114112
[
b: " "
b/1: as-byte cp >> 18 or 240
b/2: as-byte cp and 262143 >> 12 or 128
b/3: as-byte cp and 4095 >> 6 or 128
b/4: as-byte cp and 63 or 128
][
b: ""
]
]
]
]
]
b
]
@dockimbel
Copy link

Nice work Rudolf!

I guess that I really need to find some time to implement switch and case in Red/System. ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment