~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.cc

mergeĀ mainline

Show diffs side-by-side

added added

removed removed

Lines of Context:
191
191
}
192
192
 
193
193
 
194
 
/*
195
 
  Copy a multi-byte character sets with adding leading zeros.
196
 
 
197
 
  SYNOPSIS
198
 
 
199
 
  copy_aligned()
200
 
  str                   String to copy
201
 
  arg_length            Length of string. This should NOT be dividable with
202
 
                        cs->mbminlen.
203
 
  offset                arg_length % cs->mb_minlength
204
 
  cs                    Character set for 'str'
205
 
 
206
 
  NOTES
207
 
    For real multi-byte, ascii incompatible charactser sets,
208
 
    like UCS-2, add leading zeros if we have an incomplete character.
209
 
    Thus,
210
 
      SELECT _ucs2 0xAA
211
 
    will automatically be converted into
212
 
      SELECT _ucs2 0x00AA
213
 
 
214
 
  RETURN
215
 
    0  ok
216
 
    1  error
217
 
*/
218
 
 
219
 
bool String::copy_aligned(const char *str,uint32_t arg_length, uint32_t offset,
220
 
                          const CHARSET_INFO * const cs)
221
 
{
222
 
  /* How many bytes are in incomplete character */
223
 
  offset= cs->mbmaxlen - offset; /* How many zeros we should prepend */
224
 
  assert(offset && offset != cs->mbmaxlen);
225
 
 
226
 
  uint32_t aligned_length= arg_length + offset;
227
 
  if (alloc(aligned_length))
228
 
    return true;
229
 
 
230
 
  /*
231
 
    Note, this is only safe for big-endian UCS-2.
232
 
    If we add little-endian UCS-2 sometimes, this code
233
 
    will be more complicated. But it's OK for now.
234
 
  */
235
 
  memset(Ptr, 0, offset);
236
 
  memcpy(Ptr + offset, str, arg_length);
237
 
  Ptr[aligned_length]=0;
238
 
  /* str_length is always >= 0 as arg_length is != 0 */
239
 
  str_length= aligned_length;
240
 
  str_charset= cs;
241
 
  return false;
242
 
}
243
194
 
244
195
 
245
196
bool String::set_or_copy_aligned(const char *str,uint32_t arg_length,
248
199
  /* How many bytes are in incomplete character */
249
200
  uint32_t offset= (arg_length % cs->mbminlen);
250
201
 
251
 
  if (!offset) /* All characters are complete, just copy */
252
 
  {
253
 
    set(str, arg_length, cs);
254
 
    return false;
255
 
  }
256
 
  return copy_aligned(str, arg_length, offset, cs);
 
202
  assert(!offset); /* All characters are complete, just copy */
 
203
 
 
204
  set(str, arg_length, cs);
 
205
  return false;
257
206
}
258
207
 
259
208
        /* Copy with charset conversion */
271
220
  if ((from_cs == &my_charset_bin) && offset)
272
221
  {
273
222
    *errors= 0;
274
 
    return copy_aligned(str, arg_length, offset, to_cs);
 
223
    assert((from_cs == &my_charset_bin) && offset);
 
224
    return false; //copy_aligned(str, arg_length, offset, to_cs);
275
225
  }
276
226
  uint32_t new_length= to_cs->mbmaxlen*arg_length;
277
227
  if (alloc(new_length))
313
263
  return copy(str, arg_length, &my_charset_utf8_general_ci, str_charset, &dummy_errors);
314
264
}
315
265
 
316
 
 
317
 
/* This is used by mysql.cc */
318
 
 
319
 
bool String::fill(uint32_t max_length,char fill_char)
320
 
{
321
 
  if (str_length > max_length)
322
 
    Ptr[str_length=max_length]=0;
323
 
  else
324
 
  {
325
 
    if (realloc(max_length))
326
 
      return true;
327
 
    memset(Ptr+str_length, fill_char, max_length-str_length);
328
 
    str_length=max_length;
329
 
  }
330
 
  return false;
331
 
}
332
 
 
333
266
bool String::append(const String &s)
334
267
{
335
268
  if (s.length())
417
350
}
418
351
 
419
352
 
420
 
bool String::append(IO_CACHE* file, uint32_t arg_length)
421
 
{
422
 
  if (realloc(str_length+arg_length))
423
 
    return true;
424
 
  if (my_b_read(file, (unsigned char*) Ptr + str_length, arg_length))
425
 
  {
426
 
    shrink(str_length);
427
 
    return true;
428
 
  }
429
 
  str_length+=arg_length;
430
 
  return false;
431
 
}
432
 
 
433
353
bool String::append_with_prefill(const char *s,uint32_t arg_length,
434
354
                 uint32_t full_length, char fill_char)
435
355
{
558
478
}
559
479
 
560
480
 
561
 
// added by Holyfoot for "geometry" needs
562
 
int String::reserve(uint32_t space_needed, uint32_t grow_by)
563
 
{
564
 
  if (Alloced_length < str_length + space_needed)
565
 
  {
566
 
    if (realloc(Alloced_length + cmax(space_needed, grow_by) - 1))
567
 
      return true;
568
 
  }
569
 
  return false;
570
 
}
571
 
 
572
 
void String::qs_append(const char *str, uint32_t len)
573
 
{
574
 
  memcpy(Ptr + str_length, str, len + 1);
575
 
  str_length += len;
576
 
}
577
 
 
578
 
void String::qs_append(double d)
579
 
{
580
 
  char *buff = Ptr + str_length;
581
 
  str_length+= my_gcvt(d, MY_GCVT_ARG_DOUBLE, FLOATING_POINT_BUFFER - 1, buff, NULL);
582
 
}
583
 
 
584
 
void String::qs_append(double *d)
585
 
{
586
 
  double ld;
587
 
  float8get(ld, (char*) d);
588
 
  qs_append(ld);
589
 
}
590
 
 
591
 
void String::qs_append(int i)
592
 
{
593
 
  char *buff= Ptr + str_length;
594
 
  char *end= int10_to_str(i, buff, -10);
595
 
  str_length+= (int) (end-buff);
596
 
}
597
 
 
598
 
void String::qs_append(uint32_t i)
599
 
{
600
 
  char *buff= Ptr + str_length;
601
 
  char *end= int10_to_str(i, buff, 10);
602
 
  str_length+= (int) (end-buff);
603
 
}
604
481
 
605
482
/*
606
483
  Compare strings according to collation, without end space.