~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mystrings/m_string.h

  • Committer: Monty Taylor
  • Date: 2008-10-23 23:53:49 UTC
  • mto: This revision was merged to the branch mainline in revision 557.
  • Revision ID: monty@inaugust.com-20081023235349-317wgwqwgccuacmq
SplitĀ outĀ nested_join.h.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include <stdbool.h>
40
40
#include <assert.h>
41
41
#include <limits.h>
 
42
#include <ctype.h>
42
43
 
43
44
/*  This is needed for the definitions of memcpy... on solaris */
44
45
#if defined(HAVE_MEMORY_H) && !defined(__cplusplus)
80
81
extern  char *strxmov(char *dst,const char *src, ...);
81
82
extern  char *strxcpy(char *dst,const char *src, ...);
82
83
extern  char *strxncat(char *dst,size_t len, const char *src, ...);
83
 
extern  char *strxnmov(char *dst,size_t len, const char *src, ...);
84
84
extern  char *strxncpy(char *dst,size_t len, const char *src, ...);
85
85
 
86
86
/* Prototypes of normal stringfunctions (with may ours) */
141
141
extern char *llstr(int64_t value,char *buff);
142
142
extern char *ullstr(int64_t value,char *buff);
143
143
 
144
 
extern char *int2str(long val, char *dst, int radix, int upcase);
145
 
extern char *int10_to_str(long val,char *dst,int radix);
 
144
extern char *int2str(int32_t val, char *dst, int radix, int upcase);
 
145
extern char *int10_to_str(int32_t val,char *dst,int radix);
146
146
extern char *str2int(const char *src,int radix,long lower,long upper,
147
147
                         long *val);
148
148
int64_t my_strtoll10(const char *nptr, char **endptr, int *error);
149
 
#if SIZEOF_LONG == SIZEOF_LONG_LONG
150
 
#define int64_t2str(A,B,C) int2str((A),(B),(C),1)
151
 
#define int64_t10_to_str(A,B,C) int10_to_str((A),(B),(C))
152
 
#else
153
149
extern char *int64_t2str(int64_t val,char *dst,int radix);
154
150
extern char *int64_t10_to_str(int64_t val,char *dst,int radix);
155
 
#endif
156
151
 
157
152
 
158
153
#if defined(__cplusplus)
177
172
#define USTRING_WITH_LEN(X) ((unsigned char*) X), ((size_t) (sizeof(X) - 1))
178
173
#define C_STRING_WITH_LEN(X) ((char *) (X)), ((size_t) (sizeof(X) - 1))
179
174
 
180
 
/* SPACE_INT is a word that contains only spaces */
181
 
#if SIZEOF_INT == 4
182
 
#define SPACE_INT 0x20202020
183
 
#elif SIZEOF_INT == 8
184
 
#define SPACE_INT 0x2020202020202020
185
 
#else
186
 
#error define the appropriate constant for a word full of spaces
187
 
#endif
188
 
 
189
175
/**
190
176
  Skip trailing space.
191
177
 
192
 
  On most systems reading memory in larger chunks (ideally equal to the size of
193
 
  the chinks that the machine physically reads from memory) causes fewer memory
194
 
  access loops and hence increased performance.
195
 
  This is why the 'int' type is used : it's closest to that (according to how
196
 
  it's defined in C).
197
 
  So when we determine the amount of whitespace at the end of a string we do
198
 
  the following :
199
 
    1. We divide the string into 3 zones :
200
 
      a) from the start of the string (__start) to the first multiple
201
 
        of sizeof(int)  (__start_words)
202
 
      b) from the end of the string (__end) to the last multiple of sizeof(int)
203
 
        (__end_words)
204
 
      c) a zone that is aligned to sizeof(int) and can be safely accessed
205
 
        through an int *
206
 
    2. We start comparing backwards from (c) char-by-char. If all we find is
207
 
       space then we continue
208
 
    3. If there are elements in zone (b) we compare them as unsigned ints to a
209
 
       int mask (SPACE_INT) consisting of all spaces
210
 
    4. Finally we compare the remaining part (a) of the string char by char.
211
 
       This covers for the last non-space unsigned int from 3. (if any)
212
 
 
213
 
   This algorithm works well for relatively larger strings, but it will slow
214
 
   the things down for smaller strings (because of the additional calculations
215
 
   and checks compared to the naive method). Thus the barrier of length 20
216
 
   is added.
217
 
 
218
178
   @param     ptr   pointer to the input string
219
179
   @param     len   the length of the string
220
180
   @return          the last non-space character
221
181
*/
222
182
 
223
 
static inline const unsigned char *skip_trailing_space(const unsigned char *ptr,size_t len)
 
183
static inline const unsigned char *
 
184
skip_trailing_space(const unsigned char *ptr,size_t len)
224
185
{
225
186
  const unsigned char *end= ptr + len;
226
187
 
227
 
  if (len > 20)
228
 
  {
229
 
    const unsigned char *end_words= (const unsigned char *)(intptr_t)
230
 
      (((uint64_t)(intptr_t)end) / SIZEOF_INT * SIZEOF_INT);
231
 
    const unsigned char *start_words= (const unsigned char *)(intptr_t)
232
 
       ((((uint64_t)(intptr_t)ptr) + SIZEOF_INT - 1) / SIZEOF_INT * SIZEOF_INT);
233
 
 
234
 
    assert(((uint64_t)(intptr_t)ptr) >= SIZEOF_INT);
235
 
    if (end_words > ptr)
236
 
    {
237
 
      while (end > end_words && end[-1] == 0x20)
238
 
        end--;
239
 
      if (end[-1] == 0x20 && start_words < end_words)
240
 
        while (end > start_words && ((const unsigned *)end)[-1] == SPACE_INT)
241
 
          end -= SIZEOF_INT;
242
 
    }
243
 
  }
244
 
  while (end > ptr && end[-1] == 0x20)
245
 
    end--;
246
 
  return (end);
 
188
  while (end > ptr && isspace(*--end))
 
189
    continue;
 
190
  return end+1;
247
191
}
248
192
 
249
193
#endif