From 3afeee3b19e44c77778cf402bd4ec55d931444d7 Mon Sep 17 00:00:00 2001 From: Emanuil <4thmail@gmail.com> Date: Tue, 3 Sep 2013 00:14:04 +0300 Subject: [PATCH] parse * and _ emphasis types separately to optimize performance and improve readability --- Parsedown.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 701a204..b58b7de 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -616,13 +616,19 @@ class Parsedown $index ++; } } - - if (strpos($text, '*') !== FALSE or strpos($text, '_') !== FALSE) + + if (strpos($text, '_') !== FALSE) { - $text = preg_replace('/(\*\*|__)(.+?[*_]*)(?<=\S)\1/', '$2', $text); - $text = preg_replace('/(\*|_)(.+?)(?<=\S)\1/', '$2', $text); + $text = preg_replace('/__(?=\S)(.+?)(?<=\S)__/', '$1', $text); + $text = preg_replace('/_(?=\S)(.+?)(?<=\S)_/', '$1', $text); } - + + if (strpos($text, '*') !== FALSE) + { + $text = preg_replace('/\*\*(?=\S)(.+?)(?<=\S)\*\*/', '$1', $text); + $text = preg_replace('/\*(?=\S)(.+?)(?<=\S)\*/', '$1', $text); + } + $text = strtr($text, $map); return $text;