<?php

function c( $text ) {}  

/* 
*/   
function bw_get_elapsed( $tods ) {
  $tode = microtime( );
  //e( $tode );
  list( $su, $ss ) = explode(" ", $tods );
  list( $eu, $es ) = explode(" ", $tode );
  $elapsedsec  = $es - $ss;
  $elapsedu = $eu - $su;
  
  $elapsed = $elapsedsec + $elapsedu;
  
  //e( $elapsedsec );
  //e( $elapsedu );
  //e( $elapsed );
  return( $elapsed );  
}


function oik_error_reporting() {

  //error_reporting( E_ALL );  
  error_reporting( E_ALL & ~E_DEPRECATED );
  @ini_set('display_errors',1);
}

if ( !function_exists( "add_filter" ) ) {
  function add_filter( $arg1=null, $arg2=null ) {}
}


function bw_average( $elapsed, $count ) { 
  if ( $count ) {
    $average = $elapsed / $count ;
    $av = sprintf( "%1.6F", $average );
    c( $average );
    c( $av );
  } else {
    $av = $elapsed;
  }     
  return( $av );
}  

/**
 * Include the original shortcodes.php from WordPress
 * THE fixed code is in get_shortcode_17567
*/ 

require_once "shortcodes.php" ;



function dofor( $func = 'test_bag', $limit=1000 ) {
  global $p, $dome, $count ;
  $tods = microtime();
  
  for ( $i = 1; $i <= $limit; $i++ )
    $func();
  $elapsed = sprintf( "%2.6f", bw_get_elapsed( $tods ) ); 
  $average = sprintf( "%2.6f", bw_average( $elapsed, $limit ));
  //$p( "$func Ela: $elapsed Ave: $average Lim: $limit " . substr( $dome, 0, 40) );
  $codes = $count * 2; 
  $p( "$func,$elapsed,$average,$limit,$codes,". substr( $dome, 0, 41) );
} 


function wpsc( $atts =null ) {
  return( "wp" );
}


function test_data( $repeat, $text, $shortcode=null, $scount=0 ) {
 $string = "$text ";
 if ( $shortcode ) {
   for ( $count = 1; $count<= $scount; $count++ ) {
     $string .= "[$shortcode-$count] [$shortcode$count] ";
   }  
 } 
 $string = str_repeat( $string, $repeat );
 $totscs = $repeat * $scount * 2;
 $result = ",$repeat,$scount,$string";
 $result = strlen( $result ) . $result;
 return $result;
} 
 



function original() {
  global $dome;
  do_shortcode( $dome );
} 

 

$p = "q";
function q( $text ) {
echo "$text\n" ;
}


function do_hmo17567($content) {
        if ( false === strpos( $content, "[" ) ) {
          //gobang();  
          return $content; 
        }
	global $shortcode_tags;
        

	if (empty($shortcode_tags) || !is_array($shortcode_tags))
		return $content;
                
	$pattern = get_shortcode_regex();
	return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
}



function do_hmf17567($content) {
        
        if ( false === strpos( $content, "[" ) )
          return $content; 


	global $shortcode_tags;
	if (empty($shortcode_tags) || !is_array($shortcode_tags))
		return $content;
                
	$pattern = get_shortcode_17567();
	return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
}


function do_fix17567( $content ) { 
	global $shortcode_tags;

	if (empty($shortcode_tags) || !is_array($shortcode_tags))
		return $content;
	$pattern = get_shortcode_17567();
	return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
}


function get_shortcode_17567() {
	global $shortcode_tags;
	$tagnames = array_keys($shortcode_tags);
	$tagregexp = join( '|', array_map('preg_quote', $tagnames) );

	// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
        
	//	. '\\b'                              // Word boundary
	return                        
		  '\\['                              // Opening bracket
		. '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
		. "($tagregexp)"                     // 2: Shortcode name
                . '(?![\\w-])'                       // Word boundary 
		. '('                                // 3: Unroll the loop: Inside the opening shortcode tag
		.     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
		.     '(?:'
		.         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
		.         '[^\\]\\/]*'               // Not a closing bracket or forward slash
		.     ')*?'
		. ')'
		. '(?:'
		.     '(\\/)'                        // 4: Self closing tag ...
		.     '\\]'                          // ... and closing bracket
		. '|'
		.     '\\]'                          // Closing bracket
		.     '(?:'
		.         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
		.             '[^\\[]*+'             // Not an opening bracket
		.             '(?:'
		.                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
		.                 '[^\\[]*+'         // Not an opening bracket
		.             ')*+'
		.         ')'
		.         '\\[\\/\\2\\]'             // Closing shortcode tag
		.     ')?'
		. ')'
		. '(\\]?)';                          // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
}



 
function fix17567() {
  global $dome;
  do_fix17567( $dome );
}

function hmo17567() {
  global $dome;
  do_hmo17567( $dome );
}

function hmf17567() {
  global $dome;
  do_hmf17567( $dome );
}


oik_error_reporting();


$content = array();
$content[] = test_data( 1, "no shortcodes in the text." );
$content[] = test_data( 10, "no shortcodes in the text. " );
$content[] = test_data( 100, "no shortcodes in the text. " );
$content[] = test_data( 1000, "no shortcodes in the text. " );
$content[] = test_data( 2000, "code is poetry " );

$content[] = test_data( 1, "one shortcode", "wp", 1 );
$content[] = test_data( 1, "two shortcodes", "wp", 2 );
$content[] = test_data( 1, "three shorcodes", "wp", 3 );
$content[] = test_data( 100, "many [wp]'s " );
$content[] = test_data( 100, "many [[wp]]'s " );


$content[] = test_data( 1, "5 shortcodes in the text", "wp", 5 );
$content[] = test_data( 10, "10x5 shortcodes in the text", "wp", 5 );
$content[] = test_data( 5, "5x10 shortcodes in the text", "wp", 10 );
//$content[] = test_data( 100, "100x10 shortcodes in the text", "wp", 10 );
$content[] = test_data( 1, "100 shortcodes in the text", "wp", 100 );
//$content[] = test_data( 10, "10x100 shortcodes in the text", "wp", 100 );




add_shortcode( "wpdo", "wpsc" );



for ( $count = 0; $count <= 100; $count += 10 ) {
  add_shortcode( "wp-$count", "wpsc" );
  add_shortcode( "wp$count", "wpsc" );
  foreach ( $content as $dome ) {

    //echo $dome . PHP_EOL;

    dofor( "original", 1000 );
    dofor( "fix17567", 1000 );
    dofor( "hmo17567", 1000 );
    dofor( "hmf17567", 1000);
    //q(PHP_EOL);
  }
}

