array_shift is slowwwww, this approach is faster.
authorstekkel <stekkel@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Sun, 12 Feb 2006 15:48:11 +0000 (15:48 +0000)
committerstekkel <stekkel@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Sun, 12 Feb 2006 15:48:11 +0000 (15:48 +0000)
git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@10728 7612ce4b-ef26-0410-bec9-ea0150e637f0

functions/imap_messages.php

index c6eb5846d96d9da9dc473a6cd4226d1e7b60b888..f9e0e16e1dbac7b3fedb2e0505c9dcc3739b62e7 100755 (executable)
@@ -119,29 +119,34 @@ function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) {
 /**
  * Sort the message list and crunch to be as small as possible
  * (overflow could happen, so make it small if possible)
+ * @param array $aUid array with uid's
+ * @return string $s message set string
  */
-function sqimap_message_list_squisher($messages_array) {
-    if( !is_array( $messages_array ) ) {
-        return $messages_array;
-    }
-
-    sort($messages_array, SORT_NUMERIC);
-    $msgs_str = '';
-    while ($messages_array) {
-        $start = array_shift($messages_array);
-        $end = $start;
-        while (isset($messages_array[0]) && $messages_array[0] == $end + 1) {
-            $end = array_shift($messages_array);
-        }
-        if ($msgs_str != '') {
-            $msgs_str .= ',';
-        }
-        $msgs_str .= $start;
-        if ($start != $end) {
-            $msgs_str .= ':' . $end;
+function sqimap_message_list_squisher($aUid) {
+    if( !is_array( $aUid ) ) {
+        return $aUid;
+    }
+    sort($aUid, SORT_NUMERIC);
+
+    if (count($aUid)) {
+        $s = '';
+        for ($i=0,$iCnt=count($aUid);$i<$iCnt;++$i) {
+            $iStart = $aUid[$i];
+            $iEnd = $iStart;
+            while ($i<($iCnt-1) && $aUid[$i+1] == $iEnd +1) {
+                $iEnd = $aUid[$i+1];
+                ++$i;
+            }
+            if ($s) {
+                $s .= ',';
+            }
+            $s .= $iStart;
+            if ($iStart != $iEnd) {
+                $s .= ':' . $iEnd;
+            }
         }
     }
-    return $msgs_str;
+    return $s;
 }