Permalink
Browse files

Increased performance of em and strong by preparing regular expressio…

…ns in advance.
  • Loading branch information...
1 parent 1cd4dcc commit bb3ebbba457f7cec912da47c2eab453880b6d6b5 @michelf committed Jun 21, 2008
Showing with 23 additions and 8 deletions.
  1. +23 −8 markdown.php
View
@@ -220,6 +220,7 @@ function Markdown_Parser() {
# Constructor function. Initialize appropriate member variables.
#
$this->_initDetab();
+ $this->prepareItalicsAndBold();
$this->nested_brackets_re =
str_repeat('(?>[^\[\]]+|\[', $this->nested_brackets_depth).
@@ -1116,6 +1117,26 @@ function makeCodeSpan($code) {
'***' => '(?<=\S)(?<!\*)\*\*\*(?!\*)',
'___' => '(?<=\S)(?<!_)___(?!_)',
);
+ var $em_strong_prepared_relist;
+
+ function prepareItalicsAndBold() {
+ #
+ # Prepare regular expressions for seraching emphasis tokens in any
+ # context.
+ #
+ foreach ($this->em_relist as $em => $expression) {
+ foreach ($this->strong_relist as $strong => $expression) {
+ if (isset($this->em_strong_relist["$em$strong"])) {
+ $token_relist[] = $this->em_strong_relist["$em$strong"];
+ }
+ $token_relist[] = $this->strong_relist[$strong];
+ $token_relist[] = $this->em_relist[$em];
+ $token_re = '{('. implode('|', $token_relist) .')}';
+
+ $this->em_strong_prepared_relist["$em$strong"] = $token_re;
+ }
+ }
+ }
function doItalicsAndBold($text) {
$token_stack = array('');
@@ -1126,16 +1147,10 @@ function doItalicsAndBold($text) {
while (1) {
#
- # Create regular expression for seraching emphasis tokens
+ # Get prepared regular expression for seraching emphasis tokens
# in current context.
#
- $token_relist = array();
- if (isset($this->em_strong_relist["$em$strong"])) {
- $token_relist[] = $this->em_strong_relist["$em$strong"];
- }
- $token_relist[] = $this->strong_relist[$strong];
- $token_relist[] = $this->em_relist[$em];
- $token_re = '{('. implode('|', $token_relist) .')}';
+ $token_re = $this->em_strong_prepared_relist["$em$strong"];
#
# Each loop iteration seach for the next emphasis token.

0 comments on commit bb3ebbb

Please sign in to comment.