Skip to content

Commit 107bbee

Browse files
authored
py: fixup multiline string literal roundtripping (#384)
2 parents 273b6c0 + e962ad1 commit 107bbee

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

Diff for: python/selfie-lib/selfie_lib/Literals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def protect_trailing_whitespace(line: str) -> str:
160160
for line in lines
161161
)
162162

163-
return f"{TRIPLE_QUOTE}\n{protect_whitespace}{TRIPLE_QUOTE}"
163+
return f"{TRIPLE_QUOTE}{protect_whitespace}{TRIPLE_QUOTE}"
164164

165165
_char_literal_pattern = re.compile(r"""\{'(\\?.)'\}""")
166166

Diff for: python/selfie-lib/selfie_lib/Slice.py

+5
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,8 @@ def count(self, char: str) -> int:
8989

9090
def baseLineAtOffset(self, index: int) -> int:
9191
return 1 + Slice(self.base, 0, index).count("\n")
92+
93+
def starts_with(self, prefix: str) -> bool:
94+
if len(prefix) > len(self):
95+
return False
96+
return all(self[i] == prefix[i] for i in range(len(prefix)))

Diff for: python/selfie-lib/selfie_lib/SourceFile.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
156156
end_arg = -1
157157
end_paren = 0
158158
if self._content_slice[arg_start] == '"':
159-
if self._content_slice[arg_start].startswith(self.TRIPLE_QUOTE):
159+
if self._content_slice.subSequence(
160+
arg_start, len(self._content_slice)
161+
).starts_with(self.TRIPLE_QUOTE):
160162
end_arg = self._content_slice.indexOf(
161163
self.TRIPLE_QUOTE, arg_start + len(self.TRIPLE_QUOTE)
162164
)

Diff for: python/selfie-lib/tests/LiteralString_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ def test_encode_single_with_dollars(self, value, expected):
2626
@pytest.mark.parametrize(
2727
("value", "expected"),
2828
[
29-
("1", "'''\n1'''"),
30-
("\\", "'''\n\\\\'''"),
29+
("1", "'''1'''"),
30+
("\\", "'''\\\\'''"),
3131
(
3232
" leading\ntrailing ",
33-
"'''\n" + " leading\n" + "trailing \\u0020'''",
33+
"''' leading\ntrailing \\u0020'''",
3434
),
3535
],
3636
)

0 commit comments

Comments
 (0)