Skip to content

Commit

Permalink
Merge pull request #44 from SimpleRegex/feature/add_preg_quote
Browse files Browse the repository at this point in the history
Replace manual escaping with preg_quote
  • Loading branch information
KarimGeiger authored Oct 13, 2016
2 parents 34117d2 + d4dd975 commit 90a5788
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 27 deletions.
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

0 comments on commit 90a5788

Please sign in to comment.