本文整理汇总了Python中pypy.rlib.rstring.UnicodeBuilder.append_slice方法的典型用法代码示例。如果您正苦于以下问题:Python UnicodeBuilder.append_slice方法的具体用法?Python UnicodeBuilder.append_slice怎么用?Python UnicodeBuilder.append_slice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pypy.rlib.rstring.UnicodeBuilder
的用法示例。
在下文中一共展示了UnicodeBuilder.append_slice方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: W_UnicodeBuilder
# 需要导入模块: from pypy.rlib.rstring import UnicodeBuilder [as 别名]
# 或者: from pypy.rlib.rstring.UnicodeBuilder import append_slice [as 别名]
class W_UnicodeBuilder(Wrappable):
def __init__(self, space, size):
if size < 0:
self.builder = UnicodeBuilder()
else:
self.builder = UnicodeBuilder(size)
self.done = False
def _check_done(self, space):
if self.done:
raise OperationError(space.w_ValueError, space.wrap("Can't operate on a done builder"))
@unwrap_spec(size=int)
def descr__new__(space, w_subtype, size=-1):
return W_UnicodeBuilder(space, size)
@unwrap_spec(s=unicode)
def descr_append(self, space, s):
self._check_done(space)
self.builder.append(s)
@unwrap_spec(s=unicode, start=int, end=int)
def descr_append_slice(self, space, s, start, end):
self._check_done(space)
if not 0 <= start <= end <= len(s):
raise OperationError(space.w_ValueError, space.wrap("bad start/stop"))
self.builder.append_slice(s, start, end)
def descr_build(self, space):
self._check_done(space)
w_s = space.wrap(self.builder.build())
self.done = True
return w_s
示例2: backslashreplace_errors
# 需要导入模块: from pypy.rlib.rstring import UnicodeBuilder [as 别名]
# 或者: from pypy.rlib.rstring.UnicodeBuilder import append_slice [as 别名]
def backslashreplace_errors(space, w_exc):
check_exception(space, w_exc)
if space.isinstance_w(w_exc, space.w_UnicodeEncodeError):
obj = space.realunicode_w(space.getattr(w_exc, space.wrap('object')))
start = space.int_w(space.getattr(w_exc, space.wrap('start')))
w_end = space.getattr(w_exc, space.wrap('end'))
end = space.int_w(w_end)
builder = UnicodeBuilder()
pos = start
while pos < end:
oc = ord(obj[pos])
num = hex(oc)
if (oc >= 0x10000):
builder.append(u"\\U")
zeros = 8
elif (oc >= 0x100):
builder.append(u"\\u")
zeros = 4
else:
builder.append(u"\\x")
zeros = 2
lnum = len(num)
nb = zeros + 2 - lnum # num starts with '0x'
if nb > 0:
builder.append_multiple_char(u'0', nb)
builder.append_slice(unicode(num), 2, lnum)
pos += 1
return space.newtuple([space.wrap(builder.build()), w_end])
else:
typename = space.type(w_exc).getname(space, '?')
raise operationerrfmt(space.w_TypeError,
"don't know how to handle %s in error callback", typename)
示例3: func
# 需要导入模块: from pypy.rlib.rstring import UnicodeBuilder [as 别名]
# 或者: from pypy.rlib.rstring.UnicodeBuilder import append_slice [as 别名]
def func():
s = UnicodeBuilder()
s.append(u'a')
s.append(u'abc')
s.append(u'abcdef')
s.append_slice(u'abc', 1, 2)
s.append_multiple_char(u'u', 4)
return s.build()
示例4: test_unicode_builder
# 需要导入模块: from pypy.rlib.rstring import UnicodeBuilder [as 别名]
# 或者: from pypy.rlib.rstring.UnicodeBuilder import append_slice [as 别名]
def test_unicode_builder():
s = UnicodeBuilder()
s.append(u'a')
s.append(u'abc')
s.append_slice(u'abcdef', 1, 2)
s.append_multiple_char('d', 4)
assert s.build() == 'aabcbdddd'
assert isinstance(s.build(), unicode)
示例5: format
# 需要导入模块: from pypy.rlib.rstring import UnicodeBuilder [as 别名]
# 或者: from pypy.rlib.rstring.UnicodeBuilder import append_slice [as 别名]
def format(self):
lgt = len(self.fmt) + 4 * len(self.values_w) + 10
if do_unicode:
result = UnicodeBuilder(lgt)
else:
result = StringBuilder(lgt)
self.result = result
while True:
# fast path: consume as many characters as possible
fmt = self.fmt
i = i0 = self.fmtpos
while i < len(fmt):
if fmt[i] == '%':
break
i += 1
else:
result.append_slice(fmt, i0, len(fmt))
break # end of 'fmt' string
result.append_slice(fmt, i0, i)
self.fmtpos = i + 1
# interpret the next formatter
w_value = self.parse_fmt()
c = self.peekchr()
self.forward()
if c == '%':
self.std_wp(const('%'))
continue
if w_value is None:
w_value = self.nextinputvalue()
# dispatch on the formatter
# (this turns into a switch after translation)
for c1 in FORMATTER_CHARS:
if c == c1:
# 'c1' is an annotation constant here,
# so this getattr() is ok
do_fmt = getattr(self, 'fmt_' + c1)
do_fmt(w_value)
break
else:
self.unknown_fmtchar()
self.checkconsumed()
return result.build()