assorted: move getbytes to parseutil and add tests (#14076)

This commit is contained in:
Bogdan
2023-02-25 01:22:13 +02:00
committed by GitHub
parent 37455119e1
commit 7e52ea52e1
42 changed files with 96 additions and 81 deletions

View File

@@ -104,5 +104,43 @@ namespace Jackett.Common.Utils
var imdbLen = ((int)imdbid > 9999999) ? "D8" : "D7";
return "tt" + ((int)imdbid).ToString(imdbLen);
}
// ex: " 3.5 gb " -> "3758096384" , "3,5GB" -> "3758096384" , "296,98 MB" -> "311406100.48" , "1.018,29 MB" -> "1067754455.04"
// ex: "1.018.29mb" -> "1067754455.04" , "-" -> "0" , "---" -> "0"
public static long GetBytes(string str)
{
var valStr = new string(str.Where(c => char.IsDigit(c) || c == '.' || c == ',').ToArray());
valStr = (valStr.Length == 0) ? "0" : valStr.Replace(",", ".");
if (valStr.Count(c => c == '.') > 1)
{
var lastOcc = valStr.LastIndexOf('.');
valStr = valStr.Substring(0, lastOcc).Replace(".", string.Empty) + valStr.Substring(lastOcc);
}
var unit = new string(str.Where(char.IsLetter).ToArray());
var val = CoerceFloat(valStr);
return GetBytes(unit, val);
}
public static long GetBytes(string unit, float value)
{
unit = unit.Replace("i", "").ToLowerInvariant();
if (unit.Contains("kb"))
return BytesFromKB(value);
if (unit.Contains("mb"))
return BytesFromMB(value);
if (unit.Contains("gb"))
return BytesFromGB(value);
if (unit.Contains("tb"))
return BytesFromTB(value);
return (long)value;
}
public static long BytesFromTB(float tb) => BytesFromGB(tb * 1024f);
public static long BytesFromGB(float gb) => BytesFromMB(gb * 1024f);
public static long BytesFromMB(float mb) => BytesFromKB(mb * 1024f);
public static long BytesFromKB(float kb) => (long)(kb * 1024f);
}
}