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