/** * * @param array $patterns * @param array $matches * @return bool */ public function consume(array $patterns, array &$matches = null) { $this->push(); $tests = array(); foreach ($patterns as $pattern) { if (is_string($pattern)) { $tests[] = new StringTest($pattern); } elseif (is_array($pattern)) { if (count($pattern) === 2) { $tests[] = new CharTest($pattern[0], $pattern[1]); } else { $tests[] = new CharTest($pattern[0]); } } } $fetchMatches = $matches !== null; $response = $this->matcher->match($this->handle, $tests, $fetchMatches); if ($fetchMatches && $response->getSuccess()) { $matches = $response->getMatches(); } $this->pop(); $this->seek($response->getMatchesLength(), SEEK_CUR); return $response->getSuccess(); }
<?php use mermshaus\Stream\Matcher\CharTest; use mermshaus\Stream\Matcher\EofTest; use mermshaus\Stream\Matcher\Matcher; use mermshaus\Stream\Matcher\StringTest; require __DIR__ . '/../../bootstrap.php'; $matcher = new Matcher(); $data = <<<'EOT' Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx hallo welt xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxUUUUZ EOT; $dataStream = 'data://text/plain;base64,' . base64_encode($data); $handle = fopen($dataStream, 'rb'); $tests = array(new CharTest('/Z/', '*'), new CharTest('/A/'), new CharTest('/x/', '+'), new CharTest('/\\s/', '+'), new StringTest('hallo'), new CharTest('/\\s/', '+'), new StringTest('welt'), new CharTest('/\\s/', '+'), new CharTest('/x/', '+'), new CharTest('/U/', 4), new CharTest('/Z/'), new EofTest()); //fseek($handle, 0); $result = $matcher->match($handle, $tests, true); var_dump($result); fseek($handle, 69); $tests = array(new StringTest('hallo'), new CharTest('/\\s/', '+'), new StringTest('welt')); $result = $matcher->match($handle, $tests, true); var_dump($result); $dataStream = 'data://text/plain;base64,' . base64_encode('welt '); $handle = fopen($dataStream, 'rb'); $tests = array(new StringTest('welt'), new CharTest('/\\s|$/')); $result = $matcher->match($handle, $tests, true); var_dump($result);
<?php // Call: $ echo -n "hello world" | php -f ./stdin-reverse.php // Output: elloh\ndlrow\n use mermshaus\Stream\Matcher\CharTest; use mermshaus\Stream\Matcher\Matcher; require __DIR__ . '/../../bootstrap.php'; $matcher = new Matcher(); $tests = array(new CharTest('/\\S/', '+'), new CharTest('/ |$/')); $res = $matcher->match(STDIN, $tests, true); while ($res->getSuccess()) { echo strrev($res->getMatches()[0]), "\n"; $res = $matcher->match(STDIN, $tests, true); }