faqts : Computers : Programming : Languages : Python : Snippets : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 3 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

string.join() vs % and operators

Jul 5th, 2000 09:59
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 96, Darrell


"""
Packages: basic_datatypes.strings
"""
"""
We have an application where many thousands of small strings are to be
inserted and deleted from a very large buffer.
When you have 100k different small strings to replace in 4meg of text the
string.join seems to work well.
"""
def insertDeleteList(inbuf, l):
        """ Insert and delete segments in a buffer
        l is a list of (start, string, end) The input 'l' must be sorted
        If start and end are equal then string is inserted at that point
        If end > start then this range is deleted.
        If end < start then this range is duplicated
        """
        splitBuf=[]
        last=0
        for i in l:
                b=inbuf[last:i[0]]
                splitBuf.append(b)
                splitBuf.append(i[1])
                last=i[2]       # Advance past some buffer here
        splitBuf.append(inbuf[last:])
        return string.join(splitBuf,'')