0

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;
4
  • 1
    Just do the uint-replacements before the int-replacements and it should work... Commented Jan 22, 2016 at 4:29
  • maybe use regular expressions - re module Commented Jan 22, 2016 at 4:30
  • @L3viathan changing INT8 to PIN_INT8 it will change PIN_UINT8 to PIN_UPIN_INT8 Commented Jan 22, 2016 at 4:34
  • @furas Ah, good point, didn't think of that. Try re.sub(r'(U?INT\d+?)', r'PIN_\1', line). Commented Jan 22, 2016 at 4:35

2 Answers 2

1

I assume you don't have INT7s in your file.

import re
text = """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 INT8;
"""
for line in text.split("\n"):
    print re.sub(r'(U?INT\d+?)',r'PIN_\1', line)
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it using re module, try below code

import re
text = """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 INT8;
"""

for line in text.split("\n"):
    line = re.sub("UINT8 ","PIN_UINT8 ", line)
    line = re.sub("UINT16 ","PIN_UINT16 ", line)
    line = re.sub("UINT32 ","PIN_UINT32 ", line)
    line = re.sub("UINT64 ","PIN_UINT64 ", line)
    line = re.sub("[^UPIN_]INT8 ","PIN_INT32 ", line)
    line = re.sub("[^UPIN_]INT16 ","PIN_INT16 ", line)
    line = re.sub("[^UPIN_]INT32 ","PIN_INT32 ", line)
    line = re.sub("[^UPIN_]INT64 ","PIN_INT64 ", line)
    print "\t\t",line

Output

PIN_UINT32 CacheSize() const { return _cacheSize; }
PIN_UINT32 LineSize() const { return _lineSize; }
PIN_UINT32 Associativity(PIN_UINT64 obj,PIN_INT32 obj2) const { return _associativity; }
VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, PIN_UINT32 & setIndex) const
VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, PIN_UINT32 & setIndex, PIN_UINT32 & lineIndex) const
{
    const PIN_UINT32 lineMask  = _lineSize - 1PIN_INT32 ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.