Aquí está la envoltura rápida de tampón que se me ocurrió; Pude usar esto en lugar de cadenas clásicas sin cambiar el código que se esperaba consumir cadenas.
class StringView:
def __init__(self,s,start=0,size=sys.maxint):
self.s, self.start, self.stop = s, start, min(start+size,len(s))
self.size = self.stop - self.start
self._buf = buffer(s,start,self.size)
def find(self,sub,start=0,stop=None):
assert start >= 0, start
assert (stop is None) or (stop <= self.size), stop
ofs = self.s.find(sub,self.start+start,self.stop if (stop is None) else (self.start+stop))
if ofs != -1: ofs -= self.start
return ofs
def split(self,sep=None,maxsplit=sys.maxint):
assert maxsplit > 0, maxsplit
ret = []
if sep is None: #whitespace logic
pos = [self.start,self.start] # start and stop
def eat(whitespace=False):
while (pos[1] < self.stop) and (whitespace == (ord(self.s[pos[1]])<=32)):
pos[1] += 1
def eat_whitespace():
eat(True)
pos[0] = pos[1]
eat_whitespace()
while pos[1] < self.stop:
eat()
ret.append(self.__class__(self.s,pos[0],pos[1]-pos[0]))
eat_whitespace()
if len(ret) == maxsplit:
ret.append(self.__class__(self.s,pos[1]))
break
else:
start = stop = 0
while len(ret) < maxsplit:
stop = self.find(sep,start)
if -1 == stop:
break
ret.append(self.__class__(self.s,self.start+start,stop-start))
start = stop + len(sep)
ret.append(self.__class__(self.s,self.start+start,self.size-start))
return ret
def split_str(self,sep=None,maxsplit=sys.maxint):
"if you really want strings and not views"
return [str(sub) for sub in self.split(sep,maxsplit)]
def __cmp__(self,s):
if isinstance(s,self.__class__):
return cmp(self._buf,s._buf)
assert isinstance(s,str), type(s)
return cmp(self._buf,s)
def __len__(self):
return self.size
def __str__(self):
return str(self._buf)
def __repr__(self):
return "'%s'"%self._buf
if __name__=="__main__":
test_str = " this: is: a: te:st str:ing :"
test = Envelope.StringView(test_str)
print "find('is')"
print "\t",test_str.find("is")
print "\t",test.find("is")
print "find('is',4):"
print "\t",test_str.find("is",4)
print "\t",test.find("is",4)
print "find('is',4,7):"
print "\t",test_str.find("is",4,7)
print "\t",test.find("is",4,7)
print "split():"
print "\t",test_str.split()
print "\t",test.split()
print "split(None,2):"
print "\t",test_str.split(None,2)
print "\t",test.split(None,2)
print "split(':'):"
print "\t",test_str.split(":")
print "\t",test.split(":")
print "split('x'):"
print "\t",test_str.split("x")
print "\t",test.split("x")
print "''.split('x'):"
print "\t","".split("x")
print "\t",Envelope.StringView("").split("x")
Las respuestas a continuación que usan buffer() solo se aplican a 2.7. memoryview() no se puede usar con cadenas unicode, que son cadenas normales en 3.x. –