module Jikka.Common.IO where

import Data.Char (isSpace)
import System.IO

hTakeWhile :: Handle -> (Char -> Bool) -> IO String
hTakeWhile :: Handle -> (Char -> Bool) -> IO String
hTakeWhile Handle
handle Char -> Bool
pred = do
  Bool
isEOF <- Handle -> IO Bool
hIsEOF Handle
handle
  if Bool
isEOF
    then String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return String
""
    else do
      Char
c <- Handle -> IO Char
hLookAhead Handle
handle
      if Char -> Bool
pred Char
c
        then do
          Char
_ <- Handle -> IO Char
hGetChar Handle
handle
          (Char
c Char -> String -> String
forall a. a -> [a] -> [a]
:) (String -> String) -> IO String -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Handle -> (Char -> Bool) -> IO String
hTakeWhile Handle
handle Char -> Bool
pred
        else String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return String
""

hGetWord :: Handle -> IO String
hGetWord :: Handle -> IO String
hGetWord Handle
handle = do
  Handle -> (Char -> Bool) -> IO String
hTakeWhile Handle
handle Char -> Bool
isSpace
  Char
c <- Handle -> IO Char
hGetChar Handle
handle
  String
s <- Handle -> (Char -> Bool) -> IO String
hTakeWhile Handle
handle (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
isSpace)
  String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
c Char -> String -> String
forall a. a -> [a] -> [a]
: String
s)

getWord :: IO String
getWord :: IO String
getWord = Handle -> IO String
hGetWord Handle
stdin