m_string.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Copyright (c) 2000, 2012, Oracle and/or its affiliates.
  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. /* There may be prolems include all of theese. Try to test in
  14. configure with ones are needed? */
  15. /* This is needed for the definitions of strchr... on solaris */
  16. #ifndef _m_string_h
  17. #define _m_string_h
  18. #include "my_global.h" /* HAVE_* */
  19. #ifndef __USE_GNU
  20. #define __USE_GNU /* We want to use stpcpy */
  21. #endif
  22. #if defined(HAVE_STRINGS_H)
  23. #include <strings.h>
  24. #endif
  25. #if defined(HAVE_STRING_H)
  26. #include <string.h>
  27. #endif
  28. /* This is needed for the definitions of memcpy... on solaris */
  29. #if defined(HAVE_MEMORY_H) && !defined(__cplusplus)
  30. #include <memory.h>
  31. #endif
  32. #if !defined(HAVE_MEMCPY) && !defined(HAVE_MEMMOVE)
  33. # define memcpy(d, s, n) bcopy ((s), (d), (n))
  34. # define memset(A,C,B) bfill((A),(B),(C))
  35. # define memmove(d, s, n) bmove ((d), (s), (n))
  36. #elif defined(HAVE_MEMMOVE)
  37. # define bmove(d, s, n) memmove((d), (s), (n))
  38. #endif
  39. /* Unixware 7 */
  40. #if !defined(HAVE_BFILL)
  41. # define bfill(A,B,C) memset((A),(C),(B))
  42. #endif
  43. # define bmove_align(A,B,C) memcpy((A),(B),(C))
  44. # define bcmp(A,B,C) memcmp((A),(B),(C))
  45. #if !defined(bzero)
  46. # define bzero(A,B) memset((A),0,(B))
  47. #endif
  48. #if defined(__cplusplus)
  49. extern "C" {
  50. #endif
  51. /*
  52. my_str_malloc(), my_str_realloc() and my_str_free() are assigned to
  53. implementations in strings/alloc.c, but can be overridden in
  54. the calling program.
  55. */
  56. extern void *(*my_str_malloc)(size_t);
  57. extern void *(*my_str_realloc)(void *, size_t);
  58. extern void (*my_str_free)(void *);
  59. #if defined(HAVE_STPCPY) && MY_GNUC_PREREQ(3, 4) && !defined(__INTEL_COMPILER)
  60. #define strmov(A,B) __builtin_stpcpy((A),(B))
  61. #elif defined(HAVE_STPCPY)
  62. #define strmov(A,B) stpcpy((A),(B))
  63. #endif
  64. /* Declared in int2str() */
  65. extern const char _dig_vec_upper[];
  66. extern const char _dig_vec_lower[];
  67. extern char *strmov_overlapp(char *dest, const char *src);
  68. #if defined(_lint) || defined(FORCE_INIT_OF_VARS)
  69. #define LINT_INIT_STRUCT(var) bzero(&var, sizeof(var)) /* No uninitialize-warning */
  70. #else
  71. #define LINT_INIT_STRUCT(var)
  72. #endif
  73. /* Prototypes for string functions */
  74. extern void bmove_upp(uchar *dst,const uchar *src,size_t len);
  75. extern void bchange(uchar *dst,size_t old_len,const uchar *src,
  76. size_t new_len,size_t tot_len);
  77. extern void strappend(char *s,size_t len,pchar fill);
  78. extern char *strend(const char *s);
  79. extern char *strcend(const char *, pchar);
  80. extern char *strfill(char * s,size_t len,pchar fill);
  81. extern char *strmake(char *dst,const char *src,size_t length);
  82. #if !defined(__GNUC__) || (__GNUC__ < 4)
  83. #define strmake_buf(D,S) strmake(D, S, sizeof(D) - 1)
  84. #else
  85. #define strmake_buf(D,S) ({ \
  86. compile_time_assert(sizeof(D) != sizeof(char*)); \
  87. strmake(D, S, sizeof(D) - 1); \
  88. })
  89. #endif
  90. #ifndef strmov
  91. extern char *strmov(char *dst,const char *src);
  92. #endif
  93. extern char *strnmov(char *dst, const char *src, size_t n);
  94. extern char *strcont(const char *src, const char *set);
  95. extern char *strxmov(char *dst, const char *src, ...);
  96. extern char *strxnmov(char *dst, size_t len, const char *src, ...);
  97. /* Prototypes of normal stringfunctions (with may ours) */
  98. #ifndef HAVE_STRNLEN
  99. extern size_t strnlen(const char *s, size_t n);
  100. #endif
  101. extern int is_prefix(const char *, const char *);
  102. /* Conversion routines */
  103. typedef enum {
  104. MY_GCVT_ARG_FLOAT,
  105. MY_GCVT_ARG_DOUBLE
  106. } my_gcvt_arg_type;
  107. double my_strtod(const char *str, char **end, int *error);
  108. double my_atof(const char *nptr);
  109. size_t my_fcvt(double x, int precision, char *to, my_bool *error);
  110. size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
  111. my_bool *error);
  112. #define NOT_FIXED_DEC 31
  113. /*
  114. The longest string my_fcvt can return is 311 + "precision" bytes.
  115. Here we assume that we never cal my_fcvt() with precision >= NOT_FIXED_DEC
  116. (+ 1 byte for the terminating '\0').
  117. */
  118. #define FLOATING_POINT_BUFFER (311 + NOT_FIXED_DEC)
  119. /*
  120. We want to use the 'e' format in some cases even if we have enough space
  121. for the 'f' one just to mimic sprintf("%.15g") behavior for large integers,
  122. and to improve it for numbers < 10^(-4).
  123. That is, for |x| < 1 we require |x| >= 10^(-15), and for |x| > 1 we require
  124. it to be integer and be <= 10^DBL_DIG for the 'f' format to be used.
  125. We don't lose precision, but make cases like "1e200" or "0.00001" look nicer.
  126. */
  127. #define MAX_DECPT_FOR_F_FORMAT DBL_DIG
  128. /*
  129. The maximum possible field width for my_gcvt() conversion.
  130. (DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
  131. MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
  132. */
  133. #define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + MY_MAX(5, MAX_DECPT_FOR_F_FORMAT)) \
  134. extern char *llstr(longlong value,char *buff);
  135. extern char *ullstr(longlong value,char *buff);
  136. #ifndef HAVE_STRTOUL
  137. extern long strtol(const char *str, char **ptr, int base);
  138. extern ulong strtoul(const char *str, char **ptr, int base);
  139. #endif
  140. extern char *int2str(long val, char *dst, int radix, int upcase);
  141. extern char *int10_to_str(long val,char *dst,int radix);
  142. extern char *str2int(const char *src,int radix,long lower,long upper,
  143. long *val);
  144. longlong my_strtoll10(const char *nptr, char **endptr, int *error);
  145. #if SIZEOF_LONG == SIZEOF_LONG_LONG
  146. #define ll2str(A,B,C,D) int2str((A),(B),(C),(D))
  147. #define longlong10_to_str(A,B,C) int10_to_str((A),(B),(C))
  148. #undef strtoll
  149. #define strtoll(A,B,C) strtol((A),(B),(C))
  150. #define strtoull(A,B,C) strtoul((A),(B),(C))
  151. #ifndef HAVE_STRTOULL
  152. #define HAVE_STRTOULL
  153. #endif
  154. #ifndef HAVE_STRTOLL
  155. #define HAVE_STRTOLL
  156. #endif
  157. #else
  158. #ifdef HAVE_LONG_LONG
  159. extern char *ll2str(longlong val,char *dst,int radix, int upcase);
  160. extern char *longlong10_to_str(longlong val,char *dst,int radix);
  161. #if (!defined(HAVE_STRTOULL) || defined(NO_STRTOLL_PROTO))
  162. extern longlong strtoll(const char *str, char **ptr, int base);
  163. extern ulonglong strtoull(const char *str, char **ptr, int base);
  164. #endif
  165. #endif
  166. #endif
  167. #define longlong2str(A,B,C) ll2str((A),(B),(C),1)
  168. #if defined(__cplusplus)
  169. }
  170. #endif
  171. #include <mysql/plugin.h>
  172. #define STRING_WITH_LEN(X) (X), ((size_t) (sizeof(X) - 1))
  173. #define USTRING_WITH_LEN(X) ((uchar*) X), ((size_t) (sizeof(X) - 1))
  174. #define C_STRING_WITH_LEN(X) ((char *) (X)), ((size_t) (sizeof(X) - 1))
  175. struct st_mysql_const_lex_string
  176. {
  177. const char *str;
  178. size_t length;
  179. };
  180. typedef struct st_mysql_const_lex_string LEX_CSTRING;
  181. /* A variant with const and unsigned */
  182. struct st_mysql_const_unsigned_lex_string
  183. {
  184. const uchar *str;
  185. size_t length;
  186. };
  187. typedef struct st_mysql_const_unsigned_lex_string LEX_CUSTRING;
  188. static inline void lex_string_set(LEX_STRING *lex_str, const char *c_str)
  189. {
  190. lex_str->str= (char *) c_str;
  191. lex_str->length= strlen(c_str);
  192. }
  193. #ifdef __cplusplus
  194. static inline char *safe_str(char *str)
  195. { return str ? str : const_cast<char*>(""); }
  196. #endif
  197. static inline const char *safe_str(const char *str)
  198. { return str ? str : ""; }
  199. static inline size_t safe_strlen(const char *str)
  200. { return str ? strlen(str) : 0; }
  201. #endif