From ccf9f64818a95350016ffb9d3126cfcdf47ce8c7 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Sat, 27 Aug 2011 06:42:10 +0000 Subject: [PATCH] Make Assfile::SetScriptInfo slightly less slow Originally committed to SVN as r5569. --- aegisub/src/ass_file.cpp | 57 ++++++++++++++-------------------------- aegisub/src/ass_file.h | 2 +- 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/aegisub/src/ass_file.cpp b/aegisub/src/ass_file.cpp index c01342992..d9516ac87 100644 --- a/aegisub/src/ass_file.cpp +++ b/aegisub/src/ass_file.cpp @@ -632,51 +632,34 @@ int AssFile::GetScriptInfoAsInt(const wxString key) { return temp; } -void AssFile::SetScriptInfo(const wxString _key,const wxString value) { - wxString key = _key;; - key.Lower(); - key += _T(":"); - std::list::iterator cur; - std::list::iterator prev; - bool GotIn = false; +void AssFile::SetScriptInfo(wxString const& key, wxString const& value) { + wxString search_key = key.Lower() + ":"; + size_t key_size = search_key.size(); + std::list::iterator script_info_end; + bool found_script_info = false; - // Look for it - for (cur=Line.begin();cur!=Line.end();cur++) { + for (std::list::iterator cur = Line.begin(); cur != Line.end(); ++cur) { if ((*cur)->group == _T("[Script Info]")) { - GotIn = true; - wxString curText = (*cur)->GetEntryData(); - curText.Lower(); + found_script_info = true; + wxString cur_text = (*cur)->GetEntryData().Left(key_size).Lower(); - // Found - if (curText.StartsWith(key)) { - // Set value - if (value != "") { - wxString result = _key; - result += _T(": "); - result += value; - (*cur)->SetEntryData(result); - } - - // Remove key - else { + if (cur_text == search_key) { + if (value.empty()) { + delete *cur; Line.erase(cur); - return; + } + else { + (*cur)->SetEntryData(key + ": " + value); } return; } - - if (!(*cur)->GetEntryData().empty()) prev = cur; + script_info_end = cur; } - - // Add - else if (GotIn) { - if (value != "") { - wxString result = _key; - result += _T(": "); - result += value; - AssEntry *entry = new AssEntry(result); - entry->group = (*prev)->group; - Line.insert(++prev,entry); + else if (found_script_info) { + if (value.size()) { + AssEntry *entry = new AssEntry(key + ": " + value); + entry->group = "[Script Info]"; + Line.insert(script_info_end, entry); } return; } diff --git a/aegisub/src/ass_file.h b/aegisub/src/ass_file.h index 1d233cdcb..ada5ce446 100644 --- a/aegisub/src/ass_file.h +++ b/aegisub/src/ass_file.h @@ -152,7 +152,7 @@ public: /// Get the value in a [Script Info] key as string. wxString GetScriptInfo(const wxString key); /// Set the value of a [Script Info] key. Adds it if it doesn't exist. - void SetScriptInfo(const wxString key,const wxString value); + void SetScriptInfo(wxString key, wxString const& value); // Add a ";" comment in the [Script Info] section void AddComment(const wxString comment); /// @brief Add a line to the file