Changeset - r16407:50e70180295c
[Not reviewed]
master
0 2 0
rubidium - 14 years ago 2010-11-10 17:49:14
rubidium@openttd.org
(svn r21133) -Add: function to check the validity of a string (without modifying it)
2 files changed with 32 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/string.cpp
Show inline comments
 
@@ -162,6 +162,29 @@ void str_validate(char *str, const char 
 
	*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)
 
{
src/string_func.h
Show inline comments
 
@@ -118,6 +118,15 @@ void str_strip_colours(char *str);
 
void strtolower(char *str);
 

	
 
/**
 
 * Checks whether the given string is valid, i.e. contains only
 
 * valid (printable) characters and is properly terminated.
 
 * @param str  The string to validate.
 
 * @param last The last character of the string, i.e. the string
 
 *             must be terminated here or earlier.
 
 */
 
bool StrValid(const char *str, const char *last);
 

	
 
/**
 
 * Check if a string buffer is empty.
 
 *
 
 * @param s The pointer to the firste element of the buffer
0 comments (0 inline, 0 general)