示例#1
0
void write_value(scanner& sc, writer& wr, const value& val) {
    switch (val.type()) {
    case value_type::number_type: {
        read_value(sc);
        wr.append(stringize_number(val.number_value()));
        break;
    }
    case value_type::string_type: {
        read_value(sc);
        wr.append("\"");
        wr.append(escape_string(val.string_value()));
        wr.append("\"");
        break;
    }
    case value_type::bool_type: {
        read_value(sc);
        wr.append(val.bool_value() ? "true" : "false");
        break;
    }
    case value_type::group_type: {
        if (sc.peek_token().is_char('{')) {
            write_group(sc, wr, true, val.group_value());
        } else {
            read_value(sc);
            scanner dummy_scanner = make_scanner("{}");
            write_group(dummy_scanner, wr, true, val.group_value());
        }
        break;
    }
    case value_type::vector_type: {
        if (sc.peek_token().is_char('[')) {
            write_vector(sc, wr, val.vector_value());
        } else {
            read_value(sc);
            scanner dummy_scanner = make_scanner("[]");
            write_vector(dummy_scanner, wr, val.vector_value());
        }
        break;
    }
    default: {
        break;
    }
    }
}
示例#2
0
int value_length(const value& val, int wrap_length) {
    switch (val.type()) {
    case value_type::number_type: {
        return stringize_number(val.number_value()).size();
    }
    case value_type::string_type: {
        return 2 + escape_string(val.string_value()).size();
    }
    case value_type::bool_type: {
        return val.bool_value() ? 4 : 5;
    }
    case value_type::group_type: {
        return group_length(val.group_value());
    }
    case value_type::vector_type: {
        return vector_length(val.vector_value());
    }
    default: {
        return 0;
    }
    }
}