Please refactor the following code to remove duplication and smells, and make it better…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
Please refactor the following code to remove duplication and smells, and make it better overall.
////////////////////////////////////////////////////////////////////////////////
/// int64_t SizeToInt64(string str)
/// @brief changes type from formatted string to 64bit int
////////////////////////////////////////////////////////////////////////////////
int64_t Utils::SizeToInt64(string str) {
int64_t size, unit = 1;
int32_t offset = 2, div_factor = 1, str_length = str.length();
string unit_str = str.substr(str_length - 2, 2);
string unit_str1 = str.substr(str_length - 1, 1);
if (!unit_str.compare("KB")) {
unit = 1024;
} else if (!unit_str.compare("MB")) {
unit = 1024 * 1024;
} else if (!unit_str.compare("GB")) {
unit = 1024 * 1024 * 1024;
} else if (!unit_str.compare("TB")) {
unit = 1024 * 1024 * 1024 * 1024LL;
} else if (!unit_str1.compare("B")) {
offset = 1;
} else if (!unit_str1.compare("b")) {
offset = 1;
div_factor = 8;
}
size = Utils::StringToInt64(str.substr(0, str_length - offset)) * unit;
if (div_factor != 1) size = size / div_factor;
return size;
}
////////////////////////////////////////////////////////////////////////////////
/// int SizeToInt(string str)
/// @brief changes type from formatted string to 32bit int
////////////////////////////////////////////////////////////////////////////////
int32_t Utils::SizeToInt(string str) {
int32_t size, unit = 1, offset = 2, div_factor = 1, str_length = str.length();
string unit_str = str.substr(str_length - 2, 2);
string unit_str1 = str.substr(str_length - 1, 1);
if (!unit_str.compare("KB")) {
unit = 1024;
} else if (!unit_str.compare("MB")) {
unit = 1024 * 1024;
} else if (!unit_str.compare("GB")) {
unit = 1024 * 1024 * 1024;
} else if (!unit_str1.compare("B")) {
offset = 1;
} else if (!unit_str1.compare("b")) {
offset = 1;
div_factor = 8;
}
size = Utils::StringToInt(str.substr(0, str_length - offset)) * unit;
if (div_factor != 1) size = size / div_factor;
return size;
}
- Assistant
- User
Please refactor the code to remove duplication and smells, and make it better overall. Also, add support for "KiB, MiB, GiB, TiB". //////////////////////////////////////////////////////////////////////////////// /// int64_t SizeToInt64(string str) /// @brief changes type from formatted string to 64bit int //////////////////////////////////////////////////////////////////////////////// int64_t Utils::SizeToInt64(string str) { int64_t size, unit = 1; int32_t offset = 2, div_factor = 1, str_length = str.length(); string unit_str = str.substr(str_length - 2, 2); string unit_str1 = str.substr(str_length - 1, 1); if (!unit_str.compare("KB")) { unit = 1024; } else if (!unit_str.compare("MB")) { unit = 1024 * 1024; } else if (!unit_str.compare("GB")) { unit = 1024 * 1024 * 1024; } else if (!unit_str.compare("TB")) { unit = 1024 * 1024 * 1024 * 1024LL; } else if (!unit_str1.compare("B")) { offset = 1; } else if (!unit_str1.compare("b")) { offset = 1; div_factor = 8; } size = Utils::StringToInt64(str.substr(0, str_length - offset)) * unit; if (div_factor != 1) size = size / div_factor; return size; } //////////////////////////////////////////////////////////////////////////////// /// int SizeToInt(string str) /// @brief changes type from formatted string to 32bit int //////////////////////////////////////////////////////////////////////////////// int32_t Utils::SizeToInt(string str) { int32_t size, unit = 1, offset = 2, div_factor = 1, str_length = str.length(); string unit_str = str.substr(str_length - 2, 2); string unit_str1 = str.substr(str_length - 1, 1); if (!unit_str.compare("KB")) { unit = 1024; } else if (!unit_str.compare("MB")) { unit = 1024 * 1024; } else if (!unit_str.compare("GB")) { unit = 1024 * 1024 * 1024; } else if (!unit_str1.compare("B")) { offset = 1; } else if (!unit_str1.compare("b")) { offset = 1; div_factor = 8; } size = Utils::StringToInt(str.substr(0, str_length - offset)) * unit; if (div_factor != 1) size = size / div_factor; return size; }
- Assistant