From 166cd97553e918c5bdf67d38ab86dc1a9326d0da Mon Sep 17 00:00:00 2001 From: Evgenii Akentev Date: Sun, 9 Jul 2023 18:56:12 +0400 Subject: [PATCH] Fix goToLine when line number is negative --- src/System/IO/LineIndexedCursor.hs | 5 +++-- test/Main.hs | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/System/IO/LineIndexedCursor.hs b/src/System/IO/LineIndexedCursor.hs index 14d62cd..10e2819 100644 --- a/src/System/IO/LineIndexedCursor.hs +++ b/src/System/IO/LineIndexedCursor.hs @@ -83,8 +83,9 @@ getCurrentLineNumber' CursorHandle{..} = do pure cln goToLine' :: CursorHandle -> Integer -> IO Integer -goToLine' CursorHandle{..} ln = do - modifyMVar linesIdx $ \(idx, size, _) -> do +goToLine' ch@CursorHandle{..} ln = + if (ln < 0) then getCurrentLineNumber' ch + else modifyMVar linesIdx $ \(idx, size, _) -> do if ln > size then do hSeek fileHandle AbsoluteSeek (idx !! 0) -- try to read until the requested line number diff --git a/test/Main.hs b/test/Main.hs index 8e002ec..9e74031 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -29,6 +29,13 @@ main = hspec $ do l <- getCurrentLine c l `shouldBe` Just "Sed elementum velit sit amet orci mollis tincidunt." + it "goToLine is negative" $ \(_, c) -> do + ln <- goToLine c (-10) + ln `shouldBe` 0 + + l <- getCurrentLine c + l `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit." + it "goToLine is too big" $ \(_, c) -> do l <- getCurrentLine c l `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit." -- 2.34.1