Fix formatting in ParseTextDataByte and optimize message handling in ReadAllTextDataV2

This commit is contained in:
scawful
2025-04-05 17:50:49 -04:00
parent 2539724369
commit e38a7d7581
3 changed files with 76 additions and 27 deletions

View File

@@ -93,7 +93,7 @@ std::string ParseTextDataByte(uint8_t value) {
// Check for dictionary.
int dictionary = FindDictionaryEntry(value);
if (dictionary >= 0) {
return absl::StrFormat("[%s:%2X]", DICTIONARYTOKEN, dictionary);
return absl::StrFormat("[%s:%02X]", DICTIONARYTOKEN, dictionary);
}
return "";
@@ -285,12 +285,14 @@ std::vector<std::string> ParseMessageData(
parsed_message.push_back(CharEncoder.at(byte));
} else {
if (byte >= DICTOFF && byte < (DICTOFF + 97)) {
if (byte > 0 && byte <= dictionary_entries.size()) {
auto dic_entry = dictionary_entries[byte];
parsed_message.append(dic_entry.Contents);
} else {
parsed_message.append(dictionary_entries[0].Contents);
DictionaryEntry dic_entry;
for (const auto &entry : dictionary_entries) {
if (entry.ID == byte - DICTOFF) {
dic_entry = entry;
break;
}
}
parsed_message.append(dic_entry.Contents);
} else {
auto text_element = FindMatchingCommand(byte);
if (text_element != std::nullopt) {

View File

@@ -224,7 +224,6 @@ void MessageEditor::DrawDictionary() {
}
}
// TODO: Fix the command parsing.
void MessageEditor::ReadAllTextDataV2() {
// Read all text data from the ROM.
int pos = kTextData;
@@ -232,7 +231,6 @@ void MessageEditor::ReadAllTextDataV2() {
std::vector<uint8_t> raw_message;
std::vector<uint8_t> parsed_message;
std::string current_raw_message;
std::string current_parsed_message;
@@ -243,19 +241,6 @@ void MessageEditor::ReadAllTextDataV2() {
list_of_texts_.push_back(
MessageData(message_id++, pos, current_raw_message, raw_message,
current_parsed_message, parsed_message));
std::cout << "Message ID: " << message_id << std::endl;
std::cout << "Raw: " << current_raw_message << std::endl;
std::cout << "Parsed: " << current_parsed_message << std::endl;
std::cout << "Raw Bytes: ";
for (const auto &byte : raw_message) {
std::cout << util::HexByte(byte) << " ";
}
std::cout << std::endl;
std::cout << "Parsed Bytes: ";
for (const auto &byte : parsed_message) {
std::cout << util::HexByte(byte) << " ";
}
std::cout << std::endl;
raw_message.clear();
parsed_message.clear();
current_raw_message.clear();

View File

@@ -82,9 +82,8 @@ TEST_F(MessageTest, ParseMessageToData_Basic) {
TEST_F(MessageTest, ReplaceAllDictionaryWords_Success) {
std::vector<editor::DictionaryEntry> mock_dict = {
editor::DictionaryEntry(0x00, "test"),
editor::DictionaryEntry(0x01, "message")
};
editor::DictionaryEntry(0x00, "test"),
editor::DictionaryEntry(0x01, "message")};
std::string input = "This is a test message.";
auto result = editor::ReplaceAllDictionaryWords(input, mock_dict);
EXPECT_EQ(result, "This is a [D:00] [D:01].");
@@ -92,8 +91,7 @@ TEST_F(MessageTest, ReplaceAllDictionaryWords_Success) {
TEST_F(MessageTest, ReplaceAllDictionaryWords_NoMatch) {
std::vector<editor::DictionaryEntry> mock_dict = {
editor::DictionaryEntry(0x00, "hello")
};
editor::DictionaryEntry(0x00, "hello")};
std::string input = "No matching words.";
auto result = editor::ReplaceAllDictionaryWords(input, mock_dict);
EXPECT_EQ(result, "No matching words.");
@@ -102,12 +100,76 @@ TEST_F(MessageTest, ReplaceAllDictionaryWords_NoMatch) {
TEST_F(MessageTest, ParseTextDataByte_Success) {
EXPECT_EQ(editor::ParseTextDataByte(0x00), "A");
EXPECT_EQ(editor::ParseTextDataByte(0x74), "[1]");
EXPECT_EQ(editor::ParseTextDataByte(0x88), "[D:0]");
EXPECT_EQ(editor::ParseTextDataByte(0x88), "[D:00]");
}
TEST_F(MessageTest, ParseTextDataByte_Failure) {
EXPECT_EQ(editor::ParseTextDataByte(0xFF), "");
}
TEST_F(MessageTest, ParseSingleMessage_EmptyData) {
std::vector<uint8_t> mock_data = {0x7F};
int pos = 0;
auto result = editor::ParseSingleMessage(mock_data, &pos);
ASSERT_TRUE(result.ok());
const auto message_data = result.value();
EXPECT_EQ(message_data.ContentsParsed, "");
EXPECT_EQ(pos, 1);
}
TEST_F(MessageTest, OptimizeMessageForDictionary_Basic) {
std::vector<editor::DictionaryEntry> mock_dict = {
editor::DictionaryEntry(0x00, "Link"),
editor::DictionaryEntry(0x01, "Zelda")};
std::string input = "[L] rescued Zelda from danger.";
editor::MessageData message_data;
std::string optimized =
message_data.OptimizeMessageForDictionary(input, mock_dict);
EXPECT_EQ(optimized, "[L] rescued [D:01] from danger.");
}
TEST_F(MessageTest, SetMessage_Success) {
std::vector<editor::DictionaryEntry> mock_dict = {
editor::DictionaryEntry(0x00, "item")};
editor::MessageData message_data;
std::string input = "You got an item!";
message_data.SetMessage(input, mock_dict);
EXPECT_EQ(message_data.RawString, "You got an item!");
EXPECT_EQ(message_data.ContentsParsed, "You got an [D:00]!");
}
TEST_F(MessageTest, FindMatchingElement_CommandWithArgument) {
std::string input = "[W:02]";
editor::ParsedElement result = editor::FindMatchingElement(input);
EXPECT_TRUE(result.Active);
EXPECT_EQ(result.Parent.Token, "W");
EXPECT_EQ(result.Value, 0x02);
}
TEST_F(MessageTest, FindMatchingElement_InvalidCommand) {
std::string input = "[INVALID]";
editor::ParsedElement result = editor::FindMatchingElement(input);
EXPECT_FALSE(result.Active);
}
TEST_F(MessageTest, ImportMessageData_InvalidFile) {
auto result = editor::ImportMessageData("nonexistent_file.txt");
EXPECT_TRUE(result.empty());
}
TEST_F(MessageTest, BuildDictionaryEntries_CorrectSize) {
auto result = editor::BuildDictionaryEntries(rom());
EXPECT_EQ(result.size(), editor::kNumDictionaryEntries);
EXPECT_FALSE(result.empty());
}
} // namespace test
} // namespace yaze