From 93223341e75ed6b30ddf0ab4c0722413111efcee Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 8 May 2024 12:42:06 -0700 Subject: [PATCH 1/4] Add `starts_with` to Slice. --- python/selfie-lib/selfie_lib/Slice.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/python/selfie-lib/selfie_lib/Slice.py b/python/selfie-lib/selfie_lib/Slice.py index 920c2324..2141f739 100644 --- a/python/selfie-lib/selfie_lib/Slice.py +++ b/python/selfie-lib/selfie_lib/Slice.py @@ -89,3 +89,8 @@ def count(self, char: str) -> int: def baseLineAtOffset(self, index: int) -> int: return 1 + Slice(self.base, 0, index).count("\n") + + def starts_with(self, prefix: str) -> bool: + if len(prefix) > len(self): + return False + return all(self[i] == prefix[i] for i in range(len(prefix))) From 41586eec5f36f1a37d20a802c9afb2c512667602 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 8 May 2024 12:43:50 -0700 Subject: [PATCH 2/4] Fix roundtrip parsing of multiline string literals. --- python/selfie-lib/selfie_lib/SourceFile.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/selfie-lib/selfie_lib/SourceFile.py b/python/selfie-lib/selfie_lib/SourceFile.py index f9ceff33..b437d908 100644 --- a/python/selfie-lib/selfie_lib/SourceFile.py +++ b/python/selfie-lib/selfie_lib/SourceFile.py @@ -156,7 +156,9 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral: end_arg = -1 end_paren = 0 if self._content_slice[arg_start] == '"': - if self._content_slice[arg_start].startswith(self.TRIPLE_QUOTE): + if self._content_slice.subSequence( + arg_start, len(self._content_slice) + ).starts_with(self.TRIPLE_QUOTE): end_arg = self._content_slice.indexOf( self.TRIPLE_QUOTE, arg_start + len(self.TRIPLE_QUOTE) ) From f0b4367d6d907f56b8016d9c202755860bae14ed Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 8 May 2024 13:06:20 -0700 Subject: [PATCH 3/4] Extra newline in string literal roundtrip. --- python/selfie-lib/selfie_lib/Literals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/selfie-lib/selfie_lib/Literals.py b/python/selfie-lib/selfie_lib/Literals.py index 494203f9..7c143289 100644 --- a/python/selfie-lib/selfie_lib/Literals.py +++ b/python/selfie-lib/selfie_lib/Literals.py @@ -160,7 +160,7 @@ def protect_trailing_whitespace(line: str) -> str: for line in lines ) - return f"{TRIPLE_QUOTE}\n{protect_whitespace}{TRIPLE_QUOTE}" + return f"{TRIPLE_QUOTE}{protect_whitespace}{TRIPLE_QUOTE}" _char_literal_pattern = re.compile(r"""\{'(\\?.)'\}""") From e962ad12ea6e290c6c5664c80cae36d84297f6c6 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 8 May 2024 13:11:26 -0700 Subject: [PATCH 4/4] Fixup tests. --- python/selfie-lib/tests/LiteralString_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/selfie-lib/tests/LiteralString_test.py b/python/selfie-lib/tests/LiteralString_test.py index 6f00f871..d19c7a63 100644 --- a/python/selfie-lib/tests/LiteralString_test.py +++ b/python/selfie-lib/tests/LiteralString_test.py @@ -26,11 +26,11 @@ def test_encode_single_with_dollars(self, value, expected): @pytest.mark.parametrize( ("value", "expected"), [ - ("1", "'''\n1'''"), - ("\\", "'''\n\\\\'''"), + ("1", "'''1'''"), + ("\\", "'''\\\\'''"), ( " leading\ntrailing ", - "'''\n" + " leading\n" + "trailing \\u0020'''", + "''' leading\ntrailing \\u0020'''", ), ], )