From f00f23aaf23816591b26d076106856176459eed0 Mon Sep 17 00:00:00 2001 From: yggverse Date: Mon, 24 Jun 2024 21:53:56 +0300 Subject: [PATCH] make performance optimization, require parser interface --- src/Parser/Code.php | 48 +++++++++++++--------- src/Parser/Header.php | 56 ++++++++++++++----------- src/Parser/Link.php | 92 ++++++++++++++++++++++++------------------ src/Parser/Listing.php | 32 ++++++++------- src/Parser/Quote.php | 32 ++++++++------- 5 files changed, 148 insertions(+), 112 deletions(-) mode change 100644 => 100755 src/Parser/Code.php mode change 100644 => 100755 src/Parser/Header.php mode change 100644 => 100755 src/Parser/Link.php mode change 100644 => 100755 src/Parser/Listing.php mode change 100644 => 100755 src/Parser/Quote.php diff --git a/src/Parser/Code.php b/src/Parser/Code.php old mode 100644 new mode 100755 index 2b3973e..bd93101 --- a/src/Parser/Code.php +++ b/src/Parser/Code.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Yggverse\Gemtext\Parser; -class Code +class Code implements \Yggverse\Gemtext\Interface\Parser { public static function match( string $line, @@ -37,23 +37,27 @@ class Code } public static function getAlt( - string $line + string $line, + array $matches = [] ): ?string { - $matches = []; - - if (self::match($line, $matches)) + if (!$matches) { - if (isset($matches['alt'])) - { - $alt = trim( - $matches['alt'] - ); + self::match( + $line, + $matches + ); + } - if ($alt) - { - return $alt; - } + if (isset($matches['alt'])) + { + $alt = trim( + $matches['alt'] + ); + + if ($alt) + { + return $alt; } } @@ -61,16 +65,20 @@ class Code } public static function isInline( - string $line + string $line, + array $matches = [] ): bool { - $matches = []; - - if (self::match($line, $matches)) + if (!$matches) { - return isset($matches['close']); + self::match( + $line, + $matches + ); } - return false; + return isset( + $matches['close'] + ); } } \ No newline at end of file diff --git a/src/Parser/Header.php b/src/Parser/Header.php old mode 100644 new mode 100755 index 89e9c0e..69eec15 --- a/src/Parser/Header.php +++ b/src/Parser/Header.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Yggverse\Gemtext\Parser; -class Header +class Header implements \Yggverse\Gemtext\Interface\Parser { public static function match( string $line, @@ -19,42 +19,50 @@ class Header } public static function getLevel( - string $line + string $line, + array $matches = [] ): ?int { - $matches = []; - - if (self::match($line, $matches)) + if (!$matches) { - if (isset($matches['level'])) - { - return (int) strlen( - $matches['level'] - ); - } + self::match( + $line, + $matches + ); + } + + if (isset($matches['level'])) + { + return (int) strlen( + $matches['level'] + ); } return null; } public static function getText( - string $line + string $line, + array $matches = [] ): ?string { - $matches = []; - - if (self::match($line, $matches)) + if (!$matches) { - if (isset($matches['text'])) - { - $text = trim( - $matches['text'] - ); + self::match( + $line, + $matches + ); + } - if ($text) - { - return $text; - } + if (isset($matches['text'])) + { + $text = trim( + $matches['text'] + ); + + if ($text) + { + return $text; } } diff --git a/src/Parser/Link.php b/src/Parser/Link.php old mode 100644 new mode 100755 index b0b20c4..a26443d --- a/src/Parser/Link.php +++ b/src/Parser/Link.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Yggverse\Gemtext\Parser; -class Link +class Link implements \Yggverse\Gemtext\Interface\Parser { public static function match( string $line, @@ -19,23 +19,27 @@ class Link } public static function getAddress( - string $line + string $line, + array $matches = [] ): ?string { - $matches = []; - - if (self::match($line, $matches)) + if (!$matches) { - if (isset($matches['address'])) - { - $address = trim( - $matches['address'] - ); + self::match( + $line, + $matches + ); + } - if ($address) - { - return $address; - } + if (isset($matches['address'])) + { + $address = trim( + $matches['address'] + ); + + if ($address) + { + return $address; } } @@ -43,23 +47,27 @@ class Link } public static function getDate( - string $line + string $line, + array $matches = [] ): ?string { - $matches = []; - - if (self::match($line, $matches)) + if (!$matches) { - if (isset($matches['date'])) - { - $date = trim( - $matches['date'] - ); + self::match( + $line, + $matches + ); + } - if ($date) - { - return $date; - } + if (isset($matches['date'])) + { + $date = trim( + $matches['date'] + ); + + if ($date) + { + return $date; } } @@ -67,23 +75,27 @@ class Link } public static function getAlt( - string $line + string $line, + array $matches = [] ): ?string { - $matches = []; - - if (self::match($line, $matches)) + if (!$matches) { - if (isset($matches['alt'])) - { - $alt = trim( - $matches['alt'] - ); + self::match( + $line, + $matches + ); + } - if ($alt) - { - return $alt; - } + if (isset($matches['alt'])) + { + $alt = trim( + $matches['alt'] + ); + + if ($alt) + { + return $alt; } } diff --git a/src/Parser/Listing.php b/src/Parser/Listing.php old mode 100644 new mode 100755 index 885c935..19b433e --- a/src/Parser/Listing.php +++ b/src/Parser/Listing.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Yggverse\Gemtext\Parser; -class Listing +class Listing implements \Yggverse\Gemtext\Interface\Parser { public static function match( string $line, @@ -19,23 +19,27 @@ class Listing } public static function getItem( - string $line + string $line, + array $matches = [] ): ?string { - $matches = []; - - if (self::match($line, $matches)) + if (!$matches) { - if (isset($matches['item'])) - { - $item = trim( - $matches['item'] - ); + self::match( + $line, + $matches + ); + } - if ($item) - { - return $item; - } + if (isset($matches['item'])) + { + $item = trim( + $matches['item'] + ); + + if ($item) + { + return $item; } } diff --git a/src/Parser/Quote.php b/src/Parser/Quote.php old mode 100644 new mode 100755 index 69e150e..a369169 --- a/src/Parser/Quote.php +++ b/src/Parser/Quote.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Yggverse\Gemtext\Parser; -class Quote +class Quote implements \Yggverse\Gemtext\Interface\Parser { public static function match( string $line, @@ -19,23 +19,27 @@ class Quote } public static function getText( - string $line + string $line, + array $matches = [] ): ?string { - $matches = []; - - if (self::match($line, $matches)) + if (!$matches) { - if (isset($matches['text'])) - { - $text = trim( - $matches['text'] - ); + self::match( + $line, + $matches + ); + } - if ($text) - { - return $text; - } + if (isset($matches['text'])) + { + $text = trim( + $matches['text'] + ); + + if ($text) + { + return $text; } }