Files
@ r28565:9ed8a63bcee5
Branch filter:
Location: cpp/openttd-patchpack/source/src/string_func.h - annotation
r28565:9ed8a63bcee5
7.9 KiB
text/x-c
Add: [Dependabot] introduce Dependabot to keep our workflows up-to-date (#11880)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r15610:623a23fb6560 r15610:623a23fb6560 r10299:def8dff34f3b r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r24211:16bc9477a90c r21414:1c6c3407d809 r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r25296:8cd7b17aba14 r8214:9a3935f9ef4e r28492:0d3d4fd5e362 r26099:9c675ef812ea r25554:3bffaeffba17 r27295:4dfa0d5adc00 r25558:490b113014ff r20531:6a7aeb0059d3 r24523:1bc2cb7bcea1 r8214:9a3935f9ef4e r27189:2e4536857752 r25464:b2a279799858 r16407:50e70180295c r27189:2e4536857752 r27189:2e4536857752 r25648:63a5f82b93cf r27188:363fdf67b081 r27188:363fdf67b081 r27193:e816892ba57b r28087:832d624ba284 r28087:832d624ba284 r27188:363fdf67b081 r28551:f762d05f819e r28551:f762d05f819e r27306:41a10e41285c r27306:41a10e41285c r27306:41a10e41285c r27306:41a10e41285c r27306:41a10e41285c r16407:50e70180295c r10308:acdd1decd6de r10308:acdd1decd6de r19944:25a78576fb5e r10308:acdd1decd6de r23607:36c15679007d r10308:acdd1decd6de r28410:d9c73d685bbc r10308:acdd1decd6de r23607:36c15679007d r10308:acdd1decd6de r8214:9a3935f9ef4e r10308:acdd1decd6de r10308:acdd1decd6de r10308:acdd1decd6de r19944:25a78576fb5e r10308:acdd1decd6de r10308:acdd1decd6de r10308:acdd1decd6de r28410:d9c73d685bbc r8214:9a3935f9ef4e r8214:9a3935f9ef4e r10044:a1e2a7856c4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r27737:728d55b97775 r8214:9a3935f9ef4e r27737:728d55b97775 r27737:728d55b97775 r27737:728d55b97775 r27737:728d55b97775 r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r28410:d9c73d685bbc r8214:9a3935f9ef4e r27737:728d55b97775 r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r24211:16bc9477a90c r28410:d9c73d685bbc r24211:16bc9477a90c r27737:728d55b97775 r24211:16bc9477a90c r24211:16bc9477a90c r24211:16bc9477a90c r24211:16bc9477a90c r15610:623a23fb6560 r15610:623a23fb6560 r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r28410:d9c73d685bbc r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r28410:d9c73d685bbc r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r28410:d9c73d685bbc r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r28410:d9c73d685bbc r8214:9a3935f9ef4e r12010:5513d8f8e97d r8695:51e3c8a897ef r12010:5513d8f8e97d r8214:9a3935f9ef4e r8214:9a3935f9ef4e r28410:d9c73d685bbc r20614:4513b7ad6147 r20614:4513b7ad6147 r20614:4513b7ad6147 r20614:4513b7ad6147 r20614:4513b7ad6147 r20614:4513b7ad6147 r16663:6d591011eeb3 r25560:275e9355119a r16663:6d591011eeb3 r16288:bf9724e2b156 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r28410:d9c73d685bbc r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r28410:d9c73d685bbc r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r28410:d9c73d685bbc r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r28410:d9c73d685bbc r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r20615:a9017d659ca9 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r28410:d9c73d685bbc r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r16288:bf9724e2b156 r8214:9a3935f9ef4e r28410:d9c73d685bbc r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8711:0519f78b68b9 r8711:0519f78b68b9 r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r8214:9a3935f9ef4e r28410:d9c73d685bbc r8214:9a3935f9ef4e r16537:b136370bd3c2 r8214:9a3935f9ef4e r8214:9a3935f9ef4e r15214:245fcdeca80a r16557:c5fb1b96664e r15214:245fcdeca80a r15214:245fcdeca80a r15214:245fcdeca80a r11036:64a843b9f543 r15215:94de5eb4da78 r11036:64a843b9f543 r11036:64a843b9f543 r11036:64a843b9f543 r14352:2d48e17549d1 r11036:64a843b9f543 r11036:64a843b9f543 r9998:a7bc833bb082 | /*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file string_func.h Functions related to low-level strings.
*/
#ifndef STRING_FUNC_H
#define STRING_FUNC_H
#include <iosfwd>
#include "core/bitmath_func.hpp"
#include "string_type.h"
char *strecpy(char *dst, const char *src, const char *last) NOACCESS(3);
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(std::string_view str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
void StrMakeValidInPlace(char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
bool strtolower(std::string &str, std::string::size_type offs = 0);
[[nodiscard]] bool StrValid(const char *str, const char *last) NOACCESS(2);
void StrTrimInPlace(std::string &str);
[[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.
*
* @param s The pointer to the first element of the buffer
* @return true if the buffer starts with the terminating null-character or
* if the given pointer points to nullptr else return false
*/
inline bool StrEmpty(const char *s)
{
return s == nullptr || s[0] == '\0';
}
/**
* Get the length of a string, within a limited buffer.
*
* @param str The pointer to the first element of the buffer
* @param maxlen The maximum size of the buffer
* @return The length of the string
*/
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;
}
bool IsValidChar(char32_t key, CharSetFilter afilter);
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);
inline char32_t Utf8Consume(const char **s)
{
char32_t c;
*s += Utf8Decode(&c, *s);
return c;
}
template <class Titr>
inline char32_t Utf8Consume(Titr &s)
{
char32_t c;
s += Utf8Decode(&c, &*s);
return c;
}
/**
* Return the length of a UTF-8 encoded character.
* @param c Unicode character.
* @return Length of UTF-8 encoding for character.
*/
inline int8_t Utf8CharLen(char32_t c)
{
if (c < 0x80) return 1;
if (c < 0x800) return 2;
if (c < 0x10000) return 3;
if (c < 0x110000) return 4;
/* Invalid valid, we encode as a '?' */
return 1;
}
/**
* Return the length of an UTF-8 encoded value based on a single char. This
* char should be the first byte of the UTF-8 encoding. If not, or encoding
* is invalid, return value is 0
* @param c char to query length of
* @return requested size
*/
inline int8_t Utf8EncodedCharLen(char c)
{
if (GB(c, 3, 5) == 0x1E) return 4;
if (GB(c, 4, 4) == 0x0E) return 3;
if (GB(c, 5, 3) == 0x06) return 2;
if (GB(c, 7, 1) == 0x00) return 1;
/* Invalid UTF8 start encoding */
return 0;
}
/* Check if the given character is part of a UTF8 sequence */
inline bool IsUtf8Part(char c)
{
return GB(c, 6, 2) == 2;
}
/**
* Retrieve the previous UNICODE character in an UTF-8 encoded string.
* @param s char pointer pointing to (the first char of) the next character
* @return a pointer in 's' to the previous UNICODE character's first byte
* @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
*/
inline char *Utf8PrevChar(char *s)
{
char *ret = s;
while (IsUtf8Part(*--ret)) {}
return ret;
}
inline const char *Utf8PrevChar(const char *s)
{
const char *ret = s;
while (IsUtf8Part(*--ret)) {}
return ret;
}
size_t Utf8StringLength(const char *s);
size_t Utf8StringLength(const std::string &str);
/**
* Is the given character a lead surrogate code point?
* @param c The character to test.
* @return True if the character is a lead surrogate code point.
*/
inline bool Utf16IsLeadSurrogate(uint c)
{
return c >= 0xD800 && c <= 0xDBFF;
}
/**
* Is the given character a lead surrogate code point?
* @param c The character to test.
* @return True if the character is a lead surrogate code point.
*/
inline bool Utf16IsTrailSurrogate(uint c)
{
return c >= 0xDC00 && c <= 0xDFFF;
}
/**
* Convert an UTF-16 surrogate pair to the corresponding Unicode character.
* @param lead Lead surrogate code point.
* @param trail Trail surrogate code point.
* @return Decoded Unicode character.
*/
inline char32_t Utf16DecodeSurrogate(uint lead, uint trail)
{
return 0x10000 + (((lead - 0xD800) << 10) | (trail - 0xDC00));
}
/**
* Decode an UTF-16 character.
* @param c Pointer to one or two UTF-16 code points.
* @return Decoded Unicode character.
*/
inline char32_t Utf16DecodeChar(const uint16_t *c)
{
if (Utf16IsLeadSurrogate(c[0])) {
return Utf16DecodeSurrogate(c[0], c[1]);
} else {
return *c;
}
}
/**
* Is the given character a text direction character.
* @param c The character to test.
* @return true iff the character is used to influence
* the text direction.
*/
inline bool IsTextDirectionChar(char32_t c)
{
switch (c) {
case CHAR_TD_LRM:
case CHAR_TD_RLM:
case CHAR_TD_LRE:
case CHAR_TD_RLE:
case CHAR_TD_LRO:
case CHAR_TD_RLO:
case CHAR_TD_PDF:
return true;
default:
return false;
}
}
inline bool IsPrintable(char32_t c)
{
if (c < 0x20) return false;
if (c < 0xE000) return true;
if (c < 0xE200) return false;
return true;
}
/**
* Check whether UNICODE character is whitespace or not, i.e. whether
* this is a potential line-break character.
* @param c UNICODE character to check
* @return a boolean value whether 'c' is a whitespace character or not
* @see http://www.fileformat.info/info/unicode/category/Zs/list.htm
*/
inline bool IsWhitespace(char32_t c)
{
return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
}
/* Needed for NetBSD version (so feature) testing */
#if defined(__NetBSD__) || defined(__FreeBSD__)
#include <sys/param.h>
#endif
/* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
#if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE)
# undef DEFINE_STRCASESTR
#else
# define DEFINE_STRCASESTR
char *strcasestr(const char *haystack, const char *needle);
#endif /* strcasestr is available */
#endif /* STRING_FUNC_H */
|