repos / line-indexed-cursor.hs.git


commit
166cd97
parent
9de8439
author
Evgenii Akentev
date
2023-07-09 18:56:12 +0400 +04
Fix goToLine when line number is negative
2 files changed,  +10, -2
M src/System/IO/LineIndexedCursor.hs
+3, -2
 1@@ -83,8 +83,9 @@ getCurrentLineNumber' CursorHandle{..} = do
 2   pure cln
 3 
 4 goToLine' :: CursorHandle -> Integer -> IO Integer
 5-goToLine' CursorHandle{..} ln = do
 6-  modifyMVar linesIdx $ \(idx, size, _) -> do
 7+goToLine' ch@CursorHandle{..} ln =
 8+  if (ln < 0) then getCurrentLineNumber' ch
 9+  else modifyMVar linesIdx $ \(idx, size, _) -> do
10     if ln > size then do
11       hSeek fileHandle AbsoluteSeek (idx !! 0)
12       -- try to read until the requested line number
M test/Main.hs
+7, -0
 1@@ -29,6 +29,13 @@ main = hspec $ do
 2         l <- getCurrentLine c
 3         l `shouldBe` Just "Sed elementum velit sit amet orci mollis tincidunt."
 4 
 5+      it "goToLine is negative" $ \(_, c) -> do
 6+        ln <- goToLine c (-10)
 7+        ln `shouldBe` 0
 8+
 9+        l <- getCurrentLine c
10+        l `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
11+
12       it "goToLine is too big" $ \(_, c) -> do
13         l <- getCurrentLine c
14         l `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit."