src/string_func.h
author smatz
Sat, 13 Dec 2008 15:59:25 +0000
changeset 10416 b35c0a4c73c5
parent 10308 72ffe5505a38
permissions -rw-r--r--
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
     1
/* $Id$ */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
     2
10299
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
     3
/** @file string_func.h Functions related to low-level strings.
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
     4
 *
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
     5
 * @note Be aware of "dangerous" string functions; string functions that
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
     6
 * have behaviour that could easily cause buffer overruns and such:
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
     7
 * - strncpy: does not '\0' terminate when input string is longer than
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
     8
 *   the size of the output string. Use strecpy instead.
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
     9
 * - [v]snprintf: returns the length of the string as it would be written
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
    10
 *   when the output is large enough, so it can be more than the size of
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
    11
 *   the buffer and than can underflow size_t (uint-ish) which makes all
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
    12
 *   subsequent snprintf alikes write outside of the buffer. Use
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
    13
 *   [v]seprintf instead; it will return the number of bytes actually
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
    14
 *   added so no [v]seprintf will cause outside of bounds writes.
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
    15
 * - [v]sprintf: does not bounds checking: use [v]seprintf instead.
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
    16
 */
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    17
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    18
#ifndef STRING_FUNC_H
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    19
#define STRING_FUNC_H
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    20
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    21
#include "core/bitmath_func.hpp"
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    22
#include "string_type.h"
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    23
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    24
/**
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    25
 * Appends characters from one string to another.
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    26
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    27
 * Appends the source string to the destination string with respect of the
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    28
 * terminating null-character and the maximum size of the destination
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    29
 * buffer.
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    30
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    31
 * @note usage ttd_strlcat(dst, src, lengthof(dst));
10308
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
    32
 * @note lengthof() applies only to fixed size arrays
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    33
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    34
 * @param dst The buffer containing the target string
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    35
 * @param src The buffer containing the string to append
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    36
 * @param size The maximum size of the destination buffer
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    37
 */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    38
void ttd_strlcat(char *dst, const char *src, size_t size);
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    39
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    40
/**
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    41
 * Copies characters from one buffer to another.
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    42
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    43
 * Copies the source string to the destination buffer with respect of the
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    44
 * terminating null-character and the maximum size of the destination
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    45
 * buffer.
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    46
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    47
 * @note usage ttd_strlcpy(dst, src, lengthof(dst));
10308
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
    48
 * @note lengthof() applies only to fixed size arrays
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    49
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    50
 * @param dst The destination buffer
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    51
 * @param src The buffer containing the string to copy
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    52
 * @param size The maximum size of the destination buffer
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    53
 */
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    54
void ttd_strlcpy(char *dst, const char *src, size_t size);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    55
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    56
/**
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    57
 * Appends characters from one string to another.
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    58
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    59
 * Appends the source string to the destination string with respect of the
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    60
 * terminating null-character and and the last pointer to the last element
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    61
 * in the destination buffer. If the last pointer is set to NULL no
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    62
 * boundary check is performed.
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    63
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    64
 * @note usage: strecat(dst, src, lastof(dst));
10308
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
    65
 * @note lastof() applies only to fixed size arrays
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    66
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    67
 * @param dst The buffer containing the target string
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    68
 * @param src The buffer containing the string to append
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    69
 * @param last The pointer to the last element of the destination buffer
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    70
 * @return The pointer to the terminating null-character in the destination buffer
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    71
 */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    72
char *strecat(char *dst, const char *src, const char *last);
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    73
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    74
/**
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    75
 * Copies characters from one buffer to another.
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    76
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    77
 * Copies the source string to the destination buffer with respect of the
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    78
 * terminating null-character and the last pointer to the last element in
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    79
 * the destination buffer. If the last pointer is set to NULL no boundary
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    80
 * check is performed.
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    81
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    82
 * @note usage: strecpy(dst, src, lastof(dst));
10308
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
    83
 * @note lastof() applies only to fixed size arrays
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    84
 *
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    85
 * @param dst The destination buffer
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    86
 * @param src The buffer containing the string to copy
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    87
 * @param last The pointer to the last element of the destination buffer
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    88
 * @return The pointer to the terminating null-character in the destination buffer
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    89
 */
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    90
char *strecpy(char *dst, const char *src, const char *last);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    91
10299
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
    92
int CDECL seprintf(char *str, const char *last, const char *format, ...);
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 10044
diff changeset
    93
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    94
char *CDECL str_fmt(const char *str, ...);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    95
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    96
/** Scans the string for valid characters and if it finds invalid ones,
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    97
 * replaces them with a question mark '?' */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    98
void str_validate(char *str);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
    99
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   100
/** Scans the string for colour codes and strips them */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   101
void str_strip_colours(char *str);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   102
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   103
/** Convert the given string to lowercase, only works with ASCII! */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   104
void strtolower(char *str);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   105
10308
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   106
/**
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   107
 * Check if a string buffer is empty.
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   108
 *
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   109
 * @param s The pointer to the firste element of the buffer
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   110
 * @return true if the buffer starts with the terminating null-character or
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   111
 *         if the given pointer points to NULL else return false
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   112
 */
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   113
static inline bool StrEmpty(const char *s)
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   114
{
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   115
	return s == NULL || s[0] == '\0';
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   116
}
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   117
10308
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   118
/**
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   119
 * Get the length of a string, within a limited buffer.
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   120
 *
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   121
 * @param str The pointer to the firste element of the buffer
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   122
 * @param maxlen The maximum size of the buffer
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   123
 * @return The length of the string
72ffe5505a38 (svn r14553) -Doc: Add some doxygen comments
skidd13
parents: 10303
diff changeset
   124
 */
9146
dbe2317185eb (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 8711
diff changeset
   125
static inline size_t ttd_strnlen(const char *str, size_t maxlen)
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   126
{
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   127
	const char *t;
10044
a324ba35da2e (svn r14204) -Fix (r7475): when determining length of a string with limited size, first check if we are not out of bounds already
smatz
parents: 9998
diff changeset
   128
	for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   129
	return t - str;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   130
}
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   131
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   132
/** Convert the md5sum number to a 'hexadecimal' string, return next pos in buffer */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   133
char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   134
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   135
/**
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   136
 * Only allow certain keys. You can define the filter to be used. This makes
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   137
 *  sure no invalid keys can get into an editbox, like BELL.
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   138
 * @param key character to be checked
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   139
 * @param afilter the filter to use
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   140
 * @return true or false depending if the character is printable/valid or not
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   141
 */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   142
bool IsValidChar(WChar key, CharSetFilter afilter);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   143
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   144
size_t Utf8Decode(WChar *c, const char *s);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   145
size_t Utf8Encode(char *buf, WChar c);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   146
size_t Utf8TrimString(char *s, size_t maxlen);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   147
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   148
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   149
static inline WChar Utf8Consume(const char **s)
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   150
{
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   151
	WChar c;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   152
	*s += Utf8Decode(&c, *s);
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   153
	return c;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   154
}
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   155
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   156
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   157
/** Return the length of a UTF-8 encoded character.
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   158
 * @param c Unicode character.
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   159
 * @return Length of UTF-8 encoding for character.
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   160
 */
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9146
diff changeset
   161
static inline int8 Utf8CharLen(WChar c)
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   162
{
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   163
	if (c < 0x80)       return 1;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   164
	if (c < 0x800)      return 2;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   165
	if (c < 0x10000)    return 3;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   166
	if (c < 0x110000)   return 4;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   167
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   168
	/* Invalid valid, we encode as a '?' */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   169
	return 1;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   170
}
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   171
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   172
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   173
/**
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   174
 * Return the length of an UTF-8 encoded value based on a single char. This
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   175
 * char should be the first byte of the UTF-8 encoding. If not, or encoding
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   176
 * is invalid, return value is 0
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   177
 * @param c char to query length of
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   178
 * @return requested size
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   179
 */
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9146
diff changeset
   180
static inline int8 Utf8EncodedCharLen(char c)
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   181
{
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   182
	if (GB(c, 3, 5) == 0x1E) return 4;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   183
	if (GB(c, 4, 4) == 0x0E) return 3;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   184
	if (GB(c, 5, 3) == 0x06) return 2;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   185
	if (GB(c, 7, 1) == 0x00) return 1;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   186
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   187
	/* Invalid UTF8 start encoding */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   188
	return 0;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   189
}
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   190
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   191
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   192
/* Check if the given character is part of a UTF8 sequence */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   193
static inline bool IsUtf8Part(char c)
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   194
{
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   195
	return GB(c, 6, 2) == 2;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   196
}
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   197
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   198
/**
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   199
 * Retrieve the previous UNICODE character in an UTF-8 encoded string.
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   200
 * @param s char pointer pointing to (the first char of) the next character
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   201
 * @return a pointer in 's' to the previous UNICODE character's first byte
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   202
 * @note The function should not be used to determine the length of the previous
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   203
 * encoded char because it might be an invalid/corrupt start-sequence
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   204
 */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   205
static inline char *Utf8PrevChar(const char *s)
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   206
{
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   207
	const char *ret = s;
8695
6607e9b9ffe2 (svn r12368) -Codechange: use explicit body for loops and conditions and remove -Wno-empty-body from the configure script
smatz
parents: 8348
diff changeset
   208
	while (IsUtf8Part(*--ret)) {}
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   209
	return (char*)ret;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   210
}
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   211
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   212
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   213
static inline bool IsPrintable(WChar c)
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   214
{
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   215
	if (c < 0x20)   return false;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   216
	if (c < 0xE000) return true;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   217
	if (c < 0xE200) return false;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   218
	return true;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   219
}
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   220
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   221
/**
8711
1d6f1fd43e83 (svn r12385) -Fix [FS#ln]: Non-breaking spaces should not be broken.
peter1138
parents: 8695
diff changeset
   222
 * Check whether UNICODE character is whitespace or not, i.e. whether
1d6f1fd43e83 (svn r12385) -Fix [FS#ln]: Non-breaking spaces should not be broken.
peter1138
parents: 8695
diff changeset
   223
 * this is a potential line-break character.
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   224
 * @param c UNICODE character to check
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   225
 * @return a boolean value whether 'c' is a whitespace character or not
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   226
 * @see http://www.fileformat.info/info/unicode/category/Zs/list.htm
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   227
 */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   228
static inline bool IsWhitespace(WChar c)
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   229
{
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   230
	return
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   231
	  c == 0x0020 /* SPACE */ ||
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   232
	  c == 0x3000 /* IDEOGRAPHIC SPACE */
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   233
	;
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   234
}
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
diff changeset
   235
9997
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9390
diff changeset
   236
#ifndef _GNU_SOURCE
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9390
diff changeset
   237
/* strndup is a GNU extension */
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9390
diff changeset
   238
char *strndup(const char *s, size_t len);
9998
2bf26d44bc33 (svn r14155) -Fix (r14154): wrong comments
glx
parents: 9997
diff changeset
   239
#endif /* !_GNU_SOURCE */
9997
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9390
diff changeset
   240
9998
2bf26d44bc33 (svn r14155) -Fix (r14154): wrong comments
glx
parents: 9997
diff changeset
   241
#endif /* STRING_FUNC_H */