Url Parsing in AngelScript

import std.string;
import std.string_utils;

void main()
{
    // We'll parse this example URL, which includes a
    // scheme, authentication info, host, port, path,
    // query params, and query fragment.
    string s = "postgres://user:pass@host.com:5432/path?k=v#f";

    // Parse the URL and ensure there are no errors.
    array<string> urlParts = s.split("://");
    if (urlParts.length() != 2)
    {
        print("Invalid URL format");
        return;
    }

    string scheme = urlParts[0];
    string remaining = urlParts[1];

    // Accessing the scheme is straightforward.
    print(scheme);

    // Extract user info
    array<string> userInfoAndRest = remaining.split("@");
    string userInfo = userInfoAndRest[0];
    remaining = userInfoAndRest[1];

    array<string> userPass = userInfo.split(":");
    string username = userPass[0];
    string password = userPass.length() > 1 ? userPass[1] : "";

    print(userInfo);
    print(username);
    print(password);

    // Extract host and port
    array<string> hostPortAndRest = remaining.split("/");
    string hostPort = hostPortAndRest[0];
    remaining = hostPortAndRest.length() > 1 ? "/" + hostPortAndRest[1] : "";

    array<string> hostAndPort = hostPort.split(":");
    string host = hostAndPort[0];
    string port = hostAndPort.length() > 1 ? hostAndPort[1] : "";

    print(hostPort);
    print(host);
    print(port);

    // Extract path and fragment
    array<string> pathAndFragment = remaining.split("#");
    string path = pathAndFragment[0];
    string fragment = pathAndFragment.length() > 1 ? pathAndFragment[1] : "";

    print(path);
    print(fragment);

    // Extract query parameters
    array<string> pathAndQuery = path.split("?");
    path = pathAndQuery[0];
    string rawQuery = pathAndQuery.length() > 1 ? pathAndQuery[1] : "";

    print(rawQuery);

    // Parse query parameters
    dictionary queryParams;
    array<string> queryPairs = rawQuery.split("&");
    for (uint i = 0; i < queryPairs.length(); i++)
    {
        array<string> pair = queryPairs[i].split("=");
        if (pair.length() == 2)
        {
            queryParams[pair[0]] = pair[1];
        }
    }

    print(queryParams.getKeys().join(", "));
    if (queryParams.exists("k"))
    {
        print(string(queryParams["k"]));
    }
}

This AngelScript code demonstrates URL parsing, similar to the original example. Here’s an explanation of the key differences and adaptations:

  1. AngelScript doesn’t have a built-in URL parsing library, so we implement basic parsing manually using string operations.

  2. We use the std.string and std.string_utils modules for string manipulation functions.

  3. Instead of a url.Parse function, we split the URL string manually to extract its components.

  4. Error handling is simplified. We check if the URL format is valid by ensuring it has a scheme and a body separated by “://”.

  5. User information, host, port, path, fragment, and query parameters are extracted using string splitting operations.

  6. Query parameters are parsed into a dictionary object, which is similar to a map in other languages.

  7. The SplitHostPort functionality is implemented manually by splitting the host:port string.

  8. Printing is done using the print function instead of fmt.Println.

This code provides a basic URL parsing functionality in AngelScript, although it’s not as robust as the Go version due to language limitations. It demonstrates how to extract and print various components of a URL string.

To run this program, you would typically save it as a .as file and use an AngelScript interpreter or compiler to execute it. The exact method may vary depending on your AngelScript setup.