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