Skip to content
Snippets Groups Projects
Commit 742f0a22 authored by Campbell Barton's avatar Campbell Barton
Browse files

code_clean: add 'use_elem_macro' edit

parent a2a502d2
No related branches found
No related tags found
No related merge requests found
......@@ -247,6 +247,70 @@ def edit_list_from_file__use_zero_before_float_suffix(_source, data):
return edits
def edit_list_from_file__use_elem_macro(_source, data):
edits = []
from itertools import combinations
# Replace:
# (a == b || a == c)
# (a != b && a != c)
# With:
# (ELEM(a, b, c))
# (!ELEM(a, b, c))
test_equal = (
r'[\(]*'
r'([^\|\(\)]+)' # group 1 (no (|))
r'\s+==\s+'
r'([^\|\(\)]+)' # group 2 (no (|))
r'[\)]*'
)
test_not_equal = (
r'[\(]*'
r'([^\|\(\)]+)' # group 1 (no (|))
r'\s+!=\s+'
r'([^\|\(\)]+)' # group 2 (no (|))
r'[\)]*'
)
for is_equal in (True, False):
for n in reversed(range(64)):
if is_equal:
re_str = r'\(' + r'\s+\|\|\s+'.join([test_equal] * n) + r'\)'
else:
re_str = r'\(' + r'\s+\&\&\s+'.join([test_not_equal] * n) + r'\)'
for match in re.finditer(re_str, data):
var = match.group(1)
var_rest = []
groups = match.groups()
pairs = [(groups[i * 2], groups[i * 2 + 1]) for i in range(len(groups) // 2)]
found = True
for a, b in pairs:
# Unlikely but possible the checks are swapped.
if b == var and a != var:
a, b = b, a
if a != var:
found = False
break
var_rest.append(b)
if found:
edits.append((
match.span(),
'(%sELEM(%s, %s))' % (
('' if is_equal else '!'),
var,
', '.join(var_rest),
),
'__ALWAYS_FAIL__',
))
return edits
def edit_list_from_file__use_const_vars(_source, data):
edits = []
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment