Method code for $string.to_bytes()

[Turn on line numbering]
arg str;
var words, num;

words = str.explode();
if (listlen(words) == 1) {
    if (!(words = regexp(words[1], "^([0-9]+)(.*)$")))
        throw(~type, "Invalid size.");
}
num = (> words[1].to_number() <);
switch (words[2]) {
    case "b", "byte", "bytes":
        return num;
    case "k", "kb", "kbytes", "kilobytes":
        return num * 1024;
    case "m", "mb", "mbytes", "megabytes":
        return num * 1024 * 1024;
    case "g", "gb", "gbytes", "gigabytes":
        return num * 1024 * 1024 * 1024;
    default:
        throw(~type, "Invalid byte size: " + words[2]);
}

// Created 27-Mar-1995 as a part of ColdCore, see: @help Credit

Tlon