def ensure_capacity_str(arr, new_size, n_chars): # new_size is right after write index new_arr = arr curr_len = len(arr) curr_num_chars = num_total_chars(arr) needed_total_chars = getitem_str_offset(arr, new_size - 1) + n_chars # TODO: corner case test #print("new alloc", new_size, curr_len, getitem_str_offset(arr, new_size-1), n_chars, curr_num_chars) if curr_len < new_size or needed_total_chars > curr_num_chars: new_len = int(2 * curr_len if curr_len < new_size else curr_len) new_num_chars = int( 2 * curr_num_chars + n_chars if needed_total_chars > curr_num_chars else curr_num_chars) new_arr = pre_alloc_string_array(new_len, new_num_chars) copy_str_arr_slice(new_arr, arr, new_size - 1) return new_arr
def get_ctor_params(str_arr, idx): start_offset = getitem_str_offset(str_arr, idx) data_ptr = get_ctypes_ptr(get_data_ptr_ind(str_arr, start_offset)) size = getitem_str_offset(str_arr, idx + 1) - start_offset return data_ptr, size
def trim_arr_str(arr, size): # print("trim size", size, arr[size-1], getitem_str_offset(arr, size)) new_arr = pre_alloc_string_array( size, np.int64(getitem_str_offset(arr, size))) copy_str_arr_slice(new_arr, arr, size) return new_arr