impr: Implement better string limiting algorithm
This commit is contained in:
parent
d19d62b1fc
commit
026713750d
@ -294,12 +294,29 @@ namespace hex {
|
|||||||
[[nodiscard]] std::optional<std::string> getEnvironmentVariable(const std::string &env);
|
[[nodiscard]] std::optional<std::string> getEnvironmentVariable(const std::string &env);
|
||||||
|
|
||||||
[[nodiscard]] inline std::string limitStringLength(const std::string &string, size_t maxLength) {
|
[[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;
|
auto it = string.begin() + maxLength;
|
||||||
while (it != string.begin() && !std::isspace(*it)) --it;
|
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();
|
[[nodiscard]] std::optional<std::fs::path> getInitialFilePath();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user