Add message loading functionality and refactor message data handling

- Introduced yaze_load_messages function to load messages from ROM data, enhancing message management.
- Updated message data structures to use std::string_view for improved performance and clarity.
- Refactored message parsing logic to utilize modern C++ features like ranges and optional, improving readability.
- Adjusted various functions to streamline message handling and ensure consistency across the codebase.
This commit is contained in:
scawful
2025-08-03 17:55:11 -04:00
parent c9921c91bf
commit dacd9551f0
3 changed files with 56 additions and 29 deletions

View File

@@ -36,10 +36,10 @@ std::optional<TextElement> FindMatchingCommand(uint8_t b) {
}
std::optional<TextElement> FindMatchingSpecial(uint8_t value) {
auto it = std::find_if(SpecialChars.begin(), SpecialChars.end(),
[value](const TextElement &text_element) {
return text_element.ID == value;
});
auto it = std::ranges::find_if(SpecialChars,
[value](const TextElement &text_element) {
return text_element.ID == value;
});
if (it != SpecialChars.end()) {
return *it;
}
@@ -83,15 +83,15 @@ std::string ParseTextDataByte(uint8_t value) {
}
// Check for command.
auto text_element = FindMatchingCommand(value);
if (text_element != std::nullopt) {
if (auto text_element = FindMatchingCommand(value);
text_element != std::nullopt) {
return text_element->GenericToken;
}
// Check for special characters.
auto special_element = FindMatchingSpecial(value);
if (special_element != std::nullopt) {
return text_element->GenericToken;
if (auto special_element = FindMatchingSpecial(value);
special_element != std::nullopt) {
return special_element->GenericToken;
}
// Check for dictionary.
@@ -172,10 +172,10 @@ std::vector<DictionaryEntry> BuildDictionaryEntries(Rom *rom) {
AllDictionaries.push_back(DictionaryEntry{(uint8_t)i, stringBuilder.str()});
}
std::sort(AllDictionaries.begin(), AllDictionaries.end(),
[](const DictionaryEntry &a, const DictionaryEntry &b) {
return a.Contents.size() > b.Contents.size();
});
std::ranges::sort(AllDictionaries,
[](const DictionaryEntry &a, const DictionaryEntry &b) {
return a.Contents.size() > b.Contents.size();
});
return AllDictionaries;
}
@@ -319,6 +319,10 @@ std::vector<std::string> ParseMessageData(
parsed_message.append(text_element->GetParamToken());
}
}
auto special_element = FindMatchingSpecial(byte);
if (special_element != std::nullopt) {
parsed_message.append(special_element->GetParamToken());
}
}
}
pos++;

View File

@@ -12,9 +12,9 @@
namespace yaze {
namespace editor {
const uint8_t kMessageTerminator = 0x7F;
const std::string kBankToken = "BANK";
const std::string DICTIONARYTOKEN = "D";
constexpr uint8_t kMessageTerminator = 0x7F;
constexpr uint8_t DICTOFF = 0x88;
constexpr uint8_t kWidthArraySize = 100;
@@ -50,18 +50,18 @@ struct DictionaryEntry {
std::string Token = "";
DictionaryEntry() = default;
DictionaryEntry(uint8_t i, std::string s)
: Contents(s), ID(i), Length(s.length()) {
DictionaryEntry(uint8_t i, std::string_view s)
: ID(i), Contents(s), Length(s.length()) {
Token = absl::StrFormat("[%s:%02X]", DICTIONARYTOKEN, ID);
Data = ParseMessageToData(Contents);
}
bool ContainedInString(std::string s) const {
return s.find(Contents) != std::string::npos;
bool ContainedInString(std::string_view s) const {
return s.contains(Contents);
}
std::string ReplaceInstancesOfIn(std::string s) const {
std::string replaced_string = s;
std::string ReplaceInstancesOfIn(std::string_view s) const {
auto replaced_string = std::string(s);
size_t pos = replaced_string.find(Contents);
while (pos != std::string::npos) {
replaced_string.replace(pos, Contents.length(), Token);
@@ -105,9 +105,9 @@ struct MessageData {
: ID(id),
Address(address),
RawString(rawString),
ContentsParsed(parsedString),
Data(rawData),
DataParsed(parsedData),
ContentsParsed(parsedString) {}
DataParsed(parsedData) {}
// Copy constructor
MessageData(const MessageData &other) {
@@ -120,7 +120,7 @@ struct MessageData {
}
std::string OptimizeMessageForDictionary(
std::string message_string,
std::string_view message_string,
const std::vector<DictionaryEntry> &dictionary) {
std::stringstream protons;
bool command = false;
@@ -288,11 +288,8 @@ struct ParsedElement {
bool Active = false;
ParsedElement() = default;
ParsedElement(TextElement textElement, uint8_t value) {
Parent = textElement;
Value = value;
Active = true;
}
ParsedElement(const TextElement &textElement, uint8_t value)
: Parent(textElement), Value(value), Active(true) {}
};
ParsedElement FindMatchingElement(const std::string &str);