unescape :: ByteString -> ByteString
unescape = B.reverse . B.pack . snd .
           (foldl' unescapeChar (False, [])) . B.unpack
  where
    unescapeChar :: (Bool, [Word8]) -> Word8 -> (Bool, [Word8])
    unescapeChar (False, cs) c = if c == fromIntegral (ord '\\')
                                 then (True, cs)
                                 else (False, c : cs)
    unescapeChar (_, cs) c     = (False, c : cs)
unescape :: ByteString -> ByteString
unescape = B.reverse . B.pack . snd .
           (foldl' unescapeChar (False, [])) . B.unpack
  where
    unescapeChar :: (Bool, [Word8]) -> Word8 -> (Bool, [Word8])
    unescapeChar (False, cs) c = if c == fromIntegral (ord '\\')
                                 then (True, cs)
                                 else (False, c : cs)
    unescapeChar (_, cs) c     = (False, c : cs)
unescape = B.reverse . B.pack . snd .
           (foldl' unescapeChar (False, [])) . B.unpack
  where
    unescapeChar :: (Bool, [Word8]) -> Word8 -> (Bool, [Word8])
    unescapeChar (False, cs) c = if c == fromIntegral (ord '\\')
                                 then (True, cs)
                                 else (False, c : cs)
    unescapeChar (_, cs) c     = (False, c : cs)