~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to client/sql_string.cc

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

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