- 1
IT Оффтоп #232
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
0
IT Оффтоп #232
#202: https://govnokod.ru/28951 https://govnokod.xyz/_28951
#203: https://govnokod.ru/28954 https://govnokod.xyz/_28954
#204: https://govnokod.ru/28971 https://govnokod.xyz/_28971
#205: https://govnokod.ru/28986 https://govnokod.xyz/_28986
#206: https://govnokod.ru/28991 https://govnokod.xyz/_28991
#207: https://govnokod.ru/29002 https://govnokod.xyz/_29002
#208: https://govnokod.ru/29060 https://govnokod.xyz/_29060
#209: https://govnokod.ru/29070 https://govnokod.xyz/_29070
#210: https://govnokod.ru/29079 https://govnokod.xyz/_29079
#211: https://govnokod.ru/29092 https://govnokod.xyz/_29092
#212: https://govnokod.ru/29093 https://govnokod.xyz/_29093
#213: https://govnokod.ru/29104 https://govnokod.xyz/_29104
#214: https://govnokod.ru/29114 https://govnokod.xyz/_29114
#215: https://govnokod.ru/29125 https://govnokod.xyz/_29125
#216: https://govnokod.ru/29132 https://govnokod.xyz/_29132
#217: https://govnokod.ru/29147 https://govnokod.xyz/_29147
#218: https://govnokod.ru/29156 https://govnokod.xyz/_29156
#219: https://govnokod.ru/29166 https://govnokod.xyz/_29166
#220: https://govnokod.ru/29181 https://govnokod.xyz/_29181
#221: https://govnokod.ru/29185 https://govnokod.xyz/_29185
#222: https://govnokod.ru/29190 https://govnokod.xyz/_29190
#223: https://govnokod.ru/29203 https://govnokod.xyz/_29203
#224: https://govnokod.ru/29211 https://govnokod.xyz/_29211
#225: https://govnokod.ru/29212 https://govnokod.xyz/_29212
#226: https://govnokod.ru/29218 https://govnokod.xyz/_29218
#227: https://govnokod.ru/29220 https://govnokod.xyz/_29220
#228: https://govnokod.ru/29230 https://govnokod.xyz/_29230
#229: https://govnokod.ru/29235 https://govnokod.xyz/_29235
#230: https://govnokod.ru/29241 https://govnokod.xyz/_29241
#231: https://govnokod.ru/29246 https://govnokod.xyz/_29246
+1
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text.Lazy.IO as LIO
import GHC.IO.StdHandles
import Text.Regex.TDFA
import qualified Text.Regex.TDFA.Text.Lazy as RL
import Data.Array
import qualified Data.Text.Lazy as TL
import System.Environment
import System.Exit
import System.IO
import qualified Language.C.Syntax.Constants as CC
import Data.Char
printMatch t matches i =
let (offset, len) = matches ! i in
let offset' = fromIntegral offset in
let len' = fromIntegral len in
LIO.putStr $ TL.take len' $ TL.drop offset' t
printHead t matches =
let (offset, len) = matches ! 0 in
let offset' = fromIntegral offset in
let len' = fromIntegral len in
LIO.putStr $ TL.take offset' t
printTrail t matches =
let (offset, len) = matches ! 0 in
let offset' = fromIntegral offset in
let len' = fromIntegral len in
LIO.putStr $ TL.drop (offset' + len') t
need_capture_trail acc ".*" = (False, reverse acc)
need_capture_trail acc [] = (True, reverse acc)
need_capture_trail acc (c : rest) = need_capture_trail (c : acc) rest
getRE :: [String] -> Either String (RL.Regex, Bool, String)
getRE args =
case args of
(re_str : repl_str : _) ->
let (trail_needed, re_str') = need_capture_trail [] re_str in
let re_text = TL.pack $ CC.unescapeString re_str' in
case RL.compile defaultCompOpt defaultExecOpt re_text of
Right re ->
Right (re, trail_needed, CC.unescapeString repl_str)
Left err ->
Left err
_ ->
Left "Regexp expected"
-- replacement :: TL.Text -> Int -> _ -> String -> IO ()
replacement _ _ _ [] = return ()
replacement t n_matches matches (c : rest)
| ord c <= n_matches = do
printMatch t matches (ord c)
replacement t n_matches matches rest
| True = do
putChar c
replacement t n_matches matches rest
exitError :: String -> IO ()
exitError msg = do
hPutStrLn stderr msg
exitWith (ExitFailure 1)
main :: IO ()
main = do
args <- getArgs
case getRE args of
Right (re, trail_needed, repl) -> do
t <- LIO.hGetContents stdin
case RL.execute re t of
Right (Just matches) ->
do
let n_matches = snd $ bounds matches
-- print matches
printHead t matches
replacement t n_matches matches repl
if trail_needed then
printTrail t matches
else
return ()
Right Nothing -> do
exitError "Pattern not found"
Left err -> do
exitError err
Left err -> do
exitError err
Текст UNIX-way утилиты fed
Капча: p2ux