~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.cc

  • Committer: Andrew Hutchings
  • Date: 2010-11-01 22:14:18 UTC
  • mto: This revision was merged to the branch mainline in revision 1907.
  • Revision ID: andrew@linuxjedi.co.uk-20101101221418-9n9gmm4ms7fl8vo5
Fix copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
16
/* This file is originally from the mysql distribution. Coded by monty */
17
17
 
58
58
{ }
59
59
 
60
60
 
61
 
String::String(uint32_t length_arg)
 
61
String::String(size_t length_arg)
62
62
  : Ptr(NULL),
63
63
    str_length(0),
64
64
    Alloced_length(0),
70
70
 
71
71
String::String(const char *str, const CHARSET_INFO * const cs)
72
72
  : Ptr(const_cast<char *>(str)),
73
 
    str_length(static_cast<uint32_t>(strlen(str))),
 
73
    str_length(static_cast<size_t>(strlen(str))),
74
74
    Alloced_length(0),
75
75
    alloced(false),
76
76
    str_charset(cs)
77
77
{ }
78
78
 
79
79
 
80
 
String::String(const char *str, uint32_t len, const CHARSET_INFO * const cs)
 
80
String::String(const char *str, size_t len, const CHARSET_INFO * const cs)
81
81
  : Ptr(const_cast<char *>(str)),
82
82
    str_length(len),
83
83
    Alloced_length(0),
86
86
{ }
87
87
 
88
88
 
89
 
String::String(char *str, uint32_t len, const CHARSET_INFO * const cs)
 
89
String::String(char *str, size_t len, const CHARSET_INFO * const cs)
90
90
  : Ptr(str),
91
91
    str_length(len),
92
92
    Alloced_length(len),
106
106
 
107
107
void *String::operator new(size_t size, memory::Root *mem_root)
108
108
{
109
 
  return alloc_root(mem_root, static_cast<uint32_t>(size));
 
109
  return mem_root->alloc_root(static_cast<size_t>(size));
110
110
}
111
111
 
112
112
String::~String() { free(); }
113
113
 
114
 
bool String::real_alloc(uint32_t arg_length)
 
114
bool String::real_alloc(size_t arg_length)
115
115
{
116
116
  arg_length=ALIGN_SIZE(arg_length+1);
117
117
  str_length=0;
118
118
  if (Alloced_length < arg_length)
119
119
  {
120
 
    free();
 
120
    if (Alloced_length > 0)
 
121
      free();
121
122
    if (!(Ptr=(char*) malloc(arg_length)))
122
123
      return true;
123
124
    Alloced_length=arg_length;
133
134
** (for C functions)
134
135
*/
135
136
 
136
 
bool String::realloc(uint32_t alloc_length)
 
137
bool String::realloc(size_t alloc_length)
137
138
{
138
 
  uint32_t len=ALIGN_SIZE(alloc_length+1);
 
139
  size_t len=ALIGN_SIZE(alloc_length+1);
139
140
  if (Alloced_length < len)
140
141
  {
141
142
    char *new_ptr;
167
168
 
168
169
bool String::set_int(int64_t num, bool unsigned_flag, const CHARSET_INFO * const cs)
169
170
{
170
 
  uint32_t l=20*cs->mbmaxlen+1;
 
171
  size_t l=20*cs->mbmaxlen+1;
171
172
  int base= unsigned_flag ? 10 : -10;
172
173
 
173
174
  if (alloc(l))
174
175
    return true;
175
 
  str_length=(uint32_t) (cs->cset->int64_t10_to_str)(cs,Ptr,l,base,num);
 
176
  str_length=(size_t) (cs->cset->int64_t10_to_str)(cs,Ptr,l,base,num);
176
177
  str_charset=cs;
177
178
  return false;
178
179
}
179
180
 
180
 
bool String::set_real(double num,uint32_t decimals, const CHARSET_INFO * const cs)
 
181
bool String::set_real(double num,size_t decimals, const CHARSET_INFO * const cs)
181
182
{
182
183
  char buff[FLOATING_POINT_BUFFER];
183
 
  uint32_t dummy_errors;
 
184
  size_t dummy_errors;
184
185
  size_t len;
185
186
 
186
187
  str_charset=cs;
192
193
    return copy(buff, len, &my_charset_utf8_general_ci, cs, &dummy_errors);
193
194
  }
194
195
  len= internal::my_fcvt(num, decimals, buff, NULL);
195
 
  return copy(buff, (uint32_t) len, &my_charset_utf8_general_ci, cs,
 
196
  return copy(buff, (size_t) len, &my_charset_utf8_general_ci, cs,
196
197
              &dummy_errors);
197
198
}
198
199
 
218
219
  return false;
219
220
}
220
221
 
221
 
bool String::copy(const char *str,uint32_t arg_length, const CHARSET_INFO * const cs)
 
222
bool String::copy(const char *str,size_t arg_length, const CHARSET_INFO * const cs)
222
223
{
223
224
  if (alloc(arg_length))
224
225
    return true;
240
241
  arg_length            Length of string to copy.
241
242
  from_cs               Character set to copy from
242
243
  to_cs                 Character set to copy to
243
 
  uint32_t *offset      Returns number of unaligned characters.
 
244
  size_t *offset        Returns number of unaligned characters.
244
245
 
245
246
  RETURN
246
247
   0  No conversion needed
252
253
  character_set_results is NULL.
253
254
*/
254
255
 
255
 
bool String::needs_conversion(uint32_t arg_length,
 
256
bool String::needs_conversion(size_t arg_length,
256
257
                              const CHARSET_INFO * const from_cs,
257
258
                              const CHARSET_INFO * const to_cs,
258
 
                              uint32_t *offset)
 
259
                              size_t *offset)
259
260
{
260
261
  *offset= 0;
261
262
  if (!to_cs ||
271
272
 
272
273
 
273
274
 
274
 
bool String::set_or_copy_aligned(const char *str,uint32_t arg_length,
 
275
bool String::set_or_copy_aligned(const char *str,size_t arg_length,
275
276
                                 const CHARSET_INFO * const cs)
276
277
{
277
278
  /* How many bytes are in incomplete character */
278
 
  uint32_t offset= (arg_length % cs->mbminlen);
 
279
  size_t offset= (arg_length % cs->mbminlen);
279
280
 
280
281
  assert(!offset); /* All characters are complete, just copy */
281
282
 
285
286
 
286
287
        /* Copy with charset conversion */
287
288
 
288
 
bool String::copy(const char *str, uint32_t arg_length,
 
289
bool String::copy(const char *str, size_t arg_length,
289
290
                          const CHARSET_INFO * const,
290
 
                                  const CHARSET_INFO * const to_cs, uint32_t *errors)
 
291
                                  const CHARSET_INFO * const to_cs, size_t *errors)
291
292
{
292
293
  *errors= 0;
293
294
  return copy(str, arg_length, to_cs);
313
314
 
314
315
*/
315
316
 
316
 
bool String::set_ascii(const char *str, uint32_t arg_length)
 
317
bool String::set_ascii(const char *str, size_t arg_length)
317
318
{
318
319
  if (str_charset->mbminlen == 1)
319
320
  {
320
321
    set(str, arg_length, str_charset);
321
322
    return 0;
322
323
  }
323
 
  uint32_t dummy_errors;
 
324
  size_t dummy_errors;
324
325
  return copy(str, arg_length, &my_charset_utf8_general_ci, str_charset, &dummy_errors);
325
326
}
326
327
 
341
342
  Append an ASCII string to the a string of the current character set
342
343
*/
343
344
 
344
 
bool String::append(const char *s,uint32_t arg_length)
 
345
bool String::append(const char *s,size_t arg_length)
345
346
{
346
347
  if (!arg_length)
347
348
    return false;
372
373
  with character set recoding
373
374
*/
374
375
 
375
 
bool String::append(const char *s,uint32_t arg_length, const CHARSET_INFO * const)
 
376
bool String::append(const char *s,size_t arg_length, const CHARSET_INFO * const)
376
377
{
377
378
  if (realloc(str_length + arg_length))
378
379
    return true;
383
384
}
384
385
 
385
386
 
386
 
bool String::append_with_prefill(const char *s,uint32_t arg_length,
387
 
                 uint32_t full_length, char fill_char)
 
387
bool String::append_with_prefill(const char *s,size_t arg_length,
 
388
                 size_t full_length, char fill_char)
388
389
{
389
390
  int t_length= arg_length > full_length ? arg_length : full_length;
390
391
 
400
401
  return false;
401
402
}
402
403
 
403
 
uint32_t String::numchars()
 
404
size_t String::numchars()
404
405
{
405
406
  return str_charset->cset->numchars(str_charset, Ptr, Ptr+str_length);
406
407
}
407
408
 
408
 
int String::charpos(int i,uint32_t offset)
 
409
int String::charpos(int i,size_t offset)
409
410
{
410
411
  if (i <= 0)
411
412
    return i;
412
413
  return str_charset->cset->charpos(str_charset,Ptr+offset,Ptr+str_length,i);
413
414
}
414
415
 
415
 
int String::strstr(const String &s,uint32_t offset)
 
416
int String::strstr(const String &s,size_t offset)
416
417
{
417
418
  if (s.length()+offset <= str_length)
418
419
  {
443
444
** Search string from end. Offset is offset to the end of string
444
445
*/
445
446
 
446
 
int String::strrstr(const String &s,uint32_t offset)
 
447
int String::strrstr(const String &s,size_t offset)
447
448
{
448
449
  if (s.length() <= offset && offset <= str_length)
449
450
  {
475
476
  If wrong parameter or not enough memory, do nothing
476
477
*/
477
478
 
478
 
bool String::replace(uint32_t offset,uint32_t arg_length,const String &to)
 
479
bool String::replace(size_t offset,size_t arg_length,const String &to)
479
480
{
480
481
  return replace(offset,arg_length,to.ptr(),to.length());
481
482
}
482
483
 
483
 
bool String::replace(uint32_t offset,uint32_t arg_length,
484
 
                     const char *to, uint32_t to_length)
 
484
bool String::replace(size_t offset,size_t arg_length,
 
485
                     const char *to, size_t to_length)
485
486
{
486
487
  long diff = (long) to_length-(long) arg_length;
487
488
  if (offset+arg_length <= str_length)
497
498
    {
498
499
      if (diff)
499
500
      {
500
 
        if (realloc(str_length+(uint32_t) diff))
 
501
        if (realloc(str_length+(size_t) diff))
501
502
          return true;
502
503
        internal::bmove_upp((unsigned char*) Ptr+str_length+diff,
503
504
                            (unsigned char*) Ptr+str_length,
506
507
      if (to_length)
507
508
        memcpy(Ptr+offset,to,to_length);
508
509
    }
509
 
    str_length+=(uint32_t) diff;
 
510
    str_length+=(size_t) diff;
510
511
  }
511
512
  return false;
512
513
}
560
561
 
561
562
int stringcmp(const String *s,const String *t)
562
563
{
563
 
  uint32_t s_len= s->length(), t_len= t->length(), len= min(s_len,t_len);
 
564
  size_t s_len= s->length(), t_len= t->length(), len= min(s_len,t_len);
564
565
  int cmp= memcmp(s->ptr(), t->ptr(), len);
565
566
  return (cmp) ? cmp : (int) (s_len - t_len);
566
567
}
567
568
 
568
569
 
569
 
String *copy_if_not_alloced(String *to,String *from,uint32_t from_length)
 
570
String *copy_if_not_alloced(String *to,String *from,size_t from_length)
570
571
{
571
572
  if (from->Alloced_length >= from_length)
572
573
    return from;
615
616
*/
616
617
 
617
618
 
618
 
uint32_t
 
619
size_t
619
620
well_formed_copy_nchars(const CHARSET_INFO * const to_cs,
620
 
                        char *to, uint32_t to_length,
 
621
                        char *to, size_t to_length,
621
622
                        const CHARSET_INFO * const from_cs,
622
 
                        const char *from, uint32_t from_length,
623
 
                        uint32_t nchars,
 
623
                        const char *from, size_t from_length,
 
624
                        size_t nchars,
624
625
                        const char **well_formed_error_pos,
625
626
                        const char **cannot_convert_error_pos,
626
627
                        const char **from_end_pos)
627
628
{
628
 
  uint32_t res;
 
629
  size_t res;
629
630
 
630
631
  assert((to_cs == &my_charset_bin) ||
631
632
         (from_cs == &my_charset_bin) ||
651
652
  else
652
653
  {
653
654
    int well_formed_error;
654
 
    uint32_t from_offset;
 
655
    size_t from_offset;
655
656
 
656
657
    if ((from_offset= (from_length % to_cs->mbminlen)) &&
657
658
        (from_cs == &my_charset_bin))
661
662
        INSERT INTO t1 (ucs2_column) VALUES (0x01);
662
663
        0x01 -> 0x0001
663
664
      */
664
 
      uint32_t pad_length= to_cs->mbminlen - from_offset;
 
665
      size_t pad_length= to_cs->mbminlen - from_offset;
665
666
      memset(to, 0, pad_length);
666
667
      memmove(to + pad_length, from, from_offset);
667
668
      nchars--;
733
734
/* Factor the extern out */
734
735
extern const CHARSET_INFO *system_charset_info, *files_charset_info;
735
736
 
736
 
void String::append_identifier(const char *name, uint32_t in_length)
 
737
void String::append_identifier(const char *name, size_t in_length)
737
738
{
738
739
  const char *name_end;
739
740
  char quote_char;
788
789
  std::swap(str_charset, s.str_charset);
789
790
}
790
791
 
791
 
void String::q_append(const uint32_t n)
 
792
void String::q_append(const size_t n)
792
793
{
793
 
  int4store(Ptr + str_length, n);
 
794
  int8store(Ptr + str_length, n);
794
795
  str_length += 4;
795
796
}
796
797
void String::q_append(double d)
803
804
  float8store(Ptr + str_length, *d);
804
805
  str_length += 8;
805
806
}
806
 
void String::q_append(const char *data, uint32_t data_len)
 
807
void String::q_append(const char *data, size_t data_len)
807
808
{
808
809
  memcpy(Ptr + str_length, data, data_len);
809
810
  str_length += data_len;
810
811
}
811
812
 
812
 
void String::write_at_position(int position, uint32_t value)
 
813
void String::write_at_position(int position, size_t value)
813
814
{
814
 
  int4store(Ptr + position,value);
 
815
  int8store(Ptr + position,value);
815
816
}
816
817
bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
817
818
                             char *end)