本文整理汇总了Python中pypy.rlib.rstring.StringBuilder.append方法的典型用法代码示例。如果您正苦于以下问题:Python StringBuilder.append方法的具体用法?Python StringBuilder.append怎么用?Python StringBuilder.append使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pypy.rlib.rstring.StringBuilder
的用法示例。
在下文中一共展示了StringBuilder.append方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fn
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def fn(_):
s = StringBuilder()
s.append("a")
s.append("abc")
s.append_slice("abc", 1, 2)
s.append_multiple_char('d', 4)
return s.build()
示例2: a2b_hqx
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def a2b_hqx(space, ascii):
"""Decode .hqx coding. Returns (bin, done)."""
# overestimate the resulting length
res = StringBuilder(len(ascii))
done = 0
pending_value = 0
pending_bits = 0
for c in ascii:
n = ord(table_a2b_hqx[ord(c)])
if n <= 0x3F:
pending_value = (pending_value << 6) | n
pending_bits += 6
if pending_bits == 24:
# flush
res.append(chr(pending_value >> 16))
res.append(chr((pending_value >> 8) & 0xff))
res.append(chr(pending_value & 0xff))
pending_value = 0
pending_bits = 0
elif n == FAIL:
raise_Error(space, 'Illegal character')
elif n == DONE:
if pending_bits >= 8:
res.append(chr(pending_value >> (pending_bits - 8)))
if pending_bits >= 16:
res.append(chr((pending_value >> (pending_bits - 16)) & 0xff))
done = 1
break
#elif n == SKIP: pass
else:
if pending_bits > 0:
raise_Incomplete(space, 'String has incomplete number of bytes')
return space.newtuple([space.wrap(res.build()), space.wrap(done)])
示例3: rledecode_hqx
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def rledecode_hqx(space, hexbin):
"Decode hexbin RLE-coded string."
# that's a guesstimation of the resulting length
res = StringBuilder(len(hexbin))
end = len(hexbin)
i = 0
lastpushed = -1
while i < end:
c = hexbin[i]
i += 1
if c != '\x90':
res.append(c)
lastpushed = ord(c)
else:
if i == end:
raise_Incomplete(space, 'String ends with the RLE code \x90')
count = ord(hexbin[i]) - 1
i += 1
if count < 0:
res.append('\x90')
lastpushed = 0x90
else:
if lastpushed < 0:
raise_Error(space, 'String starts with the RLE code \x90')
res.append_multiple_char(chr(lastpushed), count)
return space.wrap(res.build())
示例4: readline_w
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def readline_w(self, space, w_limit=None):
# For backwards compatibility, a (slowish) readline().
limit = convert_size(space, w_limit)
old_size = -1
has_peek = space.findattr(self, space.wrap("peek"))
builder = StringBuilder()
size = 0
while limit < 0 or size < limit:
nreadahead = 1
if has_peek:
w_readahead = space.call_method(self, "peek", space.wrap(1))
if not space.isinstance_w(w_readahead, space.w_str):
raise operationerrfmt(
space.w_IOError,
"peek() should have returned a bytes object, " "not '%s'",
space.type(w_readahead).getname(space),
)
length = space.len_w(w_readahead)
if length > 0:
n = 0
buf = space.str_w(w_readahead)
if limit >= 0:
while True:
if n >= length or n >= limit:
break
n += 1
if buf[n - 1] == "\n":
break
else:
while True:
if n >= length:
break
n += 1
if buf[n - 1] == "\n":
break
nreadahead = n
w_read = space.call_method(self, "read", space.wrap(nreadahead))
if not space.isinstance_w(w_read, space.w_str):
raise operationerrfmt(
space.w_IOError,
"peek() should have returned a bytes object, " "not '%s'",
space.type(w_read).getname(space),
)
read = space.str_w(w_read)
if not read:
break
size += len(read)
builder.append(read)
if read[-1] == "\n":
break
return space.wrap(builder.build())
示例5: str_join__String_ANY
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def str_join__String_ANY(space, w_self, w_list):
list_w = space.listview(w_list)
if list_w:
self = w_self._value
reslen = 0
for i in range(len(list_w)):
w_s = list_w[i]
if not space.is_true(space.isinstance(w_s, space.w_str)):
if space.is_true(space.isinstance(w_s, space.w_unicode)):
# we need to rebuild w_list here, because the original
# w_list might be an iterable which we already consumed
w_list = space.newlist(list_w)
w_u = space.call_function(space.w_unicode, w_self)
return space.call_method(w_u, "join", w_list)
raise operationerrfmt(
space.w_TypeError,
"sequence item %d: expected string, %s " "found",
i,
space.type(w_s).getname(space, "?"),
)
reslen += len(space.str_w(w_s))
reslen += len(self) * (len(list_w) - 1)
sb = StringBuilder(reslen)
for i in range(len(list_w)):
if self and i != 0:
sb.append(self)
sb.append(space.str_w(list_w[i]))
return space.wrap(sb.build())
else:
return W_StringObject.EMPTY
示例6: str_translate__String_ANY_ANY
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def str_translate__String_ANY_ANY(space, w_string, w_table, w_deletechars=''):
"""charfilter - unicode handling is not implemented
Return a copy of the string where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given translation table,
which must be a string of length 256"""
if space.is_w(w_table, space.w_None):
table = DEFAULT_NOOP_TABLE
else:
table = space.bufferstr_w(w_table)
if len(table) != 256:
raise OperationError(
space.w_ValueError,
space.wrap("translation table must be 256 characters long"))
string = w_string._value
deletechars = space.str_w(w_deletechars)
if len(deletechars) == 0:
buf = StringBuilder(len(string))
for char in string:
buf.append(table[ord(char)])
else:
buf = StringBuilder()
deletion_table = [False] * 256
for c in deletechars:
deletion_table[ord(c)] = True
for char in string:
if not deletion_table[ord(char)]:
buf.append(table[ord(char)])
return W_StringObject(buf.build())
示例7: _read_all
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def _read_all(self, space):
"Read all the file, don't update the cache"
builder = StringBuilder()
# First copy what we have in the current buffer
current_size = self._readahead()
data = None
if current_size:
data = ''.join(self.buffer[self.pos:self.pos + current_size])
builder.append(data)
self._reader_reset_buf()
# We're going past the buffer's bounds, flush it
if self.writable:
self._writer_flush_unlocked(space, restore_pos=True)
while True:
# Read until EOF or until read() would block
w_data = space.call_method(self.w_raw, "read")
if space.is_w(w_data, space.w_None):
if current_size == 0:
return w_data
break
data = space.str_w(w_data)
size = len(data)
if size == 0:
break
builder.append(data)
current_size += size
if self.abs_pos != -1:
self.abs_pos += size
return space.wrap(builder.build())
示例8: fn
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def fn(_):
s = StringBuilder(4)
got = []
for i in range(50):
s.append(chr(33+i))
got.append(s.build())
gc.collect()
return ' '.join(got)
示例9: hexdigest
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def hexdigest(self, space):
"Return the digest value as a string of hexadecimal digits."
digest = self._digest(space)
hexdigits = '0123456789abcdef'
result = StringBuilder(self.digest_size * 2)
for c in digest:
result.append(hexdigits[(ord(c) >> 4) & 0xf])
result.append(hexdigits[ ord(c) & 0xf])
return space.wrap(result.build())
示例10: fn
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def fn():
s = StringBuilder(4)
s.append("abcd")
s.append("defg")
s.append("rty")
s.append_multiple_char('y', 1000)
gc.collect()
s.append_multiple_char('y', 1000)
res = s.build()[1000]
gc.collect()
return ord(res)
示例11: hexlify
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def hexlify(space, data):
'''Hexadecimal representation of binary data.
This function is also available as "hexlify()".'''
try:
newlength = ovfcheck(len(data) * 2)
except OverflowError:
raise OperationError(space.w_MemoryError, space.w_None)
res = StringBuilder(newlength)
for c in data:
res.append(_value2char(ord(c) >> 4))
res.append(_value2char(ord(c) & 0xf))
return space.wrap(res.build())
示例12: unhexlify
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def unhexlify(space, hexstr):
'''Binary data of hexadecimal representation.
hexstr must contain an even number of hex digits (upper or lower case).
This function is also available as "unhexlify()".'''
if len(hexstr) & 1:
raise OperationError(space.w_TypeError,
space.wrap('Odd-length string'))
res = StringBuilder(len(hexstr) >> 1)
for i in range(0, len(hexstr), 2):
a = _char2value(space, hexstr[i])
b = _char2value(space, hexstr[i+1])
res.append(chr((a << 4) | b))
return space.wrap(res.build())
示例13: readall_w
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def readall_w(self, space):
builder = StringBuilder()
while True:
w_data = space.call_method(self, "read",
space.wrap(DEFAULT_BUFFER_SIZE))
if not space.isinstance_w(w_data, space.w_str):
raise OperationError(space.w_TypeError, space.wrap(
"read() should return bytes"))
data = space.str_w(w_data)
if not data:
break
builder.append(data)
return space.wrap(builder.build())
示例14: StringBuilderWithOneCharCancellable
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
class StringBuilderWithOneCharCancellable(object):
def __init__(self, crlf, initial):
self.crlf = crlf
self.builder = StringBuilder(initial)
self.pending = -1
def _flush(self):
if self.pending >= 0:
self.builder.append(chr(self.pending))
self.pending = -1
_flush._always_inline_ = True
def append(self, c):
self._flush()
self.pending = ord(c)
def newline(self):
self._flush()
if self.crlf: self.builder.append('\r')
self.pending = ord('\n')
def to_hex(self, c):
self._flush()
uvalue = ord(c)
self.builder.append("0123456789ABCDEF"[uvalue >> 4])
self.builder.append("0123456789ABCDEF"[uvalue & 0xf])
def build(self):
self._flush()
return self.builder.build()
示例15: rlecode_hqx
# 需要导入模块: from pypy.rlib.rstring import StringBuilder [as 别名]
# 或者: from pypy.rlib.rstring.StringBuilder import append [as 别名]
def rlecode_hqx(space, data):
"Binhex RLE-code binary data."
# that's a guesstimation of the resulting length
res = StringBuilder(len(data))
i = 0
end = len(data)
while i < end:
c = data[i]
res.append(c)
if c == '\x90':
# Escape it, and ignore repetitions (*).
res.append('\x00')
else:
# Check how many following are the same
inend = i + 1
while inend < end and data[inend] == c and inend < i + 255:
inend += 1
if inend - i > 3:
# More than 3 in a row. Output RLE. For the case of more
# than 255, see (*) below.
res.append('\x90')
res.append(chr(inend - i))
i = inend
continue
i += 1
# (*) Note that we put simplicity before compatness here, like CPython.
# I am sure that if we tried harder to produce the smallest possible
# string that rledecode_hqx() would expand back to 'data', there are
# some programs somewhere that would start failing obscurely in rare
# cases.
return space.wrap(res.build())