Skip to content

Commit 9618dbc

Browse files
Introduce rawFromString convenience path operation
1 parent c6bfeee commit 9618dbc

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

core/src/Streamly/Internal/FileSystem/PosixPath.hs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ module Streamly.Internal.FileSystem.OS_PATH
5656
, unsafeFromChunk
5757
, fromChars
5858
, fromString
59+
, rawFromString
5960
, unsafeFromString
6061
-- , fromCString#
6162
-- , fromW16CString#
@@ -169,7 +170,6 @@ For APIs that have not been released yet.
169170
>>> import Data.Either (Either, isLeft)
170171
>>> import Control.Exception (SomeException, evaluate, try)
171172
172-
>>> rawFromString = Array.fromPureStream . Unicode.encodeUtf8' . Stream.fromList
173173
>>> pack = fromJust . Path.fromString
174174
>>> fails action = (try (evaluate action) :: IO (Either SomeException String)) >>= return . isLeft
175175
-}
@@ -253,7 +253,7 @@ validatePath = Common.validatePath Common.OS_NAME
253253
-- | Check if the filepath is valid i.e. does the operating system or the file
254254
-- system allow such a path in listing or creating files?
255255
--
256-
-- >>> isValid = Path.isValidPath . rawFromString
256+
-- >>> isValid = Path.isValidPath . Path.rawFromString
257257
--
258258
-- >>> isValid ""
259259
-- False
@@ -327,15 +327,25 @@ fromChars s =
327327
Common.fromChars Common.OS_NAME Unicode.UNICODE_ENCODER s
328328
>>= fromPath . OS_PATH
329329

330+
-- | Create a raw path i.e. an array representing the path. Note that the path
331+
-- is not validated, therefore, it may not be valid according to 'isValidPath'.
332+
#ifndef IS_WINDOWS
333+
rawFromString :: [Char] -> Array Word8
334+
#else
335+
rawFromString :: [Char] -> Array Word16
336+
#endif
337+
rawFromString =
338+
Common.unsafeFromChars Unicode.UNICODE_ENCODER
339+
. Stream.fromList
340+
330341
-- | Like 'fromString' but does not perform any validations mentioned under
331342
-- 'isValidPath'. Fails only if unicode encoding fails.
332343
unsafeFromString :: IsPath OS_PATH a => [Char] -> a
333344
unsafeFromString =
334345
#ifndef DEBUG
335346
unsafeFromPath
336347
. OS_PATH
337-
. Common.unsafeFromChars Unicode.UNICODE_ENCODER
338-
. Stream.fromList
348+
. rawFromString
339349
#else
340350
fromJust . fromString
341351
#endif
@@ -432,7 +442,7 @@ showRaw p =
432442
--
433443
-- >>> readRaw = fromJust . Path.fromChunk . read
434444
--
435-
-- >>> arr = rawFromString "hello"
445+
-- >>> arr = Path.rawFromString "hello"
436446
-- >>> Path.showRaw $ (Path.readRaw $ show arr :: Path.PosixPath)
437447
-- "fromList [104,101,108,108,111]"
438448
--

core/src/Streamly/Internal/FileSystem/WindowsPath.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ For APIs that have not been released yet.
1919
>>> import Data.Either (Either, isLeft)
2020
>>> import Control.Exception (SomeException, evaluate, try)
2121
22-
>>> rawFromString = Array.fromPureStream . Unicode.encodeUtf16le' . Stream.fromList
2322
>>> pack = fromJust . Path.fromString
2423
>>> fails action = (try (evaluate action) :: IO (Either SomeException String)) >>= return . isLeft
2524
-}
@@ -36,7 +35,7 @@ For APIs that have not been released yet.
3635
-- | Check if the filepath is valid i.e. does the operating system or the file
3736
-- system allow such a path in listing or creating files?
3837
--
39-
-- >>> isValid = Path.isValidPath . rawFromString
38+
-- >>> isValid = Path.isValidPath . Path.rawFromString
4039
--
4140
-- >>> isValid ""
4241
-- False
@@ -163,7 +162,7 @@ isValidPath' = Common.isValidPath' Common.Windows
163162
--
164163
-- >>> readRaw = fromJust . Path.fromChunk . read
165164
--
166-
-- >>> arr :: Array Word16 = rawFromString "hello"
165+
-- >>> arr :: Array Word16 = Path.rawFromString "hello"
167166
-- >>> Path.showRaw $ (Path.readRaw $ show arr :: Path.WindowsPath)
168167
-- "fromList [104,101,108,108,111]"
169168
--

0 commit comments

Comments
 (0)