function testExpand($input, $expected) { global $quiet; $desc = "Expansion of \"{$input}\" - "; if (!$quiet) { echo $desc; } if (tab_expand($input) != $expected) { if ($quiet) { echo $desc; } echo "FAILED\n"; return false; } else { if (!$quiet) { echo "SUCCESS\n"; } } $desc = "Collapse of \"{$expected}\" - "; if (!$quiet) { echo $desc; } if (tab_collapse($expected, 8) != $input) { if ($quiet) { echo $desc; } echo "FAILED\n"; return false; } else { if (!$quiet) { echo "SUCCESS\n"; } } return true; }
function tab_unexpand($text) { $tab_stop = 8; $lines = explode("\n", $text); foreach ($lines as $i => $line) { // Expand any tabs to spaces $line = tab_expand($line); $chunks = str_split($line, $tab_stop); $chunkCount = count($chunks); // Scan all but the last chunk for ($j = 0; $j < $chunkCount - 1; $j++) { $chunks[$j] = preg_replace('/ {2,}$/', "\t", $chunks[$j]); } // If the last chunk is a tab-stop's worth of spaces // convert it to a tab; Otherwise, leave it alone if ($chunks[$chunkCount - 1] == str_repeat(' ', $tab_stop)) { $chunks[$chunkCount - 1] = "\t"; } // Recombine the chunks $lines[$i] = implode('', $chunks); } // Recombine the lines return implode("\n", $lines); }
function tab_unexpand($text) { $tab_stop = 8; $lines = explode("\n", $text); foreach ($lines as $i => $line) { // 将制表符扩展为空格 $line = tab_expand($line); $chunks = str_split($line, $tab_stop); $chunkCount = count($chunks); // 扫描最后一个字符块以外的所有其他字符块 for ($j = 0; $j < $chunkCount - 1; $j++) { $chunks[$j] = preg_replace('/\\s{2,}$/', "\t", $chunks[$j]); } // 如果最后一个字符块是相当于制表符的空格 // 将它转换为一个制表符;否则,保持不变 if ($chunks[$chunkCount - 1] == str_repeat(' ', $tab_stop)) { $chunks[$chunkCount - 1] = "\t"; } // 重新组合字符块 $line[$i] = implode('', $chunks); } // 重新组合文本行 return implode('<br />', $lines); }
<?php function tab_expand($text) { while (strstr($text, "\t")) { $text = preg_replace_callback('/^([^\\t\\n]*)(\\t+)/m', 'tab_expand_helper', $text); } return $text; } function tab_expand_helper($matches) { $tab_stop = 8; return $matches[1] . str_repeat(' ', strlen($matches[2]) * $tab_stop - strlen($matches[1]) % $tab_stop); } $spaced = tab_expand($obj->message);
<?php include_once "dtect/arcanist/dgbarcanist/linter/TabTools.php"; $myFile = $argv[1]; $fh = fopen($myFile, 'r'); $theData = fread($fh, 50000); fclose($fh); echo tab_expand($theData);
<?php function tab_expand($text) { while (strstr($text, "\t")) { $text = preg_replace_callback('@^([^\\t\\n]*)(\\t+)@m', 'tab_expand_helper', $text); } return $text; } function tab_expand_helper($matchs) { $tab_stop = 8; return $matchs[1] . str_repeat(' ', strlen($matchs[2]) * $tab_stop - strlen($matchs[1]) % $tab_stop); } $str = 'abc def\\t'; echo $str; $spaced = tab_expand($str); echo $spaced; echo PHP_EOL; echo PHP_EOL; echo PHP_EOL;
protected function lintSpaceAlignment($path) { $lines = explode("\n", $this->getData($path)); $change = false; foreach ($lines as $line_idx => $line) { $expanded = tab_expand($line); $changedline = tab_collapse($expanded, 8); if (strcmp($line, $changedline)) { $this->raiseLintAtLine($line_idx + 1, 1, self::LINT_SPACE_ALIGNMENT, 'This line uses spaces in stead of tabs', $line, $changedline); $change = true; } } if ($change) { $this->nrautofixes++; } }