~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
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 as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/* There may be prolems include all of theese. Try to test in
17
   configure with ones are needed? */
18
19
/*  This is needed for the definitions of strchr... on solaris */
20
21
#ifndef _m_string_h
22
#define _m_string_h
23
#ifndef __USE_GNU
24
#define __USE_GNU				/* We want to use stpcpy */
25
#endif
26
#if defined(HAVE_STRINGS_H)
27
#include <strings.h>
28
#endif
29
#if defined(HAVE_STRING_H)
30
#include <string.h>
31
#endif
32
33
#ifdef _AIX
34
#undef HAVE_BCMP
35
#endif
36
37
/*  This is needed for the definitions of bzero... on solaris */
38
#if defined(HAVE_STRINGS_H)
39
#include <strings.h>
40
#endif
41
42
/*  This is needed for the definitions of memcpy... on solaris */
43
#if defined(HAVE_MEMORY_H) && !defined(__cplusplus)
44
#include <memory.h>
45
#endif
46
47
#if !defined(HAVE_MEMCPY) && !defined(HAVE_MEMMOVE)
48
# define memcpy(d, s, n)	bcopy ((s), (d), (n))
49
# define memset(A,C,B)		bfill((A),(B),(C))
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
#else
54
# define memmove(d, s, n)	bmove((d), (s), (n)) /* our bmove */
55
#endif
56
57
/* Unixware 7 */
58
#if !defined(HAVE_BFILL)
59
# define bfill(A,B,C)           memset((A),(C),(B))
60
# define bmove_align(A,B,C)    memcpy((A),(B),(C))
61
#endif
62
63
#if !defined(HAVE_BCMP)
64
# define bcopy(s, d, n)		memcpy((d), (s), (n))
65
# define bcmp(A,B,C)		memcmp((A),(B),(C))
66
# define bzero(A,B)		memset((A),0,(B))
67
# define bmove_align(A,B,C)     memcpy((A),(B),(C))
68
#endif
69
70
#if defined(__cplusplus)
71
extern "C" {
72
#endif
73
74
/*
75
  my_str_malloc() and my_str_free() are assigned to implementations in
76
  strings/alloc.c, but can be overridden in the calling program.
77
 */
78
extern void *(*my_str_malloc)(size_t);
79
extern void (*my_str_free)(void *);
80
81
#if defined(HAVE_STPCPY)
82
#define strmov(A,B) stpcpy((A),(B))
83
#endif
84
85
/* Declared in int2str() */
86
extern char _dig_vec_upper[];
87
extern char _dig_vec_lower[];
88
89
#ifdef BAD_STRING_COMPILER
90
#define strmov(A,B)  (memccpy(A,B,0,INT_MAX)-1)
91
#else
92
#define strmov_overlapp(A,B) strmov(A,B)
93
#define strmake_overlapp(A,B,C) strmake(A,B,C)
94
#endif
95
96
#ifdef BAD_MEMCPY			/* Problem with gcc on Alpha */
97
#define memcpy_fixed(A,B,C) bmove((A),(B),(C))
98
#else
99
#define memcpy_fixed(A,B,C) memcpy((A),(B),(C))
100
#endif
101
102
#if (!defined(USE_BMOVE512) || defined(HAVE_purify)) && !defined(bmove512)
103
#define bmove512(A,B,C) memcpy(A,B,C)
104
#endif
105
106
	/* Prototypes for string functions */
107
108
#if !defined(bfill) && !defined(HAVE_BFILL)
154 by Brian Aker
Removed oddball types in my_global.h
109
extern	void bfill(uchar *dst,size_t len,char fill);
1 by brian
clean slate
110
#endif
111
112
#if !defined(bzero) && !defined(HAVE_BZERO)
113
extern	void bzero(uchar * dst,size_t len);
114
#endif
115
116
#if !defined(bcmp) && !defined(HAVE_BCMP)
117
extern	size_t bcmp(const uchar *s1,const uchar *s2,size_t len);
118
#endif
119
#ifdef HAVE_purify
120
extern	size_t my_bcmp(const uchar *s1,const uchar *s2,size_t len);
121
#undef bcmp
122
#define bcmp(A,B,C) my_bcmp((A),(B),(C))
123
#define bzero_if_purify(A,B) bzero(A,B)
124
#else
125
#define bzero_if_purify(A,B)
126
#endif /* HAVE_purify */
127
128
#ifndef bmove512
129
extern	void bmove512(uchar *dst,const uchar *src,size_t len);
130
#endif
131
132
#if !defined(HAVE_BMOVE) && !defined(bmove)
133
extern	void bmove(uuchar *dst, const uchar *src,size_t len);
134
#endif
135
136
extern	void bmove_upp(uchar *dst,const uchar *src,size_t len);
137
extern	void bchange(uchar *dst,size_t old_len,const uchar *src,
138
		     size_t new_len,size_t tot_len);
154 by Brian Aker
Removed oddball types in my_global.h
139
extern	void strappend(char *s,size_t len,char fill);
1 by brian
clean slate
140
extern	char *strend(const char *s);
154 by Brian Aker
Removed oddball types in my_global.h
141
extern  char *strcend(const char *, char);
1 by brian
clean slate
142
extern	char *strfield(char *src,int fields,int chars,int blanks,
143
			   int tabch);
154 by Brian Aker
Removed oddball types in my_global.h
144
extern	char *strfill(char * s,size_t len,char fill);
1 by brian
clean slate
145
extern	char *strkey(char *dst,char *head,char *tail,char *flags);
146
extern	char *strmake(char *dst,const char *src,size_t length);
147
#ifndef strmake_overlapp
148
extern	char *strmake_overlapp(char *dst,const char *src, size_t length);
149
#endif
150
151
#ifndef strmov
152
extern	char *strmov(char *dst,const char *src);
153
#endif
154
extern	char *strnmov(char *dst,const char *src,size_t n);
155
extern	char *strsuff(const char *src,const char *suffix);
156
extern	char *strcont(const char *src,const char *set);
157
extern	char *strxcat _VARARGS((char *dst,const char *src, ...));
158
extern	char *strxmov _VARARGS((char *dst,const char *src, ...));
159
extern	char *strxcpy _VARARGS((char *dst,const char *src, ...));
160
extern	char *strxncat _VARARGS((char *dst,size_t len, const char *src, ...));
161
extern	char *strxnmov _VARARGS((char *dst,size_t len, const char *src, ...));
162
extern	char *strxncpy _VARARGS((char *dst,size_t len, const char *src, ...));
163
164
/* Prototypes of normal stringfunctions (with may ours) */
165
166
#ifdef WANT_STRING_PROTOTYPES
167
extern char *strcat(char *, const char *);
154 by Brian Aker
Removed oddball types in my_global.h
168
extern char *strchr(const char *, char);
169
extern char *strrchr(const char *, char);
1 by brian
clean slate
170
extern char *strcpy(char *, const char *);
171
extern int strcmp(const char *, const char *);
172
#ifndef __GNUC__
173
extern size_t strlen(const char *);
174
#endif
175
#endif
176
#ifndef HAVE_STRNLEN
177
extern size_t strnlen(const char *s, size_t n);
178
#endif
179
180
#if !defined(__cplusplus)
181
#ifndef HAVE_STRPBRK
182
extern char *strpbrk(const char *, const char *);
183
#endif
184
#ifndef HAVE_STRSTR
185
extern char *strstr(const char *, const char *);
186
#endif
187
#endif
188
extern int is_prefix(const char *, const char *);
189
190
/* Conversion routines */
191
typedef enum {
192
  MY_GCVT_ARG_FLOAT,
193
  MY_GCVT_ARG_DOUBLE
194
} my_gcvt_arg_type;
195
196
double my_strtod(const char *str, char **end, int *error);
197
double my_atof(const char *nptr);
198
size_t my_fcvt(double x, int precision, char *to, my_bool *error);
199
size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
200
               my_bool *error);
201
202
#define NOT_FIXED_DEC 31
203
204
/*
205
  The longest string my_fcvt can return is 311 + "precision" bytes.
206
  Here we assume that we never cal my_fcvt() with precision >= NOT_FIXED_DEC
207
  (+ 1 byte for the terminating '\0').
208
*/
209
#define FLOATING_POINT_BUFFER (311 + NOT_FIXED_DEC)
210
211
/*
212
  We want to use the 'e' format in some cases even if we have enough space
213
  for the 'f' one just to mimic sprintf("%.15g") behavior for large integers,
214
  and to improve it for numbers < 10^(-4).
215
  That is, for |x| < 1 we require |x| >= 10^(-15), and for |x| > 1 we require
216
  it to be integer and be <= 10^DBL_DIG for the 'f' format to be used.
217
  We don't lose precision, but make cases like "1e200" or "0.00001" look nicer.
218
*/
219
#define MAX_DECPT_FOR_F_FORMAT DBL_DIG
220
221
/*
222
  The maximum possible field width for my_gcvt() conversion.
223
  (DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
224
  MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
225
*/
226
#define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + max(5, MAX_DECPT_FOR_F_FORMAT))
227
  
228
152 by Brian Aker
longlong replacement
229
extern char *llstr(int64_t value,char *buff);
230
extern char *ullstr(int64_t value,char *buff);
1 by brian
clean slate
231
#ifndef HAVE_STRTOUL
232
extern long strtol(const char *str, char **ptr, int base);
233
extern ulong strtoul(const char *str, char **ptr, int base);
234
#endif
235
236
extern char *int2str(long val, char *dst, int radix, int upcase);
237
extern char *int10_to_str(long val,char *dst,int radix);
238
extern char *str2int(const char *src,int radix,long lower,long upper,
239
			 long *val);
152 by Brian Aker
longlong replacement
240
int64_t my_strtoll10(const char *nptr, char **endptr, int *error);
1 by brian
clean slate
241
#if SIZEOF_LONG == SIZEOF_LONG_LONG
152 by Brian Aker
longlong replacement
242
#define int64_t2str(A,B,C) int2str((A),(B),(C),1)
243
#define int64_t10_to_str(A,B,C) int10_to_str((A),(B),(C))
1 by brian
clean slate
244
#undef strtoll
245
#define strtoll(A,B,C) strtol((A),(B),(C))
246
#define strtoull(A,B,C) strtoul((A),(B),(C))
247
#ifndef HAVE_STRTOULL
248
#define HAVE_STRTOULL
249
#endif
250
#ifndef HAVE_STRTOLL
251
#define HAVE_STRTOLL
252
#endif
253
#else
152 by Brian Aker
longlong replacement
254
extern char *int64_t2str(int64_t val,char *dst,int radix);
255
extern char *int64_t10_to_str(int64_t val,char *dst,int radix);
1 by brian
clean slate
256
#if (!defined(HAVE_STRTOULL) || defined(NO_STRTOLL_PROTO))
152 by Brian Aker
longlong replacement
257
extern int64_t strtoll(const char *str, char **ptr, int base);
151 by Brian Aker
Ulonglong to uint64_t
258
extern uint64_t strtoull(const char *str, char **ptr, int base);
1 by brian
clean slate
259
#endif
260
#endif
261
262
263
#if defined(__cplusplus)
264
}
265
#endif
266
267
/*
268
  LEX_STRING -- a pair of a C-string and its length.
269
*/
270
271
#ifndef _my_plugin_h
272
/* This definition must match the one given in mysql/plugin.h */
273
struct st_mysql_lex_string
274
{
275
  char *str;
276
  size_t length;
277
};
278
#endif
279
typedef struct st_mysql_lex_string LEX_STRING;
280
281
#define STRING_WITH_LEN(X) (X), ((size_t) (sizeof(X) - 1))
282
#define USTRING_WITH_LEN(X) ((uchar*) X), ((size_t) (sizeof(X) - 1))
283
#define C_STRING_WITH_LEN(X) ((char *) (X)), ((size_t) (sizeof(X) - 1))
284
285
/* SPACE_INT is a word that contains only spaces */
286
#if SIZEOF_INT == 4
287
#define SPACE_INT 0x20202020
288
#elif SIZEOF_INT == 8
289
#define SPACE_INT 0x2020202020202020
290
#else
291
#error define the appropriate constant for a word full of spaces
292
#endif
293
294
/**
295
  Skip trailing space.
296
297
  On most systems reading memory in larger chunks (ideally equal to the size of
298
  the chinks that the machine physically reads from memory) causes fewer memory
299
  access loops and hence increased performance.
300
  This is why the 'int' type is used : it's closest to that (according to how
301
  it's defined in C).
302
  So when we determine the amount of whitespace at the end of a string we do
303
  the following :
304
    1. We divide the string into 3 zones :
305
      a) from the start of the string (__start) to the first multiple
306
        of sizeof(int)  (__start_words)
307
      b) from the end of the string (__end) to the last multiple of sizeof(int)
308
        (__end_words)
309
      c) a zone that is aligned to sizeof(int) and can be safely accessed
310
        through an int *
311
    2. We start comparing backwards from (c) char-by-char. If all we find is
312
       space then we continue
313
    3. If there are elements in zone (b) we compare them as unsigned ints to a
314
       int mask (SPACE_INT) consisting of all spaces
315
    4. Finally we compare the remaining part (a) of the string char by char.
316
       This covers for the last non-space unsigned int from 3. (if any)
317
318
   This algorithm works well for relatively larger strings, but it will slow
319
   the things down for smaller strings (because of the additional calculations
320
   and checks compared to the naive method). Thus the barrier of length 20
321
   is added.
322
323
   @param     ptr   pointer to the input string
324
   @param     len   the length of the string
325
   @return          the last non-space character
326
*/
327
328
static inline const uchar *skip_trailing_space(const uchar *ptr,size_t len)
329
{
330
  const uchar *end= ptr + len;
331
332
  if (len > 20)
333
  {
156 by Brian Aker
Cleanup around intptr_t
334
    const uchar *end_words= (const uchar *)(intptr_t)
335
      (((uint64_t)(intptr_t)end) / SIZEOF_INT * SIZEOF_INT);
336
    const uchar *start_words= (const uchar *)(intptr_t)
337
       ((((uint64_t)(intptr_t)ptr) + SIZEOF_INT - 1) / SIZEOF_INT * SIZEOF_INT);
1 by brian
clean slate
338
51.3.7 by Jay Pipes
Final removal of DBUG from vio/ & Round 1 cleanup of include/
339
    assert(((uint64_t)(intptr_t)ptr) >= SIZEOF_INT);
1 by brian
clean slate
340
    if (end_words > ptr)
341
    {
342
      while (end > end_words && end[-1] == 0x20)
343
        end--;
344
      if (end[-1] == 0x20 && start_words < end_words)
345
        while (end > start_words && ((unsigned *)end)[-1] == SPACE_INT)
346
          end -= SIZEOF_INT;
347
    }
348
  }
349
  while (end > ptr && end[-1] == 0x20)
350
    end--;
351
  return (end);
352
}
353
354
#endif