Index: Tools/AutoSheriff/string_helpers.py |
diff --git a/Tools/AutoSheriff/string_helpers.py b/Tools/AutoSheriff/string_helpers.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f2393fb40040c3fd82dd2ee1437702503ca33f4e |
--- /dev/null |
+++ b/Tools/AutoSheriff/string_helpers.py |
@@ -0,0 +1,20 @@ |
+# Copyright 2014 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import itertools |
+ |
+# http://stackoverflow.com/questions/9470611/how-to-do-an-inverse-range-i-e-create-a-compact-range-based-on-a-set-of-numb/9471386#9471386 |
+def re_range(lst): |
+ def sub(x): |
+ return x[1] - x[0] |
+ |
+ ranges = [] |
+ for k, iterable in itertools.groupby(enumerate(sorted(lst)), sub): |
+ rng = list(iterable) |
+ if len(rng) == 1: |
+ s = str(rng[0][1]) |
+ else: |
+ s = "%s-%s" % (rng[0][1], rng[-1][1]) |
+ ranges.append(s) |
+ return ', '.join(ranges) |