Skip to content

Instantly share code, notes, and snippets.

@dmjio
Created Jan 25, 2021
Embed
What would you like to do?
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