1
1
/*
2
2
* Edmund Mergl <E.Mergl@bawue.de>
3
3
*
4
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/oracle_compat.c,v 1.29 2000/12/03 20:45:36 tgl Exp $
4
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/oracle_compat.c,v 1.30 2000/12/07 23:22:56 tgl Exp $
5
5
*
6
6
*/
7
7
8
- #include <ctype.h>
9
-
10
8
#include "postgres.h"
11
9
10
+ #include <ctype.h>
11
+
12
12
#include "utils/builtins.h"
13
13
14
14
@@ -140,7 +140,8 @@ initcap(PG_FUNCTION_ARGS)
140
140
* Purpose:
141
141
*
142
142
* Returns string1, left-padded to length len with the sequence of
143
- * characters in string2.
143
+ * characters in string2. If len is less than the length of string1,
144
+ * instead truncate (on the right) to len.
144
145
*
145
146
********************************************************************/
146
147
@@ -153,31 +154,49 @@ lpad(PG_FUNCTION_ARGS)
153
154
text * ret ;
154
155
char * ptr1 ,
155
156
* ptr2 ,
157
+ * ptr2end ,
156
158
* ptr_ret ;
157
159
int m ,
158
- n ;
160
+ s1len ,
161
+ s2len ;
162
+
163
+ /* Negative len is silently taken as zero */
164
+ if (len < 0 )
165
+ len = 0 ;
166
+
167
+ s1len = VARSIZE (string1 ) - VARHDRSZ ;
168
+ if (s1len < 0 )
169
+ s1len = 0 ; /* shouldn't happen */
170
+
171
+ s2len = VARSIZE (string2 ) - VARHDRSZ ;
172
+ if (s2len < 0 )
173
+ s2len = 0 ; /* shouldn't happen */
159
174
160
- if (((VARSIZE (string1 ) - VARHDRSZ ) < 0 ) ||
161
- ((m = len - (VARSIZE (string1 ) - VARHDRSZ )) <= 0 ) ||
162
- ((VARSIZE (string2 ) - VARHDRSZ ) <= 0 ))
163
- PG_RETURN_TEXT_P (string1 );
175
+ if (s1len > len )
176
+ s1len = len ; /* truncate string1 to len chars */
177
+
178
+ if (s2len <= 0 )
179
+ len = s1len ; /* nothing to pad with, so don't pad */
164
180
165
181
ret = (text * ) palloc (VARHDRSZ + len );
166
182
VARATT_SIZEP (ret ) = VARHDRSZ + len ;
167
183
184
+ m = len - s1len ;
185
+
168
186
ptr2 = VARDATA (string2 );
187
+ ptr2end = ptr2 + s2len ;
169
188
ptr_ret = VARDATA (ret );
170
189
171
190
while (m -- )
172
191
{
173
- * ptr_ret ++ = * ptr2 ;
174
- ptr2 = (ptr2 == VARDATA (string2 ) + VARSIZE (string2 ) - VARHDRSZ - 1 ) ? VARDATA (string2 ) : ++ ptr2 ;
192
+ * ptr_ret ++ = * ptr2 ++ ;
193
+ if (ptr2 == ptr2end ) /* wrap around at end of s2 */
194
+ ptr2 = VARDATA (string2 );
175
195
}
176
196
177
- n = VARSIZE (string1 ) - VARHDRSZ ;
178
197
ptr1 = VARDATA (string1 );
179
198
180
- while (n -- )
199
+ while (s1len -- )
181
200
* ptr_ret ++ = * ptr1 ++ ;
182
201
183
202
PG_RETURN_TEXT_P (ret );
@@ -195,7 +214,8 @@ lpad(PG_FUNCTION_ARGS)
195
214
* Purpose:
196
215
*
197
216
* Returns string1, right-padded to length len with the sequence of
198
- * characters in string2.
217
+ * characters in string2. If len is less than the length of string1,
218
+ * instead truncate (on the right) to len.
199
219
*
200
220
********************************************************************/
201
221
@@ -208,31 +228,49 @@ rpad(PG_FUNCTION_ARGS)
208
228
text * ret ;
209
229
char * ptr1 ,
210
230
* ptr2 ,
231
+ * ptr2end ,
211
232
* ptr_ret ;
212
233
int m ,
213
- n ;
234
+ s1len ,
235
+ s2len ;
236
+
237
+ /* Negative len is silently taken as zero */
238
+ if (len < 0 )
239
+ len = 0 ;
240
+
241
+ s1len = VARSIZE (string1 ) - VARHDRSZ ;
242
+ if (s1len < 0 )
243
+ s1len = 0 ; /* shouldn't happen */
214
244
215
- if (((VARSIZE (string1 ) - VARHDRSZ ) < 0 ) ||
216
- ((m = len - (VARSIZE (string1 ) - VARHDRSZ )) <= 0 ) ||
217
- ((VARSIZE (string2 ) - VARHDRSZ ) <= 0 ))
218
- PG_RETURN_TEXT_P (string1 );
245
+ s2len = VARSIZE (string2 ) - VARHDRSZ ;
246
+ if (s2len < 0 )
247
+ s2len = 0 ; /* shouldn't happen */
248
+
249
+ if (s1len > len )
250
+ s1len = len ; /* truncate string1 to len chars */
251
+
252
+ if (s2len <= 0 )
253
+ len = s1len ; /* nothing to pad with, so don't pad */
219
254
220
255
ret = (text * ) palloc (VARHDRSZ + len );
221
256
VARATT_SIZEP (ret ) = VARHDRSZ + len ;
222
257
223
- n = VARSIZE (string1 ) - VARHDRSZ ;
258
+ m = len - s1len ;
259
+
224
260
ptr1 = VARDATA (string1 );
225
261
ptr_ret = VARDATA (ret );
226
262
227
- while (n -- )
263
+ while (s1len -- )
228
264
* ptr_ret ++ = * ptr1 ++ ;
229
265
230
266
ptr2 = VARDATA (string2 );
267
+ ptr2end = ptr2 + s2len ;
231
268
232
269
while (m -- )
233
270
{
234
- * ptr_ret ++ = * ptr2 ;
235
- ptr2 = (ptr2 == VARDATA (string2 ) + VARSIZE (string2 ) - VARHDRSZ - 1 ) ? VARDATA (string2 ) : ++ ptr2 ;
271
+ * ptr_ret ++ = * ptr2 ++ ;
272
+ if (ptr2 == ptr2end ) /* wrap around at end of s2 */
273
+ ptr2 = VARDATA (string2 );
236
274
}
237
275
238
276
PG_RETURN_TEXT_P (ret );
0 commit comments