def readline_w(self, space, w_limit=None): self._check_attached(space) self._check_closed(space) self._writeflush(space) limit = convert_size(space, w_limit) remnant = None builder = UnicodeBuilder() while True: # First, get some data if necessary has_data = self._ensure_data(space) if not has_data: # end of file if remnant: builder.append(remnant) break if remnant: assert not self.readtranslate and self.readnl == u'\r\n' assert self.decoded.pos == 0 if remnant == u'\r' and self.decoded.text[0] == u'\n': builder.append(u'\r\n') self.decoded.pos = 1 remnant = None break else: builder.append(remnant) remnant = None continue if limit >= 0: remaining = limit - builder.getlength() assert remaining >= 0 else: remaining = -1 start = self.decoded.pos assert start >= 0 found = self._scan_line_ending(remaining) end_scan = self.decoded.pos if end_scan > start: s = self.decoded.text[start:end_scan] builder.append(s) if found or (limit >= 0 and builder.getlength() >= limit): break # There may be some remaining chars we'll have to prepend to the # next chunk of data if not self.decoded.exhausted(): remnant = self.decoded.get_chars(-1) # We have consumed the buffer self.decoded.reset() result = builder.build() return space.newunicode(result)
class W_UnicodeBuilder(W_Root): def __init__(self, space, size): if size < 0: self.builder = UnicodeBuilder() else: self.builder = UnicodeBuilder(size) @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.builder.append(s) @unwrap_spec(s=unicode, start=int, end=int) def descr_append_slice(self, space, s, start, end): if not 0 <= start <= end <= len(s): raise oefmt(space.w_ValueError, "bad start/stop") self.builder.append_slice(s, start, end) def descr_build(self, space): w_s = space.newunicode(self.builder.build()) # after build(), we can continue to append more strings # to the same builder. This is supported since # 2ff5087aca28 in RPython. return w_s def descr_len(self, space): if self.builder is None: raise oefmt(space.w_ValueError, "no length of built builder") return space.newint(self.builder.getlength())
def test_unicode_builder(): s = UnicodeBuilder() s.append(u'a') s.append(u'abc') s.append_slice(u'abcdef', 1, 2) assert s.getlength() == len('aabcb') s.append_multiple_char(u'd', 4) assert s.build() == 'aabcbdddd' assert isinstance(s.build(), unicode)
def test_unicode_builder(): s = UnicodeBuilder() s.append(u'a') s.append(u'abc') s.append_slice(u'abcdef', 1, 2) assert s.getlength() == len('aabcb') s.append_multiple_char(u'd', 4) result = s.build() assert result == 'aabcbdddd' assert isinstance(result, unicode)
def test_unicode_builder(): s = UnicodeBuilder() s.append(u"a") s.append(u"abc") s.append_slice(u"abcdef", 1, 2) assert s.getlength() == len("aabcb") s.append_multiple_char(u"d", 4) result = s.build() assert result == "aabcbdddd" assert isinstance(result, unicode)
def func(): s = UnicodeBuilder() s.append(u"a") s.append(u"abc") return s.getlength()
def func(): s = UnicodeBuilder() s.append(u"a") s.append(u"abc") return s.getlength()