diff options
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 |
commit | 0341f8ee4cb7706abfdc486d6741e6103e6af02a (patch) | |
tree | 68df6c504e889bb498a578afa0d7576542a54afd /src/lib | |
parent | b952a5dde6517b7f99b4f8f2360187dbb5aa7bc5 (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.cpp | 29 | ||||
-rw-r--r-- | src/lib/util/xmlfile.h | 3 |
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, """); break; - case '&' : d += sprintf(d, "&"); break; - case '<' : d += sprintf(d, "<"); break; - case '>' : d += sprintf(d, ">"); break; - default: - *d++ = *string; - } - ++string; + case '\"': result.append("""); break; + case '&': result.append("&"); break; + case '<': result.append("<"); break; + case '>': result.append(">"); 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 |