Clone of PhatAC @ https://github.com/floaterxk/PhatAC

m_string.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  13. #ifndef _m_string_h
  14. #define _m_string_h
  15. #include "my_global.h" /* HAVE_* */
  16. #include <string.h>
  17. #define bfill please_use_memset_rather_than_bfill
  18. #define bzero please_use_memset_rather_than_bzero
  19. #define bmove please_use_memmove_rather_than_bmove
  20. #define strmov please_use_my_stpcpy_or_my_stpmov_rather_than_strmov
  21. #define strnmov please_use_my_stpncpy_or_my_stpnmov_rather_than_strnmov
  22. #include "mysql/service_my_snprintf.h"
  23. #if defined(__cplusplus)
  24. extern "C" {
  25. #endif
  26. /*
  27. my_str_malloc(), my_str_realloc() and my_str_free() are assigned to
  28. implementations in strings/alloc.c, but can be overridden in
  29. the calling program.
  30. */
  31. extern void *(*my_str_malloc)(size_t);
  32. extern void *(*my_str_realloc)(void *, size_t);
  33. extern void (*my_str_free)(void *);
  34. /* Declared in int2str() */
  35. extern char _dig_vec_upper[];
  36. extern char _dig_vec_lower[];
  37. /* Prototypes for string functions */
  38. extern void bchange(uchar *dst,size_t old_len,const uchar *src,
  39. size_t new_len,size_t tot_len);
  40. extern void strappend(char *s,size_t len,pchar fill);
  41. extern char *strend(const char *s);
  42. extern char *strcend(const char *, pchar);
  43. extern char *strfill(char * s,size_t len,pchar fill);
  44. extern char *strmake(char *dst,const char *src,size_t length);
  45. extern char *my_stpmov(char *dst,const char *src);
  46. extern char *my_stpnmov(char *dst, const char *src, size_t n);
  47. extern char *strcont(const char *src, const char *set);
  48. extern char *strxmov(char *dst, const char *src, ...);
  49. extern char *strxnmov(char *dst, size_t len, const char *src, ...);
  50. /**
  51. Copy a string from src to dst until (and including) terminating null byte.
  52. @param dst Destination
  53. @param src Source
  54. @note src and dst cannot overlap.
  55. Use my_stpmov() if src and dst overlaps.
  56. @note Unsafe, consider using my_stpnpy() instead.
  57. @return pointer to terminating null byte.
  58. */
  59. static inline char *my_stpcpy(char *dst, const char *src)
  60. {
  61. #if defined(HAVE_BUILTIN_STPCPY)
  62. return __builtin_stpcpy(dst, src);
  63. #elif defined(HAVE_STPCPY)
  64. return stpcpy(dst, src);
  65. #else
  66. /* Fallback to implementation supporting overlap. */
  67. return my_stpmov(dst, src);
  68. #endif
  69. }
  70. /**
  71. Copy fixed-size string from src to dst.
  72. @param dst Destination
  73. @param src Source
  74. @param n Maximum number of characters to copy.
  75. @note src and dst cannot overlap
  76. Use my_stpnmov() if src and dst overlaps.
  77. @return pointer to terminating null byte.
  78. */
  79. static inline char *my_stpncpy(char *dst, const char *src, size_t n)
  80. {
  81. #if defined(HAVE_STPNCPY)
  82. return stpncpy(dst, src, n);
  83. #else
  84. /* Fallback to implementation supporting overlap. */
  85. return my_stpnmov(dst, src, n);
  86. #endif
  87. }
  88. static inline longlong my_strtoll(const char *nptr, char **endptr, int base)
  89. {
  90. #if defined _WIN32
  91. return _strtoi64(nptr, endptr, base);
  92. #else
  93. return strtoll(nptr, endptr, base);
  94. #endif
  95. }
  96. static inline ulonglong my_strtoull(const char *nptr, char **endptr, int base)
  97. {
  98. #if defined _WIN32
  99. return _strtoui64(nptr, endptr, base);
  100. #else
  101. return strtoull(nptr, endptr, base);
  102. #endif
  103. }
  104. static inline char *my_strtok_r(char *str, const char *delim, char **saveptr)
  105. {
  106. #if defined _WIN32
  107. return strtok_s(str, delim, saveptr);
  108. #else
  109. return strtok_r(str, delim, saveptr);
  110. #endif
  111. }
  112. /* native_ rather than my_ since my_strcasecmp already exists */
  113. static inline int native_strcasecmp(const char *s1, const char *s2)
  114. {
  115. #if defined _WIN32
  116. return _stricmp(s1, s2);
  117. #else
  118. return strcasecmp(s1, s2);
  119. #endif
  120. }
  121. /* native_ rather than my_ for consistency with native_strcasecmp */
  122. static inline int native_strncasecmp(const char *s1, const char *s2, size_t n)
  123. {
  124. #if defined _WIN32
  125. return _strnicmp(s1, s2, n);
  126. #else
  127. return strncasecmp(s1, s2, n);
  128. #endif
  129. }
  130. /* Prototypes of normal stringfunctions (with may ours) */
  131. #ifndef HAVE_STRNLEN
  132. extern size_t strnlen(const char *s, size_t n);
  133. #endif
  134. extern int is_prefix(const char *, const char *);
  135. /* Conversion routines */
  136. typedef enum {
  137. MY_GCVT_ARG_FLOAT,
  138. MY_GCVT_ARG_DOUBLE
  139. } my_gcvt_arg_type;
  140. double my_strtod(const char *str, char **end, int *error);
  141. double my_atof(const char *nptr);
  142. size_t my_fcvt(double x, int precision, char *to, my_bool *error);
  143. size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
  144. my_bool *error);
  145. #define NOT_FIXED_DEC 31
  146. /*
  147. The longest string my_fcvt can return is 311 + "precision" bytes.
  148. Here we assume that we never cal my_fcvt() with precision >= NOT_FIXED_DEC
  149. (+ 1 byte for the terminating '\0').
  150. */
  151. #define FLOATING_POINT_BUFFER (311 + NOT_FIXED_DEC)
  152. /*
  153. We want to use the 'e' format in some cases even if we have enough space
  154. for the 'f' one just to mimic sprintf("%.15g") behavior for large integers,
  155. and to improve it for numbers < 10^(-4).
  156. That is, for |x| < 1 we require |x| >= 10^(-15), and for |x| > 1 we require
  157. it to be integer and be <= 10^DBL_DIG for the 'f' format to be used.
  158. We don't lose precision, but make cases like "1e200" or "0.00001" look nicer.
  159. */
  160. #define MAX_DECPT_FOR_F_FORMAT DBL_DIG
  161. /*
  162. The maximum possible field width for my_gcvt() conversion.
  163. (DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
  164. MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
  165. */
  166. #define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + MY_MAX(5, MAX_DECPT_FOR_F_FORMAT)) \
  167. extern char *llstr(longlong value,char *buff);
  168. extern char *ullstr(longlong value,char *buff);
  169. extern char *int2str(long val, char *dst, int radix, int upcase);
  170. extern char *int10_to_str(long val,char *dst,int radix);
  171. extern char *str2int(const char *src,int radix,long lower,long upper,
  172. long *val);
  173. longlong my_strtoll10(const char *nptr, char **endptr, int *error);
  174. #if SIZEOF_LONG == SIZEOF_LONG_LONG
  175. #define ll2str(A,B,C,D) int2str((A),(B),(C),(D))
  176. #define longlong10_to_str(A,B,C) int10_to_str((A),(B),(C))
  177. #undef strtoll
  178. #define strtoll(A,B,C) strtol((A),(B),(C))
  179. #define strtoull(A,B,C) strtoul((A),(B),(C))
  180. #else
  181. extern char *ll2str(longlong val,char *dst,int radix, int upcase);
  182. extern char *longlong10_to_str(longlong val,char *dst,int radix);
  183. #endif
  184. #define longlong2str(A,B,C) ll2str((A),(B),(C),1)
  185. #if defined(__cplusplus)
  186. }
  187. #endif
  188. /*
  189. LEX_STRING -- a pair of a C-string and its length.
  190. (it's part of the plugin API as a MYSQL_LEX_STRING)
  191. Ditto LEX_CSTRING/MYSQL_LEX_CSTRING.
  192. */
  193. #include <mysql/mysql_lex_string.h>
  194. typedef struct st_mysql_lex_string LEX_STRING;
  195. typedef struct st_mysql_const_lex_string LEX_CSTRING;
  196. #define STRING_WITH_LEN(X) (X), ((sizeof(X) - 1))
  197. #define USTRING_WITH_LEN(X) ((uchar*) X), ((sizeof(X) - 1))
  198. #define C_STRING_WITH_LEN(X) ((char *) (X)), ((sizeof(X) - 1))
  199. /**
  200. Skip trailing space.
  201. On most systems reading memory in larger chunks (ideally equal to the size of
  202. the chinks that the machine physically reads from memory) causes fewer memory
  203. access loops and hence increased performance.
  204. This is why the 'int' type is used : it's closest to that (according to how
  205. it's defined in C).
  206. So when we determine the amount of whitespace at the end of a string we do
  207. the following :
  208. 1. We divide the string into 3 zones :
  209. a) from the start of the string (__start) to the first multiple
  210. of sizeof(int) (__start_words)
  211. b) from the end of the string (__end) to the last multiple of sizeof(int)
  212. (__end_words)
  213. c) a zone that is aligned to sizeof(int) and can be safely accessed
  214. through an int *
  215. 2. We start comparing backwards from (c) char-by-char. If all we find is
  216. space then we continue
  217. 3. If there are elements in zone (b) we compare them as unsigned ints to a
  218. int mask (SPACE_INT) consisting of all spaces
  219. 4. Finally we compare the remaining part (a) of the string char by char.
  220. This covers for the last non-space unsigned int from 3. (if any)
  221. This algorithm works well for relatively larger strings, but it will slow
  222. the things down for smaller strings (because of the additional calculations
  223. and checks compared to the naive method). Thus the barrier of length 20
  224. is added.
  225. @param ptr pointer to the input string
  226. @param len the length of the string
  227. @return the last non-space character
  228. */
  229. #if defined(__sparc) || defined(__sparcv9)
  230. static inline const uchar *skip_trailing_space(const uchar *ptr,size_t len)
  231. {
  232. /* SPACE_INT is a word that contains only spaces */
  233. #if SIZEOF_INT == 4
  234. const unsigned SPACE_INT= 0x20202020U;
  235. #elif SIZEOF_INT == 8
  236. const unsigned SPACE_INT= 0x2020202020202020ULL;
  237. #else
  238. #error define the appropriate constant for a word full of spaces
  239. #endif
  240. const uchar *end= ptr + len;
  241. if (len > 20)
  242. {
  243. const uchar *end_words= (const uchar *)(intptr)
  244. (((ulonglong)(intptr)end) / SIZEOF_INT * SIZEOF_INT);
  245. const uchar *start_words= (const uchar *)(intptr)
  246. ((((ulonglong)(intptr)ptr) + SIZEOF_INT - 1) / SIZEOF_INT * SIZEOF_INT);
  247. DBUG_ASSERT(end_words > ptr);
  248. while (end > end_words && end[-1] == 0x20)
  249. end--;
  250. if (end[-1] == 0x20 && start_words < end_words)
  251. while (end > start_words && ((unsigned *)end)[-1] == SPACE_INT)
  252. end -= SIZEOF_INT;
  253. }
  254. while (end > ptr && end[-1] == 0x20)
  255. end--;
  256. return (end);
  257. }
  258. #else
  259. /*
  260. Reads 8 bytes at a time, ignoring alignment.
  261. We use uint8korr, which is fast (it simply reads a *ulonglong)
  262. on all platforms, except sparc.
  263. */
  264. static inline const uchar *skip_trailing_space(const uchar *ptr, size_t len)
  265. {
  266. const uchar *end= ptr + len;
  267. while (end - ptr >= 8)
  268. {
  269. if (uint8korr(end-8) != 0x2020202020202020ULL)
  270. break;
  271. end-= 8;
  272. }
  273. while (end > ptr && end[-1] == 0x20)
  274. end--;
  275. return (end);
  276. }
  277. #endif
  278. static inline void lex_string_set(LEX_STRING *lex_str, const char *c_str)
  279. {
  280. lex_str->str= (char *) c_str;
  281. lex_str->length= strlen(c_str);
  282. }
  283. static inline void lex_cstring_set(LEX_CSTRING *lex_str, const char *c_str)
  284. {
  285. lex_str->str= c_str;
  286. lex_str->length= strlen(c_str);
  287. }
  288. #endif