~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
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
16
/* This file is originally from the mysql distribution. Coded by monty */
17
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
18
#include <config.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
19
2241.4.14 by Stewart Smith
remove some includes from my_sys.h and instead only include where needed. This helps reduce the number of files that have to be rebuilt when you change some of the more widely included header files (such as the drizzled::identifier ones)
20
#include <drizzled/definitions.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <drizzled/internal/my_sys.h>
22
#include <drizzled/internal/m_string.h>
2241.4.14 by Stewart Smith
remove some includes from my_sys.h and instead only include where needed. This helps reduce the number of files that have to be rebuilt when you change some of the more widely included header files (such as the drizzled::identifier ones)
23
#include <drizzled/memory/root.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
24
#include <drizzled/charset.h>
1 by brian
clean slate
25
398.1.5 by Monty Taylor
Removed C++ includes and std namespace from global.h.
26
#include <algorithm>
27
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
28
#include <drizzled/sql_string.h>
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
29
1067.4.4 by Nathan Williams
The rest of the files in the drizzled directory were purged of the cmin macro and replace with std::min (except for the definition in globals.h and 1 usage in stacktrace.cc).
30
using namespace std;
31
2275.2.4 by Olaf van der Spek
Delete unused functions
32
namespace drizzled {
1271.2.4 by Tim Penhey
Add some standard converstion functions.
33
1 by brian
clean slate
34
/*****************************************************************************
35
** String functions
36
*****************************************************************************/
37
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
38
String::String()
39
  : Ptr(NULL),
40
    str_length(0),
41
    Alloced_length(0),
42
    alloced(false),
43
    str_charset(&my_charset_bin)
44
{ }
45
46
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
47
String::String(size_t length_arg)
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
48
  : Ptr(NULL),
49
    str_length(0),
50
    Alloced_length(0),
51
    alloced(false),
52
    str_charset(&my_charset_bin)
53
{
54
  (void) real_alloc(length_arg);
55
}
56
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
57
String::String(const char *str, const charset_info_st * const cs)
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
58
  : Ptr(const_cast<char *>(str)),
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
59
    str_length(static_cast<size_t>(strlen(str))),
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
60
    Alloced_length(0),
61
    alloced(false),
62
    str_charset(cs)
63
{ }
64
1253.2.5 by Monty Taylor
Merged from trunk.
65
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
66
String::String(const char *str, size_t len, const charset_info_st * const cs)
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
67
  : Ptr(const_cast<char *>(str)),
68
    str_length(len),
69
    Alloced_length(0),
70
    alloced(false),
71
    str_charset(cs)
72
{ }
73
74
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
75
String::String(char *str, size_t len, const charset_info_st * const cs)
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
76
  : Ptr(str),
77
    str_length(len),
1241.9.51 by Monty Taylor
More mysys stuff out of headers.
78
    Alloced_length(len),
1241.16.2 by Monty Taylor
Cleaned effc++ warnings from sql_string.
79
    alloced(false),
80
    str_charset(cs)
81
{ }
82
83
84
String::String(const String &str)
85
  : Ptr(str.Ptr),
86
    str_length(str.str_length),
87
    Alloced_length(str.Alloced_length),
88
    alloced(false),
89
    str_charset(str.str_charset)
90
{ }
91
92
1253.1.3 by Monty Taylor
MEM_ROOT == memory::Root
93
void *String::operator new(size_t size, memory::Root *mem_root)
1241.9.51 by Monty Taylor
More mysys stuff out of headers.
94
{
2318.6.40 by Olaf van der Spek
Refactor
95
  return mem_root->alloc(size);
1241.9.51 by Monty Taylor
More mysys stuff out of headers.
96
}
97
1022.2.29 by Monty Taylor
Fixed some no-inline warnings.
98
String::~String() { free(); }
99
2275.2.12 by Olaf van der Spek
Return void
100
void String::real_alloc(size_t arg_length)
1 by brian
clean slate
101
{
102
  arg_length=ALIGN_SIZE(arg_length+1);
103
  str_length=0;
104
  if (Alloced_length < arg_length)
105
  {
1885.2.2 by Andrew Hutchings
Fixed a double free()
106
    if (Alloced_length > 0)
107
      free();
2275.2.4 by Olaf van der Spek
Delete unused functions
108
    Ptr=(char*) malloc(arg_length);
1 by brian
clean slate
109
    Alloced_length=arg_length;
110
    alloced=1;
111
  }
112
  Ptr[0]=0;
113
}
114
115
116
/*
117
** Check that string is big enough. Set string[alloc_length] to 0
118
** (for C functions)
119
*/
120
2281.6.3 by Olaf van der Spek
Return void
121
void String::realloc(size_t alloc_length)
1 by brian
clean slate
122
{
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
123
  size_t len=ALIGN_SIZE(alloc_length+1);
1 by brian
clean slate
124
  if (Alloced_length < len)
125
  {
126
    char *new_ptr;
127
    if (alloced)
128
    {
2275.2.4 by Olaf van der Spek
Delete unused functions
129
      new_ptr= (char*) ::realloc(Ptr,len);
130
      Ptr=new_ptr;
131
      Alloced_length=len;
1 by brian
clean slate
132
    }
2275.2.4 by Olaf van der Spek
Delete unused functions
133
    else 
1 by brian
clean slate
134
    {
2275.2.4 by Olaf van der Spek
Delete unused functions
135
      new_ptr= (char*) malloc(len);
1 by brian
clean slate
136
      if (str_length)				// Avoid bugs in memcpy on AIX
2275.2.4 by Olaf van der Spek
Delete unused functions
137
        memcpy(new_ptr,Ptr,str_length);
1 by brian
clean slate
138
      new_ptr[str_length]=0;
139
      Ptr=new_ptr;
140
      Alloced_length=len;
141
      alloced=1;
142
    }
143
  }
144
  Ptr[alloc_length]=0;			// This make other funcs shorter
145
}
146
2297.1.5 by Olaf van der Spek
Return void
147
void String::set_int(int64_t num, bool unsigned_flag, const charset_info_st * const cs)
1 by brian
clean slate
148
{
2318.7.13 by Olaf van der Spek
Refactor
149
  size_t l= 20 * cs->mbmaxlen + 1;
2275.2.12 by Olaf van der Spek
Return void
150
  alloc(l);
2318.7.13 by Olaf van der Spek
Refactor
151
  str_length=(size_t) (cs->cset->int64_t10_to_str)(cs, Ptr, l, unsigned_flag ? 10 : -10,num);
1 by brian
clean slate
152
  str_charset=cs;
153
}
154
2281.6.1 by Olaf van der Spek
Return void
155
void String::set_real(double num,size_t decimals, const charset_info_st * const cs)
1 by brian
clean slate
156
{
157
  char buff[FLOATING_POINT_BUFFER];
158
  size_t len;
159
160
  str_charset=cs;
161
  if (decimals >= NOT_FIXED_DEC)
162
  {
2281.6.2 by Olaf van der Spek
Return void
163
    len= internal::my_gcvt(num, internal::MY_GCVT_ARG_DOUBLE, sizeof(buff) - 1, buff, NULL);
164
    copy(buff, len, cs);
165
    return;
1 by brian
clean slate
166
  }
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
167
  len= internal::my_fcvt(num, decimals, buff, NULL);
2281.6.2 by Olaf van der Spek
Return void
168
  copy(buff, len, cs);
1 by brian
clean slate
169
}
170
171
2281.4.12 by Olaf van der Spek
Return void
172
void String::copy()
1 by brian
clean slate
173
{
174
  if (!alloced)
175
  {
176
    Alloced_length=0;				// Force realloc
2281.4.12 by Olaf van der Spek
Return void
177
    realloc(str_length);
1 by brian
clean slate
178
  }
179
}
180
2297.1.6 by Olaf van der Spek
Return void
181
void String::copy(const String &str)
1 by brian
clean slate
182
{
2275.2.12 by Olaf van der Spek
Return void
183
  alloc(str.str_length);
1 by brian
clean slate
184
  str_length=str.str_length;
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
185
  memmove(Ptr, str.Ptr, str_length);		// May be overlapping
1 by brian
clean slate
186
  Ptr[str_length]=0;
187
  str_charset=str.str_charset;
188
}
189
2297.1.7 by Olaf van der Spek
Return void
190
void String::copy(const std::string& arg, const charset_info_st * const cs)	// Allocate new string
2039.6.4 by Brian Aker
Merge in local_instance change.
191
{
2275.2.12 by Olaf van der Spek
Return void
192
  alloc(arg.size());
2039.6.4 by Brian Aker
Merge in local_instance change.
193
194
  if ((str_length= arg.size()))
195
    memcpy(Ptr, arg.c_str(), arg.size());
196
197
  Ptr[arg.size()]= 0;
198
  str_charset= cs;
199
}
200
2281.4.16 by Olaf van der Spek
Return void
201
void String::copy(const char *str,size_t arg_length, const charset_info_st* cs)
1 by brian
clean slate
202
{
2275.2.12 by Olaf van der Spek
Return void
203
  alloc(arg_length);
1 by brian
clean slate
204
  if ((str_length=arg_length))
205
    memcpy(Ptr,str,arg_length);
206
  Ptr[arg_length]=0;
207
  str_charset=cs;
208
}
209
210
/*
211
  Checks that the source string can be just copied to the destination string
212
  without conversion.
213
214
  SYNPOSIS
215
216
  needs_conversion()
217
  arg_length		Length of string to copy.
218
  from_cs		Character set to copy from
219
  to_cs			Character set to copy to
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
220
  size_t *offset	Returns number of unaligned characters.
1 by brian
clean slate
221
222
  RETURN
223
   0  No conversion needed
224
   1  Either character set conversion or adding leading  zeros
225
      (e.g. for UCS-2) must be done
226
227
  NOTE
228
  to_cs may be NULL for "no conversion" if the system variable
229
  character_set_results is NULL.
230
*/
231
2318.7.8 by Olaf van der Spek
Remove unused param
232
bool String::needs_conversion(size_t arg_length, const charset_info_st* from_cs, const charset_info_st* to_cs)
1 by brian
clean slate
233
{
234
  if (!to_cs ||
2318.7.9 by Olaf van der Spek
Remove String::set_or_copy_aligned()
235
      to_cs == &my_charset_bin ||
236
      to_cs == from_cs ||
1 by brian
clean slate
237
      my_charset_same(from_cs, to_cs) ||
2318.7.9 by Olaf van der Spek
Remove String::set_or_copy_aligned()
238
      (from_cs == &my_charset_bin && not (arg_length % to_cs->mbminlen)))
51.1.74 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
239
    return false;
240
  return true;
1 by brian
clean slate
241
}
242
243
/*
244
  Set a string to the value of a latin1-string, keeping the original charset
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
245
1 by brian
clean slate
246
  SYNOPSIS
247
    copy_or_set()
248
    str			String of a simple charset (latin1)
249
    arg_length		Length of string
250
251
  IMPLEMENTATION
252
    If string object is of a simple character set, set it to point to the
253
    given string.
254
    If not, make a copy and convert it to the new character set.
255
256
  RETURN
257
    0	ok
258
    1	Could not allocate result buffer
259
260
*/
261
2281.6.1 by Olaf van der Spek
Return void
262
void String::set_ascii(const char *str, size_t arg_length)
1 by brian
clean slate
263
{
264
  if (str_charset->mbminlen == 1)
265
  {
266
    set(str, arg_length, str_charset);
2281.6.1 by Olaf van der Spek
Return void
267
    return;
1 by brian
clean slate
268
  }
2281.6.2 by Olaf van der Spek
Return void
269
  copy(str, arg_length, str_charset);
1 by brian
clean slate
270
}
271
2281.4.12 by Olaf van der Spek
Return void
272
void String::append(const String &s)
1 by brian
clean slate
273
{
274
  if (s.length())
275
  {
2281.4.12 by Olaf van der Spek
Return void
276
    realloc(str_length+s.length());
1 by brian
clean slate
277
    memcpy(Ptr+str_length,s.ptr(),s.length());
278
    str_length+=s.length();
279
  }
280
}
281
282
283
/*
284
  Append an ASCII string to the a string of the current character set
285
*/
286
2281.4.12 by Olaf van der Spek
Return void
287
void String::append(const char *s,size_t arg_length)
1 by brian
clean slate
288
{
2363.1.4 by Brian Aker
Fixes bug where true/false would not be interpreted correctly/displayed correctly.
289
  if (arg_length == 0)
2281.4.12 by Olaf van der Spek
Return void
290
    return;
1 by brian
clean slate
291
292
  /*
293
    For an ASCII compatinble string we can just append.
294
  */
2281.4.12 by Olaf van der Spek
Return void
295
  realloc(str_length+arg_length);
2363.1.4 by Brian Aker
Fixes bug where true/false would not be interpreted correctly/displayed correctly.
296
  memcpy(Ptr +str_length, s, arg_length);
1 by brian
clean slate
297
  str_length+=arg_length;
298
}
299
300
301
/*
302
  Append a 0-terminated ASCII string
303
*/
304
2281.4.12 by Olaf van der Spek
Return void
305
void String::append(const char *s)
1 by brian
clean slate
306
{
2281.4.12 by Olaf van der Spek
Return void
307
  append(s, strlen(s));
1 by brian
clean slate
308
}
309
2318.7.5 by Olaf van der Spek
Remove unused param
310
void String::append_with_prefill(const char *s,size_t arg_length, size_t full_length, char fill_char)
1 by brian
clean slate
311
{
312
  int t_length= arg_length > full_length ? arg_length : full_length;
313
2281.4.12 by Olaf van der Spek
Return void
314
  realloc(str_length + t_length);
1 by brian
clean slate
315
  t_length= full_length - arg_length;
316
  if (t_length > 0)
317
  {
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
318
    memset(Ptr+str_length, fill_char, t_length);
1 by brian
clean slate
319
    str_length=str_length + t_length;
320
  }
321
  append(s, arg_length);
322
}
323
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
324
size_t String::numchars()
1 by brian
clean slate
325
{
326
  return str_charset->cset->numchars(str_charset, Ptr, Ptr+str_length);
327
}
328
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
329
int String::charpos(int i,size_t offset)
1 by brian
clean slate
330
{
331
  if (i <= 0)
332
    return i;
333
  return str_charset->cset->charpos(str_charset,Ptr+offset,Ptr+str_length,i);
334
}
335
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
336
int String::strstr(const String &s,size_t offset)
1 by brian
clean slate
337
{
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
338
  if (s.length() + offset <= str_length)
1 by brian
clean slate
339
  {
340
    if (!s.length())
341
      return ((int) offset);	// Empty string is always found
342
2194.3.1 by Olaf van der Spek
Remove register keyword
343
    const char *str = Ptr+offset;
344
    const char *search=s.ptr();
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
345
    const char *last=Ptr+str_length-s.length()+1;
1 by brian
clean slate
346
    const char *search_end=s.ptr()+s.length();
347
skip:
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
348
    while (str != last)
1 by brian
clean slate
349
    {
350
      if (*str++ == *search)
351
      {
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
352
        const char* i= str; 
353
        const char* j= search + 1;
354
        while (j != search_end)
355
          if (*i++ != *j++) goto skip;
356
        return (int) (str - Ptr) - 1;
1 by brian
clean slate
357
      }
358
    }
359
  }
360
  return -1;
361
}
362
363
/*
364
** Search string from end. Offset is offset to the end of string
365
*/
366
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
367
int String::strrstr(const String &s,size_t offset)
1 by brian
clean slate
368
{
369
  if (s.length() <= offset && offset <= str_length)
370
  {
371
    if (!s.length())
372
      return offset;				// Empty string is always found
2194.3.1 by Olaf van der Spek
Remove register keyword
373
    const char *str = Ptr+offset-1;
374
    const char *search=s.ptr()+s.length()-1;
1 by brian
clean slate
375
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
376
    const char *last=Ptr+s.length()-2;
1 by brian
clean slate
377
    const char *search_end=s.ptr()-1;
378
skip:
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
379
    while (str != last)
1 by brian
clean slate
380
    {
381
      if (*str-- == *search)
382
      {
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
383
        const char* i= str; 
384
        const char* j= search-1;
385
        while (j != search_end)
386
          if (*i-- != *j--) goto skip;
387
        return (int) (i-Ptr) + 1;
1 by brian
clean slate
388
      }
389
    }
390
  }
391
  return -1;
392
}
393
394
/*
395
  Replace substring with string
396
  If wrong parameter or not enough memory, do nothing
397
*/
398
2281.4.13 by Olaf van der Spek
Return void
399
void String::replace(size_t offset,size_t arg_length,const String &to)
1 by brian
clean slate
400
{
2281.4.13 by Olaf van der Spek
Return void
401
  replace(offset,arg_length,to.ptr(),to.length());
1 by brian
clean slate
402
}
403
2281.4.13 by Olaf van der Spek
Return void
404
void String::replace(size_t offset,size_t arg_length,
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
405
                     const char *to, size_t to_length)
1 by brian
clean slate
406
{
407
  long diff = (long) to_length-(long) arg_length;
408
  if (offset+arg_length <= str_length)
409
  {
410
    if (diff < 0)
411
    {
412
      if (to_length)
413
	memcpy(Ptr+offset,to,to_length);
629.3.4 by Kristian Nielsen
Take Mats'es changes from bmove()->memcpy(), and fix all of them to be
414
      memmove(Ptr+offset+to_length, Ptr+offset+arg_length,
415
              str_length-offset-arg_length);
1 by brian
clean slate
416
    }
417
    else
418
    {
419
      if (diff)
420
      {
2281.4.13 by Olaf van der Spek
Return void
421
	realloc(str_length+(size_t) diff);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
422
	internal::bmove_upp((unsigned char*) Ptr+str_length+diff,
423
                            (unsigned char*) Ptr+str_length,
424
                            str_length-offset-arg_length);
1 by brian
clean slate
425
      }
426
      if (to_length)
427
	memcpy(Ptr+offset,to,to_length);
428
    }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
429
    str_length+=(size_t) diff;
1 by brian
clean slate
430
  }
431
}
432
433
434
435
/*
436
  Compare strings according to collation, without end space.
437
438
  SYNOPSIS
439
    sortcmp()
440
    s		First string
441
    t		Second string
442
    cs		Collation
443
444
  NOTE:
445
    Normally this is case sensitive comparison
446
447
  RETURN
448
  < 0	s < t
449
  0	s == t
450
  > 0	s > t
451
*/
452
453
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
454
int sortcmp(const String *s,const String *t, const charset_info_st * const cs)
1 by brian
clean slate
455
{
456
 return cs->coll->strnncollsp(cs,
481 by Brian Aker
Remove all of uchar.
457
                              (unsigned char *) s->ptr(),s->length(),
458
                              (unsigned char *) t->ptr(),t->length(), 0);
1 by brian
clean slate
459
}
460
461
462
/*
463
  Compare strings byte by byte. End spaces are also compared.
464
465
  SYNOPSIS
466
    stringcmp()
467
    s		First string
468
    t		Second string
469
470
  NOTE:
481 by Brian Aker
Remove all of uchar.
471
    Strings are compared as a stream of unsigned chars
1 by brian
clean slate
472
473
  RETURN
474
  < 0	s < t
475
  0	s == t
476
  > 0	s > t
477
*/
478
479
480
int stringcmp(const String *s,const String *t)
481
{
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
482
  size_t s_len= s->length(), t_len= t->length(), len= min(s_len,t_len);
1 by brian
clean slate
483
  int cmp= memcmp(s->ptr(), t->ptr(), len);
484
  return (cmp) ? cmp : (int) (s_len - t_len);
485
}
486
487
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
488
String *copy_if_not_alloced(String *to,String *from,size_t from_length)
1 by brian
clean slate
489
{
490
  if (from->Alloced_length >= from_length)
491
    return from;
492
  if (from->alloced || !to || from == to)
493
  {
494
    (void) from->realloc(from_length);
495
    return from;
496
  }
2281.6.3 by Olaf van der Spek
Return void
497
  to->realloc(from_length);
1067.4.4 by Nathan Williams
The rest of the files in the drizzled directory were purged of the cmin macro and replace with std::min (except for the definition in globals.h and 1 usage in stacktrace.cc).
498
  if ((to->str_length= min(from->str_length,from_length)))
1 by brian
clean slate
499
    memcpy(to->Ptr,from->Ptr,to->str_length);
500
  to->str_charset=from->str_charset;
501
  return to;
502
}
503
504
505
/****************************************************************************
506
  Help functions
507
****************************************************************************/
508
509
/*
510
  copy a string,
511
  with optional character set conversion,
512
  with optional left padding (for binary -> UCS2 conversion)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
513
1 by brian
clean slate
514
  SYNOPSIS
515
    well_formed_copy_nchars()
516
    to			     Store result here
517
    to_length                Maxinum length of "to" string
518
    to_cs		     Character set of "to" string
519
    from		     Copy from here
520
    from_length		     Length of from string
521
    from_cs		     From character set
522
    nchars                   Copy not more that nchars characters
523
    well_formed_error_pos    Return position when "from" is not well formed
524
                             or NULL otherwise.
525
    cannot_convert_error_pos Return position where a not convertable
526
                             character met, or NULL otherwise.
527
    from_end_pos             Return position where scanning of "from"
528
                             string stopped.
529
  NOTES
530
531
  RETURN
532
    length of bytes copied to 'to'
533
*/
534
535
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
536
size_t
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
537
well_formed_copy_nchars(const charset_info_st * const to_cs,
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
538
                        char *to, size_t to_length,
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
539
                        const charset_info_st * const from_cs,
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
540
                        const char *from, size_t from_length,
541
                        size_t nchars,
1 by brian
clean slate
542
                        const char **well_formed_error_pos,
543
                        const char **cannot_convert_error_pos,
544
                        const char **from_end_pos)
545
{
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
546
  size_t res;
1 by brian
clean slate
547
1034 by Brian Aker
Dead Code
548
  assert((to_cs == &my_charset_bin) ||
549
         (from_cs == &my_charset_bin) ||
550
         (to_cs == from_cs) ||
551
         my_charset_same(from_cs, to_cs));
552
553
  if (to_length < to_cs->mbminlen || !nchars)
554
  {
555
    *from_end_pos= from;
556
    *cannot_convert_error_pos= NULL;
557
    *well_formed_error_pos= NULL;
558
    return 0;
559
  }
560
561
  if (to_cs == &my_charset_bin)
562
  {
1067.4.4 by Nathan Williams
The rest of the files in the drizzled directory were purged of the cmin macro and replace with std::min (except for the definition in globals.h and 1 usage in stacktrace.cc).
563
    res= min(min(nchars, to_length), from_length);
1034 by Brian Aker
Dead Code
564
    memmove(to, from, res);
565
    *from_end_pos= from + res;
566
    *well_formed_error_pos= NULL;
567
    *cannot_convert_error_pos= NULL;
1 by brian
clean slate
568
  }
569
  else
570
  {
1034 by Brian Aker
Dead Code
571
    int well_formed_error;
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
572
    size_t from_offset;
1 by brian
clean slate
573
1034 by Brian Aker
Dead Code
574
    if ((from_offset= (from_length % to_cs->mbminlen)) &&
575
        (from_cs == &my_charset_bin))
1 by brian
clean slate
576
    {
1034 by Brian Aker
Dead Code
577
      /*
578
        Copying from BINARY to UCS2 needs to prepend zeros sometimes:
579
        INSERT INTO t1 (ucs2_column) VALUES (0x01);
580
        0x01 -> 0x0001
581
      */
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
582
      size_t pad_length= to_cs->mbminlen - from_offset;
1034 by Brian Aker
Dead Code
583
      memset(to, 0, pad_length);
584
      memmove(to + pad_length, from, from_offset);
585
      nchars--;
586
      from+= from_offset;
587
      from_length-= from_offset;
588
      to+= to_cs->mbminlen;
589
      to_length-= to_cs->mbminlen;
1 by brian
clean slate
590
    }
1034 by Brian Aker
Dead Code
591
592
    set_if_smaller(from_length, to_length);
593
    res= to_cs->cset->well_formed_len(to_cs, from, from + from_length,
594
                                      nchars, &well_formed_error);
595
    memmove(to, from, res);
596
    *from_end_pos= from + res;
597
    *well_formed_error_pos= well_formed_error ? from + res : NULL;
598
    *cannot_convert_error_pos= NULL;
599
    if (from_offset)
600
      res+= to_cs->mbminlen;
1 by brian
clean slate
601
  }
1034 by Brian Aker
Dead Code
602
603
  return res;
1 by brian
clean slate
604
}
605
2318.7.8 by Olaf van der Spek
Remove unused param
606
void String::print(String& str) const
1 by brian
clean slate
607
{
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
608
  const char* last= Ptr + str_length;
609
  for (const char* st= Ptr; st < last; st++)
1 by brian
clean slate
610
  {
481 by Brian Aker
Remove all of uchar.
611
    unsigned char c= *st;
1 by brian
clean slate
612
    switch (c)
613
    {
614
    case '\\':
2318.7.8 by Olaf van der Spek
Remove unused param
615
      str.append("\\\\", sizeof("\\\\")-1);
1 by brian
clean slate
616
      break;
617
    case '\0':
2318.7.8 by Olaf van der Spek
Remove unused param
618
      str.append("\\0", sizeof("\\0")-1);
1 by brian
clean slate
619
      break;
620
    case '\'':
2318.7.8 by Olaf van der Spek
Remove unused param
621
      str.append("\\'", sizeof("\\'")-1);
1 by brian
clean slate
622
      break;
623
    case '\n':
2318.7.8 by Olaf van der Spek
Remove unused param
624
      str.append("\\n", sizeof("\\n")-1);
1 by brian
clean slate
625
      break;
626
    case '\r':
2318.7.8 by Olaf van der Spek
Remove unused param
627
      str.append("\\r", sizeof("\\r")-1);
1 by brian
clean slate
628
      break;
629
    case '\032': // Ctrl-Z
2318.7.8 by Olaf van der Spek
Remove unused param
630
      str.append("\\Z", sizeof("\\Z")-1);
1 by brian
clean slate
631
      break;
632
    default:
2318.7.8 by Olaf van der Spek
Remove unused param
633
      str.append(c);
1 by brian
clean slate
634
    }
635
  }
636
}
637
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
638
/*
639
  Quote the given identifier.
640
  If the given identifier is empty, it will be quoted.
641
642
  SYNOPSIS
643
  append_identifier()
644
  name                  the identifier to be appended
645
  name_length           length of the appending identifier
646
*/
647
648
/* Factor the extern out */
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
649
extern const charset_info_st *system_charset_info;
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
650
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
651
void String::append_identifier(const char *name, size_t in_length)
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
652
{
2318.7.5 by Olaf van der Spek
Remove unused param
653
  // The identifier must be quoted as it includes a quote character or it's a keyword
654
655
  reserve(in_length * 2 + 2);
656
  const char quote_char= '`';
657
  append(&quote_char, 1);
658
659
  for (const char* name_end= name+in_length ; name < name_end ; name+= in_length)
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
660
  {
661
    unsigned char chr= (unsigned char) *name;
779.3.10 by Monty Taylor
Turned on -Wshadow.
662
    in_length= my_mbcharlen(system_charset_info, chr);
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
663
    /*
664
      my_mbcharlen can return 0 on a wrong multibyte
665
      sequence. It is possible when upgrading from 4.0,
666
      and identifier contains some accented characters.
667
      The manual says it does not work. So we'll just
668
      change length to 1 not to hang in the endless loop.
669
    */
779.3.10 by Monty Taylor
Turned on -Wshadow.
670
    if (!in_length)
671
      in_length= 1;
672
    if (in_length == 1 && chr == (unsigned char) quote_char)
2318.7.5 by Olaf van der Spek
Remove unused param
673
      append(&quote_char, 1);
674
    append(name, in_length);
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
675
  }
2318.7.5 by Olaf van der Spek
Remove unused param
676
  append(&quote_char, 1);
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
677
}
678
2318.8.1 by Olaf van der Spek
Add data_ref.h
679
void String::append_identifier(str_ref v)
680
{
681
  append_identifier(v.data(), v.size());
682
}
683
684
bool check_if_only_end_space(const charset_info_st * const cs, char *str, char *end)
1241.9.51 by Monty Taylor
More mysys stuff out of headers.
685
{
686
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
687
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
688
2023.2.1 by Brian Aker
Merge in BOOL type.
689
std::ostream& operator<<(std::ostream& output, const String &str)
690
{
691
  output << "String:(";
692
  output <<  const_cast<String&>(str).c_str();
693
  output << ", ";
694
  output << str.length();
695
  output << ")";
696
697
  return output;  // for multiple << operators.
698
}
699
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
700
} /* namespace drizzled */
701
702
bool operator==(const drizzled::String &s1, const drizzled::String &s2)
703
{
704
  return stringcmp(&s1,&s2) == 0;
705
}
706
707
bool operator!=(const drizzled::String &s1, const drizzled::String &s2)
708
{
709
  return !(s1 == s2);
710
}
711