impr: Allow custom encodings with only single-byte characters to be displayed immediately

This commit is contained in:
WerWolv 2024-01-27 16:49:31 +01:00
parent 25b4745997
commit 61048757e6
3 changed files with 31 additions and 10 deletions

View File

@ -28,8 +28,9 @@ namespace hex {
EncodingFile& operator=(EncodingFile &&other) noexcept;
[[nodiscard]] std::pair<std::string_view, size_t> getEncodingFor(std::span<u8> buffer) const;
[[nodiscard]] size_t getEncodingLengthFor(std::span<u8> buffer) const;
[[nodiscard]] size_t getLongestSequence() const { return m_longestSequence; }
[[nodiscard]] u64 getEncodingLengthFor(std::span<u8> buffer) const;
[[nodiscard]] u64 getShortestSequence() const { return m_shortestSequence; }
[[nodiscard]] u64 getLongestSequence() const { return m_longestSequence; }
[[nodiscard]] bool valid() const { return m_valid; }
@ -45,7 +46,9 @@ namespace hex {
std::string m_name;
std::string m_tableContent;
std::unique_ptr<std::map<size_t, std::map<std::vector<u8>, std::string>>> m_mapping;
size_t m_longestSequence = 0;
u64 m_shortestSequence = std::numeric_limits<u64>::max();
u64 m_longestSequence = std::numeric_limits<u64>::min();
};
}

View File

@ -15,6 +15,7 @@ namespace hex {
m_mapping = std::make_unique<std::map<size_t, std::map<std::vector<u8>, std::string>>>(*other.m_mapping);
m_tableContent = other.m_tableContent;
m_longestSequence = other.m_longestSequence;
m_shortestSequence = other.m_shortestSequence;
m_valid = other.m_valid;
m_name = other.m_name;
}
@ -23,6 +24,7 @@ namespace hex {
m_mapping = std::move(other.m_mapping);
m_tableContent = std::move(other.m_tableContent);
m_longestSequence = other.m_longestSequence;
m_shortestSequence = other.m_shortestSequence;
m_valid = other.m_valid;
m_name = std::move(other.m_name);
}
@ -66,6 +68,7 @@ namespace hex {
m_mapping = std::make_unique<std::map<size_t, std::map<std::vector<u8>, std::string>>>(*other.m_mapping);
m_tableContent = other.m_tableContent;
m_longestSequence = other.m_longestSequence;
m_shortestSequence = other.m_shortestSequence;
m_valid = other.m_valid;
m_name = other.m_name;
@ -76,6 +79,7 @@ namespace hex {
m_mapping = std::move(other.m_mapping);
m_tableContent = std::move(other.m_tableContent);
m_longestSequence = other.m_longestSequence;
m_shortestSequence = other.m_shortestSequence;
m_valid = other.m_valid;
m_name = std::move(other.m_name);
@ -144,6 +148,7 @@ namespace hex {
(*m_mapping)[keySize].insert({ std::move(fromBytes), to });
m_longestSequence = std::max(m_longestSequence, keySize);
m_shortestSequence = std::min(m_shortestSequence, keySize);
}
}

View File

@ -592,14 +592,12 @@ namespace hex::ui {
m_encodingLineStartAddresses.push_back(0);
}
if (size_t(y) < m_encodingLineStartAddresses.size()) {
const bool singleByteEncoding = m_currCustomEncoding->getLongestSequence() == 1 && m_currCustomEncoding->getShortestSequence() == 1;
if (size_t(y) < m_encodingLineStartAddresses.size() || singleByteEncoding) {
std::vector<std::pair<u64, CustomEncodingData>> encodingData;
if (m_encodingLineStartAddresses[y] >= m_bytesPerRow) {
encodingData.emplace_back(y * m_bytesPerRow + m_provider->getBaseAddress() + m_provider->getCurrentPageAddress(), CustomEncodingData(".", 1, ImGuiExt::GetCustomColorU32(ImGuiCustomCol_AdvancedEncodingUnknown)));
m_encodingLineStartAddresses.push_back(0);
} else {
u32 offset = m_encodingLineStartAddresses[y];
if (singleByteEncoding) {
u64 offset = 0;
do {
const u64 address = y * m_bytesPerRow + offset + m_provider->getBaseAddress() + m_provider->getCurrentPageAddress();
@ -608,8 +606,23 @@ namespace hex::ui {
offset += result.advance;
encodingData.emplace_back(address, result);
} while (offset < m_bytesPerRow);
} else {
if (m_encodingLineStartAddresses[y] >= m_bytesPerRow) {
encodingData.emplace_back(y * m_bytesPerRow + m_provider->getBaseAddress() + m_provider->getCurrentPageAddress(), CustomEncodingData(".", 1, ImGuiExt::GetCustomColorU32(ImGuiCustomCol_AdvancedEncodingUnknown)));
m_encodingLineStartAddresses.push_back(0);
} else {
u64 offset = m_encodingLineStartAddresses[y];
do {
const u64 address = y * m_bytesPerRow + offset + m_provider->getBaseAddress() + m_provider->getCurrentPageAddress();
m_encodingLineStartAddresses.push_back(offset - m_bytesPerRow);
auto result = queryCustomEncodingData(m_provider, *m_currCustomEncoding, address);
offset += result.advance;
encodingData.emplace_back(address, result);
} while (offset < m_bytesPerRow);
m_encodingLineStartAddresses.push_back(offset - m_bytesPerRow);
}
}
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(0, 0));