[PATCH] imaputil: make uid_sequence sort all items to improve collapsing

Sebastian Spaeth Sebastian at SSpaeth.de
Tue Aug 30 08:22:33 BST 2011


To preserve previous behavior and save a few CPU cycles, we were not
sorting UID lists and only collapsed them if they were alreay sorted.
Vincent Beffara <vbeffara at ens-lyon.fr> pointed out that this is not
always the case and unsorted lists lead to non-optimally collapsing.

Force lists to numeric types and sort them before collapsing.

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
Vincent, is that what you had in mind?

 offlineimap/imaputil.py |   12 +++++++-----
 1 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/offlineimap/imaputil.py b/offlineimap/imaputil.py
index 94d6ffc..e628df5 100644
--- a/offlineimap/imaputil.py
+++ b/offlineimap/imaputil.py
@@ -191,9 +191,10 @@ def flagsmaildir2imap(maildirflaglist):
 def uid_sequence(uidlist):
     """Collapse UID lists into shorter sequence sets
 
-    [1,2,3,4,5,10,12,13] will return "1:5,10,12:13".  This function does
-    not sort the list, and only collapses if subsequent entries form a
-    range.
+    [1,2,3,4,5,10,12,13] will return "1:5,10,12:13".  This function
+    converts items to numeric type and sorts the list to always produce
+    the minimal collapsed set.
+
     :returns: The collapsed UID list as string"""
     def getrange(start, end):
         if start == end:
@@ -203,9 +204,10 @@ def uid_sequence(uidlist):
     if not len(uidlist): return '' # Empty list, return
     start, end = None, None
     retval = []
+    # Force items to be longs and sort them
+    sorted_uids = sorted(map(int, uidlist))
 
-    for item in iter(uidlist):
-        item = int(item)
+    for item in iter(sorted_uids):
         if start == None:     # First item
             start, end = item, item
         elif item == end + 1: # Next item in a range
-- 
1.7.4.1





More information about the OfflineIMAP-project mailing list