1
0
mirror of synced 2024-11-28 09:30:51 +01:00

impr: Implement better string limiting algorithm

This commit is contained in:
WerWolv 2024-02-23 18:32:12 +01:00
parent d19d62b1fc
commit 026713750d

View File

@ -294,12 +294,29 @@ namespace hex {
[[nodiscard]] std::optional<std::string> getEnvironmentVariable(const std::string &env);
[[nodiscard]] inline std::string limitStringLength(const std::string &string, size_t maxLength) {
if (string.size() < maxLength) return string;
// If the string is shorter than the max length, return it as is
if (string.size() < maxLength)
return string;
// If the string is longer than the max length, find the last space before the max length
auto it = string.begin() + maxLength;
while (it != string.begin() && !std::isspace(*it)) --it;
return std::string(string.begin(), it) + "...";
// If there's no space before the max length, just cut the string
if (it == string.begin()) {
it = string.begin() + maxLength;
// Try to find a UTF-8 character boundary
while (it != string.begin() && (*it & 0x80) != 0x00) --it;
++it;
}
// If we still didn't find a valid boundary, just return the string as is
if (it == string.begin())
return string;
// Append
return std::string(string.begin(), it) + "";
}
[[nodiscard]] std::optional<std::fs::path> getInitialFilePath();