void doEncodeString()

in lang/c++/impl/json/JsonIO.hh [286:358]


    void doEncodeString(const char *b, size_t len, bool binary) {
        const char *e = b + len;
        out_.write('"');
        for (const char *p = b; p != e; p++) {
            if ((*p & 0x80) != 0) {
                write(b, p);
                if (binary) {
                    escapeCtl(*p);
                } else if ((*p & 0x40) == 0) {
                    throw Exception("Invalid UTF-8 sequence");
                } else {
                    int more = 1;
                    uint32_t value;
                    if ((*p & 0x20) != 0) {
                        more++;
                        if ((*p & 0x10) != 0) {
                            more++;
                            if ((*p & 0x08) != 0) {
                                throw Exception("Invalid UTF-8 sequence");
                            } else {
                                value = *p & 0x07;
                            }
                        } else {
                            value = *p & 0x0f;
                        }
                    } else {
                        value = *p & 0x1f;
                    }
                    for (int i = 0; i < more; ++i) {
                        if (++p == e || (*p & 0xc0) != 0x80) {
                            throw Exception("Invalid UTF-8 sequence");
                        }
                        value <<= 6;
                        value |= *p & 0x3f;
                    }
                    escapeUnicode(value);
                }
            } else {
                switch (*p) {
                    case '\\':
                    case '"':
                        escape(*p, b, p);
                        break;
                    case '\b':
                        escape('b', b, p);
                        break;
                    case '\f':
                        escape('f', b, p);
                        break;
                    case '\n':
                        escape('n', b, p);
                        break;
                    case '\r':
                        escape('r', b, p);
                        break;
                    case '\t':
                        escape('t', b, p);
                        break;
                    default:
                        if (std::iscntrl(*p, std::locale::classic())) {
                            write(b, p);
                            escapeCtl(*p);
                            break;
                        } else {
                            continue;
                        }
                }
            }
            b = p + 1;
        }
        write(b, e);
        out_.write('"');
    }