~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
212.5.39 by Monty Taylor
Phew. Moved my_base and my_global.
21
1 by brian
clean slate
22
#ifndef _m_string_h
23
#define _m_string_h
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
24
25
#include <drizzled/global.h>
26
1 by brian
clean slate
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
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
37
#include <stdlib.h>
38
#include <stddef.h>
39
#include <stdint.h>
40
#include <stdbool.h>
41
#include <assert.h>
42
#include <limits.h>
1 by brian
clean slate
43
44
/*  This is needed for the definitions of memcpy... on solaris */
45
#if defined(HAVE_MEMORY_H) && !defined(__cplusplus)
46
#include <memory.h>
47
#endif
48
49
#if defined(__cplusplus)
50
extern "C" {
51
#endif
52
53
/*
54
  my_str_malloc() and my_str_free() are assigned to implementations in
55
  strings/alloc.c, but can be overridden in the calling program.
56
 */
57
extern void *(*my_str_malloc)(size_t);
58
extern void (*my_str_free)(void *);
59
60
#if defined(HAVE_STPCPY)
61
#define strmov(A,B) stpcpy((A),(B))
62
#endif
63
64
/* Declared in int2str() */
65
extern char _dig_vec_upper[];
66
extern char _dig_vec_lower[];
67
68
#ifdef BAD_STRING_COMPILER
69
#define strmov(A,B)  (memccpy(A,B,0,INT_MAX)-1)
70
#else
71
#define strmov_overlapp(A,B) strmov(A,B)
72
#define strmake_overlapp(A,B,C) strmake(A,B,C)
73
#endif
74
75
#if (!defined(USE_BMOVE512) || defined(HAVE_purify)) && !defined(bmove512)
76
#define bmove512(A,B,C) memcpy(A,B,C)
77
#endif
78
79
	/* Prototypes for string functions */
80
#ifndef bmove512
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
81
extern void bmove512(unsigned char *dst,const unsigned char *src,size_t len);
1 by brian
clean slate
82
#endif
83
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
84
extern void bmove_upp(unsigned char *dst,const unsigned char *src,size_t len);
85
86
extern	void bchange(unsigned char *dst,size_t old_len,const unsigned char *src,
1 by brian
clean slate
87
		     size_t new_len,size_t tot_len);
154 by Brian Aker
Removed oddball types in my_global.h
88
extern	void strappend(char *s,size_t len,char fill);
1 by brian
clean slate
89
extern	char *strend(const char *s);
154 by Brian Aker
Removed oddball types in my_global.h
90
extern  char *strcend(const char *, char);
1 by brian
clean slate
91
extern	char *strfield(char *src,int fields,int chars,int blanks,
92
			   int tabch);
154 by Brian Aker
Removed oddball types in my_global.h
93
extern	char *strfill(char * s,size_t len,char fill);
1 by brian
clean slate
94
extern	char *strkey(char *dst,char *head,char *tail,char *flags);
95
extern	char *strmake(char *dst,const char *src,size_t length);
96
#ifndef strmake_overlapp
97
extern	char *strmake_overlapp(char *dst,const char *src, size_t length);
98
#endif
99
100
#ifndef strmov
101
extern	char *strmov(char *dst,const char *src);
102
#endif
103
extern	char *strnmov(char *dst,const char *src,size_t n);
104
extern	char *strsuff(const char *src,const char *suffix);
105
extern	char *strcont(const char *src,const char *set);
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
106
extern	char *strxcat(char *dst,const char *src, ...);
107
extern	char *strxmov(char *dst,const char *src, ...);
108
extern	char *strxcpy(char *dst,const char *src, ...);
109
extern	char *strxncat(char *dst,size_t len, const char *src, ...);
110
extern	char *strxnmov(char *dst,size_t len, const char *src, ...);
111
extern	char *strxncpy(char *dst,size_t len, const char *src, ...);
1 by brian
clean slate
112
113
/* Prototypes of normal stringfunctions (with may ours) */
114
115
#ifdef WANT_STRING_PROTOTYPES
116
extern char *strcat(char *, const char *);
154 by Brian Aker
Removed oddball types in my_global.h
117
extern char *strchr(const char *, char);
118
extern char *strrchr(const char *, char);
1 by brian
clean slate
119
extern char *strcpy(char *, const char *);
120
extern int strcmp(const char *, const char *);
121
#ifndef __GNUC__
122
extern size_t strlen(const char *);
123
#endif
124
#endif
125
#ifndef HAVE_STRNLEN
126
extern size_t strnlen(const char *s, size_t n);
127
#endif
128
129
#if !defined(__cplusplus)
130
#ifndef HAVE_STRPBRK
131
extern char *strpbrk(const char *, const char *);
132
#endif
133
#ifndef HAVE_STRSTR
134
extern char *strstr(const char *, const char *);
135
#endif
136
#endif
137
extern int is_prefix(const char *, const char *);
138
139
/* Conversion routines */
140
typedef enum {
141
  MY_GCVT_ARG_FLOAT,
142
  MY_GCVT_ARG_DOUBLE
143
} my_gcvt_arg_type;
144
145
double my_strtod(const char *str, char **end, int *error);
146
double my_atof(const char *nptr);
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
147
size_t my_fcvt(double x, int precision, char *to, bool *error);
1 by brian
clean slate
148
size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
149
               bool *error);
1 by brian
clean slate
150
151
#define NOT_FIXED_DEC 31
152
153
/*
154
  The longest string my_fcvt can return is 311 + "precision" bytes.
155
  Here we assume that we never cal my_fcvt() with precision >= NOT_FIXED_DEC
156
  (+ 1 byte for the terminating '\0').
157
*/
158
#define FLOATING_POINT_BUFFER (311 + NOT_FIXED_DEC)
159
160
/*
161
  We want to use the 'e' format in some cases even if we have enough space
162
  for the 'f' one just to mimic sprintf("%.15g") behavior for large integers,
163
  and to improve it for numbers < 10^(-4).
164
  That is, for |x| < 1 we require |x| >= 10^(-15), and for |x| > 1 we require
165
  it to be integer and be <= 10^DBL_DIG for the 'f' format to be used.
166
  We don't lose precision, but make cases like "1e200" or "0.00001" look nicer.
167
*/
168
#define MAX_DECPT_FOR_F_FORMAT DBL_DIG
169
170
/*
171
  The maximum possible field width for my_gcvt() conversion.
172
  (DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
173
  MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
174
*/
175
#define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + max(5, MAX_DECPT_FOR_F_FORMAT))
176
  
177
152 by Brian Aker
longlong replacement
178
extern char *llstr(int64_t value,char *buff);
179
extern char *ullstr(int64_t value,char *buff);
1 by brian
clean slate
180
#ifndef HAVE_STRTOUL
181
extern long strtol(const char *str, char **ptr, int base);
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
182
extern unsigned long strtoul(const char *str, char **ptr, int base);
1 by brian
clean slate
183
#endif
184
185
extern char *int2str(long val, char *dst, int radix, int upcase);
186
extern char *int10_to_str(long val,char *dst,int radix);
187
extern char *str2int(const char *src,int radix,long lower,long upper,
188
			 long *val);
152 by Brian Aker
longlong replacement
189
int64_t my_strtoll10(const char *nptr, char **endptr, int *error);
1 by brian
clean slate
190
#if SIZEOF_LONG == SIZEOF_LONG_LONG
152 by Brian Aker
longlong replacement
191
#define int64_t2str(A,B,C) int2str((A),(B),(C),1)
192
#define int64_t10_to_str(A,B,C) int10_to_str((A),(B),(C))
1 by brian
clean slate
193
#undef strtoll
194
#define strtoll(A,B,C) strtol((A),(B),(C))
195
#define strtoull(A,B,C) strtoul((A),(B),(C))
196
#ifndef HAVE_STRTOULL
197
#define HAVE_STRTOULL
198
#endif
199
#ifndef HAVE_STRTOLL
200
#define HAVE_STRTOLL
201
#endif
202
#else
152 by Brian Aker
longlong replacement
203
extern char *int64_t2str(int64_t val,char *dst,int radix);
204
extern char *int64_t10_to_str(int64_t val,char *dst,int radix);
1 by brian
clean slate
205
#if (!defined(HAVE_STRTOULL) || defined(NO_STRTOLL_PROTO))
152 by Brian Aker
longlong replacement
206
extern int64_t strtoll(const char *str, char **ptr, int base);
151 by Brian Aker
Ulonglong to uint64_t
207
extern uint64_t strtoull(const char *str, char **ptr, int base);
1 by brian
clean slate
208
#endif
209
#endif
210
211
212
#if defined(__cplusplus)
213
}
214
#endif
215
216
/*
217
  LEX_STRING -- a pair of a C-string and its length.
218
*/
219
220
#ifndef _my_plugin_h
221
/* This definition must match the one given in mysql/plugin.h */
222
struct st_mysql_lex_string
223
{
224
  char *str;
225
  size_t length;
226
};
227
#endif
228
typedef struct st_mysql_lex_string LEX_STRING;
229
230
#define STRING_WITH_LEN(X) (X), ((size_t) (sizeof(X) - 1))
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
231
#define USTRING_WITH_LEN(X) ((unsigned char*) X), ((size_t) (sizeof(X) - 1))
1 by brian
clean slate
232
#define C_STRING_WITH_LEN(X) ((char *) (X)), ((size_t) (sizeof(X) - 1))
233
234
/* SPACE_INT is a word that contains only spaces */
235
#if SIZEOF_INT == 4
236
#define SPACE_INT 0x20202020
237
#elif SIZEOF_INT == 8
238
#define SPACE_INT 0x2020202020202020
239
#else
240
#error define the appropriate constant for a word full of spaces
241
#endif
242
243
/**
244
  Skip trailing space.
245
246
  On most systems reading memory in larger chunks (ideally equal to the size of
247
  the chinks that the machine physically reads from memory) causes fewer memory
248
  access loops and hence increased performance.
249
  This is why the 'int' type is used : it's closest to that (according to how
250
  it's defined in C).
251
  So when we determine the amount of whitespace at the end of a string we do
252
  the following :
253
    1. We divide the string into 3 zones :
254
      a) from the start of the string (__start) to the first multiple
255
        of sizeof(int)  (__start_words)
256
      b) from the end of the string (__end) to the last multiple of sizeof(int)
257
        (__end_words)
258
      c) a zone that is aligned to sizeof(int) and can be safely accessed
259
        through an int *
260
    2. We start comparing backwards from (c) char-by-char. If all we find is
261
       space then we continue
262
    3. If there are elements in zone (b) we compare them as unsigned ints to a
263
       int mask (SPACE_INT) consisting of all spaces
264
    4. Finally we compare the remaining part (a) of the string char by char.
265
       This covers for the last non-space unsigned int from 3. (if any)
266
267
   This algorithm works well for relatively larger strings, but it will slow
268
   the things down for smaller strings (because of the additional calculations
269
   and checks compared to the naive method). Thus the barrier of length 20
270
   is added.
271
272
   @param     ptr   pointer to the input string
273
   @param     len   the length of the string
274
   @return          the last non-space character
275
*/
276
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
277
static inline const unsigned char *skip_trailing_space(const unsigned char *ptr,size_t len)
1 by brian
clean slate
278
{
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
279
  const unsigned char *end= ptr + len;
1 by brian
clean slate
280
281
  if (len > 20)
282
  {
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
283
    const unsigned char *end_words= (const unsigned char *)(intptr_t)
156 by Brian Aker
Cleanup around intptr_t
284
      (((uint64_t)(intptr_t)end) / SIZEOF_INT * SIZEOF_INT);
236.1.27 by Monty Taylor
Some cleanups/decoupling in mystring.
285
    const unsigned char *start_words= (const unsigned char *)(intptr_t)
156 by Brian Aker
Cleanup around intptr_t
286
       ((((uint64_t)(intptr_t)ptr) + SIZEOF_INT - 1) / SIZEOF_INT * SIZEOF_INT);
1 by brian
clean slate
287
51.3.7 by Jay Pipes
Final removal of DBUG from vio/ & Round 1 cleanup of include/
288
    assert(((uint64_t)(intptr_t)ptr) >= SIZEOF_INT);
1 by brian
clean slate
289
    if (end_words > ptr)
290
    {
291
      while (end > end_words && end[-1] == 0x20)
292
        end--;
293
      if (end[-1] == 0x20 && start_words < end_words)
294
        while (end > start_words && ((unsigned *)end)[-1] == SPACE_INT)
295
          end -= SIZEOF_INT;
296
    }
297
  }
298
  while (end > ptr && end[-1] == 0x20)
299
    end--;
300
  return (end);
301
}
302
303
#endif