2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* @file string_func.h Functions related to low-level strings.
|
2008-10-28 14:42:31 +00:00
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
2007-04-04 01:35:16 +00:00
|
|
|
|
2008-01-07 14:23:25 +00:00
|
|
|
#ifndef STRING_FUNC_H
|
|
|
|
#define STRING_FUNC_H
|
2005-02-06 13:41:02 +00:00
|
|
|
|
2014-04-25 19:25:38 +00:00
|
|
|
#include <stdarg.h>
|
2020-05-17 21:31:51 +00:00
|
|
|
#include <iosfwd>
|
2024-01-05 22:35:11 +00:00
|
|
|
#include <iterator>
|
2014-04-25 19:25:38 +00:00
|
|
|
|
2007-12-21 19:21:21 +00:00
|
|
|
#include "core/bitmath_func.hpp"
|
2023-11-13 23:26:05 +00:00
|
|
|
#include "core/span_type.hpp"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_type.h"
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2021-04-28 21:06:47 +00:00
|
|
|
char *strecat(char *dst, const char *src, const char *last) NOACCESS(3);
|
2021-09-18 00:28:57 +00:00
|
|
|
char *strecpy(char *dst, const char *src, const char *last, bool quiet_mode = false) NOACCESS(3);
|
2021-04-28 21:06:47 +00:00
|
|
|
char *stredup(const char *src, const char *last = nullptr) NOACCESS(2);
|
2005-02-06 13:41:02 +00:00
|
|
|
|
2021-04-28 21:06:47 +00:00
|
|
|
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);
|
2008-10-28 14:42:31 +00:00
|
|
|
|
2019-05-22 18:56:19 +00:00
|
|
|
std::string CDECL stdstr_fmt(const char *str, ...) WARN_FORMAT(1, 2);
|
2021-06-10 22:43:41 +00:00
|
|
|
std::string stdstr_vfmt(const char *str, va_list va) WARN_FORMAT(1, 0);
|
2005-07-29 21:52:20 +00:00
|
|
|
|
2023-11-13 23:26:05 +00:00
|
|
|
std::string FormatArrayAsHex(span<const byte> data);
|
|
|
|
|
2021-10-18 17:01:27 +00:00
|
|
|
char *StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
|
2023-05-09 19:35:50 +00:00
|
|
|
[[nodiscard]] std::string StrMakeValid(std::string_view str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
|
2021-05-29 13:18:51 +00:00
|
|
|
void StrMakeValidInPlace(char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
|
2013-07-04 16:36:47 +00:00
|
|
|
|
2021-09-18 00:28:57 +00:00
|
|
|
const char *str_fix_scc_encoded(char *str, const char *last) NOACCESS(2);
|
2006-11-16 17:41:24 +00:00
|
|
|
void str_strip_colours(char *str);
|
2024-01-05 21:12:54 +00:00
|
|
|
const char *strip_leading_colours(const char *str);
|
|
|
|
|
|
|
|
inline const char *strip_leading_colours(const std::string &str)
|
|
|
|
{
|
|
|
|
return strip_leading_colours(str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-16 02:04:57 +00:00
|
|
|
std::string str_strip_all_scc(const char *str);
|
2024-01-07 16:41:53 +00:00
|
|
|
char *str_replace_wchar(char *str, const char *last, char32_t find, char32_t replace);
|
2011-07-16 18:43:22 +00:00
|
|
|
bool strtolower(char *str);
|
2020-12-06 20:11:43 +00:00
|
|
|
bool strtolower(std::string &str, std::string::size_type offs = 0);
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2023-04-27 11:21:08 +00:00
|
|
|
[[nodiscard]] bool StrValid(const char *str, const char *last) NOACCESS(2);
|
2021-04-27 20:02:40 +00:00
|
|
|
void StrTrimInPlace(std::string &str);
|
2010-11-10 17:49:14 +00:00
|
|
|
|
2024-01-05 22:13:17 +00:00
|
|
|
const char *StrLastPathSegment(const char *path);
|
|
|
|
|
|
|
|
inline const char *StrLastPathSegment(const std::string &path)
|
|
|
|
{
|
|
|
|
return StrLastPathSegment(path.c_str());
|
|
|
|
}
|
|
|
|
|
2023-04-27 11:21:08 +00:00
|
|
|
[[nodiscard]] bool StrStartsWith(const std::string_view str, const std::string_view prefix);
|
|
|
|
[[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix);
|
|
|
|
[[nodiscard]] bool StrEndsWith(const std::string_view str, const std::string_view suffix);
|
|
|
|
[[nodiscard]] bool StrEndsWithIgnoreCase(std::string_view str, const std::string_view suffix);
|
2021-05-30 09:20:00 +00:00
|
|
|
|
2023-04-27 09:47:08 +00:00
|
|
|
[[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);
|
2023-04-27 13:39:10 +00:00
|
|
|
[[nodiscard]] int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front = false);
|
2023-11-03 19:43:18 +00:00
|
|
|
[[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);
|
2021-05-30 09:20:00 +00:00
|
|
|
|
2023-05-05 11:58:59 +00:00
|
|
|
/** 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; }
|
|
|
|
};
|
2022-12-03 14:28:50 +00:00
|
|
|
|
2008-11-02 11:05:26 +00:00
|
|
|
/**
|
|
|
|
* Check if a string buffer is empty.
|
|
|
|
*
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param s The pointer to the first element of the buffer
|
2008-11-02 11:05:26 +00:00
|
|
|
* @return true if the buffer starts with the terminating null-character or
|
2019-04-10 21:07:06 +00:00
|
|
|
* if the given pointer points to nullptr else return false
|
2008-11-02 11:05:26 +00:00
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline bool StrEmpty(const char *s)
|
2008-11-02 11:05:26 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
return s == nullptr || s[0] == '\0';
|
2008-11-02 11:05:26 +00:00
|
|
|
}
|
2006-12-10 21:39:38 +00:00
|
|
|
|
2008-11-02 11:05:26 +00:00
|
|
|
/**
|
|
|
|
* Get the length of a string, within a limited buffer.
|
|
|
|
*
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param str The pointer to the first element of the buffer
|
2008-11-02 11:05:26 +00:00
|
|
|
* @param maxlen The maximum size of the buffer
|
|
|
|
* @return The length of the string
|
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline size_t ttd_strnlen(const char *str, size_t maxlen)
|
2006-12-10 21:39:38 +00:00
|
|
|
{
|
|
|
|
const char *t;
|
2008-08-31 17:34:03 +00:00
|
|
|
for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
|
2006-12-10 21:39:38 +00:00
|
|
|
return t - str;
|
|
|
|
}
|
|
|
|
|
2024-01-05 21:12:54 +00:00
|
|
|
bool IsValidChar(char32_t key, CharSetFilter afilter);
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2024-01-05 21:12:54 +00:00
|
|
|
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);
|
2007-03-05 00:45:56 +00:00
|
|
|
size_t Utf8TrimString(char *s, size_t maxlen);
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline char32_t Utf8Consume(const char **s)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
2024-01-05 21:12:54 +00:00
|
|
|
char32_t c;
|
2006-11-16 22:05:33 +00:00
|
|
|
*s += Utf8Decode(&c, *s);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2020-05-17 21:31:51 +00:00
|
|
|
template <class Titr>
|
2024-01-06 11:19:27 +00:00
|
|
|
inline char32_t Utf8Consume(Titr &s)
|
2020-05-17 21:31:51 +00:00
|
|
|
{
|
2024-01-05 21:12:54 +00:00
|
|
|
char32_t c;
|
2020-05-17 21:31:51 +00:00
|
|
|
s += Utf8Decode(&c, &*s);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Return the length of a UTF-8 encoded character.
|
2006-11-16 22:05:33 +00:00
|
|
|
* @param c Unicode character.
|
|
|
|
* @return Length of UTF-8 encoding for character.
|
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline int8_t Utf8CharLen(char32_t c)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-05 00:45:56 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param c char to query length of
|
|
|
|
* @return requested size
|
2007-03-05 00:45:56 +00:00
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline int8_t Utf8EncodedCharLen(char c)
|
2007-03-05 00:45:56 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
/* Check if the given character is part of a UTF8 sequence */
|
2024-01-06 11:19:27 +00:00
|
|
|
inline bool IsUtf8Part(char c)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
|
|
|
return GB(c, 6, 2) == 2;
|
|
|
|
}
|
|
|
|
|
2007-03-04 00:49:40 +00:00
|
|
|
/**
|
2007-03-05 00:34:48 +00:00
|
|
|
* Retrieve the previous UNICODE character in an UTF-8 encoded string.
|
|
|
|
* @param s char pointer pointing to (the first char of) the next character
|
2007-04-04 01:35:16 +00:00
|
|
|
* @return a pointer in 's' to the previous UNICODE character's first byte
|
2007-03-05 00:34:48 +00:00
|
|
|
* @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
|
2007-03-04 00:49:40 +00:00
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline char *Utf8PrevChar(char *s)
|
2007-03-04 00:49:40 +00:00
|
|
|
{
|
2009-05-24 20:29:04 +00:00
|
|
|
char *ret = s;
|
2008-03-15 13:21:31 +00:00
|
|
|
while (IsUtf8Part(*--ret)) {}
|
2009-05-24 20:29:04 +00:00
|
|
|
return ret;
|
2007-03-04 00:49:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline const char *Utf8PrevChar(const char *s)
|
2013-08-05 20:35:27 +00:00
|
|
|
{
|
|
|
|
const char *ret = s;
|
|
|
|
while (IsUtf8Part(*--ret)) {}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-05 22:17:25 +00:00
|
|
|
size_t Utf8StringLength(const char *s);
|
2021-05-29 14:02:53 +00:00
|
|
|
size_t Utf8StringLength(const std::string &str);
|
2010-12-05 22:17:25 +00:00
|
|
|
|
2013-08-05 20:35:31 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline bool Utf16IsLeadSurrogate(uint c)
|
2013-08-05 20:35:31 +00:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline bool Utf16IsTrailSurrogate(uint c)
|
2013-08-05 20:35:31 +00:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline char32_t Utf16DecodeSurrogate(uint lead, uint trail)
|
2013-08-05 20:35:31 +00:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline char32_t Utf16DecodeChar(const uint16_t *c)
|
2013-08-05 20:35:31 +00:00
|
|
|
{
|
|
|
|
if (Utf16IsLeadSurrogate(c[0])) {
|
|
|
|
return Utf16DecodeSurrogate(c[0], c[1]);
|
|
|
|
} else {
|
|
|
|
return *c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-22 12:15:58 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline bool IsTextDirectionChar(char32_t c)
|
2010-10-22 12:15:58 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline bool IsPrintable(char32_t c)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
|
|
|
if (c < 0x20) return false;
|
|
|
|
if (c < 0xE000) return true;
|
|
|
|
if (c < 0xE200) return false;
|
|
|
|
return true;
|
|
|
|
}
|
2005-12-20 20:52:05 +00:00
|
|
|
|
2007-03-05 00:45:56 +00:00
|
|
|
/**
|
2008-03-19 20:46:56 +00:00
|
|
|
* Check whether UNICODE character is whitespace or not, i.e. whether
|
|
|
|
* this is a potential line-break character.
|
2007-03-05 00:45:56 +00:00
|
|
|
* @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
|
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline bool IsWhitespace(char32_t c)
|
2007-03-05 00:45:56 +00:00
|
|
|
{
|
2010-11-20 15:44:24 +00:00
|
|
|
return c == 0x0020 /* SPACE */ || c == 0x3000; /* IDEOGRAPHIC SPACE */
|
2007-03-05 00:45:56 +00:00
|
|
|
}
|
|
|
|
|
2010-05-18 21:17:25 +00:00
|
|
|
/* Needed for NetBSD version (so feature) testing */
|
2010-11-22 17:59:11 +00:00
|
|
|
#if defined(__NetBSD__) || defined(__FreeBSD__)
|
2010-05-18 21:17:25 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
|
2009-02-06 15:53:48 +00:00
|
|
|
/* strcasestr is available for _GNU_SOURCE, BSD and some Apple */
|
2010-05-19 17:04:02 +00:00
|
|
|
#if defined(_GNU_SOURCE) || (defined(__BSD_VISIBLE) && __BSD_VISIBLE) || (defined(__APPLE__) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))) || defined(_NETBSD_SOURCE)
|
2009-02-06 15:53:48 +00:00
|
|
|
# undef DEFINE_STRCASESTR
|
|
|
|
#else
|
|
|
|
# define DEFINE_STRCASESTR
|
2010-01-26 14:04:56 +00:00
|
|
|
char *strcasestr(const char *haystack, const char *needle);
|
2009-02-06 15:53:48 +00:00
|
|
|
#endif /* strcasestr is available */
|
|
|
|
|
2008-08-24 17:18:05 +00:00
|
|
|
#endif /* STRING_FUNC_H */
|