<commit_before>"use strict";

var samples = 2;

function optionalLocalStorageGetItem(key) {
    try {
        return localStorage.getItem(key);
    } catch(e) {
        return null;
    }
}

function optionalLocalStorageSetItem(key, value) {
    try {
        localStorage.setItem(key, value);
    } catch(e) {
        // ignore
    }
}

function build_themes(themelist) {
    // Load all ace themes, sorted by their proper name.
    var themes = themelist.themes;
    themes.sort(function (a, b) {
        if (a.caption < b.caption) {
            return -1
        } else if (a.caption > b.caption) {
            return 1;
        }
        return 0;
    });

    var themeopt,
        themefrag = document.createDocumentFragment();
    for (var i=0; i < themes.length; i++) {
        themeopt = document.createElement("option");
        themeopt.setAttribute("val", themes[i].theme);
        themeopt.textContent = themes[i].caption;
        themefrag.appendChild(themeopt);
    }
    document.getElementById("themes").appendChild(themefrag);
}

function send(path, data, callback) {
    var result = document.getElementById("result");

    result.textContent = "Running...";

    var request = new XMLHttpRequest();
    request.open("POST", path, true);
    request.setRequestHeader("Content-Type", "application/json");
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            var json;

            try {
                json = JSON.parse(request.response);
            } catch (e) {
                console.log("JSON.parse(): " + e);
            }

            if (request.status == 200) {
                callback(json);
            } else {
                result.textContent = "connection failure";
            }
        }
    }
    request.timeout = 10000;
    request.ontimeout = function() {
        result.textContent = "connection timed out"
    }
    request.send(JSON.stringify(data));
}

function evaluate(result, code, version, optimize) {
    send("/evaluate.json", {code: code, version: version, optimize: optimize},
         function(object) {
          result.textContent = object["result"];

          var div = document.createElement("div");
          div.className = "message";
          div.textContent = "Program ended.";
          result.appendChild(div);
    });
}

function compile(emit, result, code, version, optimize) {
    send("/compile.json", {emit: emit, code: code, version: version, optimize: optimize,
                           highlight: true},
         function(object) {
          if ("error" in object) {
              result.textContent = object["error"];
          } else {
              result.innerHTML = object["result"];
          }
    });
}

function format(result, session, version) {
    send("/format.json", {code: session.getValue(), version: version}, function(object) {
          if ("error" in object) {
              result.textContent = object["error"];
          } else {
              result.textContent = "";
              session.setValue(object["result"]);
          }
    });
}

function share(result, version, code) {
    //var playurl = "https://play.rust-lang.org?code=" + encodeURIComponent(code);
    var playurl = "http://kraken.room409.xyz?code=" + encodeURIComponent(code);
    if (version != "master") {
        playurl += "&version=" + encodeURIComponent(version);
    }
    if (playurl.length > 5000) {
        result.textContent = "resulting URL above character limit for sharing. " +
            "Length: " + playurl.length + "; Maximum: 5000";
        return;
    }

    var url = "https://is.gd/create.php?format=json&url=" + encodeURIComponent(playurl);

    var request = new XMLHttpRequest();
    request.open("GET", url, true);
<commit_msg>Fix short url (thanks to the Rust playpen guys for making the fix easy to find in their commit history)<commit_after>"use strict";

var samples = 2;

function optionalLocalStorageGetItem(key) {
    try {
        return localStorage.getItem(key);
    } catch(e) {
        return null;
    }
}

function optionalLocalStorageSetItem(key, value) {
    try {
        localStorage.setItem(key, value);
    } catch(e) {
        // ignore
    }
}

function build_themes(themelist) {
    // Load all ace themes, sorted by their proper name.
    var themes = themelist.themes;
    themes.sort(function (a, b) {
        if (a.caption < b.caption) {
            return -1
        } else if (a.caption > b.caption) {
            return 1;
        }
        return 0;
    });

    var themeopt,
        themefrag = document.createDocumentFragment();
    for (var i=0; i < themes.length; i++) {
        themeopt = document.createElement("option");
        themeopt.setAttribute("val", themes[i].theme);
        themeopt.textContent = themes[i].caption;
        themefrag.appendChild(themeopt);
    }
    document.getElementById("themes").appendChild(themefrag);
}

function send(path, data, callback) {
    var result = document.getElementById("result");

    result.textContent = "Running...";

    var request = new XMLHttpRequest();
    request.open("POST", path, true);
    request.setRequestHeader("Content-Type", "application/json");
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            var json;

            try {
                json = JSON.parse(request.response);
            } catch (e) {
                console.log("JSON.parse(): " + e);
            }

            if (request.status == 200) {
                callback(json);
            } else {
                result.textContent = "connection failure";
            }
        }
    }
    request.timeout = 10000;
    request.ontimeout = function() {
        result.textContent = "connection timed out"
    }
    request.send(JSON.stringify(data));
}

function evaluate(result, code, version, optimize) {
    send("/evaluate.json", {code: code, version: version, optimize: optimize},
         function(object) {
          result.textContent = object["result"];

          var div = document.createElement("div");
          div.className = "message";
          div.textContent = "Program ended.";
          result.appendChild(div);
    });
}

function compile(emit, result, code, version, optimize) {
    send("/compile.json", {emit: emit, code: code, version: version, optimize: optimize,
                           highlight: true},
         function(object) {
          if ("error" in object) {
              result.textContent = object["error"];
          } else {
              result.innerHTML = object["result"];
          }
    });
}

function format(result, session, version) {
    send("/format.json", {code: session.getValue(), version: version}, function(object) {
          if ("error" in object) {
              result.textContent = object["error"];
          } else {
              result.textContent = "";
              session.setValue(object["result"]);
          }
    });
}

function share(result, version, code) {
    var playurl = "http://kraken.room409.xyz/?code=" + encodeURIComponent(code);
    if (version != "master") {
        playurl += "&version=" + encodeURIComponent(version);
    }
    if (playurl.length > 5000) {
        result.textContent = "resulting URL above character limit for sharing. " +
            "Length: " + playurl.length + "; Maximum: 5000";
        return;
    }

    var url = "https://is.gd/create.php?format=json&url=" + encodeURIComponent(playurl);

    var request = new XMLHttpRequest();
    request.open("GET", url, true);

{'fraction_non_alphanumeric': 0.1363395225464191, 'fraction_numerical': 0.006631299734748011, 'mean_word_length': 2.2198975234842013, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3339216', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2080, 'n_words': 481}