Beispiel #1
0
TEST(GTestStringBuffer, TestToStdString) {
    const char u[] = {
        0xe3, 0x81, 0x82,
        0xe3, 0x81, 0x84,
        0xe3, 0x81, 0x86,
        0
    };

    String::CPtr s = String::create(u, String::UTF8);
    StringBuffer::Ptr sb = StringBuffer::create(s);

    ASSERT_EQ(s->toStdString(String::UTF8),  sb->toStdString());
    ASSERT_EQ(s->toStdString(String::UTF16), sb->toStdString(String::UTF16));
    ASSERT_EQ(s->toStdString(String::UTF32), sb->toStdString(String::UTF32));
}
Beispiel #2
0
String::CPtr percentDecode(String::CPtr str, String::Encoding enc) {
    if (!str || str->isEmpty())
        return String::create();

    Size len = str->length() + 1;
    char* decoded = new char[len];
    Size size = percentDecode(decoded, len, str->toStdString().c_str());
    String::CPtr res;
    switch (enc) {
    case String::UTF8:
        res = String::create(decoded, enc, size);
        break;
    case String::UTF16:
    case String::UTF16BE:
    case String::UTF16LE:
        res = String::create(decoded, enc, size >> 1);
        break;
    case String::UTF32:
    case String::UTF32BE:
    case String::UTF32LE:
        res = String::create(decoded, enc, size >> 2);
        break;
    default:
        assert(false);
        res = String::null();
    }
    delete[] decoded;
    return res;
}
Beispiel #3
0
Boolean log(const Value& val) {
    String::CPtr s = String::valueOf(val);
    if (!s)
        return false;
    std::cout << s->toStdString() << std::endl;
    return true;
}
Beispiel #4
0
JsObject::Ptr parse(String::CPtr urlStr) {
    static const String::CPtr colon = String::create(":");
    static const String::CPtr slash = String::create("/");
    static const String::CPtr sharp = String::create("#");
    static const String::CPtr question = String::create("?");

    if (!urlStr) {
        LIBJ_NULL_PTR(JsObject, nullp);
        return nullp;
    }

    struct parsed_url* url = parse_url(urlStr->toStdString().c_str());
    JsObject::Ptr obj = JsObject::create();

    obj->put(HREF, urlStr);
    if (url->scheme) {
        obj->put(PROTOCOL, String::create(url->scheme)->toLowerCase());
    }
    LIBJ_NULL_CPTR(String, port);
    if (url->port) {
        port = String::create(url->port);
        obj->put(PORT, port);
    }
    if (url->host) {
        String::CPtr hostname = String::create(url->host)->toLowerCase();
        obj->put(HOSTNAME, hostname);
        if (port) {
            obj->put(HOST, hostname->concat(colon)->concat(port));
        } else {
            obj->put(HOST, hostname);
        }
    }
    LIBJ_NULL_CPTR(String, query);
    if (url->query) {
        query = String::create(url->query);
        obj->put(QUERY, query);
    }
    if (url->path) {
        String::CPtr pathname = slash->concat(String::create(url->path));
        obj->put(PATHNAME, pathname);
        if (query) {
            obj->put(PATH, pathname->concat(question)->concat(query));
        } else {
            obj->put(PATH, pathname);
        }
    }
    if (url->username && url->password) {
        String::CPtr auth = String::create(url->username);
        auth = auth->concat(colon);
        auth = auth->concat(String::create(url->password));
        obj->put(AUTH, auth);
    }
    if (url->fragment) {
        String::CPtr hash = sharp->concat(String::create(url->fragment));
        obj->put(HASH, hash);
    }

    parsed_url_free(url);
    return obj;
}
Beispiel #5
0
Boolean error(const Value& val) {
    if (val.isUndefined() || !isPrintable(LEVEL_ERROR))
        return false;

    String::CPtr s = toString(val);
    if (s) {
        error(s->toStdString().c_str());
        return true;
    } else {
        return false;
    }
}
Beispiel #6
0
Boolean warn(const Value& val) {
    if (val.isUndefined() || !isPrintable(LEVEL_WARNING))
        return false;

    String::CPtr s = toString(val);
    if (s) {
        warn(s->toStdString().c_str());
        return true;
    } else {
        return false;
    }
}
Beispiel #7
0
Boolean log(const Value& val) {
    if (val.isUndefined() || !isPrintable(LEVEL_NORMAL))
        return false;

    String::CPtr s = toString(val);
    if (s) {
        log(s->toStdString().c_str());
        return true;
    } else {
        return false;
    }
}
Beispiel #8
0
JsObject::Ptr parse(String::CPtr urlStr) {
    if (!urlStr) return JsObject::null();

    http_parser_url info;
    std::string str = urlStr->toStdString();
    const char* cstr = str.c_str();
    int r = http_parser_parse_url(
        cstr,
        str.length(),
        0,
        &info);
    if (r) return JsObject::null();

    String::CPtr scheme   = getField(cstr, UF_SCHEMA,   &info);
    String::CPtr host     = getField(cstr, UF_HOST,     &info);
    String::CPtr port     = getField(cstr, UF_PORT,     &info);
    String::CPtr path     = getField(cstr, UF_PATH,     &info);
    String::CPtr query    = getField(cstr, UF_QUERY,    &info);
    String::CPtr fragment = getField(cstr, UF_FRAGMENT, &info);
    String::CPtr userinfo = getField(cstr, UF_USERINFO, &info);

    JsObject::Ptr urlObj = JsObject::create();

    // TODO(plenluno): protocol and host are lowercased
    urlObj->put(HREF, urlStr);

    if (scheme) {
        scheme = scheme->toLowerCase();
        StringBuilder::Ptr sb = StringBuilder::create();
        sb->appendStr(scheme);
        sb->appendChar(':');
        urlObj->put(PROTOCOL, sb->toString());
    }

    if (host) {
        host = host->toLowerCase();
        urlObj->put(HOSTNAME, host);

        if (port) {
            StringBuilder::Ptr sb = StringBuilder::create();
            sb->appendStr(host);
            sb->appendChar(':');
            sb->appendStr(port);
            urlObj->put(HOST, sb->toString());
        } else {
            urlObj->put(HOST, host);
        }
    }

    if (port) {
        urlObj->put(PORT, port);
    }

    if (path) {
        urlObj->put(PATHNAME, path);

        if (query) {
            StringBuilder::Ptr sb = StringBuilder::create();
            sb->appendStr(path);
            sb->appendChar('?');
            sb->appendStr(query);
            urlObj->put(PATH, sb->toString());
        } else {
            urlObj->put(PATH, path);
        }
    }

    if (query) {
        urlObj->put(QUERY, query);

        StringBuilder::Ptr sb = StringBuilder::create();
        sb->appendChar('?');
        sb->appendStr(query);
        urlObj->put(SEARCH, sb->toString());
    }

    if (fragment) {
        StringBuilder::Ptr sb = StringBuilder::create();
        sb->appendChar('#');
        sb->appendStr(fragment);
        urlObj->put(HASH, sb->toString());
    }

    if (userinfo) {
        urlObj->put(AUTH, userinfo);
    }

    return urlObj;
}