src/string.h
branchgamebalance
changeset 9908 0fa543611bbe
parent 9895 7bd07f43b0e3
child 6743 cabfaa4a0295
equal deleted inserted replaced
9907:3b068c3a1c74 9908:0fa543611bbe
     1 /* $Id$ */
     1 /* $Id$ */
       
     2 
       
     3 /** @file string.h */
     2 
     4 
     3 #ifndef STRING_H
     5 #ifndef STRING_H
     4 #define STRING_H
     6 #define STRING_H
     5 
     7 
     6 #include "macros.h"
     8 #include "macros.h"
     7 
     9 
     8 /*
    10 /**
     9  * dst: destination buffer
    11  * usage ttd_strlcpy(dst, src, lengthof(dst));
    10  * src: string to copy/concatenate
    12  * @param dst destination buffer
    11  * size: size of the destination buffer
    13  * @param src string to copy/concatenate
    12  * usage: ttd_strlcpy(dst, src, lengthof(dst));
    14  * @param size size of the destination buffer
    13  */
    15  */
    14 void ttd_strlcat(char *dst, const char *src, size_t size);
    16 void ttd_strlcat(char *dst, const char *src, size_t size);
    15 void ttd_strlcpy(char *dst, const char *src, size_t size);
    17 void ttd_strlcpy(char *dst, const char *src, size_t size);
    16 
    18 
    17 /*
    19 /**
    18  * dst: destination buffer
       
    19  * src: string to copy
       
    20  * last: pointer to the last element in the dst array
       
    21  *       if NULL no boundary check is performed
       
    22  * returns a pointer to the terminating \0 in the destination buffer
       
    23  * usage: strecpy(dst, src, lastof(dst));
    20  * usage: strecpy(dst, src, lastof(dst));
       
    21  * @param dst destination buffer
       
    22  * @param src string to copy
       
    23  * @param last pointer to the last element in the dst array
       
    24  *             if NULL no boundary check is performed
       
    25  * @return a pointer to the terminating \0 in the destination buffer
    24  */
    26  */
    25 char* strecat(char* dst, const char* src, const char* last);
    27 char* strecat(char* dst, const char* src, const char* last);
    26 char* strecpy(char* dst, const char* src, const char* last);
    28 char* strecpy(char* dst, const char* src, const char* last);
    27 
    29 
    28 char* CDECL str_fmt(const char* str, ...);
    30 char* CDECL str_fmt(const char* str, ...);
    36 
    38 
    37 /**
    39 /**
    38  * Valid filter types for IsValidChar.
    40  * Valid filter types for IsValidChar.
    39  */
    41  */
    40 enum CharSetFilter {
    42 enum CharSetFilter {
    41 	CS_ALPHANUMERAL,      //! Both numeric and alphabetic and spaces and stuff
    43 	CS_ALPHANUMERAL,      ///< Both numeric and alphabetic and spaces and stuff
    42 	CS_NUMERAL,           //! Only numeric ones
    44 	CS_NUMERAL,           ///< Only numeric ones
    43 	CS_ALPHA,             //! Only alphabetic values
    45 	CS_ALPHA,             ///< Only alphabetic values
    44 };
    46 };
    45 
    47 
    46 /** Convert the given string to lowercase, only works with ASCII! */
    48 /** Convert the given string to lowercase, only works with ASCII! */
    47 void strtolower(char *str);
    49 void strtolower(char *str);
    48 
    50 
   103 
   105 
   104 /**
   106 /**
   105  * Return the length of an UTF-8 encoded value based on a single char. This
   107  * Return the length of an UTF-8 encoded value based on a single char. This
   106  * char should be the first byte of the UTF-8 encoding. If not, or encoding
   108  * char should be the first byte of the UTF-8 encoding. If not, or encoding
   107  * is invalid, return value is 0
   109  * is invalid, return value is 0
       
   110  * @param c char to query length of
       
   111  * @return requested size
   108  */
   112  */
   109 static inline size_t Utf8EncodedCharLen(char c)
   113 static inline size_t Utf8EncodedCharLen(char c)
   110 {
   114 {
   111 	if (GB(c, 3, 5) == 0x1E) return 4;
   115 	if (GB(c, 3, 5) == 0x1E) return 4;
   112 	if (GB(c, 4, 4) == 0x0E) return 3;
   116 	if (GB(c, 4, 4) == 0x0E) return 3;
   125 }
   129 }
   126 
   130 
   127 /**
   131 /**
   128  * Retrieve the previous UNICODE character in an UTF-8 encoded string.
   132  * Retrieve the previous UNICODE character in an UTF-8 encoded string.
   129  * @param s char pointer pointing to (the first char of) the next character
   133  * @param s char pointer pointing to (the first char of) the next character
   130  * @returns a pointer in 's' to the previous UNICODE character's first byte
   134  * @return a pointer in 's' to the previous UNICODE character's first byte
   131  * @note The function should not be used to determine the length of the previous
   135  * @note The function should not be used to determine the length of the previous
   132  * encoded char because it might be an invalid/corrupt start-sequence
   136  * encoded char because it might be an invalid/corrupt start-sequence
   133  */
   137  */
   134 static inline char *Utf8PrevChar(const char *s)
   138 static inline char *Utf8PrevChar(const char *s)
   135 {
   139 {