m_string.h 11 KB

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