def mangle(text):
    text_bytes = text.encode('utf-8')
    buff = BytesIO(text_bytes)
    mangled = BytesIO()
    last_tok = token.INDENT
    last_line = -1
    last_col = 0
    last_line_text = ''
    open_list_dicts = 0
    tokens = tokenizer(buff.readline)
    for t, text, (line_s, col_s), (line_e, col_e), line in tokens:
        if line_s > last_line and last_line != -1:
            last_col = 0
            if last_line_text.rstrip()[-1:] == '\\':
                mangled.write(b' \\\n')
        striped = text.strip()
        list_dict_open = [token.LSQB, token.LBRACE, '[', '{']
        list_dict_close = [token.RSQB, token.RBRACE, ']', '}']
        if t in list_dict_open or striped in list_dict_open:
            open_list_dicts += 1
        elif t in list_dict_close or striped in list_dict_close:
            open_list_dicts -= 1
        if t == token.STRING and (last_tok == token.INDENT or (
            (last_tok == token.NEWLINE or last_tok == tokenize.NL or
            last_tok == token.DEDENT or last_tok == tokenize.ENCODING)
                and open_list_dicts == 0)):
            mangled.write(b'\n' * (len(text.split('\n')) - 1))
        elif t == tokenize.COMMENT:
            pass
        else:
            if col_s > last_col:
                mangled.write(b' ' * (col_s - last_col))
            if t != tokenize.ENCODING:
                mangled.write(text.encode('utf-8'))
        last_tok = t
        last_col = col_e
        last_line = line_e
        last_line_text = line
    return mangled.getvalue().decode('utf-8')