Skip to content

Commit 5e1f281

Browse files
committed
work with all known whitespace combinations
1 parent 18f8cd1 commit 5e1f281

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

lib/macro_storage_format_parser.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from lib.macro_storage_format_builder import MacroStorageFormatBuilder
44
from lib.util import Util
5-
5+
import re
66

77
class MacroStorageFormatParser:
88
def __init__(
@@ -15,18 +15,10 @@ def __init__(
1515
self.util = Util(forceUUIDsZeroed)
1616

1717
def _split_macro_body_on_content_block(self, macro_body: str) -> tuple[str, str]:
18-
if "[content]\r\n" in macro_body:
19-
splitted = macro_body.split("[content]\r\n", 1)
20-
return (splitted[0], splitted[1])
21-
if "[content]\r" in macro_body:
22-
splitted = macro_body.split("[content]\r", 1)
23-
return (splitted[0], splitted[1])
24-
if "[content]\n\r" in macro_body:
25-
splitted = macro_body.split("[content]\n\r", 1)
26-
return (splitted[0], splitted[1])
27-
if "[content]\n" in macro_body:
28-
splitted = macro_body.split("[content]\n", 1)
29-
return (splitted[0], splitted[1])
18+
if "[content]" in macro_body:
19+
splitted = re.split(r'\[content\]\s*(\r\n|\r|\n\r|\n)', macro_body, maxsplit=1)
20+
# re.split splits also the capturing group \n as [1]!
21+
return (splitted[0], splitted[2])
3022
return ("ERROR SPLITTING AT [CONTENT]", "ERROR: INVALID NEWLINE")
3123

3224
def transform(self):

lib/test_macro_storage_format_parser.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,14 @@ def test_transform___newlines(self):
121121
self.assertTrue(isinstance(result4, tuple))
122122
self.assertEqual(result4[0], "foo\n")
123123
self.assertEqual(result4[1], "bar")
124+
# ###########################################################################
125+
# CASE5: U+0020 : SPACE [SP] and U+00A0 : NO-BREAK SPACE [NBSP] combos
126+
#
127+
# GIVEN
128+
macro_body5 = "foo\n[content]    \nbar"
129+
# WHEN
130+
result5 = parser._split_macro_body_on_content_block(macro_body5)
131+
# THEN
132+
self.assertTrue(isinstance(result5, tuple))
133+
self.assertEqual(result5[0], "foo\n")
134+
self.assertEqual(result5[1], "bar")

0 commit comments

Comments
 (0)