コード例 #1
0
ファイル: geshi.php プロジェクト: akoehn/wikireader
 /**
  * this function creates the appropriate regexp string of an token array
  * you should not call this function directly, @see GeSHi::optimize_regexp_list().
  *
  * @param &$tokens array of tokens
  * @param $recursed bool to know wether we recursed or not
  * @return string
  * @author Milian Wolff <*****@*****.**>
  * @since 1.0.8
  */
 function _optimize_regexp_list_tokens_to_string(&$tokens, $recursed = false)
 {
     $list = '';
     foreach ($tokens as $token => $sub_tokens) {
         $list .= $token;
         $close_entry = isset($sub_tokens['']);
         unset($sub_tokens['']);
         if (!empty($sub_tokens)) {
             $list .= '(?:' . GeSHi::_optimize_regexp_list_tokens_to_string($sub_tokens, true) . ')';
             if ($close_entry) {
                 // make sub_tokens optional
                 $list .= '?';
             }
         }
         $list .= '|';
     }
     if (!$recursed) {
         // do some optimizations
         // common trailing strings
         // BUGGY!
         //$list = preg_replace_callback('#(?<=^|\:|\|)\w+?(\w+)(?:\|.+\1)+(?=\|)#', create_function(
         //    '$matches', 'return "(?:" . preg_replace("#" . preg_quote($matches[1], "#") . "(?=\||$)#", "", $matches[0]) . ")" . $matches[1];'), $list);
         // (?:p)? => p?
         $list = preg_replace('#\\(\\?\\:(.)\\)\\?#', '\\1?', $list);
         // (?:a|b|c|d|...)? => [abcd...]?
         // TODO: a|bb|c => [ac]|bb
         static $callback_2;
         if (!isset($callback_2)) {
             $callback_2 = create_function('$matches', 'return "[" . str_replace("|", "", $matches[1]) . "]";');
         }
         $list = preg_replace_callback('#\\(\\?\\:((?:.\\|)+.)\\)#', $callback_2, $list);
     }
     // return $list without trailing pipe
     return substr($list, 0, -1);
 }