Skip to content

Commit

Permalink
fix: Recent file entry name encoding being broken
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Jun 27, 2024
1 parent 8672a2c commit a950796
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
26 changes: 1 addition & 25 deletions lib/libimhex/include/hex/helpers/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,31 +298,7 @@ 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 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;

// 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::string limitStringLength(const std::string &string, size_t maxLength);

[[nodiscard]] std::optional<std::fs::path> getInitialFilePath();

Expand Down
26 changes: 26 additions & 0 deletions lib/libimhex/source/helpers/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,32 @@ namespace hex {
return value;
}

[[nodiscard]] std::string limitStringLength(const std::string &string, size_t maxLength) {
// 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;

// 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) + "";
}

static std::optional<std::fs::path> s_fileToOpen;
extern "C" void openFile(const char *path) {
log::info("Opening file: {0}", path);
Expand Down
4 changes: 3 additions & 1 deletion plugins/builtin/source/content/recent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ namespace hex::plugin::builtin::recent {
.entryFilePath = path,
.data = jsonData
});
} catch (...) { }
} catch (const std::exception &e) {
log::error("Failed to parse recent file: {}", e.what());
}
}

// Delete all recent provider files that are not in the list
Expand Down

0 comments on commit a950796

Please sign in to comment.