zwilias/elm-utf-tools/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

String.UTF8

Need UTF-8 bytes for your algorithm? Don't want to waste time investigating how to go back and forth between UTF-16 and UTF-8? I hear ya.

Examples in the documentation assume an import like this:

import String.UTF8 as UTF8
length : String -> Int

Number of UTF-8 codepoints in a string.

UTF8.length "a"
--> 1


UTF8.length "à"
--> 2


UTF8.length "✓"
--> 3


UTF8.length "💩"
--> 4
toBytes : String -> List Int

Convert a String to a sequence of UTF-8 bytes. The inverse of toString.

UTF8.toBytes "✓ a-ok"
--> [ 0xe2, 0x9c, 0x93, 0x20, 0x61, 0x2D, 0x6F, 0x6B ]
toString : List Int -> Result String String

Convert a sequence of UTF-8 bytes to an Elm UTF-16 String.

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 : (Int -> a -> a) -> a -> String -> a

Fold over a string, left to right, accumulating UTF-8 bytes along the way.