~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_string.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-07-30 02:39:13 UTC
  • mto: (1115.3.11 captain)
  • mto: This revision was merged to the branch mainline in revision 1121.
  • Revision ID: osullivan.padraig@gmail.com-20090730023913-o2zuocp32l6btnc2
Removing references to MY_BITMAP throughout the code base and updating calls
to MyBitmap in various places to use the new interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* This file is originally from the mysql distribution. Coded by monty */
17
17
 
18
 
#include "config.h"
19
 
 
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"
 
18
#include "global.h"
 
19
#include <mysys/my_sys.h>
 
20
#include <mystrings/m_string.h>
24
21
 
25
22
#include <algorithm>
26
23
 
27
 
#include "drizzled/sql_string.h"
28
 
 
29
24
using namespace std;
30
25
 
31
 
namespace drizzled
32
 
{
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());
44
 
   s->copy();
45
 
   return s;
46
 
}
 
26
/*
 
27
  The following extern declarations are ok as these are interface functions
 
28
  required by the string function
 
29
*/
 
30
 
 
31
extern unsigned char* sql_alloc(unsigned size);
 
32
extern void sql_element_free(void *ptr);
 
33
 
 
34
#include "sql_string.h"
47
35
 
48
36
/*****************************************************************************
49
37
** String functions
50
38
*****************************************************************************/
51
39
 
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
 
 
61
 
String::String(uint32_t length_arg)
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)),
73
 
    str_length(static_cast<uint32_t>(strlen(str))),
74
 
    Alloced_length(0),
75
 
    alloced(false),
76
 
    str_charset(cs)
77
 
{ }
78
 
 
79
 
 
80
 
String::String(const char *str, uint32_t len, const CHARSET_INFO * const cs)
81
 
  : Ptr(const_cast<char *>(str)),
82
 
    str_length(len),
83
 
    Alloced_length(0),
84
 
    alloced(false),
85
 
    str_charset(cs)
86
 
{ }
87
 
 
88
 
 
89
 
String::String(char *str, uint32_t len, const CHARSET_INFO * const cs)
90
 
  : Ptr(str),
91
 
    str_length(len),
92
 
    Alloced_length(len),
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
 
 
107
 
void *String::operator new(size_t size, memory::Root *mem_root)
108
 
{
109
 
  return alloc_root(mem_root, static_cast<uint32_t>(size));
110
 
}
111
 
 
112
40
String::~String() { free(); }
113
41
 
114
42
bool String::real_alloc(uint32_t arg_length)
186
114
  str_charset=cs;
187
115
  if (decimals >= NOT_FIXED_DEC)
188
116
  {
189
 
    len= internal::my_gcvt(num,
190
 
                           internal::MY_GCVT_ARG_DOUBLE,
191
 
                           sizeof(buff) - 1, buff, NULL);
 
117
    len= my_gcvt(num, MY_GCVT_ARG_DOUBLE, sizeof(buff) - 1, buff, NULL);
192
118
    return copy(buff, len, &my_charset_utf8_general_ci, cs, &dummy_errors);
193
119
  }
194
 
  len= internal::my_fcvt(num, decimals, buff, NULL);
 
120
  len= my_fcvt(num, decimals, buff, NULL);
195
121
  return copy(buff, (uint32_t) len, &my_charset_utf8_general_ci, cs,
196
122
              &dummy_errors);
197
123
}
499
425
      {
500
426
        if (realloc(str_length+(uint32_t) diff))
501
427
          return true;
502
 
        internal::bmove_upp((unsigned char*) Ptr+str_length+diff,
503
 
                            (unsigned char*) Ptr+str_length,
504
 
                            str_length-offset-arg_length);
 
428
        bmove_upp((unsigned char*) Ptr+str_length+diff, (unsigned char*) Ptr+str_length,
 
429
                  str_length-offset-arg_length);
505
430
      }
506
431
      if (to_length)
507
432
        memcpy(Ptr+offset,to,to_length);
588
513
  Help functions
589
514
****************************************************************************/
590
515
 
 
516
 
 
517
 
 
518
/**
 
519
  Copy string with HEX-encoding of "bad" characters.
 
520
 
 
521
  @details This functions copies the string pointed by "src"
 
522
  to the string pointed by "dst". Not more than "srclen" bytes
 
523
  are read from "src". Any sequences of bytes representing
 
524
  a not-well-formed substring (according to cs) are hex-encoded,
 
525
  and all well-formed substrings (according to cs) are copied as is.
 
526
  Not more than "dstlen" bytes are written to "dst". The number
 
527
  of bytes written to "dst" is returned.
 
528
 
 
529
   @param      cs       character set pointer of the destination string
 
530
   @param[out] dst      destination string
 
531
   @param      dstlen   size of dst
 
532
   @param      src      source string
 
533
   @param      srclen   length of src
 
534
 
 
535
   @retval     result length
 
536
*/
 
537
 
 
538
size_t
 
539
my_copy_with_hex_escaping(const CHARSET_INFO * const cs,
 
540
                          char *dst, size_t dstlen,
 
541
                          const char *src, size_t srclen)
 
542
{
 
543
  const char *srcend= src + srclen;
 
544
  char *dst0= dst;
 
545
 
 
546
  for ( ; src < srcend ; )
 
547
  {
 
548
    size_t chlen;
 
549
    if ((chlen= my_ismbchar(cs, src, srcend)))
 
550
    {
 
551
      if (dstlen < chlen)
 
552
        break; /* purecov: inspected */
 
553
      memcpy(dst, src, chlen);
 
554
      src+= chlen;
 
555
      dst+= chlen;
 
556
      dstlen-= chlen;
 
557
    }
 
558
    else if (*src & 0x80)
 
559
    {
 
560
      if (dstlen < 4)
 
561
        break; /* purecov: inspected */
 
562
      *dst++= '\\';
 
563
      *dst++= 'x';
 
564
      *dst++= _dig_vec_upper[((unsigned char) *src) >> 4];
 
565
      *dst++= _dig_vec_upper[((unsigned char) *src) & 15];
 
566
      src++;
 
567
      dstlen-= 4;
 
568
    }
 
569
    else
 
570
    {
 
571
      if (dstlen < 1)
 
572
        break; /* purecov: inspected */
 
573
      *dst++= *src++;
 
574
      dstlen--;
 
575
    }
 
576
  }
 
577
  return dst - dst0;
 
578
}
 
579
 
591
580
/*
592
581
  copy a string,
593
582
  with optional character set conversion,
788
777
  std::swap(str_charset, s.str_charset);
789
778
}
790
779
 
791
 
void String::q_append(const uint32_t n)
792
 
{
793
 
  int4store(Ptr + str_length, n);
794
 
  str_length += 4;
795
 
}
796
 
void String::q_append(double d)
797
 
{
798
 
  float8store(Ptr + str_length, d);
799
 
  str_length += 8;
800
 
}
801
 
void String::q_append(double *d)
802
 
{
803
 
  float8store(Ptr + str_length, *d);
804
 
  str_length += 8;
805
 
}
806
 
void String::q_append(const char *data, uint32_t data_len)
807
 
{
808
 
  memcpy(Ptr + str_length, data, data_len);
809
 
  str_length += data_len;
810
 
}
811
 
 
812
 
void String::write_at_position(int position, uint32_t value)
813
 
{
814
 
  int4store(Ptr + position,value);
815
 
}
816
 
bool check_if_only_end_space(const CHARSET_INFO * const cs, char *str,
817
 
                             char *end)
818
 
{
819
 
  return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end;
820
 
}
821
 
 
822
 
} /* namespace drizzled */
823
 
 
824
 
bool operator==(const drizzled::String &s1, const drizzled::String &s2)
 
780
 
 
781
bool operator==(const String &s1, const String &s2)
825
782
{
826
783
  return stringcmp(&s1,&s2) == 0;
827
784
}
828
785
 
829
 
bool operator!=(const drizzled::String &s1, const drizzled::String &s2)
 
786
bool operator!=(const String &s1, const String &s2)
830
787
{
831
788
  return !(s1 == s2);
832
789
}