Skip to content

Instantly share code, notes, and snippets.

@dmjio
Created January 25, 2021 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmjio/d17832a15a77d4daa03f6695b1708e9b to your computer and use it in GitHub Desktop.
Save dmjio/d17832a15a77d4daa03f6695b1708e9b to your computer and use it in GitHub Desktop.
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