summaryrefslogtreecommitdiff
path: root/Utility/Format.hs
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2020-01-02 18:07:10 +0000
committerSean Whitton <spwhitton@spwhitton.name>2020-01-02 18:07:10 +0000
commit1092316ba04116d7ae1d4cdf347804feacef768a (patch)
treefbc819794d4202a4ac778f0f6be57eedef7518dd /Utility/Format.hs
parentd1e0531dd8e8b842349421a898b74b212d2157e8 (diff)
parent50a5a7b101cc9c30a5a04b4c20cbf7f99fcab0ef (diff)
downloadgit-repair-1092316ba04116d7ae1d4cdf347804feacef768a.tar.gz
Merge tag '1.20200102'
tagging package git-repair version 1.20200102
Diffstat (limited to 'Utility/Format.hs')
-rw-r--r--Utility/Format.hs31
1 files changed, 20 insertions, 11 deletions
diff --git a/Utility/Format.hs b/Utility/Format.hs
index 7844963..a2470fa 100644
--- a/Utility/Format.hs
+++ b/Utility/Format.hs
@@ -11,11 +11,11 @@ module Utility.Format (
format,
decode_c,
encode_c,
- prop_isomorphic_deencode
+ prop_encode_c_decode_c_roundtrip
) where
import Text.Printf (printf)
-import Data.Char (isAlphaNum, isOctDigit, isHexDigit, isSpace, chr, ord)
+import Data.Char (isAlphaNum, isOctDigit, isHexDigit, isSpace, chr, ord, isAscii)
import Data.Maybe (fromMaybe)
import Data.Word (Word8)
import Data.List (isPrefixOf)
@@ -100,10 +100,10 @@ empty :: Frag -> Bool
empty (Const "") = True
empty _ = False
-{- Decodes a C-style encoding, where \n is a newline, \NNN is an octal
- - encoded character, and \xNN is a hex encoded character.
+{- Decodes a C-style encoding, where \n is a newline (etc),
+ - \NNN is an octal encoded character, and \xNN is a hex encoded character.
-}
-decode_c :: FormatString -> FormatString
+decode_c :: FormatString -> String
decode_c [] = []
decode_c s = unescape ("", s)
where
@@ -141,14 +141,14 @@ decode_c s = unescape ("", s)
handle n = ("", n)
{- Inverse of decode_c. -}
-encode_c :: FormatString -> FormatString
+encode_c :: String -> FormatString
encode_c = encode_c' (const False)
{- Encodes more strictly, including whitespace. -}
-encode_c_strict :: FormatString -> FormatString
+encode_c_strict :: String -> FormatString
encode_c_strict = encode_c' isSpace
-encode_c' :: (Char -> Bool) -> FormatString -> FormatString
+encode_c' :: (Char -> Bool) -> String -> FormatString
encode_c' p = concatMap echar
where
e c = '\\' : [c]
@@ -173,6 +173,15 @@ encode_c' p = concatMap echar
e_asc c = showoctal $ ord c
showoctal i = '\\' : printf "%03o" i
-{- for quickcheck -}
-prop_isomorphic_deencode :: String -> Bool
-prop_isomorphic_deencode s = s == decode_c (encode_c s)
+{- For quickcheck.
+ -
+ - Encoding and then decoding roundtrips only when
+ - the string is ascii because eg, both "\12345" and
+ - "\227\128\185" are encoded to "\343\200\271".
+ -
+ - This property papers over the problem, by only testing ascii.
+ -}
+prop_encode_c_decode_c_roundtrip :: String -> Bool
+prop_encode_c_decode_c_roundtrip s = s' == decode_c (encode_c s')
+ where
+ s' = filter isAscii s