mirror of https://github.com/mahmoud/boltons.git
a note on StringBuffer
This commit is contained in:
parent
ae4bed70af
commit
73264db3cc
|
@ -5,7 +5,7 @@ import string
|
||||||
import unicodedata
|
import unicodedata
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
from compat import str, unicode, basestring, bytes
|
from compat import unicode, bytes
|
||||||
|
|
||||||
_punct_ws_str = string.punctuation + string.whitespace
|
_punct_ws_str = string.punctuation + string.whitespace
|
||||||
_punct_re = re.compile('[' + _punct_ws_str + ']+')
|
_punct_re = re.compile('[' + _punct_ws_str + ']+')
|
||||||
|
@ -35,15 +35,24 @@ def under2camel(under_string):
|
||||||
|
|
||||||
|
|
||||||
class StringBuffer(object):
|
class StringBuffer(object):
|
||||||
|
"""
|
||||||
|
This is meant to be a better file-like string buffer.
|
||||||
|
Faster than StringIO, better encoding handling than cStringIO.
|
||||||
|
|
||||||
|
This one is for unicode text strings. Look for ByteBuffer if you
|
||||||
|
want to handle byte strings.
|
||||||
|
"""
|
||||||
def __init__(self, default_encoding=None, errors='strict'):
|
def __init__(self, default_encoding=None, errors='strict'):
|
||||||
self.data = collections.deque()
|
self.data = collections.deque()
|
||||||
self.default_encoding = default_encoding or 'utf-8'
|
self.default_encoding = default_encoding or 'utf-8'
|
||||||
|
self.errors = errors
|
||||||
|
|
||||||
def write(self, s):
|
def write(self, s):
|
||||||
if not isinstance(s, unicode):
|
if not isinstance(s, unicode):
|
||||||
enc = self.default_encoding
|
enc = self.default_encoding
|
||||||
|
errs = self.errors
|
||||||
try:
|
try:
|
||||||
s = s.decode(enc)
|
s = s.decode(enc, errs)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise ValueError('write() expected a unicode or byte string')
|
raise ValueError('write() expected a unicode or byte string')
|
||||||
self.data.append(s)
|
self.data.append(s)
|
||||||
|
|
Loading…
Reference in New Issue