From 7a7a93d7abc710f11c93a6f0a4edbea7cb0c47e5 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 18 Jun 2019 21:50:50 -0400 Subject: Move arcz_to_arcs to misc so we can use it elsewhere --- coverage/misc.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'coverage/misc.py') diff --git a/coverage/misc.py b/coverage/misc.py index d87a7b89..ad7b834f 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -281,6 +281,45 @@ def substitute_variables(text, variables=os.environ): return text +# Map chars to numbers for arcz_to_arcs +_arcz_map = {'.': -1} +_arcz_map.update(dict((c, ord(c) - ord('0')) for c in '123456789')) +_arcz_map.update(dict( + (c, 10 + ord(c) - ord('A')) for c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +)) + +def arcz_to_arcs(arcz): + """Convert a compact textual representation of arcs to a list of pairs. + + The text has space-separated pairs of letters. Period is -1, 1-9 are + 1-9, A-Z are 10 through 36. The resulting list is sorted regardless of + the order of the input pairs. + + ".1 12 2." --> [(-1,1), (1,2), (2,-1)] + + Minus signs can be included in the pairs: + + "-11, 12, 2-5" --> [(-1,1), (1,2), (2,-5)] + + """ + arcs = [] + for pair in arcz.split(): + asgn = bsgn = 1 + if len(pair) == 2: + a, b = pair + else: + assert len(pair) == 3 + if pair[0] == '-': + _, a, b = pair + asgn = -1 + else: + assert pair[1] == '-' + a, _, b = pair + bsgn = -1 + arcs.append((asgn * _arcz_map[a], bsgn * _arcz_map[b])) + return sorted(arcs) + + class BaseCoverageException(Exception): """The base of all Coverage exceptions.""" pass -- cgit v1.2.1