1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
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>
30
void Item_func_rpad::fix_length_and_dec()
32
// Handle character set for args[0] and args[2].
33
if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
35
if (args[1]->const_item())
39
if (collation.collation->mbmaxlen > 0)
41
uint64_t temp= (uint64_t) args[1]->val_int();
43
/* Assumes that the maximum length of a String is < INT32_MAX. */
44
/* Set here so that rest of code sees out-of-bound value as such. */
48
length= temp * collation.collation->mbmaxlen;
51
if (length >= MAX_BLOB_WIDTH)
53
length= MAX_BLOB_WIDTH;
56
max_length= (ulong) length;
60
max_length= MAX_BLOB_WIDTH;
66
String *Item_func_rpad::val_str(String *str)
69
uint32_t res_byte_length,res_char_length,pad_char_length,pad_byte_length;
72
/* must be int64_t to avoid truncation */
73
int64_t count= args[1]->val_int();
75
String *res= args[0]->val_str(str);
76
String *rpad= args[2]->val_str(&rpad_str);
78
if (!res || args[1]->null_value || !rpad ||
79
((count < 0) && !args[1]->unsigned_flag))
82
/* Assumes that the maximum length of a String is < INT32_MAX. */
83
/* Set here so that rest of code sees out-of-bound value as such. */
84
if ((uint64_t) count > INT32_MAX)
86
if (count <= (res_char_length= res->numchars()))
87
{ // String to pad is big enough
88
res->length(res->charpos((int) count)); // Shorten result if longer
91
pad_char_length= rpad->numchars();
93
byte_count= count * collation.collation->mbmaxlen;
94
if ((uint64_t) byte_count > current_session->variables.max_allowed_packet)
96
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
97
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
98
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
99
func_name(), current_session->variables.max_allowed_packet);
102
if (args[2]->null_value || !pad_char_length)
104
res_byte_length= res->length(); /* Must be done before alloc_buffer */
105
if (!(res= alloc_buffer(res,str,&tmp_value, (ulong) byte_count)))
108
to= (char*) res->ptr()+res_byte_length;
110
pad_byte_length= rpad->length();
111
count-= res_char_length;
112
for ( ; (uint32_t) count > pad_char_length; count-= pad_char_length)
114
memcpy(to,ptr_pad,pad_byte_length);
115
to+= pad_byte_length;
119
pad_byte_length= rpad->charpos((int) count);
120
memcpy(to,ptr_pad,(size_t) pad_byte_length);
121
to+= pad_byte_length;
123
res->length(to- (char*) res->ptr());
132
void Item_func_lpad::fix_length_and_dec()
134
// Handle character set for args[0] and args[2].
135
if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
138
if (args[1]->const_item())
142
if (collation.collation->mbmaxlen > 0)
144
uint64_t temp= (uint64_t) args[1]->val_int();
146
/* Assumes that the maximum length of a String is < INT32_MAX. */
147
/* Set here so that rest of code sees out-of-bound value as such. */
148
if (temp > INT32_MAX)
151
length= temp * collation.collation->mbmaxlen;
154
if (length >= MAX_BLOB_WIDTH)
156
length= MAX_BLOB_WIDTH;
159
max_length= (ulong) length;
163
max_length= MAX_BLOB_WIDTH;
169
String *Item_func_lpad::val_str(String *str)
172
uint32_t res_char_length,pad_char_length;
173
/* must be int64_t to avoid truncation */
174
int64_t count= args[1]->val_int();
176
String *res= args[0]->val_str(&tmp_value);
177
String *pad= args[2]->val_str(&lpad_str);
179
if (!res || args[1]->null_value || !pad ||
180
((count < 0) && !args[1]->unsigned_flag))
183
/* Assumes that the maximum length of a String is < INT32_MAX. */
184
/* Set here so that rest of code sees out-of-bound value as such. */
185
if ((uint64_t) count > INT32_MAX)
188
res_char_length= res->numchars();
190
if (count <= res_char_length)
192
res->length(res->charpos((int) count));
196
pad_char_length= pad->numchars();
197
byte_count= count * collation.collation->mbmaxlen;
199
if ((uint64_t) byte_count > current_session->variables.max_allowed_packet)
201
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
202
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
203
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
204
func_name(), current_session->variables.max_allowed_packet);
208
if (args[2]->null_value || !pad_char_length ||
209
str->alloc((uint32_t) byte_count))
213
str->set_charset(collation.collation);
214
count-= res_char_length;
215
while (count >= pad_char_length)
218
count-= pad_char_length;
221
str->append(pad->ptr(), pad->charpos((int) count), collation.collation);
232
} /* namespace drizzled */