Merge pull request #13511 from owncloud/naturalsort_speeeeeed
NaturalSort performance improvements
This commit is contained in:
commit
74de345c7f
|
@ -159,11 +159,10 @@ class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
|
||||||
* @return \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER
|
* @return \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER
|
||||||
*/
|
*/
|
||||||
public function getType() {
|
public function getType() {
|
||||||
if (isset($this->data['type'])) {
|
if (!isset($this->data['type'])) {
|
||||||
return $this->data['type'];
|
$this->data['type'] = ($this->getMimetype() === 'httpd/unix-directory') ? self::TYPE_FOLDER : self::TYPE_FILE;
|
||||||
} else {
|
|
||||||
return $this->getMimetype() === 'httpd/unix-directory' ? self::TYPE_FOLDER : self::TYPE_FILE;
|
|
||||||
}
|
}
|
||||||
|
return $this->data['type'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getData() {
|
public function getData() {
|
||||||
|
|
|
@ -12,6 +12,7 @@ namespace OC;
|
||||||
class NaturalSort {
|
class NaturalSort {
|
||||||
private static $instance;
|
private static $instance;
|
||||||
private $collator;
|
private $collator;
|
||||||
|
private $cache = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Split the given string in chunks of numbers and strings
|
* Split the given string in chunks of numbers and strings
|
||||||
|
@ -21,13 +22,15 @@ class NaturalSort {
|
||||||
private function naturalSortChunkify($t) {
|
private function naturalSortChunkify($t) {
|
||||||
// Adapted and ported to PHP from
|
// Adapted and ported to PHP from
|
||||||
// http://my.opera.com/GreyWyvern/blog/show.dml/1671288
|
// http://my.opera.com/GreyWyvern/blog/show.dml/1671288
|
||||||
|
if (isset($this->cache[$t])) {
|
||||||
|
return $this->cache[$t];
|
||||||
|
}
|
||||||
$tz = array();
|
$tz = array();
|
||||||
$x = 0;
|
$x = 0;
|
||||||
$y = -1;
|
$y = -1;
|
||||||
$n = null;
|
$n = null;
|
||||||
$length = strlen($t);
|
|
||||||
|
|
||||||
while ($x < $length) {
|
while (isset($t[$x])) {
|
||||||
$c = $t[$x];
|
$c = $t[$x];
|
||||||
// only include the dot in strings
|
// only include the dot in strings
|
||||||
$m = ((!$n && $c === '.') || ($c >= '0' && $c <= '9'));
|
$m = ((!$n && $c === '.') || ($c >= '0' && $c <= '9'));
|
||||||
|
@ -40,6 +43,7 @@ class NaturalSort {
|
||||||
$tz[$y] .= $c;
|
$tz[$y] .= $c;
|
||||||
$x++;
|
$x++;
|
||||||
}
|
}
|
||||||
|
$this->cache[$t] = $tz;
|
||||||
return $tz;
|
return $tz;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,14 +79,13 @@ class NaturalSort {
|
||||||
// instead of ["test.txt", "test (2).txt"]
|
// instead of ["test.txt", "test (2).txt"]
|
||||||
$aa = self::naturalSortChunkify($a);
|
$aa = self::naturalSortChunkify($a);
|
||||||
$bb = self::naturalSortChunkify($b);
|
$bb = self::naturalSortChunkify($b);
|
||||||
$alen = count($aa);
|
|
||||||
$blen = count($bb);
|
|
||||||
|
|
||||||
for ($x = 0; $x < $alen && $x < $blen; $x++) {
|
for ($x = 0; isset($aa[$x]) && isset($bb[$x]); $x++) {
|
||||||
$aChunk = $aa[$x];
|
$aChunk = $aa[$x];
|
||||||
$bChunk = $bb[$x];
|
$bChunk = $bb[$x];
|
||||||
if ($aChunk !== $bChunk) {
|
if ($aChunk !== $bChunk) {
|
||||||
if (is_numeric($aChunk) && is_numeric($bChunk)) {
|
// test first character (character comparison, not number comparison)
|
||||||
|
if ($aChunk[0] >= '0' && $aChunk[0] <= '9' && $bChunk[0] >= '0' && $bChunk[0] <= '9') {
|
||||||
$aNum = (int)$aChunk;
|
$aNum = (int)$aChunk;
|
||||||
$bNum = (int)$bChunk;
|
$bNum = (int)$bChunk;
|
||||||
return $aNum - $bNum;
|
return $aNum - $bNum;
|
||||||
|
@ -90,7 +93,7 @@ class NaturalSort {
|
||||||
return self::getCollator()->compare($aChunk, $bChunk);
|
return self::getCollator()->compare($aChunk, $bChunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $alen - $blen;
|
return count($aa) - count($bb);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue