m_string.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. Copyright (c) 2000, 2018, 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. /**
  22. @file include/m_string.h
  23. */
  24. #include <float.h>
  25. #include <limits.h>
  26. #include <stdbool.h> // IWYU pragma: keep
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "lex_string.h"
  32. #include "my_config.h"
  33. #include "my_inttypes.h"
  34. #include "my_macros.h"
  35. /**
  36. Definition of the null string (a null pointer of type char *),
  37. used in some of our string handling code. New code should use
  38. nullptr instead.
  39. */
  40. #define NullS (char *)0
  41. /*
  42. my_str_malloc(), my_str_realloc() and my_str_free() are assigned to
  43. implementations in strings/alloc.cc, but can be overridden in
  44. the calling program.
  45. */
  46. extern void *(*my_str_malloc)(size_t);
  47. extern void *(*my_str_realloc)(void *, size_t);
  48. extern void (*my_str_free)(void *);
  49. /* Declared in int2str() */
  50. extern char _dig_vec_upper[];
  51. extern char _dig_vec_lower[];
  52. /* Prototypes for string functions */
  53. extern char *strmake(char *dst, const char *src, size_t length);
  54. extern char *strcont(char *src, const char *set);
  55. extern char *strxmov(char *dst, const char *src, ...);
  56. extern char *strxnmov(char *dst, size_t len, const char *src, ...);
  57. /*
  58. bchange(dst, old_length, src, new_length, tot_length)
  59. replaces old_length characters at dst to new_length characters from
  60. src in a buffer with tot_length bytes.
  61. */
  62. static inline void bchange(uchar *dst, size_t old_length, const uchar *src,
  63. size_t new_length, size_t tot_length) {
  64. memmove(dst + new_length, dst + old_length, tot_length - old_length);
  65. memcpy(dst, src, new_length);
  66. }
  67. /*
  68. strend(s) returns a character pointer to the NUL which ends s. That
  69. is, strend(s)-s == strlen(s). This is useful for adding things at
  70. the end of strings. It is redundant, because strchr(s,'\0') could
  71. be used instead, but this is clearer and faster.
  72. */
  73. static inline const char *strend(const char *s) {
  74. while (*s++)
  75. ;
  76. return s - 1;
  77. }
  78. static inline char *strend(char *s) {
  79. while (*s++)
  80. ;
  81. return s - 1;
  82. }
  83. /*
  84. strcend(s, c) returns a pointer to the first place in s where c
  85. occurs, or a pointer to the end-null of s if c does not occur in s.
  86. */
  87. static inline const char *strcend(const char *s, char c) {
  88. for (;;) {
  89. if (*s == c) return s;
  90. if (!*s++) return s - 1;
  91. }
  92. }
  93. /*
  94. strfill(dest, len, fill) makes a string of fill-characters. The result
  95. string is of length == len. The des+len character is allways set to NULL.
  96. strfill() returns pointer to dest+len;
  97. */
  98. static inline char *strfill(char *s, size_t len, char fill) {
  99. while (len--) *s++ = fill;
  100. *(s) = '\0';
  101. return (s);
  102. }
  103. /*
  104. my_stpmov(dst, src) moves all the characters of src (including the
  105. closing NUL) to dst, and returns a pointer to the new closing NUL in
  106. dst. The similar UNIX routine strcpy returns the old value of dst,
  107. which I have never found useful. my_stpmov(my_stpmov(dst,a),b) moves a//b
  108. into dst, which seems useful.
  109. */
  110. static inline char *my_stpmov(char *dst, const char *src) {
  111. while ((*dst++ = *src++))
  112. ;
  113. return dst - 1;
  114. }
  115. /*
  116. my_stpnmov(dst,src,length) moves length characters, or until end, of src to
  117. dst and appends a closing NUL to dst if src is shorter than length.
  118. The result is a pointer to the first NUL in dst, or is dst+n if dst was
  119. truncated.
  120. */
  121. static inline char *my_stpnmov(char *dst, const char *src, size_t n) {
  122. while (n-- != 0) {
  123. if (!(*dst++ = *src++)) return (char *)dst - 1;
  124. }
  125. return dst;
  126. }
  127. /**
  128. Copy a string from src to dst until (and including) terminating null byte.
  129. @param dst Destination
  130. @param src Source
  131. @note src and dst cannot overlap.
  132. Use my_stpmov() if src and dst overlaps.
  133. @note Unsafe, consider using my_stpnpy() instead.
  134. @return pointer to terminating null byte.
  135. */
  136. static inline char *my_stpcpy(char *dst, const char *src) {
  137. #if defined(HAVE_BUILTIN_STPCPY)
  138. return __builtin_stpcpy(dst, src);
  139. #elif defined(HAVE_STPCPY)
  140. return stpcpy(dst, src);
  141. #else
  142. /* Fallback to implementation supporting overlap. */
  143. return my_stpmov(dst, src);
  144. #endif
  145. }
  146. /**
  147. Copy fixed-size string from src to dst.
  148. @param dst Destination
  149. @param src Source
  150. @param n Maximum number of characters to copy.
  151. @note src and dst cannot overlap
  152. Use my_stpnmov() if src and dst overlaps.
  153. @return pointer to terminating null byte.
  154. */
  155. static inline char *my_stpncpy(char *dst, const char *src, size_t n) {
  156. #if defined(HAVE_STPNCPY)
  157. return stpncpy(dst, src, n);
  158. #else
  159. /* Fallback to implementation supporting overlap. */
  160. return my_stpnmov(dst, src, n);
  161. #endif
  162. }
  163. static inline longlong my_strtoll(const char *nptr, char **endptr, int base) {
  164. #if defined _WIN32
  165. return _strtoi64(nptr, endptr, base);
  166. #else
  167. return strtoll(nptr, endptr, base);
  168. #endif
  169. }
  170. static inline ulonglong my_strtoull(const char *nptr, char **endptr, int base) {
  171. #if defined _WIN32
  172. return _strtoui64(nptr, endptr, base);
  173. #else
  174. return strtoull(nptr, endptr, base);
  175. #endif
  176. }
  177. static inline char *my_strtok_r(char *str, const char *delim, char **saveptr) {
  178. #if defined _WIN32
  179. return strtok_s(str, delim, saveptr);
  180. #else
  181. return strtok_r(str, delim, saveptr);
  182. #endif
  183. }
  184. /* native_ rather than my_ since my_strcasecmp already exists */
  185. static inline int native_strcasecmp(const char *s1, const char *s2) {
  186. #if defined _WIN32
  187. return _stricmp(s1, s2);
  188. #else
  189. return strcasecmp(s1, s2);
  190. #endif
  191. }
  192. /* native_ rather than my_ for consistency with native_strcasecmp */
  193. static inline int native_strncasecmp(const char *s1, const char *s2, size_t n) {
  194. #if defined _WIN32
  195. return _strnicmp(s1, s2, n);
  196. #else
  197. return strncasecmp(s1, s2, n);
  198. #endif
  199. }
  200. /*
  201. is_prefix(s, t) returns 1 if s starts with t.
  202. A empty t is always a prefix.
  203. */
  204. static inline int is_prefix(const char *s, const char *t) {
  205. while (*t)
  206. if (*s++ != *t++) return 0;
  207. return 1; /* WRONG */
  208. }
  209. /* Conversion routines */
  210. typedef enum { MY_GCVT_ARG_FLOAT, MY_GCVT_ARG_DOUBLE } my_gcvt_arg_type;
  211. double my_strtod(const char *str, const char **end, int *error);
  212. double my_atof(const char *nptr);
  213. size_t my_fcvt(double x, int precision, char *to, bool *error);
  214. size_t my_fcvt_compact(double x, char *to, bool *error);
  215. size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
  216. bool *error);
  217. #define NOT_FIXED_DEC 31
  218. /*
  219. The longest string my_fcvt can return is 311 + "precision" bytes.
  220. Here we assume that we never cal my_fcvt() with precision >= NOT_FIXED_DEC
  221. (+ 1 byte for the terminating '\0').
  222. */
  223. #define FLOATING_POINT_BUFFER (311 + NOT_FIXED_DEC)
  224. /*
  225. We want to use the 'e' format in some cases even if we have enough space
  226. for the 'f' one just to mimic sprintf("%.15g") behavior for large integers,
  227. and to improve it for numbers < 10^(-4).
  228. That is, for |x| < 1 we require |x| >= 10^(-15), and for |x| > 1 we require
  229. it to be integer and be <= 10^DBL_DIG for the 'f' format to be used.
  230. We don't lose precision, but make cases like "1e200" or "0.00001" look nicer.
  231. */
  232. #define MAX_DECPT_FOR_F_FORMAT DBL_DIG
  233. /*
  234. The maximum possible field width for my_gcvt() conversion.
  235. (DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
  236. MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
  237. */
  238. #define MY_GCVT_MAX_FIELD_WIDTH \
  239. (DBL_DIG + 4 + MY_MAX(5, MAX_DECPT_FOR_F_FORMAT))
  240. extern char *int2str(long val, char *dst, int radix, int upcase);
  241. C_MODE_START
  242. extern char *int10_to_str(long val, char *dst, int radix);
  243. C_MODE_END
  244. extern const char *str2int(const char *src, int radix, long lower, long upper,
  245. long *val);
  246. longlong my_strtoll10(const char *nptr, const char **endptr, int *error);
  247. #if SIZEOF_LONG == SIZEOF_LONG_LONG
  248. #define ll2str(A, B, C, D) int2str((A), (B), (C), (D))
  249. #define longlong10_to_str(A, B, C) int10_to_str((A), (B), (C))
  250. #undef strtoll
  251. #define strtoll(A, B, C) strtol((A), (B), (C))
  252. #define strtoull(A, B, C) strtoul((A), (B), (C))
  253. #else
  254. extern char *ll2str(longlong val, char *dst, int radix, int upcase);
  255. extern char *longlong10_to_str(longlong val, char *dst, int radix);
  256. #endif
  257. #define longlong2str(A, B, C) ll2str((A), (B), (C), 1)
  258. /*
  259. This function saves a longlong value in a buffer and returns the pointer to
  260. the buffer.
  261. */
  262. static inline char *llstr(longlong value, char *buff) {
  263. longlong10_to_str(value, buff, -10);
  264. return buff;
  265. }
  266. static inline char *ullstr(longlong value, char *buff) {
  267. longlong10_to_str(value, buff, 10);
  268. return buff;
  269. }
  270. #define STRING_WITH_LEN(X) (X), ((sizeof(X) - 1))
  271. #define C_STRING_WITH_LEN(X) ((char *)(X)), ((sizeof(X) - 1))
  272. /**
  273. Skip trailing space (ASCII spaces only).
  274. @return New end of the string.
  275. */
  276. static inline const uchar *skip_trailing_space(const uchar *ptr, size_t len) {
  277. const uchar *end = ptr + len;
  278. while (end - ptr >= 8) {
  279. uint64_t chunk;
  280. memcpy(&chunk, end - 8, sizeof(chunk));
  281. if (chunk != 0x2020202020202020ULL) break;
  282. end -= 8;
  283. }
  284. while (end > ptr && end[-1] == 0x20) end--;
  285. return (end);
  286. }
  287. /*
  288. Format a double (representing number of bytes) into a human-readable string.
  289. @param buf Buffer used for printing
  290. @param buf_len Length of buffer
  291. @param dbl_val Value to be formatted
  292. @note
  293. Sample output format: 42 1K 234M 2G
  294. If we exceed ULLONG_MAX YiB we give up, and convert to "+INF".
  295. @todo Consider writing KiB GiB etc, since we use 1024 rather than 1000
  296. */
  297. static inline void human_readable_num_bytes(char *buf, int buf_len,
  298. double dbl_val) {
  299. const char size[] = {'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
  300. unsigned int i;
  301. for (i = 0; dbl_val > 1024 && i < sizeof(size) - 1; i++) dbl_val /= 1024;
  302. const char mult = size[i];
  303. // 18446744073709551615 Yottabytes should be enough for most ...
  304. if (dbl_val > ULLONG_MAX)
  305. snprintf(buf, buf_len, "+INF");
  306. else
  307. snprintf(buf, buf_len, "%llu%c", (unsigned long long)dbl_val, mult);
  308. }
  309. static inline void lex_string_set(LEX_STRING *lex_str, char *c_str) {
  310. lex_str->str = c_str;
  311. lex_str->length = strlen(c_str);
  312. }
  313. static inline void lex_cstring_set(LEX_CSTRING *lex_str, const char *c_str) {
  314. lex_str->str = c_str;
  315. lex_str->length = strlen(c_str);
  316. }
  317. #endif