Method code for $string.to_bytes()

[Turn off line numbering]
  1: arg str;
  2: var words, num;
  3: 
  4: words = str.explode();
  5: if (listlen(words) == 1) {
  6:     if (!(words = regexp(words[1], "^([0-9]+)(.*)$")))
  7:         throw(~type, "Invalid size.");
  8: }
  9: num = (> words[1].to_number() <);
 10: switch (words[2]) {
 11:     case "b", "byte", "bytes":
 12:         return num;
 13:     case "k", "kb", "kbytes", "kilobytes":
 14:         return num * 1024;
 15:     case "m", "mb", "mbytes", "megabytes":
 16:         return num * 1024 * 1024;
 17:     case "g", "gb", "gbytes", "gigabytes":
 18:         return num * 1024 * 1024 * 1024;
 19:     default:
 20:         throw(~type, "Invalid byte size: " + words[2]);
 21: }

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

Tlon