Need UTF-8 bytes for your algorithm? Don't want to waste time investigating how to go back and forth between UTF-32 and UTF-8? I hear ya.
length : String -> Basics.Int
Number of UTF-8 codepoints in a string.
import String.UTF8 as UTF8
UTF8.length "a"
--> 1
UTF8.length "à"
--> 2
UTF8.length "✓"
--> 3
UTF8.length "💩"
--> 4
toBytes : String -> List Basics.Int
Convert a String
to a sequence of UTF-8 bytes. The inverse of toString
.
import String.UTF8 as UTF8
UTF8.toBytes "✓ a-ok"
--> [ 0xe2, 0x9c, 0x93, 0x20, 0x61, 0x2D, 0x6F, 0x6B ]
toString : List Basics.Int -> Result String String
Convert a sequence of UTF-8 bytes to an Elm String
.
import String.UTF8 as UTF8
UTF8.toString [ 0x68, 0x65, 0x6C, 0x6C, 0x6F ]
--> Ok "hello"
UTF8.toString [ 0xF0, 0x9F, 0x92, 0xA9 ]
--> Ok "💩"
If the input is not a valid UTF-8 sequence, you'll receive an error.
UTF8.toString [ 0xF0]
--> Err "invalid UTF-8 sequence"
foldl : (Basics.Int -> a -> a) -> a -> String -> a
Fold over a string, left to right, accumulating UTF-8 bytes along the way.