def str_from_buffer(raw_buf, gc_buf, allocated_size, needed_size): """ Converts from a pair returned by alloc_buffer to a high-level string. The returned string will be truncated to needed_size. """ assert allocated_size >= needed_size if gc_buf and (allocated_size == needed_size): return hlstrtype(gc_buf) new_buf = lltype.malloc(STRTYPE, needed_size) if gc_buf: copy_string_contents(gc_buf, new_buf, 0, 0, needed_size) else: copy_raw_to_string(raw_buf, new_buf, 0, needed_size) return hlstrtype(new_buf)
def str_from_buffer(raw_buf, gc_buf, allocated_size, needed_size): """ Converts from a pair returned by alloc_buffer to a high-level string. The returned string will be truncated to needed_size. """ assert allocated_size >= needed_size if gc_buf and (allocated_size == needed_size): return hlstrtype(gc_buf) new_buf = lltype.malloc(STRTYPE, needed_size) str_chars_offset = (offsetof(STRTYPE, 'chars') + \ itemoffsetof(STRTYPE.chars, 0)) if gc_buf: src = cast_ptr_to_adr(gc_buf) + str_chars_offset else: src = cast_ptr_to_adr(raw_buf) + itemoffsetof(TYPEP.TO, 0) dest = cast_ptr_to_adr(new_buf) + str_chars_offset raw_memcopy(src, dest, llmemory.sizeof(ll_char_type) * needed_size) keepalive_until_here(gc_buf) keepalive_until_here(new_buf) return hlstrtype(new_buf)
def str_from_buffer(raw_buf, gc_buf, case_num, allocated_size, needed_size): """ Converts from a pair returned by alloc_buffer to a high-level string. The returned string will be truncated to needed_size. """ assert allocated_size >= needed_size if allocated_size != needed_size: from rpython.rtyper.lltypesystem.lloperation import llop if llop.shrink_array(lltype.Bool, gc_buf, needed_size): pass # now 'gc_buf' is smaller else: gc_buf = mallocfn(needed_size) case_num = 2 if case_num == 2: copy_raw_to_string(raw_buf, gc_buf, 0, needed_size) return hlstrtype(gc_buf)
def charpsize2str(cp, size): ll_str = mallocfn(size) copy_raw_to_string(cp, ll_str, 0, size) result = hlstrtype(ll_str) assert result is not None return result