MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / StrValid

Function StrValid

src/string.cpp:205–219  ·  view source on GitHub ↗

* Checks whether the given string is valid, i.e. contains only * valid (printable) characters and is properly terminated. * @note std::span is used instead of std::string_view as we are validating fixed-length string buffers, and * std::string_view's constructor will assume a C-string that ends with a NUL terminator, which is one of the things * we are checking. * @param str Span of chars to

Source from the content-addressed store, hash-verified

203 * @param str Span of chars to validate.
204 */
205bool StrValid(std::span<const char> str)
206{
207 /* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
208 StringConsumer consumer(str);
209 while (consumer.AnyBytesLeft()) {
210 auto c = consumer.TryReadUtf8();
211 if (!c.has_value()) return false; // invalid codepoint
212 if (*c == 0) return true; // NUL termination
213 if (!IsPrintable(*c) || (*c >= SCC_SPRITE_START && *c <= SCC_SPRITE_END)) {
214 return false;
215 }
216 }
217
218 return false; // missing NUL termination
219}
220
221/**
222 * Trim the spaces from given string in place, i.e. the string buffer that

Callers 1

IsValidMethod · 0.85

Calls 3

IsPrintableFunction · 0.85
AnyBytesLeftMethod · 0.80
TryReadUtf8Method · 0.80

Tested by

no test coverage detected