Advertisement
martixy

Text preprocessor

Jun 28th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function format(string $format, array $tokens): string
  2. {
  3.     $prev = '';
  4.  
  5.     $tag = '';
  6.     $nextCond = [];
  7.     $context = 'out'; // out, start, end, in
  8.  
  9.     $escapees = '{?}';
  10.     $result = '';
  11.     $escaped = $prevEscaped = false;
  12.  
  13.     //Lotta spaghetti... There's gotta be a way to simplify the conditions. Right?
  14.     for ($i = 0; $i < strlen($format); $i++) {
  15.         $ch = $format[$i];
  16.         $prevEscaped = $escaped;
  17.         if (strpos($escapees, $ch) !== false && $i>0 && $format[$i-1] === '\\') {
  18.             $escaped = true;
  19.         } else {
  20.             $escaped = false;
  21.             if ($ch === '\\' && strlen($format) > $i+1 && strpos($escapees, $format[$i+1]) !== false) continue;
  22.         }
  23.  
  24.         if ($context === 'start') {
  25.             if ($ch === '{') $result .= '{';
  26.             else $context = 'in';
  27.         }
  28.  
  29.         if ($format[$i] === '{' && !$escaped && $context === 'out') {
  30.             $context = 'start';
  31.             continue;
  32.         } elseif ($format[$i] === '}' && !$escaped && ($context === 'in' || $context === 'inc')) {
  33.             $context = 'end';
  34.         }
  35.  
  36.         if ($context === 'end') {
  37.             if ($format[$i-1] === '?' && !$prevEscaped) {
  38.                 $nextCond[] = $tag;
  39.                 $nextCond[] = '';
  40.             } else {
  41.                 foreach ($nextCond as $item) {
  42.                     if ($item[-1] === '?') {
  43.                         if (array_key_exists($tag, $tokens) && !empty($tokens[$tag])) {
  44.                             if ($item[0] === '?') {
  45.                                 if (array_key_exists($prev, $tokens) && !empty($tokens[$prev]))
  46.                                     $result .= substr($item, 1, strlen($item)-1);
  47.                             }
  48.                             else $result .= substr($item, 0, strlen($item)-1);
  49.                         }
  50.                     }
  51.                     else $result .= $item;
  52.                 }
  53.                 $nextCond = [];
  54.             }
  55.             if ($tag[0] !== '?' && $tag[-1] !== '?') {
  56.                 if (array_key_exists($tag, $tokens))
  57.                     $result .= $tokens[$tag];
  58.                 $prev = $tag;
  59.             } elseif ($tag[0] === '?' && ($tag[-1] !== '?' || $prevEscaped) && array_key_exists($prev, $tokens) && !empty($tokens[$prev])) {
  60.                 $result .= substr($tag, 1);
  61.             }
  62.             $context = 'out';
  63.             $tag = '';
  64.             continue;
  65.         }
  66.  
  67.         if ($context === 'in') {
  68.             $tag .= $format[$i];
  69.         }
  70.  
  71.         if ($context === 'out') {
  72.             if (!empty($nextCond))
  73.                 $nextCond[count($nextCond)-1] .= $ch;
  74.             else $result .= $ch;
  75.         }
  76.     }
  77.     return $result;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement