diff options
author | Michael Lutz <michi@icosahedron.de> | 2020-05-17 23:32:03 +0200 |
---|---|---|
committer | Michael Lutz <michi@icosahedron.de> | 2020-05-21 20:02:34 +0200 |
commit | 715aa67a9c13444ee76e717bfa656472f5fb2ac3 (patch) | |
tree | 3a9cd303082ced0bceb6246edc02102a28fcc3fd /src/string.cpp | |
parent | 8aef14386fc403ee631f176abf0ec86af7dcd37b (diff) | |
download | openttd-715aa67a9c13444ee76e717bfa656472f5fb2ac3.tar.xz |
Codechange: Use std::string in INI file parsing.
Diffstat (limited to 'src/string.cpp')
-rw-r--r-- | src/string.cpp | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/src/string.cpp b/src/string.cpp index 90d287c9e..bec7c8e92 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -185,18 +185,11 @@ void str_fix_scc_encoded(char *str, const char *last) } -/** - * Scans the string for valid characters and if it finds invalid ones, - * replaces them with a question mark '?' (if not ignored) - * @param str the string to validate - * @param last the last valid character of str - * @param settings the settings for the string validation. - */ -void str_validate(char *str, const char *last, StringValidationSettings settings) +template <class T> +static void str_validate(T &dst, const char *str, const char *last, StringValidationSettings settings) { /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */ - char *dst = str; while (str <= last && *str != '\0') { size_t len = Utf8EncodedCharLen(*str); /* If the character is unknown, i.e. encoded length is 0 @@ -220,7 +213,7 @@ void str_validate(char *str, const char *last, StringValidationSettings settings do { *dst++ = *str++; } while (--len != 0); - } else if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\n') { + } else if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\n') { *dst++ = *str++; } else { if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\r' && str[1] == '\n') { @@ -232,12 +225,42 @@ void str_validate(char *str, const char *last, StringValidationSettings settings if ((settings & SVS_REPLACE_WITH_QUESTION_MARK) != 0) *dst++ = '?'; } } +} +/** + * Scans the string for valid characters and if it finds invalid ones, + * replaces them with a question mark '?' (if not ignored) + * @param str the string to validate + * @param last the last valid character of str + * @param settings the settings for the string validation. + */ +void str_validate(char *str, const char *last, StringValidationSettings settings) +{ + char *dst = str; + str_validate(dst, str, last, settings); *dst = '\0'; } /** * Scans the string for valid characters and if it finds invalid ones, + * replaces them with a question mark '?' (if not ignored) + * @param str the string to validate + * @param settings the settings for the string validation. + */ +std::string str_validate(const std::string &str, StringValidationSettings settings) +{ + auto buf = str.data(); + auto last = buf + str.size(); + + std::ostringstream dst; + std::ostreambuf_iterator<char> dst_iter(dst); + str_validate(dst_iter, buf, last, settings); + + return dst.str(); +} + +/** + * Scans the string for valid characters and if it finds invalid ones, * replaces them with a question mark '?'. * @param str the string to validate */ |