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 15, 2023
1 parent 4c813c0 commit a2c6b3d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
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
1 change: 1 addition & 0 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
packages: ./

10 changes: 5 additions & 5 deletions filepath.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ library
, deepseq
, exceptions
, template-haskell
, os-string >=2.0.0
, os-string >=2.0.1

ghc-options: -Wall

Expand All @@ -116,7 +116,7 @@ test-suite filepath-tests
, base
, bytestring >=0.11.3.0
, filepath
, os-string >=2.0.0
, os-string >=2.0.1
, QuickCheck >=2.7 && <2.15

default-language: Haskell2010
Expand All @@ -138,7 +138,7 @@ test-suite filepath-equivalent-tests
, base
, bytestring >=0.11.3.0
, filepath
, os-string >=2.0.0
, os-string >=2.0.1
, QuickCheck >=2.7 && <2.15

test-suite abstract-filepath
Expand All @@ -157,7 +157,7 @@ test-suite abstract-filepath
, bytestring >=0.11.3.0
, deepseq
, filepath
, os-string >=2.0.0
, os-string >=2.0.1
, QuickCheck >=2.7 && <2.15
, quickcheck-classes-base ^>=0.6.2

Expand All @@ -172,7 +172,7 @@ benchmark bench-filepath
, bytestring >=0.11.3.0
, deepseq
, filepath
, os-string >=2.0.0
, os-string >=2.0.1
, tasty-bench

ghc-options: -with-rtsopts=-A32m

0 comments on commit a2c6b3d

Please sign in to comment.