Skip to content

Commit

Permalink
Add 'unsafeEncodeUtf' from os-string
Browse files Browse the repository at this point in the history
  • Loading branch information
hasufell committed Dec 13, 2023
1 parent 4c813c0 commit b7d182f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions System/OsPath/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module System.OsPath
#endif
-- * Filepath construction
, PS.encodeUtf
, PS.unsafeEncodeUtf
, PS.encodeWith
, PS.encodeFS
#if defined(WINDOWS) || defined(POSIX)
Expand Down Expand Up @@ -117,6 +118,7 @@ import System.OsString.Windows as PS
, decodeFS
, pack
, encodeUtf
, unsafeEncodeUtf
, encodeWith
, encodeFS
, unpack
Expand Down Expand Up @@ -149,6 +151,7 @@ import System.OsString.Posix as PS
, decodeFS
, pack
, encodeUtf
, unsafeEncodeUtf
, encodeWith
, encodeFS
, unpack
Expand All @@ -165,6 +168,7 @@ import System.OsPath.Internal as PS
, decodeFS
, pack
, encodeUtf
, unsafeEncodeUtf
, encodeWith
, encodeFS
, unpack
Expand Down
11 changes: 10 additions & 1 deletion System/OsPath/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import GHC.IO.Encoding.UTF16 ( mkUTF16le )
#else
import qualified System.OsPath.Posix as PF
import GHC.IO.Encoding.UTF8 ( mkUTF8 )
import GHC.Stack (HasCallStack)
#endif


Expand All @@ -42,10 +43,18 @@ import GHC.IO.Encoding.UTF8 ( mkUTF8 )
-- 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 => FilePath -> m OsPath
encodeUtf = OS.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 = OS.unsafeEncodeUtf

-- | Encode a 'FilePath' with the specified encoding.
encodeWith :: TextEncoding -- ^ unix text encoding
-> TextEncoding -- ^ windows text encoding
Expand Down
5 changes: 5 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
packages: ./

source-repository-package
type: git
location: https://github.com/haskell/os-string.git
tag: 7c95d636943f2e89eb9ca2056d620fe30fdf2dc2

0 comments on commit b7d182f

Please sign in to comment.