Source

text / tests / benchmarks / Cut.hs

The default branch has multiple heads

import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.Text.IO as T
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy.Encoding as TL
import qualified Data.Text.Lazy.IO as TL
import qualified Data.Text.Lazy as TL
import Data.Int (Int64)
import Numeric (readDec)
import System.Environment (getArgs)

bytestring file s e = do
  t <- B.readFile file
  B.putStr (cut t)
  where
    cut = B.unlines . map (B.take (e - s) . B.drop s) . B.lines

lazyBytestring file s e = do
  t <- BL.readFile file
  BL.putStr (cut (fromIntegral s) (fromIntegral e) t)
  where
    cut s e = BL.unlines . map (BL.take (e - s) . BL.drop s) . BL.lines

lazyText file s e = do
  t <- TL.readFile file
  TL.putStr (cut (fromIntegral s) (fromIntegral e) t)
  where
    cut s e = TL.unlines . map (TL.take (e - s) . TL.drop s) . TL.lines

text file s e = do
  t <- T.readFile file
  T.putStr (cut t)
  where
    cut = T.unlines . map (T.take (e - s) . T.drop s) . T.lines

textBS file s e = do
  bs <- B.readFile file
  T.putStr . cut . T.decodeUtf8 $ bs
  where
    cut = T.unlines . map (T.take (e - s) . T.drop s) . T.lines

lazyTextBS file s e = do
  t <- BL.readFile file
  TL.putStr (cut (fromIntegral s) (fromIntegral e) (TL.decodeUtf8 t))
  where
    cut s e = TL.unlines . map (TL.take (e - s) . TL.drop s) . TL.lines

main = do
  (name : ss : es : file : _) <- getArgs
  let [(s',"")] = readDec ss
      [(e,"")] = readDec es
      s = s' - 1
  case name of
    "bs" -> bytestring file s e
    "lbs" -> lazyBytestring file s e
    "ltext" -> lazyText file s e
    "text" -> text file s e
    "ltextBS" -> lazyTextBS file s e
    "textBS" -> textBS file s e
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.