~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;
2385.2.6 by Olaf van der Spek
Refactor
187
  str_charset= str.str_charset;
1 by brian
clean slate
188
}
189
2385.2.6 by Olaf van der Spek
Refactor
190
void String::copy(const std::string& arg, const charset_info_st* 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());
2385.2.6 by Olaf van der Spek
Refactor
193
  str_length= arg.size();
194
  memcpy(Ptr, arg.c_str(), arg.size());
2039.6.4 by Brian Aker
Merge in local_instance change.
195
  Ptr[arg.size()]= 0;
196
  str_charset= cs;
197
}
198
2281.4.16 by Olaf van der Spek
Return void
199
void String::copy(const char *str,size_t arg_length, const charset_info_st* cs)
1 by brian
clean slate
200
{
2275.2.12 by Olaf van der Spek
Return void
201
  alloc(arg_length);
1 by brian
clean slate
202
  if ((str_length=arg_length))
203
    memcpy(Ptr,str,arg_length);
204
  Ptr[arg_length]=0;
205
  str_charset=cs;
206
}
207
208
/*
209
  Checks that the source string can be just copied to the destination string
210
  without conversion.
211
212
  SYNPOSIS
213
214
  needs_conversion()
215
  arg_length		Length of string to copy.
216
  from_cs		Character set to copy from
217
  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).
218
  size_t *offset	Returns number of unaligned characters.
1 by brian
clean slate
219
220
  RETURN
221
   0  No conversion needed
222
   1  Either character set conversion or adding leading  zeros
223
      (e.g. for UCS-2) must be done
224
225
  NOTE
226
  to_cs may be NULL for "no conversion" if the system variable
227
  character_set_results is NULL.
228
*/
229
2318.7.8 by Olaf van der Spek
Remove unused param
230
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
231
{
232
  if (!to_cs ||
2318.7.9 by Olaf van der Spek
Remove String::set_or_copy_aligned()
233
      to_cs == &my_charset_bin ||
234
      to_cs == from_cs ||
1 by brian
clean slate
235
      my_charset_same(from_cs, to_cs) ||
2318.7.9 by Olaf van der Spek
Remove String::set_or_copy_aligned()
236
      (from_cs == &my_charset_bin && not (arg_length % to_cs->mbminlen)))
51.1.74 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
237
    return false;
238
  return true;
1 by brian
clean slate
239
}
240
241
/*
242
  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:
243
1 by brian
clean slate
244
  SYNOPSIS
245
    copy_or_set()
246
    str			String of a simple charset (latin1)
247
    arg_length		Length of string
248
249
  IMPLEMENTATION
250
    If string object is of a simple character set, set it to point to the
251
    given string.
252
    If not, make a copy and convert it to the new character set.
253
254
  RETURN
255
    0	ok
256
    1	Could not allocate result buffer
257
258
*/
259
2281.6.1 by Olaf van der Spek
Return void
260
void String::set_ascii(const char *str, size_t arg_length)
1 by brian
clean slate
261
{
262
  if (str_charset->mbminlen == 1)
263
  {
264
    set(str, arg_length, str_charset);
2281.6.1 by Olaf van der Spek
Return void
265
    return;
1 by brian
clean slate
266
  }
2281.6.2 by Olaf van der Spek
Return void
267
  copy(str, arg_length, str_charset);
1 by brian
clean slate
268
}
269
270
271
/*
272
  Append an ASCII string to the a string of the current character set
273
*/
274
2281.4.12 by Olaf van der Spek
Return void
275
void String::append(const char *s,size_t arg_length)
1 by brian
clean slate
276
{
2363.1.4 by Brian Aker
Fixes bug where true/false would not be interpreted correctly/displayed correctly.
277
  if (arg_length == 0)
2281.4.12 by Olaf van der Spek
Return void
278
    return;
1 by brian
clean slate
279
280
  /*
281
    For an ASCII compatinble string we can just append.
282
  */
2385.1.3 by Olaf van der Spek
Refactor getTableName
283
  realloc(str_length + arg_length);
284
  memcpy(Ptr + str_length, s, arg_length);
285
  str_length+= arg_length;
1 by brian
clean slate
286
}
287
288
/*
289
  Append a 0-terminated ASCII string
290
*/
291
2281.4.12 by Olaf van der Spek
Return void
292
void String::append(const char *s)
1 by brian
clean slate
293
{
2281.4.12 by Olaf van der Spek
Return void
294
  append(s, strlen(s));
1 by brian
clean slate
295
}
296
2385.1.3 by Olaf van der Spek
Refactor getTableName
297
void String::append(str_ref s)
298
{
299
  append(s.data(), s.size());
300
}
301
2318.7.5 by Olaf van der Spek
Remove unused param
302
void String::append_with_prefill(const char *s,size_t arg_length, size_t full_length, char fill_char)
1 by brian
clean slate
303
{
304
  int t_length= arg_length > full_length ? arg_length : full_length;
305
2281.4.12 by Olaf van der Spek
Return void
306
  realloc(str_length + t_length);
1 by brian
clean slate
307
  t_length= full_length - arg_length;
308
  if (t_length > 0)
309
  {
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
310
    memset(Ptr+str_length, fill_char, t_length);
1 by brian
clean slate
311
    str_length=str_length + t_length;
312
  }
313
  append(s, arg_length);
314
}
315
2385.2.6 by Olaf van der Spek
Refactor
316
size_t String::numchars() const
1 by brian
clean slate
317
{
318
  return str_charset->cset->numchars(str_charset, Ptr, Ptr+str_length);
319
}
320
2385.2.6 by Olaf van der Spek
Refactor
321
int String::charpos(int i,size_t offset) const
1 by brian
clean slate
322
{
2385.2.6 by Olaf van der Spek
Refactor
323
  return i <= 0 ? i : str_charset->cset->charpos(str_charset, Ptr + offset, Ptr + str_length, i);
1 by brian
clean slate
324
}
325
2385.2.6 by Olaf van der Spek
Refactor
326
int String::strstr(const String &s, size_t offset)
1 by brian
clean slate
327
{
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
328
  if (s.length() + offset <= str_length)
1 by brian
clean slate
329
  {
330
    if (!s.length())
331
      return ((int) offset);	// Empty string is always found
332
2194.3.1 by Olaf van der Spek
Remove register keyword
333
    const char *str = Ptr+offset;
334
    const char *search=s.ptr();
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
335
    const char *last=Ptr+str_length-s.length()+1;
1 by brian
clean slate
336
    const char *search_end=s.ptr()+s.length();
337
skip:
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
338
    while (str != last)
1 by brian
clean slate
339
    {
340
      if (*str++ == *search)
341
      {
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
342
        const char* i= str; 
343
        const char* j= search + 1;
344
        while (j != search_end)
345
          if (*i++ != *j++) goto skip;
346
        return (int) (str - Ptr) - 1;
1 by brian
clean slate
347
      }
348
    }
349
  }
350
  return -1;
351
}
352
353
/*
354
** Search string from end. Offset is offset to the end of string
355
*/
356
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
357
int String::strrstr(const String &s,size_t offset)
1 by brian
clean slate
358
{
359
  if (s.length() <= offset && offset <= str_length)
360
  {
361
    if (!s.length())
362
      return offset;				// Empty string is always found
2194.3.1 by Olaf van der Spek
Remove register keyword
363
    const char *str = Ptr+offset-1;
364
    const char *search=s.ptr()+s.length()-1;
1 by brian
clean slate
365
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
366
    const char *last=Ptr+s.length()-2;
1 by brian
clean slate
367
    const char *search_end=s.ptr()-1;
368
skip:
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
369
    while (str != last)
1 by brian
clean slate
370
    {
371
      if (*str-- == *search)
372
      {
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
373
        const char* i= str; 
374
        const char* j= search-1;
375
        while (j != search_end)
376
          if (*i-- != *j--) goto skip;
377
        return (int) (i-Ptr) + 1;
1 by brian
clean slate
378
      }
379
    }
380
  }
381
  return -1;
382
}
383
384
/*
385
  Replace substring with string
386
  If wrong parameter or not enough memory, do nothing
387
*/
388
2281.4.13 by Olaf van der Spek
Return void
389
void String::replace(size_t offset,size_t arg_length,const String &to)
1 by brian
clean slate
390
{
2281.4.13 by Olaf van der Spek
Return void
391
  replace(offset,arg_length,to.ptr(),to.length());
1 by brian
clean slate
392
}
393
2281.4.13 by Olaf van der Spek
Return void
394
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).
395
                     const char *to, size_t to_length)
1 by brian
clean slate
396
{
397
  long diff = (long) to_length-(long) arg_length;
398
  if (offset+arg_length <= str_length)
399
  {
400
    if (diff < 0)
401
    {
402
      if (to_length)
403
	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
404
      memmove(Ptr+offset+to_length, Ptr+offset+arg_length,
405
              str_length-offset-arg_length);
1 by brian
clean slate
406
    }
407
    else
408
    {
409
      if (diff)
410
      {
2281.4.13 by Olaf van der Spek
Return void
411
	realloc(str_length+(size_t) diff);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
412
	internal::bmove_upp((unsigned char*) Ptr+str_length+diff,
413
                            (unsigned char*) Ptr+str_length,
414
                            str_length-offset-arg_length);
1 by brian
clean slate
415
      }
416
      if (to_length)
417
	memcpy(Ptr+offset,to,to_length);
418
    }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
419
    str_length+=(size_t) diff;
1 by brian
clean slate
420
  }
421
}
422
423
424
425
/*
426
  Compare strings according to collation, without end space.
427
428
  SYNOPSIS
429
    sortcmp()
430
    s		First string
431
    t		Second string
432
    cs		Collation
433
434
  NOTE:
435
    Normally this is case sensitive comparison
436
437
  RETURN
438
  < 0	s < t
439
  0	s == t
440
  > 0	s > t
441
*/
442
443
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
444
int sortcmp(const String *s,const String *t, const charset_info_st * const cs)
1 by brian
clean slate
445
{
446
 return cs->coll->strnncollsp(cs,
481 by Brian Aker
Remove all of uchar.
447
                              (unsigned char *) s->ptr(),s->length(),
448
                              (unsigned char *) t->ptr(),t->length(), 0);
1 by brian
clean slate
449
}
450
451
452
/*
453
  Compare strings byte by byte. End spaces are also compared.
454
455
  SYNOPSIS
456
    stringcmp()
457
    s		First string
458
    t		Second string
459
460
  NOTE:
481 by Brian Aker
Remove all of uchar.
461
    Strings are compared as a stream of unsigned chars
1 by brian
clean slate
462
463
  RETURN
464
  < 0	s < t
465
  0	s == t
466
  > 0	s > t
467
*/
468
469
470
int stringcmp(const String *s,const String *t)
471
{
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
472
  size_t s_len= s->length(), t_len= t->length(), len= min(s_len,t_len);
1 by brian
clean slate
473
  int cmp= memcmp(s->ptr(), t->ptr(), len);
474
  return (cmp) ? cmp : (int) (s_len - t_len);
475
}
476
477
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
478
String *copy_if_not_alloced(String *to,String *from,size_t from_length)
1 by brian
clean slate
479
{
480
  if (from->Alloced_length >= from_length)
481
    return from;
482
  if (from->alloced || !to || from == to)
483
  {
484
    (void) from->realloc(from_length);
485
    return from;
486
  }
2281.6.3 by Olaf van der Spek
Return void
487
  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).
488
  if ((to->str_length= min(from->str_length,from_length)))
1 by brian
clean slate
489
    memcpy(to->Ptr,from->Ptr,to->str_length);
490
  to->str_charset=from->str_charset;
491
  return to;
492
}
493
494
495
/****************************************************************************
496
  Help functions
497
****************************************************************************/
498
499
/*
500
  copy a string,
501
  with optional character set conversion,
502
  with optional left padding (for binary -> UCS2 conversion)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
503
1 by brian
clean slate
504
  SYNOPSIS
505
    well_formed_copy_nchars()
506
    to			     Store result here
507
    to_length                Maxinum length of "to" string
508
    to_cs		     Character set of "to" string
509
    from		     Copy from here
510
    from_length		     Length of from string
511
    from_cs		     From character set
512
    nchars                   Copy not more that nchars characters
513
    well_formed_error_pos    Return position when "from" is not well formed
514
                             or NULL otherwise.
515
    cannot_convert_error_pos Return position where a not convertable
516
                             character met, or NULL otherwise.
517
    from_end_pos             Return position where scanning of "from"
518
                             string stopped.
519
  NOTES
520
521
  RETURN
522
    length of bytes copied to 'to'
523
*/
524
525
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
526
size_t
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
527
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).
528
                        char *to, size_t to_length,
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
529
                        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).
530
                        const char *from, size_t from_length,
531
                        size_t nchars,
1 by brian
clean slate
532
                        const char **well_formed_error_pos,
533
                        const char **cannot_convert_error_pos,
534
                        const char **from_end_pos)
535
{
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
536
  size_t res;
1 by brian
clean slate
537
1034 by Brian Aker
Dead Code
538
  assert((to_cs == &my_charset_bin) ||
539
         (from_cs == &my_charset_bin) ||
540
         (to_cs == from_cs) ||
541
         my_charset_same(from_cs, to_cs));
542
543
  if (to_length < to_cs->mbminlen || !nchars)
544
  {
545
    *from_end_pos= from;
546
    *cannot_convert_error_pos= NULL;
547
    *well_formed_error_pos= NULL;
548
    return 0;
549
  }
550
551
  if (to_cs == &my_charset_bin)
552
  {
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).
553
    res= min(min(nchars, to_length), from_length);
1034 by Brian Aker
Dead Code
554
    memmove(to, from, res);
555
    *from_end_pos= from + res;
556
    *well_formed_error_pos= NULL;
557
    *cannot_convert_error_pos= NULL;
1 by brian
clean slate
558
  }
559
  else
560
  {
1034 by Brian Aker
Dead Code
561
    int well_formed_error;
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
562
    size_t from_offset;
1 by brian
clean slate
563
1034 by Brian Aker
Dead Code
564
    if ((from_offset= (from_length % to_cs->mbminlen)) &&
565
        (from_cs == &my_charset_bin))
1 by brian
clean slate
566
    {
1034 by Brian Aker
Dead Code
567
      /*
568
        Copying from BINARY to UCS2 needs to prepend zeros sometimes:
569
        INSERT INTO t1 (ucs2_column) VALUES (0x01);
570
        0x01 -> 0x0001
571
      */
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
572
      size_t pad_length= to_cs->mbminlen - from_offset;
1034 by Brian Aker
Dead Code
573
      memset(to, 0, pad_length);
574
      memmove(to + pad_length, from, from_offset);
575
      nchars--;
576
      from+= from_offset;
577
      from_length-= from_offset;
578
      to+= to_cs->mbminlen;
579
      to_length-= to_cs->mbminlen;
1 by brian
clean slate
580
    }
1034 by Brian Aker
Dead Code
581
582
    set_if_smaller(from_length, to_length);
2440.2.14 by Olaf van der Spek
Refactor
583
    res= to_cs->cset->well_formed_len(*to_cs, str_ref(from, from_length), nchars, &well_formed_error);
1034 by Brian Aker
Dead Code
584
    memmove(to, from, res);
585
    *from_end_pos= from + res;
586
    *well_formed_error_pos= well_formed_error ? from + res : NULL;
587
    *cannot_convert_error_pos= NULL;
588
    if (from_offset)
589
      res+= to_cs->mbminlen;
1 by brian
clean slate
590
  }
1034 by Brian Aker
Dead Code
591
592
  return res;
1 by brian
clean slate
593
}
594
2318.7.8 by Olaf van der Spek
Remove unused param
595
void String::print(String& str) const
1 by brian
clean slate
596
{
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
597
  const char* last= Ptr + str_length;
598
  for (const char* st= Ptr; st < last; st++)
1 by brian
clean slate
599
  {
481 by Brian Aker
Remove all of uchar.
600
    unsigned char c= *st;
1 by brian
clean slate
601
    switch (c)
602
    {
603
    case '\\':
2318.7.8 by Olaf van der Spek
Remove unused param
604
      str.append("\\\\", sizeof("\\\\")-1);
1 by brian
clean slate
605
      break;
606
    case '\0':
2318.7.8 by Olaf van der Spek
Remove unused param
607
      str.append("\\0", sizeof("\\0")-1);
1 by brian
clean slate
608
      break;
609
    case '\'':
2318.7.8 by Olaf van der Spek
Remove unused param
610
      str.append("\\'", sizeof("\\'")-1);
1 by brian
clean slate
611
      break;
612
    case '\n':
2318.7.8 by Olaf van der Spek
Remove unused param
613
      str.append("\\n", sizeof("\\n")-1);
1 by brian
clean slate
614
      break;
615
    case '\r':
2318.7.8 by Olaf van der Spek
Remove unused param
616
      str.append("\\r", sizeof("\\r")-1);
1 by brian
clean slate
617
      break;
618
    case '\032': // Ctrl-Z
2318.7.8 by Olaf van der Spek
Remove unused param
619
      str.append("\\Z", sizeof("\\Z")-1);
1 by brian
clean slate
620
      break;
621
    default:
2318.7.8 by Olaf van der Spek
Remove unused param
622
      str.append(c);
1 by brian
clean slate
623
    }
624
  }
625
}
626
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
627
/*
628
  Quote the given identifier.
629
  If the given identifier is empty, it will be quoted.
630
631
  SYNOPSIS
632
  append_identifier()
633
  name                  the identifier to be appended
634
  name_length           length of the appending identifier
635
*/
636
637
/* Factor the extern out */
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
638
extern const charset_info_st *system_charset_info;
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
639
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
640
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
641
{
2318.7.5 by Olaf van der Spek
Remove unused param
642
  // The identifier must be quoted as it includes a quote character or it's a keyword
643
644
  reserve(in_length * 2 + 2);
645
  const char quote_char= '`';
646
  append(&quote_char, 1);
647
648
  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
649
  {
650
    unsigned char chr= (unsigned char) *name;
779.3.10 by Monty Taylor
Turned on -Wshadow.
651
    in_length= my_mbcharlen(system_charset_info, chr);
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
652
    /*
653
      my_mbcharlen can return 0 on a wrong multibyte
654
      sequence. It is possible when upgrading from 4.0,
655
      and identifier contains some accented characters.
656
      The manual says it does not work. So we'll just
657
      change length to 1 not to hang in the endless loop.
658
    */
779.3.10 by Monty Taylor
Turned on -Wshadow.
659
    if (!in_length)
660
      in_length= 1;
661
    if (in_length == 1 && chr == (unsigned char) quote_char)
2318.7.5 by Olaf van der Spek
Remove unused param
662
      append(&quote_char, 1);
663
    append(name, in_length);
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
664
  }
2318.7.5 by Olaf van der Spek
Remove unused param
665
  append(&quote_char, 1);
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
666
}
667
2318.8.1 by Olaf van der Spek
Add data_ref.h
668
void String::append_identifier(str_ref v)
669
{
670
  append_identifier(v.data(), v.size());
671
}
672
673
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.
674
{
675
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
676
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
677
2023.2.1 by Brian Aker
Merge in BOOL type.
678
std::ostream& operator<<(std::ostream& output, const String &str)
679
{
680
  output << "String:(";
681
  output <<  const_cast<String&>(str).c_str();
682
  output << ", ";
683
  output << str.length();
684
  output << ")";
685
686
  return output;  // for multiple << operators.
687
}
688
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
689
} /* namespace drizzled */
690
691
bool operator==(const drizzled::String &s1, const drizzled::String &s2)
692
{
693
  return stringcmp(&s1,&s2) == 0;
694
}
695
696
bool operator!=(const drizzled::String &s1, const drizzled::String &s2)
697
{
698
  return !(s1 == s2);
699
}
700