zwilias/elm-utf-tools/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

String.UTF32

Need to work in unicode codepoints? Tired of UTF-8's silliness? You came to the right place!

Examples in the documentation assume an import like this:

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

Calculates the number UTF-32 characters in a String.

String.length "💩"
--> 2


UTF32.length "💩"
--> 1
toBytes : String -> List Int

Converts a String to a list of unicode codepoints. The inverse of toString

UTF32.toBytes "hello"
--> [ 0x68, 0x65, 0x6C, 0x6C, 0x6F ]


UTF32.toBytes "💩"
--> [ 0x0001F4A9 ]
toString : List Int -> String

Build an UTF-16 String from a list of unicode codepoints.

UTF32.toString [ 0x68, 0x65, 0x6C, 0x6C, 0x6F ]
--> "hello"


UTF32.toString [ 0x0001F4A9 ]
--> "💩"
byteToString : Int -> String

Convert a single codepoint to the equivalent Elm string.

byteToString 0x0001F4A9
--> "💩"
foldl : (Int -> a -> a) -> a -> String -> a

Fold over a string, left to right, accumulating unicode codepoints.

foldlUTF8 : (Int -> a -> a) -> a -> List Int -> Result String a

Fold over a list of UTF-8 bytes, converting them to unicode codepoints and feeding those to your accumulator. If the input contains invalid sequences, you'll receive an invalid UTF-8 sequence error.