Skip to content

Commit f478911

Browse files
committed
Add 'unsafeEncodeUtf' from os-string
1 parent a26087a commit f478911

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

System/OsPath/Common.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module System.OsPath
4242
#endif
4343
-- * Filepath construction
4444
, PS.encodeUtf
45+
, PS.unsafeEncodeUtf
4546
, PS.encodeWith
4647
, PS.encodeFS
4748
#if defined(WINDOWS) || defined(POSIX)
@@ -117,6 +118,7 @@ import System.OsString.Windows as PS
117118
, decodeFS
118119
, pack
119120
, encodeUtf
121+
, unsafeEncodeUtf
120122
, encodeWith
121123
, encodeFS
122124
, unpack
@@ -149,6 +151,7 @@ import System.OsString.Posix as PS
149151
, decodeFS
150152
, pack
151153
, encodeUtf
154+
, unsafeEncodeUtf
152155
, encodeWith
153156
, encodeFS
154157
, unpack
@@ -165,6 +168,7 @@ import System.OsPath.Internal as PS
165168
, decodeFS
166169
, pack
167170
, encodeUtf
171+
, unsafeEncodeUtf
168172
, encodeWith
169173
, encodeFS
170174
, unpack

System/OsPath/Internal.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import GHC.IO.Encoding.UTF16 ( mkUTF16le )
3434
import qualified System.OsPath.Posix as PF
3535
import GHC.IO.Encoding.UTF8 ( mkUTF8 )
3636
#endif
37+
import GHC.Stack (HasCallStack)
3738

3839

3940

@@ -42,10 +43,18 @@ import GHC.IO.Encoding.UTF8 ( mkUTF8 )
4243
-- On windows this encodes as UTF16-LE (strictly), which is a pretty good guess.
4344
-- On unix this encodes as UTF8 (strictly), which is a good guess.
4445
--
45-
-- Throws a 'EncodingException' if encoding fails.
46+
-- Throws an 'EncodingException' if encoding fails. If the input does not
47+
-- contain surrogate chars, you can use 'unsafeEncodeUtf'.
4648
encodeUtf :: MonadThrow m => FilePath -> m OsPath
4749
encodeUtf = OS.encodeUtf
4850

51+
-- | Unsafe unicode friendly encoding.
52+
--
53+
-- Like 'encodeUtf', except it crashes when the input contains
54+
-- surrogate chars. For sanitized input, this can be useful.
55+
unsafeEncodeUtf :: HasCallStack => String -> OsString
56+
unsafeEncodeUtf = OS.unsafeEncodeUtf
57+
4958
-- | Encode a 'FilePath' with the specified encoding.
5059
encodeWith :: TextEncoding -- ^ unix text encoding
5160
-> TextEncoding -- ^ windows text encoding

cabal.project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
packages: ./
2+

filepath.cabal

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ library
9696
, deepseq
9797
, exceptions
9898
, template-haskell
99-
, os-string >=2.0.0
99+
, os-string >=2.0.1
100100

101101
ghc-options: -Wall
102102

@@ -116,7 +116,7 @@ test-suite filepath-tests
116116
, base
117117
, bytestring >=0.11.3.0
118118
, filepath
119-
, os-string >=2.0.0
119+
, os-string >=2.0.1
120120
, QuickCheck >=2.7 && <2.15
121121

122122
default-language: Haskell2010
@@ -138,7 +138,7 @@ test-suite filepath-equivalent-tests
138138
, base
139139
, bytestring >=0.11.3.0
140140
, filepath
141-
, os-string >=2.0.0
141+
, os-string >=2.0.1
142142
, QuickCheck >=2.7 && <2.15
143143

144144
test-suite abstract-filepath
@@ -157,7 +157,7 @@ test-suite abstract-filepath
157157
, bytestring >=0.11.3.0
158158
, deepseq
159159
, filepath
160-
, os-string >=2.0.0
160+
, os-string >=2.0.1
161161
, QuickCheck >=2.7 && <2.15
162162
, quickcheck-classes-base ^>=0.6.2
163163

@@ -172,7 +172,7 @@ benchmark bench-filepath
172172
, bytestring >=0.11.3.0
173173
, deepseq
174174
, filepath
175-
, os-string >=2.0.0
175+
, os-string >=2.0.1
176176
, tasty-bench
177177

178178
ghc-options: -with-rtsopts=-A32m

0 commit comments

Comments
 (0)