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