~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Merged in latest plugin-slot-reorg.

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 <drizzled/server_includes.h>
 
21
#include CSTDINT_H
 
22
#include <drizzled/function/str/trim.h>
 
23
 
 
24
String *Item_func_ltrim::val_str(String *str)
 
25
{
 
26
  assert(fixed == 1);
 
27
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
 
28
  String tmp(buff,sizeof(buff),system_charset_info);
 
29
  String *res, *remove_str;
 
30
  uint32_t remove_length;
 
31
 
 
32
  res= args[0]->val_str(str);
 
33
  if ((null_value=args[0]->null_value))
 
34
    return 0;
 
35
  remove_str= &remove;                          /* Default value. */
 
36
  if (arg_count == 2)
 
37
  {
 
38
    remove_str= args[1]->val_str(&tmp);
 
39
    if ((null_value= args[1]->null_value))
 
40
      return 0;
 
41
  }
 
42
 
 
43
  if ((remove_length= remove_str->length()) == 0 ||
 
44
      remove_length > res->length())
 
45
    return res;
 
46
 
 
47
  ptr= (char*) res->ptr();
 
48
  end= ptr+res->length();
 
49
  if (remove_length == 1)
 
50
  {
 
51
    char chr=(*remove_str)[0];
 
52
    while (ptr != end && *ptr == chr)
 
53
      ptr++;
 
54
  }
 
55
  else
 
56
  {
 
57
    const char *r_ptr=remove_str->ptr();
 
58
    end-=remove_length;
 
59
    while (ptr <= end && !memcmp(ptr, r_ptr, remove_length))
 
60
      ptr+=remove_length;
 
61
    end+=remove_length;
 
62
  }
 
63
  if (ptr == res->ptr())
 
64
    return res;
 
65
  tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
 
66
  return &tmp_value;
 
67
}
 
68
 
 
69
String *Item_func_rtrim::val_str(String *str)
 
70
{
 
71
  assert(fixed == 1);
 
72
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
 
73
  String tmp(buff, sizeof(buff), system_charset_info);
 
74
  String *res, *remove_str;
 
75
  uint32_t remove_length;
 
76
 
 
77
  res= args[0]->val_str(str);
 
78
  if ((null_value=args[0]->null_value))
 
79
    return 0;
 
80
  remove_str= &remove;                          /* Default value. */
 
81
  if (arg_count == 2)
 
82
  {
 
83
    remove_str= args[1]->val_str(&tmp);
 
84
    if ((null_value= args[1]->null_value))
 
85
      return 0;
 
86
  }
 
87
 
 
88
  if ((remove_length= remove_str->length()) == 0 ||
 
89
      remove_length > res->length())
 
90
    return res;
 
91
 
 
92
  ptr= (char*) res->ptr();
 
93
  end= ptr+res->length();
 
94
  char *p=ptr;
 
95
  register uint32_t l;
 
96
  if (remove_length == 1)
 
97
  {
 
98
    char chr=(*remove_str)[0];
 
99
    if (use_mb(res->charset()))
 
100
    {
 
101
      while (ptr < end)
 
102
      {
 
103
        if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l,p=ptr;
 
104
        else ++ptr;
 
105
      }
 
106
      ptr=p;
 
107
    }
 
108
    while (ptr != end  && end[-1] == chr)
 
109
      end--;
 
110
  }
 
111
  else
 
112
  {
 
113
    const char *r_ptr=remove_str->ptr();
 
114
    if (use_mb(res->charset()))
 
115
    {
 
116
loop:
 
117
      while (ptr + remove_length < end)
 
118
      {
 
119
        if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
 
120
        else ++ptr;
 
121
      }
 
122
      if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
 
123
      {
 
124
        end-=remove_length;
 
125
        ptr=p;
 
126
        goto loop;
 
127
      }
 
128
    }
 
129
    else
 
130
    {
 
131
      while (ptr + remove_length <= end &&
 
132
          !memcmp(end-remove_length, r_ptr, remove_length))
 
133
        end-=remove_length;
 
134
    }
 
135
  }
 
136
  if (end == res->ptr()+res->length())
 
137
    return res;
 
138
  tmp_value.set(*res,0,(uint) (end-res->ptr()));
 
139
  return &tmp_value;
 
140
}
 
141
 
 
142
 
 
143
String *Item_func_trim::val_str(String *str)
 
144
{
 
145
  assert(fixed == 1);
 
146
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
 
147
  const char *r_ptr;
 
148
  String tmp(buff, sizeof(buff), system_charset_info);
 
149
  String *res, *remove_str;
 
150
  uint32_t remove_length;
 
151
 
 
152
  res= args[0]->val_str(str);
 
153
  if ((null_value=args[0]->null_value))
 
154
    return 0;
 
155
  remove_str= &remove;                          /* Default value. */
 
156
  if (arg_count == 2)
 
157
  {
 
158
    remove_str= args[1]->val_str(&tmp);
 
159
    if ((null_value= args[1]->null_value))
 
160
      return 0;
 
161
  }
 
162
 
 
163
  if ((remove_length= remove_str->length()) == 0 ||
 
164
      remove_length > res->length())
 
165
    return res;
 
166
 
 
167
  ptr= (char*) res->ptr();
 
168
  end= ptr+res->length();
 
169
  r_ptr= remove_str->ptr();
 
170
  while (ptr+remove_length <= end && !memcmp(ptr,r_ptr,remove_length))
 
171
    ptr+=remove_length;
 
172
  if (use_mb(res->charset()))
 
173
  {
 
174
    char *p=ptr;
 
175
    register uint32_t l;
 
176
 loop:
 
177
    while (ptr + remove_length < end)
 
178
    {
 
179
      if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
 
180
      else ++ptr;
 
181
    }
 
182
    if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
 
183
    {
 
184
      end-=remove_length;
 
185
      ptr=p;
 
186
      goto loop;
 
187
    }
 
188
    ptr=p;
 
189
  }
 
190
  else
 
191
  {
 
192
    while (ptr + remove_length <= end &&
 
193
           !memcmp(end-remove_length,r_ptr,remove_length))
 
194
      end-=remove_length;
 
195
  }
 
196
  if (ptr == res->ptr() && end == ptr+res->length())
 
197
    return res;
 
198
  tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
 
199
  return &tmp_value;
 
200
}
 
201
 
 
202
void Item_func_trim::fix_length_and_dec()
 
203
{
 
204
  max_length= args[0]->max_length;
 
205
  if (arg_count == 1)
 
206
  {
 
207
    collation.set(args[0]->collation);
 
208
    remove.set_charset(collation.collation);
 
209
    remove.set_ascii(" ",1);
 
210
  }
 
211
  else
 
212
  {
 
213
    // Handle character set for args[1] and args[0].
 
214
    // Note that we pass args[1] as the first item, and args[0] as the second.
 
215
    if (agg_arg_charsets(collation, &args[1], 2, MY_COLL_CMP_CONV, -1))
 
216
      return;
 
217
  }
 
218
}
 
219
 
 
220
void Item_func_trim::print(String *str, enum_query_type query_type)
 
221
{
 
222
  if (arg_count == 1)
 
223
  {
 
224
    Item_func::print(str, query_type);
 
225
    return;
 
226
  }
 
227
  str->append(Item_func_trim::func_name());
 
228
  str->append('(');
 
229
  str->append(mode_name());
 
230
  str->append(' ');
 
231
  args[1]->print(str, query_type);
 
232
  str->append(STRING_WITH_LEN(" from "));
 
233
  args[0]->print(str, query_type);
 
234
  str->append(')');
 
235
}