Skip to content

Commit 570444d

Browse files
committed
Document caveats for encodeWith, fixes #15
1 parent e1dd3bc commit 570444d

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

System/OsString/Common.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,24 @@ unsafeEncodeUtf = either (error . displayException) id . encodeWith utf16le
218218
unsafeEncodeUtf = either (error . displayException) id . encodeWith utf8
219219
#endif
220220

221+
#ifdef WINDOWS
221222
-- | Encode a 'String' with the specified encoding.
222-
encodeWith :: TextEncoding
223+
--
224+
-- Note: We expect a "wide char" encoding (e.g. UCS-2 or UTF-16). Anything
225+
-- that works with @Word16@ boundaries. Picking an incompatible encoding may crash
226+
-- filepath operations.
227+
encodeWith :: TextEncoding -- ^ text encoding (wide char)
223228
-> String
224229
-> Either EncodingException PLATFORM_STRING
225230
encodeWith enc str = unsafePerformIO $ do
226-
#ifdef WINDOWS
227231
r <- try @SomeException $ GHC.withCStringLen enc str $ \cstr -> WindowsString <$> BS8.packCStringLen cstr
228232
evaluate $ force $ first (flip EncodingError Nothing . displayException) r
229233
#else
234+
-- | Encode a 'String' with the specified encoding.
235+
encodeWith :: TextEncoding
236+
-> String
237+
-> Either EncodingException PLATFORM_STRING
238+
encodeWith enc str = unsafePerformIO $ do
230239
r <- try @SomeException $ GHC.withCStringLen enc str $ \cstr -> PosixString <$> BSP.packCStringLen cstr
231240
evaluate $ force $ first (flip EncodingError Nothing . displayException) r
232241
#endif

System/OsString/Internal.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ encodeUtf = fmap OsString . PF.encodeUtf
5555
unsafeEncodeUtf :: HasCallStack => String -> OsString
5656
unsafeEncodeUtf = OsString . PF.unsafeEncodeUtf
5757

58-
-- | Encode an 'OsString' given the platform specific encodings.
58+
-- | Encode a 'FilePath' with the specified encoding.
59+
--
60+
-- Note: on windows, we expect a "wide char" encoding (e.g. UCS-2 or UTF-16). Anything
61+
-- that works with @Word16@ boundaries. Picking an incompatible encoding may crash
62+
-- filepath operations.
5963
encodeWith :: TextEncoding -- ^ unix text encoding
60-
-> TextEncoding -- ^ windows text encoding
64+
-> TextEncoding -- ^ windows text encoding (wide char)
6165
-> String
6266
-> Either EncodingException OsString
6367
#if defined(mingw32_HOST_OS) || defined(__MINGW32__)

0 commit comments

Comments
 (0)