* 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
| 203 | * @param str Span of chars to validate. |
| 204 | */ |
| 205 | bool 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 |
no test coverage detected