summaryrefslogtreecommitdiff
path: root/src/string.cpp
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-05-29 11:21:38 +0200
committerGitHub <noreply@github.com>2021-05-29 11:21:38 +0200
commitca9a7df7522a614b839a83e1ba9b6a7b08642b8a (patch)
tree172dedad1d930ef7699838272e779c9d7218319b /src/string.cpp
parent4d74e519074bec9a07c2997715ab635ac0e8f084 (diff)
downloadopenttd-ca9a7df7522a614b839a83e1ba9b6a7b08642b8a.tar.xz
Codechange: rename str_validate to StrMakeValid(InPlace) (#9304)
This to be more explicit the function changes the value, and not returns yes/no.
Diffstat (limited to 'src/string.cpp')
-rw-r--r--src/string.cpp53
1 files changed, 27 insertions, 26 deletions
diff --git a/src/string.cpp b/src/string.cpp
index a76006fe8..930d0dba1 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -186,7 +186,7 @@ void str_fix_scc_encoded(char *str, const char *last)
template <class T>
-static void str_validate(T &dst, const char *str, const char *last, StringValidationSettings settings)
+static void StrMakeValidInPlace(T &dst, const char *str, const char *last, StringValidationSettings settings)
{
/* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
@@ -246,50 +246,51 @@ static void str_validate(T &dst, const char *str, const char *last, StringValida
}
/**
- * 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.
+ * Scans the string for invalid characters and replaces then 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)
+void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
{
char *dst = str;
- str_validate(dst, str, last, settings);
+ StrMakeValidInPlace(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.
+ * Scans the string for invalid characters and replaces then with a
+ * question mark '?' (if not ignored).
+ * Only use this function when you are sure the string ends with a '\0';
+ * otherwise use StrMakeValidInPlace(str, last, settings) variant.
+ * @param str The string (of which you are sure ends with '\0') to validate.
+ */
+void StrMakeValidInPlace(const char *str, StringValidationSettings settings)
+{
+ /* We know it is '\0' terminated. */
+ StrMakeValidInPlace(const_cast<char *>(str), str + strlen(str), settings);
+}
+
+/**
+ * Scans the string for invalid characters and replaces then 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)
+std::string StrMakeValid(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);
+ StrMakeValidInPlace(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
- */
-void ValidateString(const char *str)
-{
- /* We know it is '\0' terminated. */
- str_validate(const_cast<char *>(str), str + strlen(str) + 1);
-}
-
-
-/**
* Checks whether the given string is valid, i.e. contains only
* valid (printable) characters and is properly terminated.
* @param str The string to validate.