Skip to content
Browse files

MDL-33635 improve collatorlib::ksort() coding style and perf

Thanks Eloy Lafuente for spotting my sloppy coding style!
  • Loading branch information...
1 parent b71d0b4 commit bc53b4e9d70bff702655ca4ac14fff31b6fce2f3 @skodak committed Jun 13, 2012
Showing with 15 additions and 4 deletions.
  1. +14 −0 lib/tests/textlib_test.php
  2. +1 −4 lib/textlib.class.php
View
14 lib/tests/textlib_test.php
@@ -476,6 +476,13 @@ public function test_asort() {
$this->assertSame(array_values($arr), array('aa', 'ab', 'áb', 'Áb', 'cc'), $this->error);
$this->assertSame(array_keys($arr), array(1, 'b', 'a', 'x', 0), $this->error);
$this->assertTrue($result);
+
+ $a = array(2=>'b', 1=>'c');
+ $c =& $a;
+ $b =& $a;
+ collatorlib::asort($b);
+ $this->assertSame($a, $b);
+ $this->assertSame($c, $b);
}
/**
@@ -565,6 +572,13 @@ public function test_ksort() {
$this->assertSame(array_keys($arr), array('1.1.1', '1.2', '1.20.2'));
$this->assertSame(array_values($arr), array(array(), $obj, null));
$this->assertTrue($result);
+
+ $a = array(2=>'b', 1=>'c');
+ $c =& $a;
+ $b =& $a;
+ collatorlib::ksort($b);
+ $this->assertSame($a, $b);
+ $this->assertSame($c, $b);
}
}
View
5 lib/textlib.class.php
@@ -872,10 +872,7 @@ public static function ksort(array &$arr, $sortflag = collatorlib::SORT_STRING)
}
// This is a bit slow, but we need to keep the references
$original = $arr;
- $count = count($arr);
- for($i=0; $i<$count; $i++) {
- array_pop($arr);
- }
+ $arr = array(); // Surprisingly this does not break references outside
foreach ($keys as $key) {
$arr[$key] = $original[$key];
}

0 comments on commit bc53b4e

Please sign in to comment.
Something went wrong with that request. Please try again.