def test_string_builder(): s = StringBuilder() s.append("a") s.append("abc") assert s.getlength() == len('aabc') s.append("a") s.append_slice("abc", 1, 2) s.append_multiple_char('d', 4) assert s.build() == "aabcabdddd"
def readall_w(self, space): builder = StringBuilder() while True: w_data = space.call_method(self, "read", space.wrap(DEFAULT_BUFFER_SIZE)) if space.is_w(w_data, space.w_None): if not builder.getlength(): return w_data break 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())
def a2b_uu(space, ascii): "Decode a line of uuencoded data." if len(ascii) == 0: # obscure case, for compability with CPython length = (-0x20) & 0x3f else: length = (ord(ascii[0]) - 0x20) & 0x3f res = StringBuilder(length) for i in range(1, len(ascii), 4): A = _a2b_read(space, ascii, i) B = _a2b_read(space, ascii, i+1) C = _a2b_read(space, ascii, i+2) D = _a2b_read(space, ascii, i+3) # _a2b_write(space, res, length, A << 2 | B >> 4) _a2b_write(space, res, length, (B & 0xf) << 4 | C >> 2) _a2b_write(space, res, length, (C & 0x3) << 6 | D) remaining = length - res.getlength() if remaining > 0: res.append_multiple_char('\x00', remaining) return space.wrap(res.build())
def a2b_uu(space, ascii): "Decode a line of uuencoded data." if len(ascii) == 0: # obscure case, for compability with CPython length = (-0x20) & 0x3f else: length = (ord(ascii[0]) - 0x20) & 0x3f res = StringBuilder(length) for i in range(1, len(ascii), 4): A = _a2b_read(space, ascii, i) B = _a2b_read(space, ascii, i + 1) C = _a2b_read(space, ascii, i + 2) D = _a2b_read(space, ascii, i + 3) # _a2b_write(space, res, length, A << 2 | B >> 4) _a2b_write(space, res, length, (B & 0xf) << 4 | C >> 2) _a2b_write(space, res, length, (C & 0x3) << 6 | D) remaining = length - res.getlength() if remaining > 0: res.append_multiple_char('\x00', remaining) return space.wrap(res.build())
class PackFormatIterator(FormatIterator): def __init__(self, space, args_w, size): self.space = space self.args_w = args_w self.args_index = 0 self.result = StringBuilder(size) # This *should* be always unroll safe, the only way to get here is by # unroll the interpret function, which means the fmt is const, and thus # this should be const (in theory ;) @jit.unroll_safe @specialize.arg(1) def operate(self, fmtdesc, repetitions): if fmtdesc.needcount: fmtdesc.pack(self, repetitions) else: for i in range(repetitions): fmtdesc.pack(self) _operate_is_specialized_ = True @jit.unroll_safe def align(self, mask): pad = (-self.result.getlength()) & mask self.result.append_multiple_char('\x00', pad) def finished(self): if self.args_index != len(self.args_w): raise StructError("too many arguments for struct format") def accept_obj_arg(self): try: w_obj = self.args_w[self.args_index] except IndexError: raise StructError("struct format requires more arguments") self.args_index += 1 return w_obj if PACK_ACCEPTS_BROKEN_INPUT: # permissive version - accepts float arguments too def accept_int_arg(self): return self._accept_integral("int_w") def accept_uint_arg(self): return self._accept_integral("uint_w") def accept_longlong_arg(self): return self._accept_integral("r_longlong_w") def accept_ulonglong_arg(self): return self._accept_integral("r_ulonglong_w") @specialize.arg(1) def _accept_integral(self, meth): space = self.space w_obj = self.accept_obj_arg() if (space.isinstance_w(w_obj, space.w_int) or space.isinstance_w(w_obj, space.w_long)): w_index = w_obj else: w_index = None w_index_method = space.lookup(w_obj, "__index__") if w_index_method is not None: try: w_index = space.index(w_obj) except OperationError, e: if not e.match(space, space.w_TypeError): raise pass if w_index is None: w_index = self._maybe_float(w_obj) return getattr(space, meth)(w_index) def _maybe_float(self, w_obj): space = self.space if space.is_true(space.isinstance(w_obj, space.w_float)): space.warn("struct: integer argument expected, got float", space.w_DeprecationWarning) else: space.warn("integer argument expected, got non-integer", space.w_DeprecationWarning) return space.int(w_obj) # wrapped float -> wrapped int or long
def func(): s = StringBuilder() s.append("a") s.append("abc") return s.getlength()