Skip to content

Commit 90a5788

Browse files
authored
Merge pull request #44 from SimpleRegex/feature/add_preg_quote
Replace manual escaping with preg_quote
2 parents 34117d2 + d4dd975 commit 90a5788

File tree

2 files changed

+3
-27
lines changed

2 files changed

+3
-27
lines changed

src/Builder.php

+1-25
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
*/
3838
class Builder extends TestMethodProvider
3939
{
40-
const NON_LITERAL_CHARACTERS = '[\\^$.|?*+()';
4140
const METHOD_TYPE_BEGIN = 0b00001;
4241
const METHOD_TYPE_CHARACTER = 0b00010;
4342
const METHOD_TYPE_GROUP = 0b00100;
@@ -170,7 +169,6 @@ public function oneOf(string $chars)
170169
$this->validateAndAddMethodType(self::METHOD_TYPE_CHARACTER, self::METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
171170

172171
$chars = $this->escape($chars);
173-
$chars = $this->escapeRangeSpecificChars($chars);
174172

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

188186
$chars = $this->escape($chars);
189-
$chars = $this->escapeRangeSpecificChars($chars);
190187

191188
return $this->add('[^' . $chars . ']');
192189
}
@@ -576,28 +573,7 @@ public function until($toCondition) : self
576573
*/
577574
protected function escape(string $chars)
578575
{
579-
return implode('', array_map([$this, 'escapeChar'], str_split($chars)));
580-
}
581-
582-
/**
583-
* Escape specific character.
584-
*
585-
* @param string $char
586-
* @return string
587-
*/
588-
protected function escapeChar(string $char)
589-
{
590-
return (strpos(static::NON_LITERAL_CHARACTERS, $char) !== false ? '\\' : '') . $char;
591-
}
592-
593-
/**
594-
* Escape '-' and ']' in string to be used in range.
595-
*
596-
* @return string
597-
*/
598-
protected function escapeRangeSpecificChars(string $chars)
599-
{
600-
return str_replace(['-', ']'], ['\\-', '\\]'], $chars);
576+
return preg_quote($chars);
601577
}
602578

603579
/**

tests/LanguageInterpreterTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function testParser()
1313

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

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

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

0 commit comments

Comments
 (0)