~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to include/m_string.h

  • Committer: Monty Taylor
  • Date: 2008-07-11 15:23:55 UTC
  • mto: (77.6.1 glibclient-merge)
  • mto: This revision was merged to the branch mainline in revision 134.
  • Revision ID: monty@inaugust.com-20080711152355-o92cod4mg0u9ck03
More const-correctness.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
/*  This is needed for the definitions of strchr... on solaris */
20
20
 
21
 
 
22
 
#ifndef DRIZZLED_INTERNAL_M_STRING_H
23
 
#define DRIZZLED_INTERNAL_M_STRING_H
24
 
 
 
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
25
26
#if defined(HAVE_STRINGS_H)
26
27
#include <strings.h>
27
28
#endif
29
30
#include <string.h>
30
31
#endif
31
32
 
32
 
#include <stdlib.h>
33
 
#include <stddef.h>
34
 
#include <cassert>
35
 
#include <limits.h>
36
 
#include <ctype.h>
 
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
37
41
 
38
42
/*  This is needed for the definitions of memcpy... on solaris */
39
43
#if defined(HAVE_MEMORY_H) && !defined(__cplusplus)
40
44
#include <memory.h>
41
45
#endif
42
46
 
43
 
namespace drizzled
44
 
{
45
 
namespace internal
46
 
{
47
 
 
48
 
extern void bmove_upp(unsigned char *dst,const unsigned char *src,size_t len);
49
 
 
50
 
extern  void bchange(unsigned char *dst,size_t old_len,const unsigned char *src,
 
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)
 
109
extern  void bfill(uchar *dst,size_t len,pchar fill);
 
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,
51
138
                     size_t new_len,size_t tot_len);
 
139
extern  void strappend(char *s,size_t len,pchar fill);
 
140
extern  char *strend(const char *s);
 
141
extern  char *strcend(const char *, pchar);
52
142
extern  char *strfield(char *src,int fields,int chars,int blanks,
53
143
                           int tabch);
54
 
extern  char *strfill(char * s,size_t len,char fill);
 
144
extern  char *strfill(char * s,size_t len,pchar fill);
 
145
extern  size_t strinstr(const char *str,const char *search);
 
146
extern  size_t r_strinstr(const char *str, size_t from, const char *search);
55
147
extern  char *strkey(char *dst,char *head,char *tail,char *flags);
56
148
extern  char *strmake(char *dst,const char *src,size_t length);
 
149
#ifndef strmake_overlapp
 
150
extern  char *strmake_overlapp(char *dst,const char *src, size_t length);
 
151
#endif
57
152
 
 
153
#ifndef strmov
 
154
extern  char *strmov(char *dst,const char *src);
 
155
#endif
 
156
extern  char *strnmov(char *dst,const char *src,size_t n);
58
157
extern  char *strsuff(const char *src,const char *suffix);
59
 
extern  char *strxcat(char *dst,const char *src, ...);
60
 
extern  char *strxmov(char *dst,const char *src, ...);
61
 
extern  char *strxcpy(char *dst,const char *src, ...);
62
 
extern  char *strxncat(char *dst,size_t len, const char *src, ...);
63
 
extern  char *strxncpy(char *dst,size_t len, const char *src, ...);
 
158
extern  char *strcont(const char *src,const char *set);
 
159
extern  char *strxcat _VARARGS((char *dst,const char *src, ...));
 
160
extern  char *strxmov _VARARGS((char *dst,const char *src, ...));
 
161
extern  char *strxcpy _VARARGS((char *dst,const char *src, ...));
 
162
extern  char *strxncat _VARARGS((char *dst,size_t len, const char *src, ...));
 
163
extern  char *strxnmov _VARARGS((char *dst,size_t len, const char *src, ...));
 
164
extern  char *strxncpy _VARARGS((char *dst,size_t len, const char *src, ...));
 
165
 
 
166
/* Prototypes of normal stringfunctions (with may ours) */
 
167
 
 
168
#ifdef WANT_STRING_PROTOTYPES
 
169
extern char *strcat(char *, const char *);
 
170
extern char *strchr(const char *, pchar);
 
171
extern char *strrchr(const char *, pchar);
 
172
extern char *strcpy(char *, const char *);
 
173
extern int strcmp(const char *, const char *);
 
174
#ifndef __GNUC__
 
175
extern size_t strlen(const char *);
 
176
#endif
 
177
#endif
 
178
#ifndef HAVE_STRNLEN
 
179
extern size_t strnlen(const char *s, size_t n);
 
180
#endif
 
181
 
 
182
#if !defined(__cplusplus)
 
183
#ifndef HAVE_STRPBRK
 
184
extern char *strpbrk(const char *, const char *);
 
185
#endif
 
186
#ifndef HAVE_STRSTR
 
187
extern char *strstr(const char *, const char *);
 
188
#endif
 
189
#endif
 
190
extern int is_prefix(const char *, const char *);
64
191
 
65
192
/* Conversion routines */
66
193
typedef enum {
70
197
 
71
198
double my_strtod(const char *str, char **end, int *error);
72
199
double my_atof(const char *nptr);
73
 
size_t my_fcvt(double x, int precision, char *to, bool *error);
 
200
size_t my_fcvt(double x, int precision, char *to, my_bool *error);
74
201
size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
75
 
               bool *error);
 
202
               my_bool *error);
76
203
 
77
 
#define NOT_FIXED_DEC (uint8_t)31
 
204
#define NOT_FIXED_DEC 31
78
205
 
79
206
/*
80
207
  The longest string my_fcvt can return is 311 + "precision" bytes.
98
225
  (DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
99
226
  MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
100
227
*/
101
 
#define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + cmax(5, MAX_DECPT_FOR_F_FORMAT))
102
 
 
103
 
 
104
 
extern char *llstr(int64_t value,char *buff);
105
 
extern char *ullstr(int64_t value,char *buff);
106
 
 
107
 
extern char *int2str(int32_t val, char *dst, int radix, int upcase);
108
 
extern char *int10_to_str(int32_t val,char *dst,int radix);
109
 
int64_t my_strtoll10(const char *nptr, char **endptr, int *error);
110
 
extern char *int64_t2str(int64_t val,char *dst,int radix);
111
 
extern char *int64_t10_to_str(int64_t val,char *dst,int radix);
112
 
 
 
228
#define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + max(5, MAX_DECPT_FOR_F_FORMAT))
 
229
  
 
230
 
 
231
extern char *llstr(longlong value,char *buff);
 
232
extern char *ullstr(longlong value,char *buff);
 
233
#ifndef HAVE_STRTOUL
 
234
extern long strtol(const char *str, char **ptr, int base);
 
235
extern ulong strtoul(const char *str, char **ptr, int base);
 
236
#endif
 
237
 
 
238
extern char *int2str(long val, char *dst, int radix, int upcase);
 
239
extern char *int10_to_str(long val,char *dst,int radix);
 
240
extern char *str2int(const char *src,int radix,long lower,long upper,
 
241
                         long *val);
 
242
longlong my_strtoll10(const char *nptr, char **endptr, int *error);
 
243
#if SIZEOF_LONG == SIZEOF_LONG_LONG
 
244
#define longlong2str(A,B,C) int2str((A),(B),(C),1)
 
245
#define longlong10_to_str(A,B,C) int10_to_str((A),(B),(C))
 
246
#undef strtoll
 
247
#define strtoll(A,B,C) strtol((A),(B),(C))
 
248
#define strtoull(A,B,C) strtoul((A),(B),(C))
 
249
#ifndef HAVE_STRTOULL
 
250
#define HAVE_STRTOULL
 
251
#endif
 
252
#ifndef HAVE_STRTOLL
 
253
#define HAVE_STRTOLL
 
254
#endif
 
255
#else
 
256
extern char *longlong2str(longlong val,char *dst,int radix);
 
257
extern char *longlong10_to_str(longlong val,char *dst,int radix);
 
258
#if (!defined(HAVE_STRTOULL) || defined(NO_STRTOLL_PROTO))
 
259
extern longlong strtoll(const char *str, char **ptr, int base);
 
260
extern ulonglong strtoull(const char *str, char **ptr, int base);
 
261
#endif
 
262
#endif
 
263
 
 
264
 
 
265
#if defined(__cplusplus)
 
266
}
 
267
#endif
 
268
 
 
269
/*
 
270
  LEX_STRING -- a pair of a C-string and its length.
 
271
*/
 
272
 
 
273
#ifndef _my_plugin_h
 
274
/* This definition must match the one given in mysql/plugin.h */
 
275
struct st_mysql_lex_string
 
276
{
 
277
  char *str;
 
278
  size_t length;
 
279
};
 
280
#endif
 
281
typedef struct st_mysql_lex_string LEX_STRING;
 
282
 
 
283
#define STRING_WITH_LEN(X) (X), ((size_t) (sizeof(X) - 1))
 
284
#define USTRING_WITH_LEN(X) ((uchar*) X), ((size_t) (sizeof(X) - 1))
 
285
#define C_STRING_WITH_LEN(X) ((char *) (X)), ((size_t) (sizeof(X) - 1))
 
286
 
 
287
/* SPACE_INT is a word that contains only spaces */
 
288
#if SIZEOF_INT == 4
 
289
#define SPACE_INT 0x20202020
 
290
#elif SIZEOF_INT == 8
 
291
#define SPACE_INT 0x2020202020202020
 
292
#else
 
293
#error define the appropriate constant for a word full of spaces
 
294
#endif
113
295
 
114
296
/**
115
297
  Skip trailing space.
116
298
 
 
299
  On most systems reading memory in larger chunks (ideally equal to the size of
 
300
  the chinks that the machine physically reads from memory) causes fewer memory
 
301
  access loops and hence increased performance.
 
302
  This is why the 'int' type is used : it's closest to that (according to how
 
303
  it's defined in C).
 
304
  So when we determine the amount of whitespace at the end of a string we do
 
305
  the following :
 
306
    1. We divide the string into 3 zones :
 
307
      a) from the start of the string (__start) to the first multiple
 
308
        of sizeof(int)  (__start_words)
 
309
      b) from the end of the string (__end) to the last multiple of sizeof(int)
 
310
        (__end_words)
 
311
      c) a zone that is aligned to sizeof(int) and can be safely accessed
 
312
        through an int *
 
313
    2. We start comparing backwards from (c) char-by-char. If all we find is
 
314
       space then we continue
 
315
    3. If there are elements in zone (b) we compare them as unsigned ints to a
 
316
       int mask (SPACE_INT) consisting of all spaces
 
317
    4. Finally we compare the remaining part (a) of the string char by char.
 
318
       This covers for the last non-space unsigned int from 3. (if any)
 
319
 
 
320
   This algorithm works well for relatively larger strings, but it will slow
 
321
   the things down for smaller strings (because of the additional calculations
 
322
   and checks compared to the naive method). Thus the barrier of length 20
 
323
   is added.
 
324
 
117
325
   @param     ptr   pointer to the input string
118
326
   @param     len   the length of the string
119
327
   @return          the last non-space character
120
328
*/
121
329
 
122
 
static inline const unsigned char *
123
 
skip_trailing_space(const unsigned char *ptr, size_t len)
 
330
static inline const uchar *skip_trailing_space(const uchar *ptr,size_t len)
124
331
{
125
 
  const unsigned char *end= ptr + len;
126
 
 
127
 
  while (end > ptr && isspace(*--end))
128
 
    continue;
129
 
  return end+1;
 
332
  const uchar *end= ptr + len;
 
333
 
 
334
  if (len > 20)
 
335
  {
 
336
    const uchar *end_words= (const uchar *)(intptr)
 
337
      (((ulonglong)(intptr)end) / SIZEOF_INT * SIZEOF_INT);
 
338
    const uchar *start_words= (const uchar *)(intptr)
 
339
       ((((ulonglong)(intptr)ptr) + SIZEOF_INT - 1) / SIZEOF_INT * SIZEOF_INT);
 
340
 
 
341
    DBUG_ASSERT(((ulonglong)(intptr)ptr) >= SIZEOF_INT);
 
342
    if (end_words > ptr)
 
343
    {
 
344
      while (end > end_words && end[-1] == 0x20)
 
345
        end--;
 
346
      if (end[-1] == 0x20 && start_words < end_words)
 
347
        while (end > start_words && ((unsigned *)end)[-1] == SPACE_INT)
 
348
          end -= SIZEOF_INT;
 
349
    }
 
350
  }
 
351
  while (end > ptr && end[-1] == 0x20)
 
352
    end--;
 
353
  return (end);
130
354
}
131
355
 
132
 
} /* namespace internal */
133
 
} /* namespace drizzled */
134
 
 
135
 
#endif /* DRIZZLED_INTERNAL_M_STRING_H */
 
356
#endif