summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib
diff options
context:
space:
mode:
author AJR <ajrhacker@users.noreply.github.com>2023-02-12 19:15:55 -0500
committer AJR <ajrhacker@users.noreply.github.com>2023-02-12 19:16:07 -0500
commit0341f8ee4cb7706abfdc486d6741e6103e6af02a (patch)
tree68df6c504e889bb498a578afa0d7576542a54afd /src/lib
parentb952a5dde6517b7f99b4f8f2360187dbb5aa7bc5 (diff)
xmlfile.cpp: Replace implementation of normalize_string with similar one in infoxml.cpp that returns a new std::string instead of a static buffer
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/util/xmlfile.cpp29
-rw-r--r--src/lib/util/xmlfile.h3
2 files changed, 14 insertions, 18 deletions
diff --git a/src/lib/util/xmlfile.cpp b/src/lib/util/xmlfile.cpp
index be7bab597ca..39604a29a87 100644
--- a/src/lib/util/xmlfile.cpp
+++ b/src/lib/util/xmlfile.cpp
@@ -782,29 +782,24 @@ void data_node::set_attribute_float(const char *name, float value)
// to ensure it doesn't contain embedded tags
//-------------------------------------------------
-const char *normalize_string(const char *string)
+std::string normalize_string(std::string_view string)
{
- static char buffer[1024];
- char *d = &buffer[0];
+ std::string result;
+ result.reserve(string.length());
- if (string != nullptr)
+ for (char ch : string)
{
- while (*string)
+ switch (ch)
{
- switch (*string)
- {
- case '\"' : d += sprintf(d, "&quot;"); break;
- case '&' : d += sprintf(d, "&amp;"); break;
- case '<' : d += sprintf(d, "&lt;"); break;
- case '>' : d += sprintf(d, "&gt;"); break;
- default:
- *d++ = *string;
- }
- ++string;
+ case '\"': result.append("&quot;"); break;
+ case '&': result.append("&amp;"); break;
+ case '<': result.append("&lt;"); break;
+ case '>': result.append("&gt;"); break;
+ default: result.append(1, ch); break;
}
}
- *d++ = 0;
- return buffer;
+
+ return result;
}
diff --git a/src/lib/util/xmlfile.h b/src/lib/util/xmlfile.h
index 8d0f2b8c36c..8949ede2d6d 100644
--- a/src/lib/util/xmlfile.h
+++ b/src/lib/util/xmlfile.h
@@ -16,6 +16,7 @@
#include <list>
#include <string>
+#include <string_view>
#include <utility>
@@ -245,7 +246,7 @@ private:
/* ----- miscellaneous interfaces ----- */
/* normalize a string into something that can be written to an XML file */
-const char *normalize_string(const char *string);
+std::string normalize_string(std::string_view string);
} // namespace util::xml