Update to 14.0-beta1

This commit is contained in:
dP
2024-02-04 02:18:17 +05:30
parent 79037e2c65
commit 33ef333b57
1325 changed files with 138461 additions and 70983 deletions
+41 -55
View File
@@ -7,55 +7,44 @@
/**
* @file string_func.h Functions related to low-level strings.
*
* @note Be aware of "dangerous" string functions; string functions that
* have behaviour that could easily cause buffer overruns and such:
* - strncpy: does not '\0' terminate when input string is longer than
* the size of the output string. Use strecpy instead.
* - [v]snprintf: returns the length of the string as it would be written
* when the output is large enough, so it can be more than the size of
* the buffer and than can underflow size_t (uint-ish) which makes all
* subsequent snprintf alikes write outside of the buffer. Use
* [v]seprintf instead; it will return the number of bytes actually
* added so no [v]seprintf will cause outside of bounds writes.
* - [v]sprintf: does not bounds checking: use [v]seprintf instead.
*/
#ifndef STRING_FUNC_H
#define STRING_FUNC_H
#include <stdarg.h>
#include <iosfwd>
#include "core/bitmath_func.hpp"
#include "core/span_type.hpp"
#include "string_type.h"
char *strecat(char *dst, const char *src, const char *last) NOACCESS(3);
char *strecpy(char *dst, const char *src, const char *last) NOACCESS(3);
char *stredup(const char *src, const char *last = nullptr) NOACCESS(2);
int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4) NOACCESS(2);
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap) WARN_FORMAT(3, 0) NOACCESS(2);
char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
std::string FormatArrayAsHex(span<const byte> data);
std::string FormatArrayAsHex(std::span<const byte> data);
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
[[nodiscard]] std::string StrMakeValid(const std::string &str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
[[nodiscard]] std::string StrMakeValid(std::string_view str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
void StrMakeValidInPlace(char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
void str_fix_scc_encoded(char *str, const char *last) NOACCESS(2);
void str_strip_colours(char *str);
bool strtolower(char *str);
bool strtolower(std::string &str, std::string::size_type offs = 0);
bool StrValid(const char *str, const char *last) NOACCESS(2);
[[nodiscard]] bool StrValid(const char *str, const char *last) NOACCESS(2);
void StrTrimInPlace(std::string &str);
bool StrStartsWith(const std::string_view str, const std::string_view prefix);
bool StrEndsWith(const std::string_view str, const std::string_view suffix);
[[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix);
[[nodiscard]] bool StrEndsWithIgnoreCase(std::string_view str, const std::string_view suffix);
[[nodiscard]] int StrCompareIgnoreCase(const std::string_view str1, const std::string_view str2);
[[nodiscard]] bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2);
[[nodiscard]] int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front = false);
[[nodiscard]] bool StrNaturalContains(const std::string_view str, const std::string_view value);
[[nodiscard]] bool StrNaturalContainsIgnoreCase(const std::string_view str, const std::string_view value);
bool ConvertHexToBytes(std::string_view hex, std::span<uint8_t> bytes);
/** Case insensitive comparator for strings, for example for use in std::map. */
struct CaseInsensitiveComparator {
bool operator()(const std::string_view s1, const std::string_view s2) const { return StrCompareIgnoreCase(s1, s2) < 0; }
};
/**
* Check if a string buffer is empty.
@@ -64,7 +53,7 @@ bool StrEndsWith(const std::string_view str, const std::string_view suffix);
* @return true if the buffer starts with the terminating null-character or
* if the given pointer points to nullptr else return false
*/
static inline bool StrEmpty(const char *s)
inline bool StrEmpty(const char *s)
{
return s == nullptr || s[0] == '\0';
}
@@ -76,34 +65,33 @@ static inline bool StrEmpty(const char *s)
* @param maxlen The maximum size of the buffer
* @return The length of the string
*/
static inline size_t ttd_strnlen(const char *str, size_t maxlen)
inline size_t ttd_strnlen(const char *str, size_t maxlen)
{
const char *t;
for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
return t - str;
}
char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
bool IsValidChar(char32_t key, CharSetFilter afilter);
bool IsValidChar(WChar key, CharSetFilter afilter);
size_t Utf8Decode(WChar *c, const char *s);
size_t Utf8Encode(char *buf, WChar c);
size_t Utf8Encode(std::ostreambuf_iterator<char> &buf, WChar c);
size_t Utf8Decode(char32_t *c, const char *s);
size_t Utf8Encode(char *buf, char32_t c);
size_t Utf8Encode(std::ostreambuf_iterator<char> &buf, char32_t c);
size_t Utf8Encode(std::back_insert_iterator<std::string> &buf, char32_t c);
size_t Utf8TrimString(char *s, size_t maxlen);
static inline WChar Utf8Consume(const char **s)
inline char32_t Utf8Consume(const char **s)
{
WChar c;
char32_t c;
*s += Utf8Decode(&c, *s);
return c;
}
template <class Titr>
static inline WChar Utf8Consume(Titr &s)
inline char32_t Utf8Consume(Titr &s)
{
WChar c;
char32_t c;
s += Utf8Decode(&c, &*s);
return c;
}
@@ -113,7 +101,7 @@ static inline WChar Utf8Consume(Titr &s)
* @param c Unicode character.
* @return Length of UTF-8 encoding for character.
*/
static inline int8 Utf8CharLen(WChar c)
inline int8_t Utf8CharLen(char32_t c)
{
if (c < 0x80) return 1;
if (c < 0x800) return 2;
@@ -132,7 +120,7 @@ static inline int8 Utf8CharLen(WChar c)
* @param c char to query length of
* @return requested size
*/
static inline int8 Utf8EncodedCharLen(char c)
inline int8_t Utf8EncodedCharLen(char c)
{
if (GB(c, 3, 5) == 0x1E) return 4;
if (GB(c, 4, 4) == 0x0E) return 3;
@@ -145,7 +133,7 @@ static inline int8 Utf8EncodedCharLen(char c)
/* Check if the given character is part of a UTF8 sequence */
static inline bool IsUtf8Part(char c)
inline bool IsUtf8Part(char c)
{
return GB(c, 6, 2) == 2;
}
@@ -157,14 +145,14 @@ static inline bool IsUtf8Part(char c)
* @note The function should not be used to determine the length of the previous
* encoded char because it might be an invalid/corrupt start-sequence
*/
static inline char *Utf8PrevChar(char *s)
inline char *Utf8PrevChar(char *s)
{
char *ret = s;
while (IsUtf8Part(*--ret)) {}
return ret;
}
static inline const char *Utf8PrevChar(const char *s)
inline const char *Utf8PrevChar(const char *s)
{
const char *ret = s;
while (IsUtf8Part(*--ret)) {}
@@ -179,7 +167,7 @@ size_t Utf8StringLength(const std::string &str);
* @param c The character to test.
* @return True if the character is a lead surrogate code point.
*/
static inline bool Utf16IsLeadSurrogate(uint c)
inline bool Utf16IsLeadSurrogate(uint c)
{
return c >= 0xD800 && c <= 0xDBFF;
}
@@ -189,7 +177,7 @@ static inline bool Utf16IsLeadSurrogate(uint c)
* @param c The character to test.
* @return True if the character is a lead surrogate code point.
*/
static inline bool Utf16IsTrailSurrogate(uint c)
inline bool Utf16IsTrailSurrogate(uint c)
{
return c >= 0xDC00 && c <= 0xDFFF;
}
@@ -200,7 +188,7 @@ static inline bool Utf16IsTrailSurrogate(uint c)
* @param trail Trail surrogate code point.
* @return Decoded Unicode character.
*/
static inline WChar Utf16DecodeSurrogate(uint lead, uint trail)
inline char32_t Utf16DecodeSurrogate(uint lead, uint trail)
{
return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
}
@@ -210,7 +198,7 @@ static inline WChar Utf16DecodeSurrogate(uint lead, uint trail)
* @param c Pointer to one or two UTF-16 code points.
* @return Decoded Unicode character.
*/
static inline WChar Utf16DecodeChar(const uint16 *c)
inline char32_t Utf16DecodeChar(const uint16_t *c)
{
if (Utf16IsLeadSurrogate(c[0])) {
return Utf16DecodeSurrogate(c[0], c[1]);
@@ -225,7 +213,7 @@ static inline WChar Utf16DecodeChar(const uint16 *c)
* @return true iff the character is used to influence
* the text direction.
*/
static inline bool IsTextDirectionChar(WChar c)
inline bool IsTextDirectionChar(char32_t c)
{
switch (c) {
case CHAR_TD_LRM:
@@ -242,7 +230,7 @@ static inline bool IsTextDirectionChar(WChar c)
}
}
static inline bool IsPrintable(WChar c)
inline bool IsPrintable(char32_t c)
{
if (c < 0x20) return false;
if (c < 0xE000) return true;
@@ -257,7 +245,7 @@ static inline bool IsPrintable(WChar c)
* @return a boolean value whether 'c' is a whitespace character or not
* @see http://www.fileformat.info/info/unicode/category/Zs/list.htm
*/
static inline bool IsWhitespace(WChar c)
inline bool IsWhitespace(char32_t c)
{
return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
}
@@ -275,6 +263,4 @@ static inline bool IsWhitespace(WChar c)
char *strcasestr(const char *haystack, const char *needle);
#endif /* strcasestr is available */
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front = false);
#endif /* STRING_FUNC_H */