summaryrefslogtreecommitdiff
path: root/src/string.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2010-11-10 17:49:14 +0000
committerrubidium <rubidium@openttd.org>2010-11-10 17:49:14 +0000
commit17b514ccd191f8aa57a1464c4223c3a3f5034ad9 (patch)
tree9250caa0dc994e0293ff9516fb1fb6d742f488f4 /src/string.cpp
parent8cd48767f93deb6ac4ef084574971e8f74a21038 (diff)
downloadopenttd-17b514ccd191f8aa57a1464c4223c3a3f5034ad9.tar.xz
(svn r21133) -Add: function to check the validity of a string (without modifying it)
Diffstat (limited to 'src/string.cpp')
-rw-r--r--src/string.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/string.cpp b/src/string.cpp
index 54a484b79..bad29649d 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -162,6 +162,29 @@ void str_validate(char *str, const char *last, bool allow_newlines, bool ignore)
*dst = '\0';
}
+bool StrValid(const char *str, const char *last)
+{
+ /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
+
+ while (str <= last && *str != '\0') {
+ size_t len = Utf8EncodedCharLen(*str);
+ /* Encoded length is 0 if the character isn't known.
+ * The length check is needed to prevent Utf8Decode to read
+ * over the terminating '\0' if that happens to be placed
+ * within the encoding of an UTF8 character. */
+ if (len == 0 || str + len > last) return false;
+
+ WChar c;
+ len = Utf8Decode(&c, str);
+ if (!IsPrintable(c) || (c >= SCC_SPRITE_START && c <= SCC_SPRITE_END)) {
+ return false;
+ }
+
+ str += len;
+ }
+
+ return *str == '\0';
+}
void str_strip_colours(char *str)
{