Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UTF-8 Conversion Functions #2

Open
RobertFischer opened this issue Apr 6, 2018 · 3 comments
Open

UTF-8 Conversion Functions #2

RobertFischer opened this issue Apr 6, 2018 · 3 comments

Comments

@RobertFischer
Copy link

It'd be nice to have explicit functions for converting ByteString to and from UTF8, since that's the common use case.

This is the code that I find very useful in my local library:

asUTF8 :: (ToText a, FromText (UTF8 b)) => a -> b
asUTF8 = unUTF8 . fromText . toText
{-# INLINE asUTF8 #-}
{-# SPECIALIZE INLINE asUTF8 :: String-> LazyByteString          #-}
{-# SPECIALIZE INLINE asUTF8 :: StrictText -> LazyByteString     #-}
{-# SPECIALIZE INLINE asUTF8 :: LazyText -> LazyByteString       #-}
{-# SPECIALIZE INLINE asUTF8 :: String-> StrictByteString        #-}
{-# SPECIALIZE INLINE asUTF8 :: StrictText -> StrictByteString   #-}
{-# SPECIALIZE INLINE asUTF8 :: LazyText -> StrictByteString     #-}
{-# SPECIALIZE INLINE asUTF8 :: String-> ShortByteString         #-}
{-# SPECIALIZE INLINE asUTF8 :: StrictText -> ShortByteString    #-}
{-# SPECIALIZE INLINE asUTF8 :: LazyText -> ShortByteString      #-}

toUTF8 :: (DecodeText f (UTF8 a), FromText b) => a -> f b
toUTF8 = decodeConvertText . UTF8
{-# INLINE toUTF8 #-}
{-# SPECIALIZE INLINE toUTF8 :: LazyByteString -> Maybe String       #-}
{-# SPECIALIZE INLINE toUTF8 :: LazyByteString -> Maybe StrictText   #-}
{-# SPECIALIZE INLINE toUTF8 :: LazyByteString -> Maybe LazyText     #-}
{-# SPECIALIZE INLINE toUTF8 :: StrictByteString -> Maybe String     #-}
{-# SPECIALIZE INLINE toUTF8 :: StrictByteString -> Maybe StrictText #-}
{-# SPECIALIZE INLINE toUTF8 :: StrictByteString -> Maybe LazyText   #-}
{-# SPECIALIZE INLINE toUTF8 :: ShortByteString -> Maybe String      #-}
{-# SPECIALIZE INLINE toUTF8 :: ShortByteString -> Maybe StrictText  #-}
{-# SPECIALIZE INLINE toUTF8 :: ShortByteString -> Maybe LazyText    #-}

If this is something you're interested in, let me know, and I'm happy to submit a pull request.

@jxv
Copy link
Contributor

jxv commented Apr 6, 2018

This is useful. Yes, send a PR.

@lexi-lambda
Copy link
Collaborator

The names seem wrong to me. toUTF8 seems like it ought to be fromUTF8, since it is converting from the UTF-8 encoding to a textual representation. Similarly, I think asUTF8 ought to be toUTF8.

That aside, I think these functions seem useful. I would be willing to merge them if you submitted a PR.

@RobertFischer
Copy link
Author

Happy to change the names. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants