unsplitModes ::
  [(Bool,Char,ByteString)] ->
  [ByteString]
unsplitModes modes
  = B8.pack (foldr combineModeChars (const "") modes True)
  : [arg | (_,_,arg) <- modes, not (B.null arg)]
  where
  combineModeChars (q,m,_) rest p
    | p == q = m : rest p
    | q = '+' : m : rest True
    | otherwise = '-' : m : rest False


-- | Split up a mode change command and arguments into individual changes
-- given a configuration.
unsplitModes ::
  [(Bool,Char,ByteString)] ->
  [ByteString]
unsplitModes modes
  = B8.pack (foldr combineModeChars (const "") modes True)
  : [arg | (_,_,arg) <- modes, not (B.null arg)]
  where
  combineModeChars (q,m,_) rest p
    | p == q = m : rest p
    | q = '+' : m : rest True
    | otherwise = '-' : m : rest False


-- | Split up a mode change command and arguments into individual changes
-- given a configuration.
unsplitModes modes
  = B8.pack (foldr combineModeChars (const "") modes True)
  : [arg | (_,_,arg) <- modes, not (B.null arg)]
  where
  combineModeChars (q,m,_) rest p
    | p == q = m : rest p
    | q = '+' : m : rest True
    | otherwise = '-' : m : rest False


-- | Split up a mode change command and arguments into individual changes
-- given a configuration.