So I have a file and I want to go line by and replace some words that are very similar and that actually some of them are substrings of others:
uint8=("UINT8 ","PIN_UINT8 ")
uint16=("UINT16 ","PIN_UINT16 ")
uint32=("UINT32 ","PIN_UINT32 ")
uint64=("UINT64 ","PIN_UINT64 ")
int8=("INT8 ","PIN_INT32 ")
int16=("INT16 ","PIN_INT16 ")
int32=("INT32 ","PIN_INT32 ")
int64=("INT64 ","PIN_INT64 ")
Those are the replacements. But I need to be careful because INT8 its a substring of UINT8 so even if I have a match I need to check if it is a UINT8 or INT8.
I've wrote some code but is far from working.
while pos!=-1:
print line[pos:]
if "INT8 " in line[pos:]:
pos=line.find("INT8",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint8)
else:
print line[pos-1]
line = line.rstrip().replace(*int8)
pos+=5 #len(PIN_)+1
elif "INT16 " in line[pos:]:
pos=line.find("INT16",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint16)
else:
print line[pos-1]
line = line.rstrip().replace(*int16)
pos+=5
elif "INT32 " in line[pos:]:
pos=line.find("INT32",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint32)
else:
print line[pos-1]
line = line.rstrip().replace(*int32)
pos+=5
elif "INT64 " in line[pos:]:
pos=line.find("INT64",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint64)
else:
print line[pos-1]
line = line.rstrip().replace(*int64)
pos+=5
else:
pos =-1
Here there are some lines of the file so you can know if the response is correct and works:
UINT32 CacheSize() const { return _cacheSize; } UINT32 LineSize() const { return _lineSize; } UINT32 Associativity(UINT64 obj, INT8 obj2) const { return _associativity; } VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex) const VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex, UINT32 & lineIndex) const { const UINT32 lineMask = _lineSize - 1;
remoduleINT8toPIN_INT8it will changePIN_UINT8toPIN_UPIN_INT8re.sub(r'(U?INT\d+?)', r'PIN_\1', line).