MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
m_string.h
1 /*
2  Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 /* There may be prolems include all of theese. Try to test in
18  configure with ones are needed? */
19 
20 /* This is needed for the definitions of strchr... on solaris */
21 
22 #ifndef _m_string_h
23 #define _m_string_h
24 
25 #include "my_global.h" /* HAVE_* */
26 
27 #ifndef __USE_GNU
28 #define __USE_GNU /* We want to use stpcpy */
29 #endif
30 #if defined(HAVE_STRINGS_H)
31 #include <strings.h>
32 #endif
33 #if defined(HAVE_STRING_H)
34 #include <string.h>
35 #endif
36 
37 /* need by my_vsnprintf */
38 #include <stdarg.h>
39 
40 /* This is needed for the definitions of memcpy... on solaris */
41 #if defined(HAVE_MEMORY_H) && !defined(__cplusplus)
42 #include <memory.h>
43 #endif
44 
45 #define bfill please_use_memset_rather_than_bfill()
46 #define bzero please_use_memset_rather_than_bzero()
47 
48 #if !defined(HAVE_MEMCPY) && !defined(HAVE_MEMMOVE)
49 # define memcpy(d, s, n) bcopy ((s), (d), (n))
50 # define memmove(d, s, n) bmove ((d), (s), (n))
51 #elif defined(HAVE_MEMMOVE)
52 # define bmove(d, s, n) memmove((d), (s), (n))
53 #endif
54 
55 #if defined(__cplusplus)
56 extern "C" {
57 #endif
58 
59 /*
60  my_str_malloc(), my_str_realloc() and my_str_free() are assigned to
61  implementations in strings/alloc.c, but can be overridden in
62  the calling program.
63  */
64 extern void *(*my_str_malloc)(size_t);
65 extern void *(*my_str_realloc)(void *, size_t);
66 extern void (*my_str_free)(void *);
67 
68 #if defined(HAVE_STPCPY) && MY_GNUC_PREREQ(3, 4) && !defined(__INTEL_COMPILER)
69 #define strmov(A,B) __builtin_stpcpy((A),(B))
70 #elif defined(HAVE_STPCPY)
71 #define strmov(A,B) stpcpy((A),(B))
72 #ifndef stpcpy
73 extern char *stpcpy(char *, const char *); /* For AIX with gcc 2.95.3 */
74 #endif
75 #endif
76 
77 /* Declared in int2str() */
78 extern char _dig_vec_upper[];
79 extern char _dig_vec_lower[];
80 
81 #ifndef strmov
82 #define strmov_overlapp(A,B) strmov(A,B)
83 #define strmake_overlapp(A,B,C) strmake(A,B,C)
84 #endif
85 
86  /* Prototypes for string functions */
87 
88 extern void bmove_upp(uchar *dst,const uchar *src,size_t len);
89 extern void bchange(uchar *dst,size_t old_len,const uchar *src,
90  size_t new_len,size_t tot_len);
91 extern void strappend(char *s,size_t len,pchar fill);
92 extern char *strend(const char *s);
93 extern char *strcend(const char *, pchar);
94 extern char *strfill(char * s,size_t len,pchar fill);
95 extern char *strmake(char *dst,const char *src,size_t length);
96 
97 #ifndef strmov
98 extern char *strmov(char *dst,const char *src);
99 #else
100 extern char *strmov_overlapp(char *dst,const char *src);
101 #endif
102 extern char *strnmov(char *dst, const char *src, size_t n);
103 extern char *strcont(const char *src, const char *set);
104 extern char *strxmov(char *dst, const char *src, ...);
105 extern char *strxnmov(char *dst, size_t len, const char *src, ...);
106 
107 /* Prototypes of normal stringfunctions (with may ours) */
108 #ifndef HAVE_STRNLEN
109 extern size_t strnlen(const char *s, size_t n);
110 #endif
111 
112 extern int is_prefix(const char *, const char *);
113 
114 /* Conversion routines */
115 typedef enum {
116  MY_GCVT_ARG_FLOAT,
117  MY_GCVT_ARG_DOUBLE
118 } my_gcvt_arg_type;
119 
120 double my_strtod(const char *str, char **end, int *error);
121 double my_atof(const char *nptr);
122 size_t my_fcvt(double x, int precision, char *to, my_bool *error);
123 size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
124  my_bool *error);
125 
126 #define NOT_FIXED_DEC 31
127 
128 /*
129  The longest string my_fcvt can return is 311 + "precision" bytes.
130  Here we assume that we never cal my_fcvt() with precision >= NOT_FIXED_DEC
131  (+ 1 byte for the terminating '\0').
132 */
133 #define FLOATING_POINT_BUFFER (311 + NOT_FIXED_DEC)
134 
135 /*
136  We want to use the 'e' format in some cases even if we have enough space
137  for the 'f' one just to mimic sprintf("%.15g") behavior for large integers,
138  and to improve it for numbers < 10^(-4).
139  That is, for |x| < 1 we require |x| >= 10^(-15), and for |x| > 1 we require
140  it to be integer and be <= 10^DBL_DIG for the 'f' format to be used.
141  We don't lose precision, but make cases like "1e200" or "0.00001" look nicer.
142 */
143 #define MAX_DECPT_FOR_F_FORMAT DBL_DIG
144 
145 /*
146  The maximum possible field width for my_gcvt() conversion.
147  (DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
148  MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
149 */
150 #define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + MY_MAX(5, MAX_DECPT_FOR_F_FORMAT)) \
151 
152 extern char *llstr(longlong value,char *buff);
153 extern char *ullstr(longlong value,char *buff);
154 #ifndef HAVE_STRTOUL
155 extern long strtol(const char *str, char **ptr, int base);
156 extern ulong strtoul(const char *str, char **ptr, int base);
157 #endif
158 
159 extern char *int2str(long val, char *dst, int radix, int upcase);
160 extern char *int10_to_str(long val,char *dst,int radix);
161 extern char *str2int(const char *src,int radix,long lower,long upper,
162  long *val);
163 longlong my_strtoll10(const char *nptr, char **endptr, int *error);
164 #if SIZEOF_LONG == SIZEOF_LONG_LONG
165 #define ll2str(A,B,C,D) int2str((A),(B),(C),(D))
166 #define longlong10_to_str(A,B,C) int10_to_str((A),(B),(C))
167 #undef strtoll
168 #define strtoll(A,B,C) strtol((A),(B),(C))
169 #define strtoull(A,B,C) strtoul((A),(B),(C))
170 #ifndef HAVE_STRTOULL
171 #define HAVE_STRTOULL
172 #endif
173 #ifndef HAVE_STRTOLL
174 #define HAVE_STRTOLL
175 #endif
176 #else
177 #ifdef HAVE_LONG_LONG
178 extern char *ll2str(longlong val,char *dst,int radix, int upcase);
179 extern char *longlong10_to_str(longlong val,char *dst,int radix);
180 #if (!defined(HAVE_STRTOULL) || defined(NO_STRTOLL_PROTO))
181 extern longlong strtoll(const char *str, char **ptr, int base);
182 extern ulonglong strtoull(const char *str, char **ptr, int base);
183 #endif
184 #endif
185 #endif
186 #define longlong2str(A,B,C) ll2str((A),(B),(C),1)
187 
188 #if defined(__cplusplus)
189 }
190 #endif
191 
192 /*
193  LEX_STRING -- a pair of a C-string and its length.
194  (it's part of the plugin API as a MYSQL_LEX_STRING)
195 */
196 
197 #include <mysql/plugin.h>
198 typedef struct st_mysql_lex_string LEX_STRING;
199 
200 #define STRING_WITH_LEN(X) (X), ((size_t) (sizeof(X) - 1))
201 #define USTRING_WITH_LEN(X) ((uchar*) X), ((size_t) (sizeof(X) - 1))
202 #define C_STRING_WITH_LEN(X) ((char *) (X)), ((size_t) (sizeof(X) - 1))
203 
205 {
206  const char *str;
207  size_t length;
208 };
210 
211 /* SPACE_INT is a word that contains only spaces */
212 #if SIZEOF_INT == 4
213 #define SPACE_INT 0x20202020
214 #elif SIZEOF_INT == 8
215 #define SPACE_INT 0x2020202020202020
216 #else
217 #error define the appropriate constant for a word full of spaces
218 #endif
219 
254 static inline const uchar *skip_trailing_space(const uchar *ptr,size_t len)
255 {
256  const uchar *end= ptr + len;
257 
258  if (len > 20)
259  {
260  const uchar *end_words= (const uchar *)(intptr)
261  (((ulonglong)(intptr)end) / SIZEOF_INT * SIZEOF_INT);
262  const uchar *start_words= (const uchar *)(intptr)
263  ((((ulonglong)(intptr)ptr) + SIZEOF_INT - 1) / SIZEOF_INT * SIZEOF_INT);
264 
265  DBUG_ASSERT(((ulonglong)(intptr)ptr) >= SIZEOF_INT);
266  if (end_words > ptr)
267  {
268  while (end > end_words && end[-1] == 0x20)
269  end--;
270  if (end[-1] == 0x20 && start_words < end_words)
271  while (end > start_words && ((unsigned *)end)[-1] == SPACE_INT)
272  end -= SIZEOF_INT;
273  }
274  }
275  while (end > ptr && end[-1] == 0x20)
276  end--;
277  return (end);
278 }
279 
280 static inline void lex_string_set(LEX_STRING *lex_str, const char *c_str)
281 {
282  lex_str->str= (char *) c_str;
283  lex_str->length= strlen(c_str);
284 }
285 
286 #endif