Skip to content

Commit 32bad35

Browse files
committed
Merge branch 'unsafeEncodeUtf'
2 parents f47ab7a + fe934e7 commit 32bad35

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

System/OsString.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module System.OsString
2121

2222
-- * OsString construction
2323
, encodeUtf
24+
, unsafeEncodeUtf
2425
, encodeWith
2526
, encodeFS
2627
, osstr
@@ -130,6 +131,7 @@ import System.OsString.Internal
130131
( unsafeFromChar
131132
, toChar
132133
, encodeUtf
134+
, unsafeEncodeUtf
133135
, encodeWith
134136
, encodeFS
135137
, osstr

System/OsString/Common.hs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module System.OsString.MODULE_NAME
3030

3131
-- * String construction
3232
, encodeUtf
33+
, unsafeEncodeUtf
3334
, encodeWith
3435
, encodeFS
3536
, fromBytes
@@ -177,7 +178,7 @@ import GHC.IO.Encoding.UTF8 ( mkUTF8 )
177178
import qualified System.OsString.Data.ByteString.Short as BSP
178179
#endif
179180
import GHC.Stack (HasCallStack)
180-
import Prelude (Bool(..), Int, Maybe(..), IO, String, Either(..), fmap, ($), (.), mconcat, fromEnum, fromInteger, mempty, fromIntegral, fail, (<$>), show, either, pure, const, flip)
181+
import Prelude (Bool(..), Int, Maybe(..), IO, String, Either(..), fmap, ($), (.), mconcat, fromEnum, fromInteger, mempty, fromIntegral, fail, (<$>), show, either, pure, const, flip, error, id)
181182
import Data.Bifunctor ( bimap )
182183
import qualified System.OsString.Data.ByteString.Short.Word16 as BS16
183184
import qualified System.OsString.Data.ByteString.Short as BS8
@@ -189,13 +190,15 @@ import qualified System.OsString.Data.ByteString.Short as BS8
189190
--
190191
-- This encodes as UTF16-LE (strictly), which is a pretty good guess.
191192
--
192-
-- Throws an 'EncodingException' if encoding fails.
193+
-- Throws an 'EncodingException' if encoding fails. If the input does not
194+
-- contain surrogate chars, you can use @unsafeEncodeUtf@.
193195
#else
194196
-- | Partial unicode friendly encoding.
195197
--
196198
-- This encodes as UTF8 (strictly), which is a good guess.
197199
--
198-
-- Throws an 'EncodingException' if encoding fails.
200+
-- Throws an 'EncodingException' if encoding fails. If the input does not
201+
-- contain surrogate chars, you can use 'unsafeEncodeUtf'.
199202
#endif
200203
encodeUtf :: MonadThrow m => String -> m PLATFORM_STRING
201204
#ifdef WINDOWS
@@ -204,6 +207,17 @@ encodeUtf = either throwM pure . encodeWith utf16le
204207
encodeUtf = either throwM pure . encodeWith utf8
205208
#endif
206209

210+
-- | Unsafe unicode friendly encoding.
211+
--
212+
-- Like 'encodeUtf', except it crashes when the input contains
213+
-- surrogate chars. For sanitized input, this can be useful.
214+
unsafeEncodeUtf :: HasCallStack => String -> PLATFORM_STRING
215+
#ifdef WINDOWS
216+
unsafeEncodeUtf = either (error . displayException) id . encodeWith utf16le
217+
#else
218+
unsafeEncodeUtf = either (error . displayException) id . encodeWith utf8
219+
#endif
220+
207221
-- | Encode a 'String' with the specified encoding.
208222
encodeWith :: TextEncoding
209223
-> String

System/OsString/Internal.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,18 @@ import Data.Coerce (coerce)
4242
-- On windows this encodes as UTF16-LE (strictly), which is a pretty good guess.
4343
-- On unix this encodes as UTF8 (strictly), which is a good guess.
4444
--
45-
-- Throws a 'EncodingException' if encoding fails.
45+
-- Throws an 'EncodingException' if encoding fails. If the input does not
46+
-- contain surrogate chars, you can use 'unsafeEncodeUtf'.
4647
encodeUtf :: MonadThrow m => String -> m OsString
4748
encodeUtf = fmap OsString . PF.encodeUtf
4849

50+
-- | Unsafe unicode friendly encoding.
51+
--
52+
-- Like 'encodeUtf', except it crashes when the input contains
53+
-- surrogate chars. For sanitized input, this can be useful.
54+
unsafeEncodeUtf :: HasCallStack => String -> OsString
55+
unsafeEncodeUtf = OsString . PF.unsafeEncodeUtf
56+
4957
-- | Encode an 'OsString' given the platform specific encodings.
5058
encodeWith :: TextEncoding -- ^ unix text encoding
5159
-> TextEncoding -- ^ windows text encoding

0 commit comments

Comments
 (0)