miniBill/elm-utf-tools/1.0.0

String.UTF8

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 -> 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
toList : String -> List Int

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

import String.UTF8 as UTF8


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

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

import String.UTF8 as UTF8


UTF8.fromList [ 0x68, 0x65, 0x6C, 0x6C, 0x6F ]
--> Ok "hello"


UTF8.fromList [ 0xF0, 0x9F, 0x92, 0xA9 ]
--> Ok "💩"

If the input is not a valid UTF-8 sequence, you'll receive an error.

UTF8.fromList [ 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.