Skip to content

Commit acffc29

Browse files
authored
Shorten binary serialization for true and false (#256)
Fixes #225
1 parent dbd6059 commit acffc29

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

scrapscript.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,8 @@ class Variant(Object):
984984
TYPE_ACCESS := b"@",
985985
TYPE_SPREAD := b"S",
986986
TYPE_NAMED_SPREAD := b"R",
987+
TYPE_TRUE := b"T",
988+
TYPE_FALSE := b"F",
987989
]
988990
FLAG_REF = 0x80
989991

@@ -1091,6 +1093,10 @@ def serialize(self, obj: Object) -> None:
10911093
self.serialize(item)
10921094
return
10931095
if isinstance(obj, Variant):
1096+
if obj.tag == "true" and isinstance(obj.value, Hole):
1097+
return self.emit(TYPE_TRUE)
1098+
if obj.tag == "false" and isinstance(obj.value, Hole):
1099+
return self.emit(TYPE_FALSE)
10941100
# TODO(max): Determine if this should be a ref
10951101
self.emit(TYPE_VARIANT)
10961102
# TODO(max): String pool (via refs) for strings longer than some length?
@@ -1329,6 +1335,12 @@ def parse(self) -> Object:
13291335
return Spread()
13301336
if ty == TYPE_NAMED_SPREAD:
13311337
return Spread(self._string())
1338+
if ty == TYPE_TRUE:
1339+
assert not is_ref
1340+
return Variant("true", Hole())
1341+
if ty == TYPE_FALSE:
1342+
assert not is_ref
1343+
return Variant("false", Hole())
13321344
raise NotImplementedError(bytes(ty))
13331345

13341346

@@ -2531,7 +2543,7 @@ def do_GET(self) -> None:
25312543
if scrap is not None:
25322544
self.send_response(200)
25332545
self.send_header("Content-Type", "application/scrap; charset=binary")
2534-
self.send_header("Content-Disposition", f'attachment; filename={json.dumps(f"{path}.scrap")}')
2546+
self.send_header("Content-Disposition", f"attachment; filename={json.dumps(f'{path}.scrap')}")
25352547
self.send_header("Content-Length", str(len(scrap)))
25362548
self.end_headers()
25372549
self.wfile.write(scrap)

scrapscript_tests.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3778,6 +3778,22 @@ def test_access(self) -> None:
37783778
def test_spread(self) -> None:
37793779
self.assertEqual(self._serialize(Spread()), TYPE_SPREAD)
37803780
self.assertEqual(self._serialize(Spread("rest")), TYPE_NAMED_SPREAD + b"\x08rest")
3781+
3782+
def test_true_variant(self) -> None:
3783+
obj = Variant("true", Hole())
3784+
self.assertEqual(self._serialize(obj), TYPE_TRUE)
3785+
3786+
def test_false_variant(self) -> None:
3787+
obj = Variant("false", Hole())
3788+
self.assertEqual(self._serialize(obj), TYPE_FALSE)
3789+
3790+
def test_true_variant_with_non_hole_uses_regular_variant(self) -> None:
3791+
obj = Variant("true", Int(123))
3792+
self.assertEqual(self._serialize(obj), TYPE_VARIANT + b"\x08truei\xf6\x01")
3793+
3794+
def test_false_variant_with_non_hole_uses_regular_variant(self) -> None:
3795+
obj = Variant("false", Int(123))
3796+
self.assertEqual(self._serialize(obj), TYPE_VARIANT + b"\x0afalsei\xf6\x01")
37813797

37823798

37833799
class RoundTripSerializationTests(unittest.TestCase):

0 commit comments

Comments
 (0)