impr: Limit string length by cutting out part of the middle instead
This commit is contained in:
parent
91f6aae9ef
commit
ed56b3dd12
@ -685,24 +685,38 @@ namespace hex {
|
|||||||
return string;
|
return string;
|
||||||
|
|
||||||
// If the string is longer than the max length, find the last space before the max length
|
// 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 / 2;
|
||||||
while (it != string.begin() && !std::isspace(*it)) --it;
|
while (it != string.begin() && !std::isspace(*it)) --it;
|
||||||
|
|
||||||
// If there's no space before the max length, just cut the string
|
// If there's no space before the max length, just cut the string
|
||||||
if (it == string.begin()) {
|
if (it == string.begin()) {
|
||||||
it = string.begin() + maxLength;
|
it = string.begin() + maxLength / 2;
|
||||||
|
|
||||||
// Try to find a UTF-8 character boundary
|
// Try to find a UTF-8 character boundary
|
||||||
while (it != string.begin() && (*it & 0x80) == 0x00) --it;
|
while (it != string.begin() && (*it & 0x80) == 0x00) --it;
|
||||||
++it;
|
--it;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we still didn't find a valid boundary, just return the string as is
|
// If we still didn't find a valid boundary, just return the string as is
|
||||||
if (it == string.begin())
|
if (it == string.begin())
|
||||||
return string;
|
return string;
|
||||||
|
|
||||||
// Append
|
auto result = std::string(string.begin(), it) + "…";
|
||||||
return std::string(string.begin(), it) + "…";
|
|
||||||
|
// If the string is longer than the max length, find the last space before the max length
|
||||||
|
it = string.end() - 1 - maxLength / 2;
|
||||||
|
while (it != string.end() && !std::isspace(*it)) ++it;
|
||||||
|
|
||||||
|
// If there's no space before the max length, just cut the string
|
||||||
|
if (it == string.end()) {
|
||||||
|
it = string.end() - 1 - maxLength / 2;
|
||||||
|
|
||||||
|
// Try to find a UTF-8 character boundary
|
||||||
|
while (it != string.end() && (*it & 0x80) == 0x00) ++it;
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result + std::string(it, string.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::optional<std::fs::path> s_fileToOpen;
|
static std::optional<std::fs::path> s_fileToOpen;
|
||||||
|
Loading…
Reference in New Issue
Block a user