~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/sql_string.cc

  • Committer: Monty Taylor
  • Date: 2008-07-05 17:07:46 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: monty@inaugust.com-20080705170746-8aq11u9fuwtfwy85
Removed my_alarm. Made my_lock only do the non-alarm version. Moved my_lock to MyISAM where it belongs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/* This file is originally from the mysql distribution. Coded by monty */
 
17
 
 
18
#ifdef USE_PRAGMA_IMPLEMENTATION
 
19
#pragma implementation                          // gcc: Class implementation
 
20
#endif
 
21
 
 
22
#include <my_global.h>
 
23
#include <my_sys.h>
 
24
#include <m_string.h>
 
25
#include <m_ctype.h>
 
26
#ifdef HAVE_FCONVERT
 
27
#include <floatingpoint.h>
 
28
#endif
 
29
 
 
30
/*
 
31
  The following extern declarations are ok as these are interface functions
 
32
  required by the string function
 
33
*/
 
34
 
 
35
extern void sql_alloc(size_t size);
 
36
extern void sql_element_free(void *ptr);
 
37
 
 
38
#include "sql_string.h"
 
39
 
 
40
/*****************************************************************************
 
41
** String functions
 
42
*****************************************************************************/
 
43
 
 
44
bool String::real_alloc(uint32 arg_length)
 
45
{
 
46
  arg_length=ALIGN_SIZE(arg_length+1);
 
47
  str_length=0;
 
48
  if (Alloced_length < arg_length)
 
49
  {
 
50
    free();
 
51
    if (!(Ptr=(char*) my_malloc(arg_length,MYF(MY_WME))))
 
52
      return TRUE;
 
53
    Alloced_length=arg_length;
 
54
    alloced=1;
 
55
  }
 
56
  Ptr[0]=0;
 
57
  return FALSE;
 
58
}
 
59
 
 
60
 
 
61
/*
 
62
** Check that string is big enough. Set string[alloc_length] to 0
 
63
** (for C functions)
 
64
*/
 
65
 
 
66
bool String::realloc(uint32 alloc_length)
 
67
{
 
68
  uint32 len=ALIGN_SIZE(alloc_length+1);
 
69
  if (Alloced_length < len)
 
70
  {
 
71
    char *new_ptr;
 
72
    if (alloced)
 
73
    {
 
74
      if ((new_ptr= (char*) my_realloc(Ptr,len,MYF(MY_WME))))
 
75
      {
 
76
        Ptr=new_ptr;
 
77
        Alloced_length=len;
 
78
      }
 
79
      else
 
80
        return TRUE;                            // Signal error
 
81
    }
 
82
    else if ((new_ptr= (char*) my_malloc(len,MYF(MY_WME))))
 
83
    {
 
84
      if (str_length)                           // Avoid bugs in memcpy on AIX
 
85
        memcpy(new_ptr,Ptr,str_length);
 
86
      new_ptr[str_length]=0;
 
87
      Ptr=new_ptr;
 
88
      Alloced_length=len;
 
89
      alloced=1;
 
90
    }
 
91
    else
 
92
      return TRUE;                      // Signal error
 
93
  }
 
94
  Ptr[alloc_length]=0;                  // This make other funcs shorter
 
95
  return FALSE;
 
96
}
 
97
 
 
98
bool String::set(longlong num, CHARSET_INFO *cs)
 
99
{
 
100
  uint l=20*cs->mbmaxlen+1;
 
101
 
 
102
  if (alloc(l))
 
103
    return TRUE;
 
104
  str_length=(uint32) (cs->cset->longlong10_to_str)(cs,Ptr,l,-10,num);
 
105
  str_charset=cs;
 
106
  return FALSE;
 
107
}
 
108
 
 
109
bool String::set(ulonglong num, CHARSET_INFO *cs)
 
110
{
 
111
  uint l=20*cs->mbmaxlen+1;
 
112
 
 
113
  if (alloc(l))
 
114
    return TRUE;
 
115
  str_length=(uint32) (cs->cset->longlong10_to_str)(cs,Ptr,l,10,num);
 
116
  str_charset=cs;
 
117
  return FALSE;
 
118
}
 
119
 
 
120
bool String::set(double num,uint decimals, CHARSET_INFO *cs)
 
121
{
 
122
  char buff[331];
 
123
  uint dummy_errors;
 
124
 
 
125
  str_charset=cs;
 
126
  if (decimals >= NOT_FIXED_DEC)
 
127
  {
 
128
    uint32 len= my_sprintf(buff,(buff, "%.14g",num));// Enough for a DATETIME
 
129
    return copy(buff, len, &my_charset_latin1, cs, &dummy_errors);
 
130
  }
 
131
#ifdef HAVE_FCONVERT
 
132
  int decpt,sign;
 
133
  char *pos,*to;
 
134
 
 
135
  VOID(fconvert(num,(int) decimals,&decpt,&sign,buff+1));
 
136
  if (!my_isdigit(&my_charset_latin1, buff[1]))
 
137
  {                                             // Nan or Inf
 
138
    pos=buff+1;
 
139
    if (sign)
 
140
    {
 
141
      buff[0]='-';
 
142
      pos=buff;
 
143
    }
 
144
    uint dummy_errors;
 
145
    return copy(pos,(uint32) strlen(pos), &my_charset_latin1, cs, &dummy_errors);
 
146
  }
 
147
  if (alloc((uint32) ((uint32) decpt+3+decimals)))
 
148
    return TRUE;
 
149
  to=Ptr;
 
150
  if (sign)
 
151
    *to++='-';
 
152
 
 
153
  pos=buff+1;
 
154
  if (decpt < 0)
 
155
  {                                     /* value is < 0 */
 
156
    *to++='0';
 
157
    if (!decimals)
 
158
      goto end;
 
159
    *to++='.';
 
160
    if ((uint32) -decpt > decimals)
 
161
      decpt= - (int) decimals;
 
162
    decimals=(uint32) ((int) decimals+decpt);
 
163
    while (decpt++ < 0)
 
164
      *to++='0';
 
165
  }
 
166
  else if (decpt == 0)
 
167
  {
 
168
    *to++= '0';
 
169
    if (!decimals)
 
170
      goto end;
 
171
    *to++='.';
 
172
  }
 
173
  else
 
174
  {
 
175
    while (decpt-- > 0)
 
176
      *to++= *pos++;
 
177
    if (!decimals)
 
178
      goto end;
 
179
    *to++='.';
 
180
  }
 
181
  while (decimals--)
 
182
    *to++= *pos++;
 
183
 
 
184
end:
 
185
  *to=0;
 
186
  str_length=(uint32) (to-Ptr);
 
187
  return FALSE;
 
188
#else
 
189
#ifdef HAVE_SNPRINTF
 
190
  buff[sizeof(buff)-1]=0;                       // Safety
 
191
  snprintf(buff,sizeof(buff)-1, "%.*f",(int) decimals,num);
 
192
#else
 
193
  sprintf(buff,"%.*f",(int) decimals,num);
 
194
#endif
 
195
  return copy(buff,(uint32) strlen(buff), &my_charset_latin1, cs,
 
196
              &dummy_errors);
 
197
#endif
 
198
}
 
199
 
 
200
 
 
201
bool String::copy()
 
202
{
 
203
  if (!alloced)
 
204
  {
 
205
    Alloced_length=0;                           // Force realloc
 
206
    return realloc(str_length);
 
207
  }
 
208
  return FALSE;
 
209
}
 
210
 
 
211
bool String::copy(const String &str)
 
212
{
 
213
  if (alloc(str.str_length))
 
214
    return TRUE;
 
215
  str_length=str.str_length;
 
216
  bmove(Ptr,str.Ptr,str_length);                // May be overlapping
 
217
  Ptr[str_length]=0;
 
218
  str_charset=str.str_charset;
 
219
  return FALSE;
 
220
}
 
221
 
 
222
bool String::copy(const char *str,uint32 arg_length, CHARSET_INFO *cs)
 
223
{
 
224
  if (alloc(arg_length))
 
225
    return TRUE;
 
226
  if ((str_length=arg_length))
 
227
    memcpy(Ptr,str,arg_length);
 
228
  Ptr[arg_length]=0;
 
229
  str_charset=cs;
 
230
  return FALSE;
 
231
}
 
232
 
 
233
 
 
234
/*
 
235
  Checks that the source string can be just copied to the destination string
 
236
  without conversion.
 
237
 
 
238
  SYNPOSIS
 
239
 
 
240
  needs_conversion()
 
241
  arg_length            Length of string to copy.
 
242
  from_cs               Character set to copy from
 
243
  to_cs                 Character set to copy to
 
244
  uint32 *offset        Returns number of unaligned characters.
 
245
 
 
246
  RETURN
 
247
   0  No conversion needed
 
248
   1  Either character set conversion or adding leading  zeros
 
249
      (e.g. for UCS-2) must be done
 
250
 
 
251
  NOTE
 
252
  to_cs may be NULL for "no conversion" if the system variable
 
253
  character_set_results is NULL.
 
254
*/
 
255
 
 
256
bool String::needs_conversion(uint32 arg_length,
 
257
                              CHARSET_INFO *from_cs,
 
258
                              CHARSET_INFO *to_cs,
 
259
                              uint32 *offset)
 
260
{
 
261
  *offset= 0;
 
262
  if (!to_cs ||
 
263
      (to_cs == &my_charset_bin) || 
 
264
      (to_cs == from_cs) ||
 
265
      my_charset_same(from_cs, to_cs) ||
 
266
      ((from_cs == &my_charset_bin) &&
 
267
       (!(*offset=(arg_length % to_cs->mbminlen)))))
 
268
    return FALSE;
 
269
  return TRUE;
 
270
}
 
271
 
 
272
 
 
273
/*
 
274
  Copy a multi-byte character sets with adding leading zeros.
 
275
 
 
276
  SYNOPSIS
 
277
 
 
278
  copy_aligned()
 
279
  str                   String to copy
 
280
  arg_length            Length of string. This should NOT be dividable with
 
281
                        cs->mbminlen.
 
282
  offset                arg_length % cs->mb_minlength
 
283
  cs                    Character set for 'str'
 
284
 
 
285
  NOTES
 
286
    For real multi-byte, ascii incompatible charactser sets,
 
287
    like UCS-2, add leading zeros if we have an incomplete character.
 
288
    Thus, 
 
289
      SELECT _ucs2 0xAA 
 
290
    will automatically be converted into
 
291
      SELECT _ucs2 0x00AA
 
292
 
 
293
  RETURN
 
294
    0  ok
 
295
    1  error
 
296
*/
 
297
 
 
298
bool String::copy_aligned(const char *str,uint32 arg_length, uint32 offset,
 
299
                          CHARSET_INFO *cs)
 
300
{
 
301
  /* How many bytes are in incomplete character */
 
302
  offset= cs->mbmaxlen - offset; /* How many zeros we should prepend */
 
303
  DBUG_ASSERT(offset && offset != cs->mbmaxlen);
 
304
 
 
305
  uint32 aligned_length= arg_length + offset;
 
306
  if (alloc(aligned_length))
 
307
    return TRUE;
 
308
  
 
309
  /*
 
310
    Note, this is only safe for little-endian UCS-2.
 
311
    If we add big-endian UCS-2 sometimes, this code
 
312
    will be more complicated. But it's OK for now.
 
313
  */
 
314
  bzero((char*) Ptr, offset);
 
315
  memcpy(Ptr + offset, str, arg_length);
 
316
  Ptr[aligned_length]=0;
 
317
  /* str_length is always >= 0 as arg_length is != 0 */
 
318
  str_length= aligned_length;
 
319
  str_charset= cs;
 
320
  return FALSE;
 
321
}
 
322
 
 
323
 
 
324
bool String::set_or_copy_aligned(const char *str,uint32 arg_length,
 
325
                                 CHARSET_INFO *cs)
 
326
{
 
327
  /* How many bytes are in incomplete character */
 
328
  uint32 offset= (arg_length % cs->mbminlen); 
 
329
  
 
330
  if (!offset) /* All characters are complete, just copy */
 
331
  {
 
332
    set(str, arg_length, cs);
 
333
    return FALSE;
 
334
  }
 
335
  return copy_aligned(str, arg_length, offset, cs);
 
336
}
 
337
 
 
338
        /* Copy with charset convertion */
 
339
 
 
340
bool String::copy(const char *str, uint32 arg_length,
 
341
                  CHARSET_INFO *from_cs, CHARSET_INFO *to_cs, uint *errors)
 
342
{
 
343
  uint32 offset;
 
344
  if (!needs_conversion(arg_length, from_cs, to_cs, &offset))
 
345
  {
 
346
    *errors= 0;
 
347
    return copy(str, arg_length, to_cs);
 
348
  }
 
349
  if ((from_cs == &my_charset_bin) && offset)
 
350
  {
 
351
    *errors= 0;
 
352
    return copy_aligned(str, arg_length, offset, to_cs);
 
353
  }
 
354
  uint32 new_length= to_cs->mbmaxlen*arg_length;
 
355
  if (alloc(new_length))
 
356
    return TRUE;
 
357
  str_length=copy_and_convert((char*) Ptr, new_length, to_cs,
 
358
                              str, arg_length, from_cs, errors);
 
359
  str_charset=to_cs;
 
360
  return FALSE;
 
361
}
 
362
 
 
363
 
 
364
/*
 
365
  Set a string to the value of a latin1-string, keeping the original charset
 
366
  
 
367
  SYNOPSIS
 
368
    copy_or_set()
 
369
    str                 String of a simple charset (latin1)
 
370
    arg_length          Length of string
 
371
 
 
372
  IMPLEMENTATION
 
373
    If string object is of a simple character set, set it to point to the
 
374
    given string.
 
375
    If not, make a copy and convert it to the new character set.
 
376
 
 
377
  RETURN
 
378
    0   ok
 
379
    1   Could not allocate result buffer
 
380
 
 
381
*/
 
382
 
 
383
bool String::set_ascii(const char *str, uint32 arg_length)
 
384
{
 
385
  if (str_charset->mbminlen == 1)
 
386
  {
 
387
    set(str, arg_length, str_charset);
 
388
    return 0;
 
389
  }
 
390
  uint dummy_errors;
 
391
  return copy(str, arg_length, &my_charset_latin1, str_charset, &dummy_errors);
 
392
}
 
393
 
 
394
 
 
395
/* This is used by mysql.cc */
 
396
 
 
397
bool String::fill(uint32 max_length,char fill_char)
 
398
{
 
399
  if (str_length > max_length)
 
400
    Ptr[str_length=max_length]=0;
 
401
  else
 
402
  {
 
403
    if (realloc(max_length))
 
404
      return TRUE;
 
405
    bfill(Ptr+str_length,max_length-str_length,fill_char);
 
406
    str_length=max_length;
 
407
  }
 
408
  return FALSE;
 
409
}
 
410
 
 
411
void String::strip_sp()
 
412
{
 
413
   while (str_length && my_isspace(str_charset,Ptr[str_length-1]))
 
414
    str_length--;
 
415
}
 
416
 
 
417
bool String::append(const String &s)
 
418
{
 
419
  if (s.length())
 
420
  {
 
421
    if (realloc(str_length+s.length()))
 
422
      return TRUE;
 
423
    memcpy(Ptr+str_length,s.ptr(),s.length());
 
424
    str_length+=s.length();
 
425
  }
 
426
  return FALSE;
 
427
}
 
428
 
 
429
 
 
430
/*
 
431
  Append an ASCII string to the a string of the current character set
 
432
*/
 
433
 
 
434
bool String::append(const char *s,uint32 arg_length)
 
435
{
 
436
  if (!arg_length)
 
437
    return FALSE;
 
438
 
 
439
  /*
 
440
    For an ASCII incompatible string, e.g. UCS-2, we need to convert
 
441
  */
 
442
  if (str_charset->mbminlen > 1)
 
443
  {
 
444
    uint32 add_length=arg_length * str_charset->mbmaxlen;
 
445
    uint dummy_errors;
 
446
    if (realloc(str_length+ add_length))
 
447
      return TRUE;
 
448
    str_length+= copy_and_convert(Ptr+str_length, add_length, str_charset,
 
449
                                  s, arg_length, &my_charset_latin1,
 
450
                                  &dummy_errors);
 
451
    return FALSE;
 
452
  }
 
453
 
 
454
  /*
 
455
    For an ASCII compatinble string we can just append.
 
456
  */
 
457
  if (realloc(str_length+arg_length))
 
458
    return TRUE;
 
459
  memcpy(Ptr+str_length,s,arg_length);
 
460
  str_length+=arg_length;
 
461
  return FALSE;
 
462
}
 
463
 
 
464
 
 
465
/*
 
466
  Append a 0-terminated ASCII string
 
467
*/
 
468
 
 
469
bool String::append(const char *s)
 
470
{
 
471
  return append(s, strlen(s));
 
472
}
 
473
 
 
474
 
 
475
/*
 
476
  Append a string in the given charset to the string
 
477
  with character set recoding
 
478
*/
 
479
 
 
480
bool String::append(const char *s,uint32 arg_length, CHARSET_INFO *cs)
 
481
{
 
482
  uint32 dummy_offset;
 
483
  
 
484
  if (needs_conversion(arg_length, cs, str_charset, &dummy_offset))
 
485
  {
 
486
    uint32 add_length= arg_length / cs->mbminlen * str_charset->mbmaxlen;
 
487
    uint dummy_errors;
 
488
    if (realloc(str_length + add_length)) 
 
489
      return TRUE;
 
490
    str_length+= copy_and_convert(Ptr+str_length, add_length, str_charset,
 
491
                                  s, arg_length, cs, &dummy_errors);
 
492
  }
 
493
  else
 
494
  {
 
495
    if (realloc(str_length + arg_length)) 
 
496
      return TRUE;
 
497
    memcpy(Ptr + str_length, s, arg_length);
 
498
    str_length+= arg_length;
 
499
  }
 
500
  return FALSE;
 
501
}
 
502
 
 
503
 
 
504
bool String::append(IO_CACHE* file, uint32 arg_length)
 
505
{
 
506
  if (realloc(str_length+arg_length))
 
507
    return TRUE;
 
508
  if (my_b_read(file, (uchar*) Ptr + str_length, arg_length))
 
509
  {
 
510
    shrink(str_length);
 
511
    return TRUE;
 
512
  }
 
513
  str_length+=arg_length;
 
514
  return FALSE;
 
515
}
 
516
 
 
517
bool String::append_with_prefill(const char *s,uint32 arg_length,
 
518
                 uint32 full_length, char fill_char)
 
519
{
 
520
  int t_length= arg_length > full_length ? arg_length : full_length;
 
521
 
 
522
  if (realloc(str_length + t_length))
 
523
    return TRUE;
 
524
  t_length= full_length - arg_length;
 
525
  if (t_length > 0)
 
526
  {
 
527
    bfill(Ptr+str_length, t_length, fill_char);
 
528
    str_length=str_length + t_length;
 
529
  }
 
530
  append(s, arg_length);
 
531
  return FALSE;
 
532
}
 
533
 
 
534
uint32 String::numchars()
 
535
{
 
536
  return str_charset->cset->numchars(str_charset, Ptr, Ptr+str_length);
 
537
}
 
538
 
 
539
int String::charpos(int i,uint32 offset)
 
540
{
 
541
  if (i <= 0)
 
542
    return i;
 
543
  return str_charset->cset->charpos(str_charset,Ptr+offset,Ptr+str_length,i);
 
544
}
 
545
 
 
546
int String::strstr(const String &s,uint32 offset)
 
547
{
 
548
  if (s.length()+offset <= str_length)
 
549
  {
 
550
    if (!s.length())
 
551
      return ((int) offset);    // Empty string is always found
 
552
 
 
553
    register const char *str = Ptr+offset;
 
554
    register const char *search=s.ptr();
 
555
    const char *end=Ptr+str_length-s.length()+1;
 
556
    const char *search_end=s.ptr()+s.length();
 
557
skip:
 
558
    while (str != end)
 
559
    {
 
560
      if (*str++ == *search)
 
561
      {
 
562
        register char *i,*j;
 
563
        i=(char*) str; j=(char*) search+1;
 
564
        while (j != search_end)
 
565
          if (*i++ != *j++) goto skip;
 
566
        return (int) (str-Ptr) -1;
 
567
      }
 
568
    }
 
569
  }
 
570
  return -1;
 
571
}
 
572
 
 
573
/*
 
574
** Search string from end. Offset is offset to the end of string
 
575
*/
 
576
 
 
577
int String::strrstr(const String &s,uint32 offset)
 
578
{
 
579
  if (s.length() <= offset && offset <= str_length)
 
580
  {
 
581
    if (!s.length())
 
582
      return offset;                            // Empty string is always found
 
583
    register const char *str = Ptr+offset-1;
 
584
    register const char *search=s.ptr()+s.length()-1;
 
585
 
 
586
    const char *end=Ptr+s.length()-2;
 
587
    const char *search_end=s.ptr()-1;
 
588
skip:
 
589
    while (str != end)
 
590
    {
 
591
      if (*str-- == *search)
 
592
      {
 
593
        register char *i,*j;
 
594
        i=(char*) str; j=(char*) search-1;
 
595
        while (j != search_end)
 
596
          if (*i-- != *j--) goto skip;
 
597
        return (int) (i-Ptr) +1;
 
598
      }
 
599
    }
 
600
  }
 
601
  return -1;
 
602
}
 
603
 
 
604
/*
 
605
  Replace substring with string
 
606
  If wrong parameter or not enough memory, do nothing
 
607
*/
 
608
 
 
609
bool String::replace(uint32 offset,uint32 arg_length,const String &to)
 
610
{
 
611
  return replace(offset,arg_length,to.ptr(),to.length());
 
612
}
 
613
 
 
614
bool String::replace(uint32 offset,uint32 arg_length,
 
615
                     const char *to, uint32 to_length)
 
616
{
 
617
  long diff = (long) to_length-(long) arg_length;
 
618
  if (offset+arg_length <= str_length)
 
619
  {
 
620
    if (diff < 0)
 
621
    {
 
622
      if (to_length)
 
623
        memcpy(Ptr+offset,to,to_length);
 
624
      bmove(Ptr+offset+to_length,Ptr+offset+arg_length,
 
625
            str_length-offset-arg_length);
 
626
    }
 
627
    else
 
628
    {
 
629
      if (diff)
 
630
      {
 
631
        if (realloc(str_length+(uint32) diff))
 
632
          return TRUE;
 
633
        bmove_upp((uchar*) Ptr+str_length+diff, (uchar*) Ptr+str_length,
 
634
                  str_length-offset-arg_length);
 
635
      }
 
636
      if (to_length)
 
637
        memcpy(Ptr+offset,to,to_length);
 
638
    }
 
639
    str_length+=(uint32) diff;
 
640
  }
 
641
  return FALSE;
 
642
}
 
643
 
 
644
 
 
645
// added by Holyfoot for "geometry" needs
 
646
int String::reserve(uint32 space_needed, uint32 grow_by)
 
647
{
 
648
  if (Alloced_length < str_length + space_needed)
 
649
  {
 
650
    if (realloc(Alloced_length + max(space_needed, grow_by) - 1))
 
651
      return TRUE;
 
652
  }
 
653
  return FALSE;
 
654
}
 
655
 
 
656
void String::qs_append(const char *str, uint32 len)
 
657
{
 
658
  memcpy(Ptr + str_length, str, len + 1);
 
659
  str_length += len;
 
660
}
 
661
 
 
662
void String::qs_append(double d)
 
663
{
 
664
  char *buff = Ptr + str_length;
 
665
  str_length+= my_gcvt(d, MY_GCVT_ARG_DOUBLE, FLOATING_POINT_BUFFER - 1, buff, NULL);
 
666
}
 
667
 
 
668
void String::qs_append(double *d)
 
669
{
 
670
  double ld;
 
671
  float8get(ld, (char*) d);
 
672
  qs_append(ld);
 
673
}
 
674
 
 
675
void String::qs_append(int i)
 
676
{
 
677
  char *buff= Ptr + str_length;
 
678
  char *end= int10_to_str(i, buff, -10);
 
679
  str_length+= (int) (end-buff);
 
680
}
 
681
 
 
682
void String::qs_append(uint i)
 
683
{
 
684
  char *buff= Ptr + str_length;
 
685
  char *end= int10_to_str(i, buff, 10);
 
686
  str_length+= (int) (end-buff);
 
687
}
 
688
 
 
689
/*
 
690
  Compare strings according to collation, without end space.
 
691
 
 
692
  SYNOPSIS
 
693
    sortcmp()
 
694
    s           First string
 
695
    t           Second string
 
696
    cs          Collation
 
697
 
 
698
  NOTE:
 
699
    Normally this is case sensitive comparison
 
700
 
 
701
  RETURN
 
702
  < 0   s < t
 
703
  0     s == t
 
704
  > 0   s > t
 
705
*/
 
706
 
 
707
 
 
708
int sortcmp(const String *s,const String *t, CHARSET_INFO *cs)
 
709
{
 
710
 return cs->coll->strnncollsp(cs,
 
711
                              (unsigned char *) s->ptr(),s->length(),
 
712
                              (unsigned char *) t->ptr(),t->length(), 0);
 
713
}
 
714
 
 
715
 
 
716
/*
 
717
  Compare strings byte by byte. End spaces are also compared.
 
718
 
 
719
  SYNOPSIS
 
720
    stringcmp()
 
721
    s           First string
 
722
    t           Second string
 
723
 
 
724
  NOTE:
 
725
    Strings are compared as a stream of unsigned chars
 
726
 
 
727
  RETURN
 
728
  < 0   s < t
 
729
  0     s == t
 
730
  > 0   s > t
 
731
*/
 
732
 
 
733
 
 
734
int stringcmp(const String *s,const String *t)
 
735
{
 
736
  uint32 s_len=s->length(),t_len=t->length(),len=min(s_len,t_len);
 
737
  int cmp= memcmp(s->ptr(), t->ptr(), len);
 
738
  return (cmp) ? cmp : (int) (s_len - t_len);
 
739
}
 
740
 
 
741
 
 
742
String *copy_if_not_alloced(String *to,String *from,uint32 from_length)
 
743
{
 
744
  if (from->Alloced_length >= from_length)
 
745
    return from;
 
746
  if (from->alloced || !to || from == to)
 
747
  {
 
748
    (void) from->realloc(from_length);
 
749
    return from;
 
750
  }
 
751
  if (to->realloc(from_length))
 
752
    return from;                                // Actually an error
 
753
  if ((to->str_length=min(from->str_length,from_length)))
 
754
    memcpy(to->Ptr,from->Ptr,to->str_length);
 
755
  to->str_charset=from->str_charset;
 
756
  return to;
 
757
}
 
758
 
 
759
 
 
760
/****************************************************************************
 
761
  Help functions
 
762
****************************************************************************/
 
763
 
 
764
/*
 
765
  copy a string from one character set to another
 
766
  
 
767
  SYNOPSIS
 
768
    copy_and_convert()
 
769
    to                  Store result here
 
770
    to_cs               Character set of result string
 
771
    from                Copy from here
 
772
    from_length         Length of from string
 
773
    from_cs             From character set
 
774
 
 
775
  NOTES
 
776
    'to' must be big enough as form_length * to_cs->mbmaxlen
 
777
 
 
778
  RETURN
 
779
    length of bytes copied to 'to'
 
780
*/
 
781
 
 
782
 
 
783
uint32
 
784
copy_and_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs, 
 
785
                 const char *from, uint32 from_length, CHARSET_INFO *from_cs,
 
786
                 uint *errors)
 
787
{
 
788
  int         cnvres;
 
789
  my_wc_t     wc;
 
790
  const uchar *from_end= (const uchar*) from+from_length;
 
791
  char *to_start= to;
 
792
  uchar *to_end= (uchar*) to+to_length;
 
793
  my_charset_conv_mb_wc mb_wc= from_cs->cset->mb_wc;
 
794
  my_charset_conv_wc_mb wc_mb= to_cs->cset->wc_mb;
 
795
  uint error_count= 0;
 
796
 
 
797
  while (1)
 
798
  {
 
799
    if ((cnvres= (*mb_wc)(from_cs, &wc, (uchar*) from,
 
800
                                      from_end)) > 0)
 
801
      from+= cnvres;
 
802
    else if (cnvres == MY_CS_ILSEQ)
 
803
    {
 
804
      error_count++;
 
805
      from++;
 
806
      wc= '?';
 
807
    }
 
808
    else if (cnvres > MY_CS_TOOSMALL)
 
809
    {
 
810
      /*
 
811
        A correct multibyte sequence detected
 
812
        But it doesn't have Unicode mapping.
 
813
      */
 
814
      error_count++;
 
815
      from+= (-cnvres);
 
816
      wc= '?';
 
817
    }
 
818
    else
 
819
      break;  // Not enough characters
 
820
 
 
821
outp:
 
822
    if ((cnvres= (*wc_mb)(to_cs, wc, (uchar*) to, to_end)) > 0)
 
823
      to+= cnvres;
 
824
    else if (cnvres == MY_CS_ILUNI && wc != '?')
 
825
    {
 
826
      error_count++;
 
827
      wc= '?';
 
828
      goto outp;
 
829
    }
 
830
    else
 
831
      break;
 
832
  }
 
833
  *errors= error_count;
 
834
  return (uint32) (to - to_start);
 
835
}
 
836
 
 
837
 
 
838
void String::print(String *str)
 
839
{
 
840
  char *st= (char*)Ptr, *end= st+str_length;
 
841
  for (; st < end; st++)
 
842
  {
 
843
    uchar c= *st;
 
844
    switch (c)
 
845
    {
 
846
    case '\\':
 
847
      str->append(STRING_WITH_LEN("\\\\"));
 
848
      break;
 
849
    case '\0':
 
850
      str->append(STRING_WITH_LEN("\\0"));
 
851
      break;
 
852
    case '\'':
 
853
      str->append(STRING_WITH_LEN("\\'"));
 
854
      break;
 
855
    case '\n':
 
856
      str->append(STRING_WITH_LEN("\\n"));
 
857
      break;
 
858
    case '\r':
 
859
      str->append(STRING_WITH_LEN("\\r"));
 
860
      break;
 
861
    case 26: //Ctrl-Z
 
862
      str->append(STRING_WITH_LEN("\\z"));
 
863
      break;
 
864
    default:
 
865
      str->append(c);
 
866
    }
 
867
  }
 
868
}
 
869
 
 
870
 
 
871
/*
 
872
  Exchange state of this object and argument.
 
873
 
 
874
  SYNOPSIS
 
875
    String::swap()
 
876
 
 
877
  RETURN
 
878
    Target string will contain state of this object and vice versa.
 
879
*/
 
880
 
 
881
void String::swap(String &s)
 
882
{
 
883
  swap_variables(char *, Ptr, s.Ptr);
 
884
  swap_variables(uint32, str_length, s.str_length);
 
885
  swap_variables(uint32, Alloced_length, s.Alloced_length);
 
886
  swap_variables(bool, alloced, s.alloced);
 
887
  swap_variables(CHARSET_INFO*, str_charset, s.str_charset);
 
888
}