Skip to content

Commit 1e7fcae

Browse files
committed
Avoid extra space when removing PGN annotations
1 parent 89c1261 commit 1e7fcae

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

chess/pgn.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,37 @@
120120
SKIP_MOVETEXT_REGEX = re.compile(r""";|\{|\}""")
121121

122122

123-
CLOCK_REGEX = re.compile(r"""\[%clk\s(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+(?:\.\d*)?)\]""")
124-
EMT_REGEX = re.compile(r"""\[%emt\s(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+(?:\.\d*)?)\]""")
123+
CLOCK_REGEX = re.compile(r"""(?P<prefix>\s?)\[%clk\s(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+(?:\.\d*)?)\](?P<suffix>\s?)""")
124+
EMT_REGEX = re.compile(r"""(?P<prefix>\s?)\[%emt\s(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+(?:\.\d*)?)\](?P<suffix>\s?)""")
125125

126126
EVAL_REGEX = re.compile(r"""
127+
(?P<prefix>\s?)
127128
\[%eval\s(?:
128129
\#(?P<mate>[+-]?\d+)
129130
|(?P<cp>[+-]?(?:\d{0,10}\.\d{1,2}|\d{1,10}\.?))
130131
)(?:
131132
,(?P<depth>\d+)
132133
)?\]
134+
(?P<suffix>\s?)
133135
""", re.VERBOSE)
134136

135137
ARROWS_REGEX = re.compile(r"""
138+
(?P<prefix>\s?)
136139
\[%(?:csl|cal)\s(?P<arrows>
137140
[RGYB][a-h][1-8](?:[a-h][1-8])?
138141
(?:,[RGYB][a-h][1-8](?:[a-h][1-8])?)*
139142
)\]
143+
(?P<suffix>\s?)
140144
""", re.VERBOSE)
141145

146+
def _condense_affix(infix: str):
147+
def repl(match):
148+
if infix:
149+
return match.group("prefix") + infix + match.group("suffix")
150+
else:
151+
return match.group("prefix") and match.group("suffix")
152+
return repl
153+
142154

143155
TAG_ROSTER = ["Event", "Site", "Date", "Round", "White", "Black", "Result"]
144156

@@ -434,7 +446,7 @@ def set_eval(self, score: Optional[chess.engine.PovScore], depth: Optional[int]
434446
elif score.white().mate():
435447
eval = f"[%eval #{score.white().mate()}{depth_suffix}]"
436448

437-
self.comment, found = EVAL_REGEX.subn(eval, self.comment, count=1)
449+
self.comment, found = EVAL_REGEX.subn(_condense_affix(eval), self.comment, count=1)
438450

439451
if not found and eval:
440452
if self.comment and not self.comment.endswith(" "):
@@ -471,16 +483,18 @@ def set_arrows(self, arrows: Iterable[Union[chess.svg.Arrow, Tuple[Square, Squar
471483
pass
472484
(csl if arrow.tail == arrow.head else cal).append(arrow.pgn()) # type: ignore
473485

474-
self.comment = ARROWS_REGEX.sub("", self.comment).strip()
486+
self.comment = ARROWS_REGEX.sub(_condense_affix(""), self.comment)
475487

476488
prefix = ""
477489
if csl:
478490
prefix += f"[%csl {','.join(csl)}]"
479491
if cal:
480492
prefix += f"[%cal {','.join(cal)}]"
481493

482-
if prefix:
483-
self.comment = prefix + " " + self.comment if self.comment else prefix
494+
if prefix and self.comment and not self.comment.startswith(" ") and not self.comment.startswith("\n"):
495+
self.comment = prefix + " " + self.comment
496+
else:
497+
self.comment = prefix + self.comment
484498

485499
def clock(self) -> Optional[float]:
486500
"""
@@ -509,7 +523,7 @@ def set_clock(self, seconds: Optional[float]) -> None:
509523
seconds_part = f"{seconds:06.3f}".rstrip("0").rstrip(".")
510524
clk = f"[%clk {hours:d}:{minutes:02d}:{seconds_part}]"
511525

512-
self.comment, found = CLOCK_REGEX.subn(clk, self.comment, count=1)
526+
self.comment, found = CLOCK_REGEX.subn(_condense_affix(clk), self.comment, count=1)
513527

514528
if not found and clk:
515529
if self.comment and not self.comment.endswith(" ") and not self.comment.endswith("\n"):
@@ -543,7 +557,7 @@ def set_emt(self, seconds: Optional[float]) -> None:
543557
seconds_part = f"{seconds:06.3f}".rstrip("0").rstrip(".")
544558
emt = f"[%emt {hours:d}:{minutes:02d}:{seconds_part}]"
545559

546-
self.comment, found = EMT_REGEX.subn(emt, self.comment, count=1)
560+
self.comment, found = EMT_REGEX.subn(_condense_affix(emt), self.comment, count=1)
547561

548562
if not found and emt:
549563
if self.comment and not self.comment.endswith(" ") and not self.comment.endswith("\n"):

test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2774,9 +2774,13 @@ def test_annotations(self):
27742774
self.assertEqual(game.comment, "[%csl Ga1][%cal Ra1h1,Gb1b8] foo [%bar] baz [%clk 3:25:45] [%eval #1,5] [%emt 0:05:21]")
27752775
self.assertEqual(game.emt(), emt)
27762776

2777+
game.set_eval(None)
2778+
self.assertEqual(game.comment, "[%csl Ga1][%cal Ra1h1,Gb1b8] foo [%bar] baz [%clk 3:25:45] [%emt 0:05:21]")
2779+
27772780
game.set_emt(None)
2781+
self.assertEqual(game.comment, "[%csl Ga1][%cal Ra1h1,Gb1b8] foo [%bar] baz [%clk 3:25:45]")
2782+
27782783
game.set_clock(None)
2779-
game.set_eval(None)
27802784
game.set_arrows([])
27812785
self.assertEqual(game.comment, "foo [%bar] baz")
27822786

0 commit comments

Comments
 (0)