diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2017-02-03 07:29:59 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2017-02-03 07:29:59 -0500 |
commit | 1b757d383820b78b97e86f2902aa6c339bddeda3 (patch) | |
tree | 9dde322fb401a2655b52a37be77ac950a6a81093 /coverage/misc.py | |
parent | 179ffff7263c5e978a03366d86819b6e4c1f8714 (diff) | |
download | python-coveragepy-1b757d383820b78b97e86f2902aa6c339bddeda3.tar.gz |
Fiddle with, and test, format_lines
Diffstat (limited to 'coverage/misc.py')
-rw-r--r-- | coverage/misc.py | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/coverage/misc.py b/coverage/misc.py index 9c4843e..28aa3b0 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -110,23 +110,28 @@ def format_lines(statements, lines): For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and `lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14". + Both `lines` and `statements` can be any iterable. All of the elements of + `lines` must be in `statements`, and all of the values must be positive + integers. + """ - pairs = [] - i = 0 - j = 0 - start = None statements = sorted(statements) lines = sorted(lines) - while i < len(statements) and j < len(lines): - if statements[i] == lines[j]: - if start is None: - start = lines[j] - end = lines[j] - j += 1 + + pairs = [] + start = None + lidx = 0 + for stmt in statements: + if lidx >= len(lines): + break + if stmt == lines[lidx]: + lidx += 1 + if not start: + start = stmt + end = stmt elif start: pairs.append((start, end)) start = None - i += 1 if start: pairs.append((start, end)) ret = ', '.join(map(nice_pair, pairs)) |