diff --git a/src/Builder.php b/src/Builder.php index 12c150a..d4251e4 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -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; @@ -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 . ']'); } @@ -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 . ']'); } @@ -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); } /** diff --git a/tests/LanguageInterpreterTest.php b/tests/LanguageInterpreterTest.php index 1079403..6be8a06 100644 --- a/tests/LanguageInterpreterTest.php +++ b/tests/LanguageInterpreterTest.php @@ -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')); @@ -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());