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

Add unsafeEncodeUtf, fixes #5 #7

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions System/OsString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module System.OsString

-- * OsString construction
, encodeUtf
, unsafeEncodeUtf
, encodeWith
, encodeFS
, osstr
Expand Down Expand Up @@ -130,6 +131,7 @@ import System.OsString.Internal
( unsafeFromChar
, toChar
, encodeUtf
, unsafeEncodeUtf
, encodeWith
, encodeFS
, osstr
Expand Down
20 changes: 17 additions & 3 deletions System/OsString/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module System.OsString.MODULE_NAME

-- * String construction
, encodeUtf
, unsafeEncodeUtf
, encodeWith
, encodeFS
, fromBytes
Expand Down Expand Up @@ -177,7 +178,7 @@ import GHC.IO.Encoding.UTF8 ( mkUTF8 )
import qualified System.OsString.Data.ByteString.Short as BSP
#endif
import GHC.Stack (HasCallStack)
import Prelude (Bool(..), Int, Maybe(..), IO, String, Either(..), fmap, ($), (.), mconcat, fromEnum, fromInteger, mempty, fromIntegral, fail, (<$>), show, either, pure, const, flip)
import Prelude (Bool(..), Int, Maybe(..), IO, String, Either(..), fmap, ($), (.), mconcat, fromEnum, fromInteger, mempty, fromIntegral, fail, (<$>), show, either, pure, const, flip, error, id)
import Data.Bifunctor ( bimap )
import qualified System.OsString.Data.ByteString.Short.Word16 as BS16
import qualified System.OsString.Data.ByteString.Short as BS8
Expand All @@ -189,13 +190,15 @@ import qualified System.OsString.Data.ByteString.Short as BS8
--
-- This encodes as UTF16-LE (strictly), which is a pretty good guess.
--
-- Throws an 'EncodingException' if encoding fails.
-- Throws an 'EncodingException' if encoding fails. If the input does not
-- contain surrogate chars, you can use @unsafeEncodeUtf@.
#else
-- | Partial unicode friendly encoding.
--
-- This encodes as UTF8 (strictly), which is a good guess.
--
-- Throws an 'EncodingException' if encoding fails.
-- Throws an 'EncodingException' if encoding fails. If the input does not
-- contain surrogate chars, you can use 'unsafeEncodeUtf'.
#endif
encodeUtf :: MonadThrow m => String -> m PLATFORM_STRING
#ifdef WINDOWS
Expand All @@ -204,6 +207,17 @@ encodeUtf = either throwM pure . encodeWith utf16le
encodeUtf = either throwM pure . encodeWith utf8
#endif

-- | Unsafe unicode friendly encoding.
--
-- Like 'encodeUtf', except it crashes when the input contains
-- surrogate chars. For sanitized input, this can be useful.
unsafeEncodeUtf :: HasCallStack => String -> PLATFORM_STRING
#ifdef WINDOWS
unsafeEncodeUtf = either (error . displayException) id . encodeWith utf16le
#else
unsafeEncodeUtf = either (error . displayException) id . encodeWith utf8
#endif

-- | Encode a 'String' with the specified encoding.
encodeWith :: TextEncoding
-> String
Expand Down
10 changes: 9 additions & 1 deletion System/OsString/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ import Data.Coerce (coerce)
-- On windows this encodes as UTF16-LE (strictly), which is a pretty good guess.
-- On unix this encodes as UTF8 (strictly), which is a good guess.
--
-- Throws a 'EncodingException' if encoding fails.
-- Throws an 'EncodingException' if encoding fails. If the input does not
-- contain surrogate chars, you can use 'unsafeEncodeUtf'.
encodeUtf :: MonadThrow m => String -> m OsString
encodeUtf = fmap OsString . PF.encodeUtf

-- | Unsafe unicode friendly encoding.
--
-- Like 'encodeUtf', except it crashes when the input contains
-- surrogate chars. For sanitized input, this can be useful.
unsafeEncodeUtf :: HasCallStack => String -> OsString
unsafeEncodeUtf = OsString . PF.unsafeEncodeUtf

-- | Encode an 'OsString' given the platform specific encodings.
encodeWith :: TextEncoding -- ^ unix text encoding
-> TextEncoding -- ^ windows text encoding
Expand Down