Precision of the data will be inevitably lost, but reversing this code would yield in a function such as
func robotize_size(size: String) -> int:
var suffixes = {
"KiB": 1 << 10,
"MiB": 1 << 20,
"GiB": 1 << 30,
"TiB": 1 << 40,
"PiB": 1 << 50,
"EiB": 1 << 60,
"B": 1 # this muse be checked last
}
for suffix in suffixes:
if not size.ends_with(suffix):
continue
var value = size.trim_suffix(suffix)
if not value.strip_edges().is_valid_float():
# String invalid
return -1
return int(value.to_float() * suffixes[suffix])
# String does not contain any prefix
return -1
Seems to be working fine. suffixes could be moved as const outside the function to optimize it a bit, so it’s not constructed every function call.