Fixed problem parsing games with square brackets in move string

This commit is contained in:
Alf Magne Kalleland
2013-02-23 04:00:46 +01:00
parent 8f4fa18895
commit 76a0970db0
2 changed files with 16 additions and 3 deletions

View File

@@ -27,9 +27,8 @@ class PgnParser
$this->pgnContent = $content;
}
private function cleanPgn()
public function cleanPgn($c)
{
$c = $this->pgnContent;
$c = preg_replace("/\\$[0-9]+/s", "", $c);
$c = str_replace("({", "( {", $c);
$c = preg_replace("/{([^\[]*?)\[([^}]?)}/s", '{$1-SB-$2}', $c);
@@ -37,7 +36,7 @@ class PgnParser
$c = preg_replace("/\t/s", "", $c);
$c = preg_replace("/\]\s+\[/s", "]\n[", $c);
$c = str_replace(" [", "[", $c);
$c = preg_replace("/([^\]])(\n+)\[/si", "$1\n\n[", $c);
$c = preg_replace("/([^\]])(\n+)\[([a-z])/si", "$1\n\n[$3", $c);
$c = preg_replace("/\n{3,}/s", "\n\n", $c);
$c = str_replace("-SB-", "[", $c);
$c = str_replace("0-0-0", "O-O-O", $c);

View File

@@ -1836,6 +1836,20 @@ class ParserTest extends PHPUnit_Framework_TestCase
$this->assertEquals(82, count($game['moves']));
}
/**
* @test
*/
public function shouldSplitPgnIntoCorrectGames(){
// given
$pgnParser = new PgnParser("pgn/1001-brilliant-checkmates.pgn");
// when
$games = $pgnParser->getUnparsedGames();
// then
$this->assertEquals(994, count($games));
}
private function getSpasskyFischerGameWith3FoldReptition(){
$parser = $this->getParser();
$moves = 'e4,d6,d4,g6,Nc3,Nf6,f4,Bg7,Nf3,c5,dxc5,Qa5,Bd3,Qxc5,Qe2,O-O,Be3,Qa5,O-O,Bg4,Rad1,Nc6,Bc4,Nh5,Bb3,Bxc3,bxc3,Qxc3,f5,Nf6,h3,Bxf3,Qxf3,Na5,Rd3,Qc7,Bh6,Nxb3,cxb3,Qc5+,Kh1,Qe5,Bxf8,Rxf8,Re3,Rc8,fxg6,hxg6,Qf4,Qxf4,Rxf4,Nd7,Rf2,Ne5,Kh2,Rc1,Ree2,Nc6,Rc2,Re1,Rfe2,Ra1,Kg3,Kg7,Rcd2,Rf1,Rf2,Re1,Rfe2,Rf1,Re3,a6,Rc3,Re1,Rc4,Rf1,Rdc2,Ra1,Rf2,Re1,Rfc2,g5,Rc1,Re2,R1c2,Re1,Rc1,Re2,R1c2';