Skip to content

Commit

Permalink
Added 'debug' flag to .cabal file and debug mode to executable. Fix b…
Browse files Browse the repository at this point in the history
…ug with numbers without comma/dot.
  • Loading branch information
Krzysztof Skrzętnicki committed Dec 2, 2009
1 parent b5240a5 commit 8cd0b6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
4 changes: 4 additions & 0 deletions sorty.cabal
Expand Up @@ -56,8 +56,12 @@ source-repository this
Location: http://github.com/Tener/haskell-sorty.git
Tag: 0.1

flag debug
default: False

executable sorty
main-is: sorty.hs
build-depends: bytestring >= 0.9.1 && < 0.10,
base >= 4.1.0 && < 5
if flag(debug)
cpp-options: -DDEBUG
25 changes: 20 additions & 5 deletions sorty.hs
@@ -1,3 +1,6 @@
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -O #-}

module Main where

import qualified Data.ByteString.Lazy as BSL
Expand All @@ -6,6 +9,11 @@ import Data.Ord ( comparing )
import Data.Char
import Data.Ratio

#ifdef DEBUG
import Debug.Trace
import System.IO
#endif

transUnit :: String -> Integer
transUnit "G" = 1024 * transUnit "M"
transUnit "M" = 1024 * transUnit "K"
Expand All @@ -20,29 +28,36 @@ transUnit "" = 1
firstColumnSize :: BSL.ByteString -> Integer
firstColumnSize line = let
(pre,_) = BSL.break (isSpace . e2e) line
(preComma,postComma) = BSL.break ((`elem` ".,") . e2e) pre
(preUnit,unit) = BSL.break (isAlpha . e2e) postComma
(num,unit) = BSL.break (isAlpha . e2e) pre
(preComm,postComm) = BSL.break ((`elem` ".,") . e2e) num

toString = map e2e . BSL.unpack
readSafe x = (fst . last) $ (0,"") : reads x

a,b,c :: Ratio Integer
a = readSafe (toString preComma) % 1
b = let l = fromIntegral $ 10 * BSL.length preUnit in
a = readSafe (toString preComm) % 1
b = let l = fromIntegral $ 10 * BSL.length postComm in
case l of
0 -> 0
_ -> readSafe (toString preUnit) % l
_ -> readSafe (toString postComm) % l
c = (transUnit (toString unit)) % 1
result = round ((a+b) * c)
in
#ifdef DEBUG
traceShow (map round [a,b,c], map toString [line,pre,num,preComm,postComm,unit]) result
#else
result
#endif



e2e :: (Enum a, Enum b) => a -> b
e2e = toEnum . fromEnum

main = do
#ifdef DEBUG
hPutStrLn stderr "DEBUG MODE!"
#endif
input <- BSL.getContents
mapM_ BSL.putStrLn (sortBy (comparing firstColumnSize) $ BSL.split (e2e '\n') input)

Expand Down

0 comments on commit 8cd0b6c

Please sign in to comment.