Skip to content

Commit

Permalink
Modified "ApplyTextInsertingCommands" to directly manipulate the pass…
Browse files Browse the repository at this point in the history
…ed std::string instead of creating a copy
  • Loading branch information
florianessl committed May 24, 2024
1 parent 4bfc9bc commit b9ea8ad
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
4 changes: 3 additions & 1 deletion src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,9 @@ std::string Game_Interpreter::CommandString(lcf::rpg::EventCommand const& com) {
#endif
return ToString(com.string);
}
return PendingMessage::ApplyTextInsertingCommands(ToString(com.string), Player::escape_char, Game_Message::CommandCodeInserter);
std::string command_string = ToString(com.string);
PendingMessage::ApplyTextInsertingCommands(command_string, Player::escape_char, Game_Message::CommandCodeInserter);
return command_string;
}


Expand Down
6 changes: 4 additions & 2 deletions src/game_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ static std::optional<std::string> CommandCodeInserterNoRecurse(char ch, const ch
std::string str = ToString(Main_Data::game_strings->Get(value));

// \t[] is evaluated but command codes inside it are not evaluated again
return PendingMessage::ApplyTextInsertingCommands(str, escape_char, PendingMessage::DefaultCommandInserter);
PendingMessage::ApplyTextInsertingCommands(str, escape_char, PendingMessage::DefaultCommandInserter);
return str;
}

return PendingMessage::DefaultCommandInserter(ch, iter, end, escape_char);
Expand All @@ -179,7 +180,8 @@ std::optional<std::string> Game_Message::CommandCodeInserter(char ch, const char
std::string str = ToString(Main_Data::game_strings->Get(value));

// Command codes in \t[] are evaluated once.
return PendingMessage::ApplyTextInsertingCommands(str, escape_char, CommandCodeInserterNoRecurse);
PendingMessage::ApplyTextInsertingCommands(str, escape_char, CommandCodeInserterNoRecurse);
return str;
}

return PendingMessage::DefaultCommandInserter(ch, iter, end, escape_char);
Expand Down
4 changes: 3 additions & 1 deletion src/game_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ std::string Game_Strings::Extract(StringView string, bool as_hex) {
cmd_fn = ManiacsCommandInserter;
}

return PendingMessage::ApplyTextInsertingCommands(ToString(string), Player::escape_char, cmd_fn);
std::string str = ToString(string);
PendingMessage::ApplyTextInsertingCommands(str, Player::escape_char, cmd_fn);
return str;
}

std::optional<std::string> Game_Strings::ManiacsCommandInserter(char ch, const char** iter, const char* end, uint32_t escape_char) {
Expand Down
35 changes: 15 additions & 20 deletions src/pending_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ PendingMessage::PendingMessage(PendingMessage::CommandInserter cmd_fn) :

int PendingMessage::PushLineImpl(std::string msg) {
RemoveControlChars(msg);
msg = ApplyTextInsertingCommands(std::move(msg), Player::escape_char, command_inserter);
ApplyTextInsertingCommands(msg, Player::escape_char, command_inserter);
texts.push_back(std::move(msg));
return texts.size();
}
Expand Down Expand Up @@ -94,17 +94,15 @@ void PendingMessage::SetChoiceResetColors(bool value) {
choice_reset_color = value;
}

std::string PendingMessage::ApplyTextInsertingCommands(std::string input, uint32_t escape_char, const CommandInserter& cmd_fn) {
void PendingMessage::ApplyTextInsertingCommands(std::string& input, uint32_t escape_char, const CommandInserter& cmd_fn) {
if (input.empty()) {
return input;
return;
}

std::string output;

const char* iter = input.data();
const auto end = input.data() + input.size();
auto end = input.data() + input.size();

const char* start_copy = iter;
const char* start_replace = nullptr;
while (iter != end) {
auto ret = Utils::UTF8Next(iter, end);
if (ret.ch != escape_char) {
Expand All @@ -117,8 +115,7 @@ std::string PendingMessage::ApplyTextInsertingCommands(std::string input, uint32
break;
}

output.append(start_copy, iter - start_copy);
start_copy = iter;
start_replace = iter;

iter = ret.next;
if (iter == end) {
Expand All @@ -130,19 +127,17 @@ std::string PendingMessage::ApplyTextInsertingCommands(std::string input, uint32

auto fn_res = cmd_fn(ch, &iter, end, escape_char);
if (fn_res) {
output.append(*fn_res);
start_copy = iter;
}
}
size_t repl_pos = start_replace - input.data();
size_t repl_len = iter - start_replace;
size_t insert_len = fn_res->size();

if (start_copy == input.data()) {
// Fast path - no substitutions occured, so just move the input into the return value.
output = std::move(input);
} else {
output.append(start_copy, end - start_copy);
}
input.replace(repl_pos, repl_len, fn_res->data(), insert_len);

return output;
iter = input.data() + repl_pos + insert_len;
end = input.data() + input.size();
start_replace = nullptr;
}
}
}

std::optional<std::string> PendingMessage::DefaultCommandInserter(char ch, const char** iter, const char* end, uint32_t escape_char) {
Expand Down
2 changes: 1 addition & 1 deletion src/pending_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PendingMessage {

void SetIsEventMessage(bool value) { is_event_message = value; }
bool IsEventMessage() const { return is_event_message; }
static std::string ApplyTextInsertingCommands(std::string input, uint32_t escape_char, const CommandInserter& cmd_fn);
static void ApplyTextInsertingCommands(std::string& input, uint32_t escape_char, const CommandInserter& cmd_fn);

private:
int PushLineImpl(std::string msg);
Expand Down

0 comments on commit b9ea8ad

Please sign in to comment.