niklasf / python-chess

A chess library for Python, with move generation and validation, PGN parsing and writing, Polyglot opening book reading, Gaviota tablebase probing, Syzygy tablebase probing, and UCI/XBoard engine communication
https://python-chess.readthedocs.io/en/latest/
GNU General Public License v3.0
2.4k stars 526 forks source link

Lenient whitespace parsing for engines #1043

Closed MarkZH closed 1 year ago

MarkZH commented 1 year ago

Fixes #970

This PR is an attempt at reducing the number of assumptions about what whitespace can appear in messages received from engines. This is done in several parts:

  1. A new function _next_token() extracts the next whitespace delimited word from a string, returning it with whitespace stripped out along with the rest of the line unchanged. This allows for string equality comparisons instead of str.startswith() and for the whitespace in the remaining string to be untouched, unlike with str.split().
  2. UCI options are parsed with a regex split that splits off parameter names without changing the whitespace in the parameter values.
  3. If a received line is expected to exactly match another string, it will be compared with a strip() version of it: if line.strip() == "readyok".

Other repeated code is refactored into functions.

niklasf commented 1 year ago

Perfect! Thanks.