Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/src/Streamly/Internal/FileSystem/Path/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,10 @@ unsafeFromChars :: (Unbox a) =>
-> Stream Identity Char
-> Array a
unsafeFromChars encode s =
let n = runIdentity $ Stream.fold Fold.length s
-- The encoded array may be longer than the char count. We are encoding it
-- twice, but it may still be cheaper than reallocating the array or
-- oversizing the array.
let n = runIdentity $ Stream.fold Fold.length (encode s)
in Array.fromPureStreamN n (encode s)

-- XXX Writing a custom fold for parsing a Posix path may be better for
Expand Down
5 changes: 5 additions & 0 deletions test/Streamly/Test/FileSystem/PosixPath.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ testFromString = describe "fromString" $ do
str (p "/usr/bin") `shouldBe` "/usr/bin"
it "relative roundtrip" $
str (p "a/b/c") `shouldBe` "a/b/c"
-- test correct array size allocation for unicode encoding
it "non-ASCII roundtrip preserves trailing bytes" $
str (p "\945.txt") `shouldBe` "\945.txt"
it "multi-byte UTF-8 roundtrip (4-byte char)" $
str (p "\x1F600/file") `shouldBe` "\x1F600/file"

-------------------------------------------------------------------------------
-- Separators
Expand Down
7 changes: 7 additions & 0 deletions test/Streamly/Test/FileSystem/WindowsPath.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ testFromString = describe "fromString" $ do
str (p "C:\\Users") `shouldBe` "C:\\Users"
it "forward slashes preserved on roundtrip" $
str (p "a/b") `shouldBe` "a/b"
-- test correct array size allocation for unicode encoding
it "non-ASCII (BMP) roundtrip" $
str (p "\945.txt") `shouldBe` "\945.txt"
-- Non-BMP chars require a UTF-16 surrogate pair (2 words for 1 char),
-- which would be truncated if the array were sized by char count.
it "multi-word UTF-16 roundtrip (non-BMP char)" $
str (p "\x1F600\\file") `shouldBe` "\x1F600\\file"

-------------------------------------------------------------------------------
-- Validation
Expand Down
Loading