From f478911d554bfef09b22f6262b2f7b4237b8f5c0 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Tue, 12 Dec 2023 15:41:12 +0800 Subject: [PATCH] Add 'unsafeEncodeUtf' from os-string --- System/OsPath/Common.hs | 4 ++++ System/OsPath/Internal.hs | 11 ++++++++++- cabal.project | 1 + filepath.cabal | 10 +++++----- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/System/OsPath/Common.hs b/System/OsPath/Common.hs index 0af0ed60..7c2de597 100644 --- a/System/OsPath/Common.hs +++ b/System/OsPath/Common.hs @@ -42,6 +42,7 @@ module System.OsPath #endif -- * Filepath construction , PS.encodeUtf + , PS.unsafeEncodeUtf , PS.encodeWith , PS.encodeFS #if defined(WINDOWS) || defined(POSIX) @@ -117,6 +118,7 @@ import System.OsString.Windows as PS , decodeFS , pack , encodeUtf + , unsafeEncodeUtf , encodeWith , encodeFS , unpack @@ -149,6 +151,7 @@ import System.OsString.Posix as PS , decodeFS , pack , encodeUtf + , unsafeEncodeUtf , encodeWith , encodeFS , unpack @@ -165,6 +168,7 @@ import System.OsPath.Internal as PS , decodeFS , pack , encodeUtf + , unsafeEncodeUtf , encodeWith , encodeFS , unpack diff --git a/System/OsPath/Internal.hs b/System/OsPath/Internal.hs index 49c1c593..3b81d686 100644 --- a/System/OsPath/Internal.hs +++ b/System/OsPath/Internal.hs @@ -34,6 +34,7 @@ import GHC.IO.Encoding.UTF16 ( mkUTF16le ) import qualified System.OsPath.Posix as PF import GHC.IO.Encoding.UTF8 ( mkUTF8 ) #endif +import GHC.Stack (HasCallStack) @@ -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 diff --git a/cabal.project b/cabal.project index 6f920794..fd88a8be 100644 --- a/cabal.project +++ b/cabal.project @@ -1 +1,2 @@ packages: ./ + diff --git a/filepath.cabal b/filepath.cabal index 616579ee..7bf73171 100644 --- a/filepath.cabal +++ b/filepath.cabal @@ -96,7 +96,7 @@ library , deepseq , exceptions , template-haskell - , os-string >=2.0.0 + , os-string >=2.0.1 ghc-options: -Wall @@ -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 @@ -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 @@ -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 @@ -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