Skip to content

Commit

Permalink
Add unsafeEncodeUtf, fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
hasufell committed Dec 12, 2023
1 parent f9b1285 commit 7c95d63
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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

0 comments on commit 7c95d63

Please sign in to comment.