~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/str/pad.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
#include CSTDINT_H
22
 
#include <drizzled/function/str/pad.h>
23
 
#include <drizzled/error.h>
24
 
#include <drizzled/function/str/alloc_buffer.h>
25
 
#include <drizzled/session.h>
26
 
 
27
 
void Item_func_rpad::fix_length_and_dec()
28
 
{
29
 
  // Handle character set for args[0] and args[2].
30
 
  if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
31
 
    return;
32
 
  if (args[1]->const_item())
33
 
  {
34
 
    uint64_t length= 0;
35
 
 
36
 
    if (collation.collation->mbmaxlen > 0)
37
 
    {
38
 
      uint64_t temp= (uint64_t) args[1]->val_int();
39
 
 
40
 
      /* Assumes that the maximum length of a String is < INT32_MAX. */
41
 
      /* Set here so that rest of code sees out-of-bound value as such. */
42
 
      if (temp > INT32_MAX)
43
 
        temp = INT32_MAX;
44
 
 
45
 
      length= temp * collation.collation->mbmaxlen;
46
 
    }
47
 
 
48
 
    if (length >= MAX_BLOB_WIDTH)
49
 
    {
50
 
      length= MAX_BLOB_WIDTH;
51
 
      maybe_null= 1;
52
 
    }
53
 
    max_length= (ulong) length;
54
 
  }
55
 
  else
56
 
  {
57
 
    max_length= MAX_BLOB_WIDTH;
58
 
    maybe_null= 1;
59
 
  }
60
 
}
61
 
 
62
 
 
63
 
String *Item_func_rpad::val_str(String *str)
64
 
{
65
 
  assert(fixed == 1);
66
 
  uint32_t res_byte_length,res_char_length,pad_char_length,pad_byte_length;
67
 
  char *to;
68
 
  const char *ptr_pad;
69
 
  /* must be int64_t to avoid truncation */
70
 
  int64_t count= args[1]->val_int();
71
 
  int64_t byte_count;
72
 
  String *res= args[0]->val_str(str);
73
 
  String *rpad= args[2]->val_str(&rpad_str);
74
 
 
75
 
  if (!res || args[1]->null_value || !rpad ||
76
 
      ((count < 0) && !args[1]->unsigned_flag))
77
 
    goto err;
78
 
  null_value=0;
79
 
  /* Assumes that the maximum length of a String is < INT32_MAX. */
80
 
  /* Set here so that rest of code sees out-of-bound value as such. */
81
 
  if ((uint64_t) count > INT32_MAX)
82
 
    count= INT32_MAX;
83
 
  if (count <= (res_char_length= res->numchars()))
84
 
  {                                             // String to pad is big enough
85
 
    res->length(res->charpos((int) count));     // Shorten result if longer
86
 
    return (res);
87
 
  }
88
 
  pad_char_length= rpad->numchars();
89
 
 
90
 
  byte_count= count * collation.collation->mbmaxlen;
91
 
  if ((uint64_t) byte_count > current_session->variables.max_allowed_packet)
92
 
  {
93
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
94
 
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
95
 
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
96
 
                        func_name(), current_session->variables.max_allowed_packet);
97
 
    goto err;
98
 
  }
99
 
  if (args[2]->null_value || !pad_char_length)
100
 
    goto err;
101
 
  res_byte_length= res->length();       /* Must be done before alloc_buffer */
102
 
  if (!(res= alloc_buffer(res,str,&tmp_value, (ulong) byte_count)))
103
 
    goto err;
104
 
 
105
 
  to= (char*) res->ptr()+res_byte_length;
106
 
  ptr_pad=rpad->ptr();
107
 
  pad_byte_length= rpad->length();
108
 
  count-= res_char_length;
109
 
  for ( ; (uint32_t) count > pad_char_length; count-= pad_char_length)
110
 
  {
111
 
    memcpy(to,ptr_pad,pad_byte_length);
112
 
    to+= pad_byte_length;
113
 
  }
114
 
  if (count)
115
 
  {
116
 
    pad_byte_length= rpad->charpos((int) count);
117
 
    memcpy(to,ptr_pad,(size_t) pad_byte_length);
118
 
    to+= pad_byte_length;
119
 
  }
120
 
  res->length(to- (char*) res->ptr());
121
 
  return (res);
122
 
 
123
 
 err:
124
 
  null_value=1;
125
 
  return 0;
126
 
}
127
 
 
128
 
 
129
 
void Item_func_lpad::fix_length_and_dec()
130
 
{
131
 
  // Handle character set for args[0] and args[2].
132
 
  if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
133
 
    return;
134
 
 
135
 
  if (args[1]->const_item())
136
 
  {
137
 
    uint64_t length= 0;
138
 
 
139
 
    if (collation.collation->mbmaxlen > 0)
140
 
    {
141
 
      uint64_t temp= (uint64_t) args[1]->val_int();
142
 
 
143
 
      /* Assumes that the maximum length of a String is < INT32_MAX. */
144
 
      /* Set here so that rest of code sees out-of-bound value as such. */
145
 
      if (temp > INT32_MAX)
146
 
        temp= INT32_MAX;
147
 
 
148
 
      length= temp * collation.collation->mbmaxlen;
149
 
    }
150
 
 
151
 
    if (length >= MAX_BLOB_WIDTH)
152
 
    {
153
 
      length= MAX_BLOB_WIDTH;
154
 
      maybe_null= 1;
155
 
    }
156
 
    max_length= (ulong) length;
157
 
  }
158
 
  else
159
 
  {
160
 
    max_length= MAX_BLOB_WIDTH;
161
 
    maybe_null= 1;
162
 
  }
163
 
}
164
 
 
165
 
 
166
 
String *Item_func_lpad::val_str(String *str)
167
 
{
168
 
  assert(fixed == 1);
169
 
  uint32_t res_char_length,pad_char_length;
170
 
  /* must be int64_t to avoid truncation */
171
 
  int64_t count= args[1]->val_int();
172
 
  int64_t byte_count;
173
 
  String *res= args[0]->val_str(&tmp_value);
174
 
  String *pad= args[2]->val_str(&lpad_str);
175
 
 
176
 
  if (!res || args[1]->null_value || !pad ||
177
 
      ((count < 0) && !args[1]->unsigned_flag))
178
 
    goto err;
179
 
  null_value=0;
180
 
  /* Assumes that the maximum length of a String is < INT32_MAX. */
181
 
  /* Set here so that rest of code sees out-of-bound value as such. */
182
 
  if ((uint64_t) count > INT32_MAX)
183
 
    count= INT32_MAX;
184
 
 
185
 
  res_char_length= res->numchars();
186
 
 
187
 
  if (count <= res_char_length)
188
 
  {
189
 
    res->length(res->charpos((int) count));
190
 
    return res;
191
 
  }
192
 
 
193
 
  pad_char_length= pad->numchars();
194
 
  byte_count= count * collation.collation->mbmaxlen;
195
 
 
196
 
  if ((uint64_t) byte_count > current_session->variables.max_allowed_packet)
197
 
  {
198
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
199
 
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
200
 
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
201
 
                        func_name(), current_session->variables.max_allowed_packet);
202
 
    goto err;
203
 
  }
204
 
 
205
 
  if (args[2]->null_value || !pad_char_length ||
206
 
      str->alloc((uint32_t) byte_count))
207
 
    goto err;
208
 
 
209
 
  str->length(0);
210
 
  str->set_charset(collation.collation);
211
 
  count-= res_char_length;
212
 
  while (count >= pad_char_length)
213
 
  {
214
 
    str->append(*pad);
215
 
    count-= pad_char_length;
216
 
  }
217
 
  if (count > 0)
218
 
    str->append(pad->ptr(), pad->charpos((int) count), collation.collation);
219
 
 
220
 
  str->append(*res);
221
 
  null_value= 0;
222
 
  return str;
223
 
 
224
 
err:
225
 
  null_value= 1;
226
 
  return 0;
227
 
}
228