Commit 9c2c55d9 authored by Camil Staps's avatar Camil Staps 🍃

Rename cs_* fields of EncodingSt to es_* (it used to be CompressSt)

parent 0d435eba
Pipeline #32598 passed with stage
in 1 minute and 55 seconds
......@@ -80,7 +80,7 @@ decode :: !{#Char} -> Maybe a | gBinaryDecode{|*|} a
class GenBinary a | gBinaryEncode{|*|}, gBinaryEncodingSize{|*|}, gBinaryDecode{|*|} a
:: *EncodingSt = {cs_pos :: !Int, cs_bits :: !*{#Char}, cs_cons_path :: ![ConsPos]}
:: *EncodingSt = {es_pos :: !Int, es_bits :: !*{#Char}, es_cons_path :: ![ConsPos]}
generic gBinaryEncode a :: !a !*EncodingSt -> *EncodingSt
gBinaryEncode{|UNIT|} _ st = st
......@@ -119,16 +119,16 @@ gBinaryDecode{|PAIR|} fx fy st
= case (mbX, mbY) of
(Just x, Just y) = (Just $ PAIR x y, st)
_ = (Nothing, st)
gBinaryDecode{|EITHER|} fl fr st = case st.cs_cons_path of
gBinaryDecode{|EITHER|} fl fr st = case st.es_cons_path of
[] = (Nothing, st)
[ConsLeft:path] = appFst (fmap LEFT) $ fl {st & cs_cons_path=path}
[ConsRight:path] = appFst (fmap RIGHT) $ fr {st & cs_cons_path=path}
[ConsLeft:path] = appFst (fmap LEFT) $ fl {st & es_cons_path=path}
[ConsRight:path] = appFst (fmap RIGHT) $ fr {st & es_cons_path=path}
gBinaryDecode{|CONS|} f st = appFst (fmap CONS) $ f st
gBinaryDecode{|FIELD|} f st = appFst (fmap \x -> FIELD x) $ f st
gBinaryDecode{|OBJECT of {gtd_conses,gtd_num_conses}|} f st =
case decodeIntWithNBits (ceil_log2 0 gtd_num_conses) st of
(Nothing, st) = (Nothing, st)
(Just i, st) = appFst (fmap \x -> OBJECT x) $ f {st & cs_cons_path=getConsPath (gtd_conses!!i)}
(Just i, st) = appFst (fmap \x -> OBJECT x) $ f {st & es_cons_path=getConsPath (gtd_conses!!i)}
gBinaryDecode{|RECORD|} f st = appFst (fmap RECORD) $ f st
derive gBinaryDecode Int, Real, Bool, Char, String, [], {}, {!}, (), (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,),
......
......@@ -12,10 +12,10 @@ encode x
#! encoded_size = gBinaryEncodingSize{|*|} x 0
#! arr_size = (encoded_size+7) >> 3
#! bits = createArray arr_size '\0'
= (gBinaryEncode{|*|} x (mkEncodingSt bits)).cs_bits
= (gBinaryEncode{|*|} x (mkEncodingSt bits)).es_bits
mkEncodingSt :: !*{#Char} -> *EncodingSt
mkEncodingSt arr = {cs_pos = 0, cs_bits = arr, cs_cons_path=[]}
mkEncodingSt arr = {es_pos = 0, es_bits = arr, es_cons_path=[]}
generic gBinaryEncode a :: !a !*EncodingSt -> *EncodingSt
gBinaryEncode{|Int|} x st = encodeInt x st
......@@ -42,13 +42,13 @@ encodeChar :: !Char !*EncodingSt -> *EncodingSt
encodeChar c st = encodeIntUsingNBytes 1 (toInt c) st
encodeBool :: !Bool !*EncodingSt -> *EncodingSt
encodeBool False st = {st & cs_pos = st.cs_pos + 1}
encodeBool True cs=:{cs_pos = pos, cs_bits = bits}
encodeBool False st = {st & es_pos = st.es_pos + 1}
encodeBool True cs=:{es_pos = pos, es_bits = bits}
#! byte_pos = pos >> 3
#! bit_pos = pos bitand 7
#! int = toInt bits.[byte_pos]
#! bit_mask = 1 << bit_pos
= {cs & cs_pos = inc pos, cs_bits = {bits & [byte_pos] = toChar $ int bitor bit_mask}}
= {cs & es_pos = inc pos, es_bits = {bits & [byte_pos] = toChar $ int bitor bit_mask}}
encodeReal :: !Real !*EncodingSt -> *EncodingSt
encodeReal real st = IF_INT_64_OR_32
......@@ -71,11 +71,11 @@ where
encode :: !Int !*EncodingSt -> *EncodingSt
encode 0 st = st
encode remainingBytes st
#! byte_pos = st.cs_pos >> 3
#! byte_pos = st.es_pos >> 3
#! st =
{ st
& cs_bits = {st.cs_bits & [byte_pos] = toChar $ int >> ((numBytes - remainingBytes) * 8)}
, cs_pos = st.cs_pos + 8
& es_bits = {st.es_bits & [byte_pos] = toChar $ int >> ((numBytes - remainingBytes) * 8)}
, es_pos = st.es_pos + 8
}
= encode (dec remainingBytes) st
......@@ -116,16 +116,16 @@ gBinaryDecode{|PAIR|} fx fy st
= case (mbX, mbY) of
(Just x, Just y) = (Just $ PAIR x y, st)
_ = (Nothing, st)
gBinaryDecode{|EITHER|} fl fr st = case st.cs_cons_path of
gBinaryDecode{|EITHER|} fl fr st = case st.es_cons_path of
[] = (Nothing, st)
[ConsLeft:path] = appFst (fmap LEFT) $ fl {st & cs_cons_path=path}
[ConsRight:path] = appFst (fmap RIGHT) $ fr {st & cs_cons_path=path}
[ConsLeft:path] = appFst (fmap LEFT) $ fl {st & es_cons_path=path}
[ConsRight:path] = appFst (fmap RIGHT) $ fr {st & es_cons_path=path}
gBinaryDecode{|CONS|} f st = appFst (fmap CONS) $ f st
gBinaryDecode{|FIELD|} f st = appFst (fmap \x -> FIELD x) $ f st
gBinaryDecode{|OBJECT of {gtd_conses,gtd_num_conses}|} f st =
case decodeIntWithNBits (ceil_log2 0 gtd_num_conses) st of
(Nothing, st) = (Nothing, st)
(Just i, st) = appFst (fmap \x -> OBJECT x) $ f {st & cs_cons_path=getConsPath (gtd_conses!!i)}
(Just i, st) = appFst (fmap \x -> OBJECT x) $ f {st & es_cons_path=getConsPath (gtd_conses!!i)}
gBinaryDecode{|RECORD|} f st = appFst (fmap RECORD) $ f st
gBinaryDecode{|[]|} f st = decodeList f st
gBinaryDecode{|{}|} f st = decodeArray f st
......@@ -140,7 +140,7 @@ decodeChar st
= (toChar <$> mbInt, st)
decodeBool :: !*EncodingSt -> (!Maybe Bool, !*EncodingSt)
decodeBool cs=:{cs_pos = pos, cs_bits = bits}
decodeBool cs=:{es_pos = pos, es_bits = bits}
#! s = size bits
#! byte_pos = pos >> 3
#! bit_pos = pos bitand 7
......@@ -148,7 +148,7 @@ decodeBool cs=:{cs_pos = pos, cs_bits = bits}
#! int = toInt bits.[byte_pos]
#! bit_mask = 1 << bit_pos
#! bit = (bit_mask bitand int) <> 0
= (Just bit, {cs & cs_pos = inc pos})
= (Just bit, {cs & es_pos = inc pos})
decodeReal :: !*EncodingSt -> (!Maybe Real, !*EncodingSt)
decodeReal st = IF_INT_64_OR_32 decodeReal64 decodeReal32 $ st
......@@ -188,16 +188,16 @@ where
arrToList xs = [x \\ x <-: xs]
decodeIntWithNBytes :: !Int !*EncodingSt -> (!Maybe Int, !*EncodingSt)
decodeIntWithNBytes numBytes st=:{cs_pos} = decode numBytes 0 $ withByteAlignedPosition st
decodeIntWithNBytes numBytes st=:{es_pos} = decode numBytes 0 $ withByteAlignedPosition st
where
// we can decode an entire byte at once, as the start position is byte-aligned
decode :: !Int !Int !*EncodingSt -> (!Maybe Int, !*EncodingSt)
decode 0 int st = (Just int, st)
decode remainingBytes int st=:{cs_bits}
#! byte_pos = st.cs_pos >> 3
| byte_pos == size cs_bits = (Nothing, st)
#! byte = toInt cs_bits.[byte_pos]
= decode (dec remainingBytes) (byte << ((numBytes - remainingBytes) * 8) + int) {st & cs_pos = st.cs_pos + 8}
decode remainingBytes int st=:{es_bits}
#! byte_pos = st.es_pos >> 3
| byte_pos == size es_bits = (Nothing, st)
#! byte = toInt es_bits.[byte_pos]
= decode (dec remainingBytes) (byte << ((numBytes - remainingBytes) * 8) + int) {st & es_pos = st.es_pos + 8}
decodeIntWithNBits :: !Int !*EncodingSt -> (!Maybe Int, !*EncodingSt)
decodeIntWithNBits numBits st = decode numBits 0 st
......@@ -211,7 +211,7 @@ where
= decode (remainingBits - 1) (int + (if (fromJust mbBool) 1 0 << (numBits - remainingBits))) st
withByteAlignedPosition :: !*EncodingSt -> *EncodingSt
withByteAlignedPosition st=:{cs_pos} = {st & cs_pos = byteAlignedPosition cs_pos}
withByteAlignedPosition st=:{es_pos} = {st & es_pos = byteAlignedPosition es_pos}
byteAlignedPosition :: !Int -> Int
byteAlignedPosition pos = (pos + 7) bitand -8
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment