Skip to content
Permalink
Browse files

Dumper: added truncateString()

  • Loading branch information...
dg committed Sep 13, 2019
1 parent 0e0cb2e commit 364de8317454acf7fe62b82a3197795fba7e9e5d
Showing with 64 additions and 29 deletions.
  1. +35 −29 src/Tracy/Dumper/Dumper.php
  2. +29 −0 tests/Tracy/Dumper.truncateString(().phpt
@@ -504,46 +504,52 @@ public static function formatSnapshotAttribute(array &$snapshot): string
*/
public static function encodeString(string $s, int $maxLength = null): string
{
static $table;
if ($table === null) {
foreach (array_merge(range("\x00", "\x1F"), range("\x7F", "\xFF")) as $ch) {
$table[$ch] = '\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT);
}
$table['\\'] = '\\\\';
$table["\r"] = '\r';
$table["\n"] = '\n';
$table["\t"] = '\t';
}
if ($maxLength && strlen($s) > $maxLength) { // shortens to $maxLength in UTF-8 or longer
if (function_exists('mb_substr')) {
$s = mb_substr($tmp = $s, 0, $maxLength, 'UTF-8');
$shortened = $s !== $tmp;
} else {
$i = $len = 0;
$maxI = $maxLength * 4; // max UTF-8 length
do {
if (($s[$i] < "\x80" || $s[$i] >= "\xC0") && (++$len > $maxLength) || $i >= $maxI) {
$s = substr($s, 0, $i);
$shortened = true;
break;
}
} while (isset($s[++$i]));
}
if ($maxLength) {
$s = self::truncateString($tmp = $s, $maxLength);
$shortened = $s !== $tmp;
}
if (preg_match('#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u', $s) || preg_last_error()) { // is binary?
if ($maxLength && strlen($s) > $maxLength) {
$s = substr($s, 0, $maxLength);
$shortened = true;
static $table;
if ($table === null) {
foreach (array_merge(range("\x00", "\x1F"), range("\x7F", "\xFF")) as $ch) {
$table[$ch] = '\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT);
}
$table['\\'] = '\\\\';
$table["\r"] = '\r';
$table["\n"] = '\n';
$table["\t"] = '\t';
}
$s = strtr($s, $table);
}
return $s . (empty($shortened) ? '' : ' ... ');
}
/**
* @internal
*/
public static function truncateString(string $s, int $maxLength): string
{
if (!preg_match('##u', $s)) {
$s = substr($s, 0, $maxLength); // not UTF-8
} elseif (function_exists('mb_substr')) {
$s = mb_substr($s, 0, $maxLength, 'UTF-8');
} else {
$i = $len = 0;
do {
if (($s[$i] < "\x80" || $s[$i] >= "\xC0") && (++$len > $maxLength)) {
$s = substr($s, 0, $i);
break;
}
} while (isset($s[++$i]));
}
return $s;
}
/**
* @param int|string $k
* @return int|string
@@ -0,0 +1,29 @@
<?php
/**
* Test: Tracy\Dumper::truncateString()
*/
declare(strict_types=1);
use Tester\Assert;
use Tracy\Dumper;
require __DIR__ . '/../bootstrap.php';
Assert::same('', Dumper::truncateString('', 1));
Assert::same('h', Dumper::truncateString('hello', 1));
Assert::same('hello', Dumper::truncateString('hello', 5));
Assert::same('hello', Dumper::truncateString('hello', 6));
Assert::same('Iñtërnâtiônàlizætiøn', Dumper::truncateString('Iñtërnâtiônàlizætiøn', 20));
Assert::same('Iñtër', Dumper::truncateString('Iñtërnâtiônàlizætiøn', 5));
Assert::same("\x00", Dumper::truncateString("\x00\x01", 1));
Assert::same("\x00\x01", Dumper::truncateString("\x00\x01", 5));
Assert::same('bad', Dumper::truncateString("bad\xff", 3));
Assert::same("bad\xff", Dumper::truncateString("bad\xff", 4));
Assert::same("bad\xff", Dumper::truncateString("bad\xff", 5));

0 comments on commit 364de83

Please sign in to comment.
You can’t perform that action at this time.