Skip to content

Commit

Permalink
Regexp is always used for regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 21, 2025
1 parent 2c9fa6a commit d628696
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 32 deletions.
3 changes: 2 additions & 1 deletion src/Bridges/Latte/TexyMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Texy\Bridges\Latte;

use Latte;
use Texy\Regexp;
use Texy\Texy;


Expand Down Expand Up @@ -45,7 +46,7 @@ public function texyOpened(Latte\MacroNode $node)
public function texyClosed(Latte\MacroNode $node)

Check failure on line 46 in src/Bridges/Latte/TexyMacro.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter $node of method Texy\Bridges\Latte\TexyMacro::texyClosed() has invalid type Latte\MacroNode.
{
$text = $node->content;

Check failure on line 48 in src/Bridges/Latte/TexyMacro.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to property $content on an unknown class Latte\MacroNode.
if (preg_match('#^([\t ]*)\S#m', $text, $m)) { // remove & restore indentation
if ($m = Regexp::match($text, '#^([\t ]*)\S#m')) { // remove & restore indentation
$text = str_replace(["\r", "\n" . $m[1]], ['', "\n"], $text);
$this->texy->htmlOutputModule->baseIndent = strlen($m[1]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Texy/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static function webalize(string $s, string $charlist = ''): string
{
$s = self::toAscii($s);
$s = strtolower($s);
$s = Regexp::replace($s, '#[^a-z0-9' . preg_quote($charlist, '#') . ']+#', '-');
$s = Regexp::replace($s, '#[^a-z0-9' . Regexp::quote($charlist) . ']+#', '-');
$s = trim($s, '-');
return $s;
}
Expand Down Expand Up @@ -149,7 +149,7 @@ public static function outdent(string $s, bool $firstLine = false): string
public static function isRelative(string $URL): bool
{
// check for scheme, or absolute path, or absolute URL
return !preg_match('#[a-z][a-z0-9+.-]{0,20}:|[\#/?]#Ai', $URL);
return !Regexp::match($URL, '#[a-z][a-z0-9+.-]{0,20}:|[\#/?]#Ai');
}


Expand Down
2 changes: 1 addition & 1 deletion src/Texy/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function setProperties(?string $s): void
$ch = $s[$p];

if ($ch === '(') { // title
preg_match('#(?:\\\\\)|[^)\n])++\)#', $s, $m, 0, $p);
$m = Regexp::match($s, '#(?:\\\\\)|[^)\n])++\)#', offset: $p);
$this->title = Helpers::unescapeHtml(str_replace('\)', ')', trim(substr($m[0], 1, -1))));
$p += strlen($m[0]);

Expand Down
2 changes: 1 addition & 1 deletion src/Texy/Modules/BlockModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function pattern(Texy\BlockParser $parser, array $matches): HtmlElement|s
// [4] => ... content

$mod = new Texy\Modifier($mMod);
$parts = preg_split('#\s+#u', $mParam, 2);
$parts = Texy\Regexp::split($mParam, '#\s+#u', limit: 2);
$blocktype = empty($parts[0]) ? 'block/default' : 'block/' . $parts[0];
$param = empty($parts[1]) ? null : $parts[1];

Expand Down
2 changes: 1 addition & 1 deletion src/Texy/Modules/EmoticonModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private function beforeParse(): void

$pattern = [];
foreach ($this->icons as $key => $foo) {
$pattern[] = preg_quote($key, '#') . '+'; // last char can be repeated
$pattern[] = Texy\Regexp::quote($key) . '+'; // last char can be repeated
}

$this->texy->registerLinePattern(
Expand Down
13 changes: 6 additions & 7 deletions src/Texy/Modules/HtmlModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Texy;
use Texy\HtmlElement;
use Texy\Patterns;
use Texy\Regexp;


/**
Expand Down Expand Up @@ -163,7 +164,7 @@ private function solveComment(Texy\HandlerInvocation $invocation, string $conten
}

// sanitize comment
$content = Texy\Regexp::replace($content, '#-{2,}#', ' - ');
$content = Regexp::replace($content, '#-{2,}#', ' - ');
$content = trim($content, '-');

return $this->texy->protect('<!--' . $content . '-->', Texy\Texy::CONTENT_MARKUP);
Expand Down Expand Up @@ -272,7 +273,7 @@ private function validateAttrs(HtmlElement $el, Texy\Texy $texy): bool

$texy->summary['links'][] = $el->attrs['href'];
}
} elseif (preg_match('#^h[1-6]#i', $name)) {
} elseif (Regexp::match($name, '#^h[1-6]#i')) {
$texy->headingModule->TOC[] = [
'el' => $el,
'level' => (int) substr($name, 1),
Expand All @@ -286,12 +287,10 @@ private function validateAttrs(HtmlElement $el, Texy\Texy $texy): bool

private function parseAttributes(string $attrs): array
{
$matches = $res = [];
preg_match_all(
'#([a-z0-9\_:-]+)\s*(?:=\s*(\'[^\']*\'|"[^"]*"|[^\'"\s]+))?()#isu',
$res = [];
$matches = Regexp::matchAll(
$attrs,
$matches,
PREG_SET_ORDER,
'#([a-z0-9\_:-]+)\s*(?:=\s*(\'[^\']*\'|"[^"]*"|[^\'"\s]+))?()#isu',
);

foreach ($matches as $m) {
Expand Down
9 changes: 5 additions & 4 deletions src/Texy/Modules/LinkModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Texy\LineParser;
use Texy\Link;
use Texy\Patterns;
use Texy\Regexp;


/**
Expand Down Expand Up @@ -334,7 +335,7 @@ private function checkLink(Link $link): void
// special supported case
$link->URL = 'http://' . $link->URL;

} elseif (preg_match('#' . self::$EMAIL . '$#Au', $link->URL)) {
} elseif (Regexp::match($link->URL, '#' . self::$EMAIL . '$#Au')) {
// email
$link->URL = 'mailto:' . $link->URL;

Expand All @@ -352,17 +353,17 @@ private function checkLink(Link $link): void
*/
private function textualUrl(Link $link): string
{
if ($this->texy->obfuscateEmail && preg_match('#^' . self::$EMAIL . '$#u', $link->raw)) { // email
if ($this->texy->obfuscateEmail && Regexp::match($link->raw, '#^' . self::$EMAIL . '$#u')) { // email
return str_replace('@', '&#64;<!-- -->', $link->raw);
}

if ($this->shorten && preg_match('#^(https?://|ftp://|www\.|/)#i', $link->raw)) {
if ($this->shorten && Regexp::match($link->raw, '#^(https?://|ftp://|www\.|/)#i')) {
$raw = strncasecmp($link->raw, 'www.', 4) === 0
? 'none://' . $link->raw
: $link->raw;

// parse_url() in PHP damages UTF-8 - use regular expression
if (!preg_match('~^(?:(?P<scheme>[a-z]+):)?(?://(?P<host>[^/?#]+))?(?P<path>(?:/|^)(?!/)[^?#]*)?(?:\?(?P<query>[^#]*))?(?:#(?P<fragment>.*))?()$~', $raw, $parts)) {
if (!($parts = Regexp::match($raw, '~^(?:(?P<scheme>[a-z]+):)?(?://(?P<host>[^/?#]+))?(?P<path>(?:/|^)(?!/)[^?#]*)?(?:\?(?P<query>[^#]*))?(?:#(?P<fragment>.*))?()$~'))) {
return $link->raw;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Texy/Modules/ListModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Texy\HtmlElement;
use Texy\Modifier;
use Texy\Patterns;
use Texy\Regexp;


/**
Expand Down Expand Up @@ -91,7 +92,7 @@ public function patternList(BlockParser $parser, array $matches): ?HtmlElement

$bullet = $min = null;
foreach ($this->bullets as $type => $desc) {
if (preg_match('#' . $desc[0] . '#Au', $mBullet)) {
if (Regexp::match($mBullet, '#' . $desc[0] . '#Au')) {
$bullet = $desc[3] ?? $desc[0];
$min = isset($desc[3]) ? 2 : 1;
$el->setName($desc[1] ? 'ol' : 'ul');
Expand Down Expand Up @@ -151,7 +152,7 @@ public function patternDefList(BlockParser $parser, array $matches): HtmlElement

$bullet = null;
foreach ($this->bullets as $desc) {
if (preg_match('#' . $desc[0] . '#Au', $mBullet)) {
if (Regexp::match($mBullet, '#' . $desc[0] . '#Au')) {
$bullet = $desc[3] ?? $desc[0];
break;
}
Expand Down
8 changes: 3 additions & 5 deletions src/Texy/Modules/LongWordsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,12 @@ private function pattern(array $matches): string
return $mWord;
}

$chars = [];
preg_match_all(
'#[' . Texy\Patterns::MARK . ']+|.#u',
$chars = Texy\Regexp::matchAll(
$mWord,
$chars,
'#[' . Texy\Patterns::MARK . ']+|.#u',
);

$chars = $chars[0];
$chars = array_column($chars, 0);
if (count($chars) < $this->wordLimit) {
return $mWord;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Texy/Modules/ParagraphModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function __construct(Texy\Texy $texy)
public function process(Texy\BlockParser $parser, string $content, Texy\HtmlElement $el): void
{
$parts = $parser->isIndented()
? preg_split('#(\n(?!\ )|\n{2,})#', $content, -1, PREG_SPLIT_NO_EMPTY)
: preg_split('#(\n{2,})#', $content, -1, PREG_SPLIT_NO_EMPTY);
? Regexp::split($content, '#(\n(?!\ )|\n{2,})#', skipEmpty: true)
: Regexp::split($content, '#(\n{2,})#', skipEmpty: true);

foreach ($parts as $s) {
$s = trim($s);
Expand Down Expand Up @@ -90,7 +90,7 @@ private function solve(
// leave element p

// block contains text
} elseif (preg_match('#[^\s' . Texy\Patterns::MARK . ']#u', $content)) {
} elseif (Regexp::match($content, '#[^\s' . Texy\Patterns::MARK . ']#u')) {
// leave element p

// block contains only replaced element
Expand Down
5 changes: 3 additions & 2 deletions src/Texy/Modules/ScriptModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Texy\Modules;

use Texy;
use Texy\Regexp;


/**
Expand Down Expand Up @@ -51,11 +52,11 @@ public function pattern(Texy\LineParser $parser, array $matches): Texy\HtmlEleme
$raw = null;
$args = [];
// function (arg, arg, ...) or function: arg, arg
if ($matches = Texy\Regexp::match($cmd, '#^([a-z_][a-z0-9_-]*)\s*(?:\(([^()]*)\)|:(.*))$#iu')) {
if ($matches = Regexp::match($cmd, '#^([a-z_][a-z0-9_-]*)\s*(?:\(([^()]*)\)|:(.*))$#iu')) {
$cmd = $matches[1];
$raw = trim($matches[3] ?? $matches[2]);
if ($raw !== '') {
$args = preg_split('#\s*' . preg_quote($this->separator, '#') . '\s*#u', $raw);
$args = Regexp::split($raw, '#\s*' . Regexp::quote($this->separator) . '\s*#u');
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Texy/Regexp.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public static function replace(
}


public static function quote(string $s): string
{
return preg_quote($s, '#');
}


/** @internal */
public static function pcre(string $func, array $args)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Texy/Texy.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ final public function registerLinePattern(

final public function registerBlockPattern(callable $handler, string $pattern, string $name): void
{
// if (!preg_match('#(.)\^.*\$\1[a-z]*#is', $pattern)) die("Texy: Not a block pattern $name");
// if (!Regexp::match($pattern, '#(.)\^.*\$\1[a-z]*#is')) die("Texy: Not a block pattern $name");
if (!isset($this->allowed[$name])) {
$this->allowed[$name] = true;
}
Expand Down Expand Up @@ -519,8 +519,8 @@ final public function checkURL(string $URL, string $type): bool
{
// absolute URL with scheme? check scheme!
return empty($this->urlSchemeFilters[$type])
|| !preg_match('#\s*[a-z][a-z0-9+.-]{0,20}:#Ai', $URL) // http: | mailto:
|| preg_match($this->urlSchemeFilters[$type], $URL);
|| !Regexp::match($URL, '#\s*[a-z][a-z0-9+.-]{0,20}:#Ai') // http: | mailto:
|| Regexp::match($URL, $this->urlSchemeFilters[$type]);
}


Expand Down

0 comments on commit d628696

Please sign in to comment.