cba3ce08df
I was happily able to repurpose some code from Git.Filename to handle this. I remember writing that code... a whole afternoon at a coffee shop, after which I felt I'd struggled with Haskell and git, and sorta lost, in needing to write this nasty peice of code. But was also pleased at the use of a pair of functions and quickcheck that allowed me to get it 100% right. So, turns out I not only got it right, but the code wasn't as special-purpose as I'd feared. Yay!
28 lines
727 B
Haskell
28 lines
727 B
Haskell
{- Some git commands output encoded filenames, in a rather annoyingly complex
|
|
- C-style encoding.
|
|
-
|
|
- Copyright 2010, 2011 Joey Hess <joey@kitenet.net>
|
|
-
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
-}
|
|
|
|
module Git.Filename where
|
|
|
|
import Utility.Format (decode_c, encode_c)
|
|
|
|
import Common
|
|
|
|
decode :: String -> FilePath
|
|
decode [] = []
|
|
decode f@(c:s)
|
|
-- encoded strings will be inside double quotes
|
|
| c == '"' && end s == ['"'] = decode_c $ beginning s
|
|
| otherwise = f
|
|
|
|
{- Should not need to use this, except for testing decode. -}
|
|
encode :: FilePath -> String
|
|
encode s = "\"" ++ encode_c s ++ "\""
|
|
|
|
{- for quickcheck -}
|
|
prop_idempotent_deencode :: String -> Bool
|
|
prop_idempotent_deencode s = s == decode (encode s)
|