Example #1
0
void KURL::setHostAndPort(const String& hostAndPort)
{
    size_t separator = hostAndPort.find(':');
    if (!separator)
        return;

    if (separator == kNotFound) {
        url::Replacements<char> replacements;
        StringUTF8Adaptor hostUTF8(hostAndPort);
        replacements.SetHost(charactersOrEmpty(hostUTF8), url::Component(0, hostUTF8.length()));
        replaceComponents(replacements);
        return;
    }

    String host = hostAndPort.substring(0, separator);
    String port = parsePortFromStringPosition(hostAndPort, separator + 1);

    StringUTF8Adaptor hostUTF8(host);
    StringUTF8Adaptor portUTF8(port);

    url::Replacements<char> replacements;
    replacements.SetHost(charactersOrEmpty(hostUTF8), url::Component(0, hostUTF8.length()));
    replacements.SetPort(charactersOrEmpty(portUTF8), url::Component(0, portUTF8.length()));
    replaceComponents(replacements);
}
void KURL::setHostAndPort(const String& hostAndPort)
{
    String host = hostAndPort;
    String port;
    int hostEnd = hostAndPort.find(":");
    if (hostEnd != -1) {
        host = hostAndPort.left(hostEnd);
        port = hostAndPort.substring(hostEnd + 1);
    }

    StringUTF8Adaptor hostUTF8(host);
    StringUTF8Adaptor portUTF8(port);

    url_canon::Replacements<char> replacements;
    // Host can't be removed, so we always set.
    replacements.SetHost(charactersOrEmpty(hostUTF8), url_parse::Component(0, hostUTF8.length()));

    if (!portUTF8.length()) // Port may be removed, so we support clearing.
        replacements.ClearPort();
    else
        replacements.SetPort(charactersOrEmpty(portUTF8), url_parse::Component(0, portUTF8.length()));

    replaceComponents(replacements);
}