Commit a1c92065 authored by sparonuz's avatar sparonuz
Browse files

More readable, removed a lot of useless workaround

parent 9f1b80b5
......@@ -4,6 +4,7 @@ import warnings
from os.path import isfile, join, dirname, abspath
from AutoRPE.UtilsRPE.Error import PointerTarged, ProcedureNotFound, MemberNotFound
import AutoRPE.UtilsRPE.precision_module as precision_module
import itertools
# We have a problem with the working precision, which can be different in different moments
# i.e. double when implementing emulator, single when modifying code for final implementation
......@@ -2368,31 +2369,31 @@ def replace_variable_exact_match(piece_to_replace, replacement, str):
piece_to_replace = piece_to_replace + "\\b(?!%val)"
piece_to_replace = piece_to_replace + "\\b(?! *\()"
# Replace all occurence outside string
# Before performing the substitution remove strings to avoid substitution inside them
mask = literal_mask(str)
new_string = ""
literal_part = []
literal_substitution = []
literal_to_substitute = ["##", "$$"]
n_of_literal = -1
outside_str = True
# Transforms the string in a list of char (strings are immutable and cannot be modified)
str = list(itertools.chain.from_iterable([list(piece) for piece in list(str)]))
for index, i in enumerate(mask):
if not i:
new_string += str[index]
outside_str = True
else:
if outside_str:
if i:
# A new literal is beginning, add a list to store its value
if index == 0 or not mask[index - 1]:
n_of_literal += 1
literal_substitution.append([])
literal_part.append([])
outside_str = False
# The char is stored for late substitution
literal_part[n_of_literal].append(str[index])
new_string += literal_to_substitute[n_of_literal % 2]
literal_substitution[n_of_literal].append(literal_to_substitute[n_of_literal % 2])
# The char is overwrite with a placeholder
str[index] = "#"
# Reconvert the list to string and substitute the fix
str = "".join(str)
str = re.sub(piece_to_replace, replacement, str)
str = re.sub(piece_to_replace, replacement, new_string)
# Substitutes one at a time the placeholder for the char
for index, substitution in enumerate(literal_part):
str = str.replace("".join(literal_substitution[index]), "".join(literal_part[index]), 1)
str = re.sub("#+", "".join(literal_part[index]), str, 1)
return str
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment