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