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
20
#include <drizzled/server_includes.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>
27
void Item_func_rpad::fix_length_and_dec()
29
// Handle character set for args[0] and args[2].
30
if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
32
if (args[1]->const_item())
36
if (collation.collation->mbmaxlen > 0)
38
uint64_t temp= (uint64_t) args[1]->val_int();
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. */
45
length= temp * collation.collation->mbmaxlen;
48
if (length >= MAX_BLOB_WIDTH)
50
length= MAX_BLOB_WIDTH;
53
max_length= (ulong) length;
57
max_length= MAX_BLOB_WIDTH;
63
String *Item_func_rpad::val_str(String *str)
66
uint32_t res_byte_length,res_char_length,pad_char_length,pad_byte_length;
69
/* must be int64_t to avoid truncation */
70
int64_t count= args[1]->val_int();
72
String *res= args[0]->val_str(str);
73
String *rpad= args[2]->val_str(&rpad_str);
75
if (!res || args[1]->null_value || !rpad ||
76
((count < 0) && !args[1]->unsigned_flag))
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)
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
88
pad_char_length= rpad->numchars();
90
byte_count= count * collation.collation->mbmaxlen;
91
if ((uint64_t) byte_count > current_session->variables.max_allowed_packet)
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);
99
if (args[2]->null_value || !pad_char_length)
101
res_byte_length= res->length(); /* Must be done before alloc_buffer */
102
if (!(res= alloc_buffer(res,str,&tmp_value, (ulong) byte_count)))
105
to= (char*) res->ptr()+res_byte_length;
107
pad_byte_length= rpad->length();
108
count-= res_char_length;
109
for ( ; (uint32_t) count > pad_char_length; count-= pad_char_length)
111
memcpy(to,ptr_pad,pad_byte_length);
112
to+= pad_byte_length;
116
pad_byte_length= rpad->charpos((int) count);
117
memcpy(to,ptr_pad,(size_t) pad_byte_length);
118
to+= pad_byte_length;
120
res->length(to- (char*) res->ptr());
129
void Item_func_lpad::fix_length_and_dec()
131
// Handle character set for args[0] and args[2].
132
if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
135
if (args[1]->const_item())
139
if (collation.collation->mbmaxlen > 0)
141
uint64_t temp= (uint64_t) args[1]->val_int();
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)
148
length= temp * collation.collation->mbmaxlen;
151
if (length >= MAX_BLOB_WIDTH)
153
length= MAX_BLOB_WIDTH;
156
max_length= (ulong) length;
160
max_length= MAX_BLOB_WIDTH;
166
String *Item_func_lpad::val_str(String *str)
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();
173
String *res= args[0]->val_str(&tmp_value);
174
String *pad= args[2]->val_str(&lpad_str);
176
if (!res || args[1]->null_value || !pad ||
177
((count < 0) && !args[1]->unsigned_flag))
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)
185
res_char_length= res->numchars();
187
if (count <= res_char_length)
189
res->length(res->charpos((int) count));
193
pad_char_length= pad->numchars();
194
byte_count= count * collation.collation->mbmaxlen;
196
if ((uint64_t) byte_count > current_session->variables.max_allowed_packet)
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);
205
if (args[2]->null_value || !pad_char_length ||
206
str->alloc((uint32_t) byte_count))
210
str->set_charset(collation.collation);
211
count-= res_char_length;
212
while (count >= pad_char_length)
215
count-= pad_char_length;
218
str->append(pad->ptr(), pad->charpos((int) count), collation.collation);