Created
January 25, 2021 15:58
-
-
Save dmjio/d17832a15a77d4daa03f6695b1708e9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import qualified Data.ByteString.Lazy as BL | |
import qualified Data.ByteString.Lazy.Char8 as BL8 | |
import System.Environment | |
import Debug.Trace | |
main :: IO () | |
main = do | |
fileName:currentPath:newPath:[] <- getArgs | |
bytes <- BL8.readFile fileName | |
let newBytes = findAndReplace (BL8.pack currentPath) (BL8.pack newPath) bytes | |
BL8.writeFile "new" newBytes | |
putStrLn $ "Wrote " <> fileName | |
findAndReplace | |
:: BL8.ByteString | |
-- ^ current | |
-> BL8.ByteString | |
-- ^ new | |
-> BL8.ByteString | |
-- ^ binary | |
-> BL8.ByteString | |
-- ^ new binary | |
findAndReplace current new = go | |
where | |
go bytes | BL.null bytes = mempty | |
go bytes = | |
case BL.splitAt (BL8.length current) bytes of | |
(ls,rs) | |
| ls == current -> new <> go rs | |
| otherwise -> BL.take 1 ls <> go (BL.drop 1 ls <> rs) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment