Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Replace manual escaping with preg_quote #44

Merged
merged 1 commit into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 1 addition & 25 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
*/
class Builder extends TestMethodProvider
{
const NON_LITERAL_CHARACTERS = '[\\^$.|?*+()';
const METHOD_TYPE_BEGIN = 0b00001;
const METHOD_TYPE_CHARACTER = 0b00010;
const METHOD_TYPE_GROUP = 0b00100;
Expand Down Expand Up @@ -170,7 +169,6 @@ public function oneOf(string $chars)
$this->validateAndAddMethodType(self::METHOD_TYPE_CHARACTER, self::METHOD_TYPES_ALLOWED_FOR_CHARACTERS);

$chars = $this->escape($chars);
$chars = $this->escapeRangeSpecificChars($chars);

return $this->add('[' . $chars . ']');
}
Expand All @@ -186,7 +184,6 @@ public function notOneOf(string $chars)
$this->validateAndAddMethodType(self::METHOD_TYPE_CHARACTER, self::METHOD_TYPES_ALLOWED_FOR_CHARACTERS);

$chars = $this->escape($chars);
$chars = $this->escapeRangeSpecificChars($chars);

return $this->add('[^' . $chars . ']');
}
Expand Down Expand Up @@ -576,28 +573,7 @@ public function until($toCondition) : self
*/
protected function escape(string $chars)
{
return implode('', array_map([$this, 'escapeChar'], str_split($chars)));
}

/**
* Escape specific character.
*
* @param string $char
* @return string
*/
protected function escapeChar(string $char)
{
return (strpos(static::NON_LITERAL_CHARACTERS, $char) !== false ? '\\' : '') . $char;
}

/**
* Escape '-' and ']' in string to be used in range.
*
* @return string
*/
protected function escapeRangeSpecificChars(string $chars)
{
return str_replace(['-', ']'], ['\\-', '\\]'], $chars);
return preg_quote($chars);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/LanguageInterpreterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testParser()

$srl = new SRL('begin with literally "http", optional "s", literally "://", optional "www.",' .
'anything once or more, literally ".com", must end');
$this->assertEquals('/^(?:http)(?:(?:s))?(?::\/\/)(?:(?:www\.))?.+(?:\.com)$/', $srl->get());
$this->assertEquals('/^(?:http)(?:(?:s))?(?:\:\/\/)(?:(?:www\.))?.+(?:\.com)$/', $srl->get());
$this->assertTrue($srl->isMatching('http://www.ebay.com'));
$this->assertTrue($srl->isMatching('https://google.com'));
$this->assertFalse($srl->isMatching('htt://google.com'));
Expand Down Expand Up @@ -52,7 +52,7 @@ public function testParser()
$this->assertEquals('/^[^a-z][^A-Z][^f-o][^O-z]/', $srl->get());

$srl = new SRL('starts with not one of "!@#/"');
$this->assertEquals('/^[^!@#\/]/', $srl->get());
$this->assertEquals('/^[^\!@#\/]/', $srl->get());

$srl = new SRL('backslash');
$this->assertEquals('/\\\\/', $srl->get());
Expand Down