PostgreSQL Source Code git master
Loading...
Searching...
No Matches
oracle_compat.c File Reference
#include "postgres.h"
#include "common/int.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/formatting.h"
#include "utils/memutils.h"
#include "varatt.h"
Include dependency graph for oracle_compat.c:

Go to the source code of this file.

Functions

static textdotrim (const char *string, int stringlen, const char *set, int setlen, bool doltrim, bool dortrim)
 
static byteadobyteatrim (bytea *string, bytea *set, bool doltrim, bool dortrim)
 
Datum lower (PG_FUNCTION_ARGS)
 
Datum upper (PG_FUNCTION_ARGS)
 
Datum initcap (PG_FUNCTION_ARGS)
 
Datum casefold (PG_FUNCTION_ARGS)
 
Datum lpad (PG_FUNCTION_ARGS)
 
Datum rpad (PG_FUNCTION_ARGS)
 
Datum btrim (PG_FUNCTION_ARGS)
 
Datum btrim1 (PG_FUNCTION_ARGS)
 
Datum byteatrim (PG_FUNCTION_ARGS)
 
Datum bytealtrim (PG_FUNCTION_ARGS)
 
Datum byteartrim (PG_FUNCTION_ARGS)
 
Datum ltrim (PG_FUNCTION_ARGS)
 
Datum ltrim1 (PG_FUNCTION_ARGS)
 
Datum rtrim (PG_FUNCTION_ARGS)
 
Datum rtrim1 (PG_FUNCTION_ARGS)
 
Datum translate (PG_FUNCTION_ARGS)
 
Datum ascii (PG_FUNCTION_ARGS)
 
Datum chr (PG_FUNCTION_ARGS)
 
Datum repeat (PG_FUNCTION_ARGS)
 

Function Documentation

◆ ascii()

Datum ascii ( PG_FUNCTION_ARGS  )

Definition at line 950 of file oracle_compat.c.

951{
952 text *string = PG_GETARG_TEXT_PP(0);
954 unsigned char *data;
955
956 if (VARSIZE_ANY_EXHDR(string) <= 0)
958
959 data = (unsigned char *) VARDATA_ANY(string);
960
961 if (encoding == PG_UTF8 && *data > 127)
962 {
963 /* return the code point for Unicode */
964
965 int result = 0,
966 tbytes = 0,
967 i;
968
969 if (*data >= 0xF0)
970 {
971 result = *data & 0x07;
972 tbytes = 3;
973 }
974 else if (*data >= 0xE0)
975 {
976 result = *data & 0x0F;
977 tbytes = 2;
978 }
979 else
980 {
981 Assert(*data > 0xC0);
982 result = *data & 0x1f;
983 tbytes = 1;
984 }
985
986 Assert(tbytes > 0);
987
988 for (i = 1; i <= tbytes; i++)
989 {
990 Assert((data[i] & 0xC0) == 0x80);
991 result = (result << 6) + (data[i] & 0x3f);
992 }
993
994 PG_RETURN_INT32(result);
995 }
996 else
997 {
998 if (pg_encoding_max_length(encoding) > 1 && *data > 127)
1001 errmsg("requested character too large")));
1002
1003
1005 }
1006}
#define Assert(condition)
Definition c.h:885
int32_t int32
Definition c.h:554
int errcode(int sqlerrcode)
Definition elog.c:874
int errmsg(const char *fmt,...)
Definition elog.c:1093
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
static char * encoding
Definition initdb.c:139
int i
Definition isn.c:77
#define PG_UTF8
Definition mbprint.c:43
int GetDatabaseEncoding(void)
Definition mbutils.c:1389
const void * data
static int fb(int x)
Definition c.h:718
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
int pg_encoding_max_length(int encoding)
Definition wchar.c:2235

References Assert, data, encoding, ereport, errcode(), errmsg(), ERROR, fb(), GetDatabaseEncoding(), i, pg_encoding_max_length(), PG_GETARG_TEXT_PP, PG_RETURN_INT32, PG_UTF8, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by float4out(), float8out_internal(), and pg_to_ascii().

◆ btrim()

Datum btrim ( PG_FUNCTION_ARGS  )

Definition at line 359 of file oracle_compat.c.

360{
361 text *string = PG_GETARG_TEXT_PP(0);
362 text *set = PG_GETARG_TEXT_PP(1);
363 text *ret;
364
365 ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
367 true, true);
368
369 PG_RETURN_TEXT_P(ret);
370}
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
static text * dotrim(const char *string, int stringlen, const char *set, int setlen, bool doltrim, bool dortrim)

References dotrim(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ btrim1()

Datum btrim1 ( PG_FUNCTION_ARGS  )

Definition at line 379 of file oracle_compat.c.

380{
381 text *string = PG_GETARG_TEXT_PP(0);
382 text *ret;
383
384 ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
385 " ", 1,
386 true, true);
387
388 PG_RETURN_TEXT_P(ret);
389}

References dotrim(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ bytealtrim()

Datum bytealtrim ( PG_FUNCTION_ARGS  )

Definition at line 665 of file oracle_compat.c.

666{
667 bytea *string = PG_GETARG_BYTEA_PP(0);
668 bytea *set = PG_GETARG_BYTEA_PP(1);
669 bytea *ret;
670
671 ret = dobyteatrim(string, set, true, false);
672
674}
#define PG_GETARG_BYTEA_PP(n)
Definition fmgr.h:309
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
static bytea * dobyteatrim(bytea *string, bytea *set, bool doltrim, bool dortrim)

References dobyteatrim(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

◆ byteartrim()

Datum byteartrim ( PG_FUNCTION_ARGS  )

Definition at line 692 of file oracle_compat.c.

693{
694 bytea *string = PG_GETARG_BYTEA_PP(0);
695 bytea *set = PG_GETARG_BYTEA_PP(1);
696 bytea *ret;
697
698 ret = dobyteatrim(string, set, false, true);
699
701}

References dobyteatrim(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

◆ byteatrim()

Datum byteatrim ( PG_FUNCTION_ARGS  )

Definition at line 638 of file oracle_compat.c.

639{
640 bytea *string = PG_GETARG_BYTEA_PP(0);
641 bytea *set = PG_GETARG_BYTEA_PP(1);
642 bytea *ret;
643
644 ret = dobyteatrim(string, set, true, true);
645
647}

References dobyteatrim(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

◆ casefold()

Datum casefold ( PG_FUNCTION_ARGS  )

Definition at line 130 of file oracle_compat.c.

131{
133 char *out_string;
134 text *result;
135
139 result = cstring_to_text(out_string);
141
142 PG_RETURN_TEXT_P(result);
143}
#define PG_GET_COLLATION()
Definition fmgr.h:198
char * str_casefold(const char *buff, size_t nbytes, Oid collid)
void pfree(void *pointer)
Definition mcxt.c:1616
text * cstring_to_text(const char *s)
Definition varlena.c:182

References cstring_to_text(), fb(), pfree(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, str_casefold(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ chr()

Definition at line 1031 of file oracle_compat.c.

1032{
1034 uint32 cvalue;
1035 text *result;
1037
1038 /*
1039 * Error out on arguments that make no sense or that we can't validly
1040 * represent in the encoding.
1041 */
1042 if (arg < 0)
1043 ereport(ERROR,
1045 errmsg("character number must be positive")));
1046 else if (arg == 0)
1047 ereport(ERROR,
1049 errmsg("null character not permitted")));
1050
1051 cvalue = arg;
1052
1053 if (encoding == PG_UTF8 && cvalue > 127)
1054 {
1055 /* for Unicode we treat the argument as a code point */
1056 int bytes;
1057 unsigned char *wch;
1058
1059 /*
1060 * We only allow valid Unicode code points; per RFC3629 that stops at
1061 * U+10FFFF, even though 4-byte UTF8 sequences can hold values up to
1062 * U+1FFFFF.
1063 */
1064 if (cvalue > 0x0010ffff)
1065 ereport(ERROR,
1067 errmsg("requested character too large for encoding: %u",
1068 cvalue)));
1069
1070 if (cvalue > 0xffff)
1071 bytes = 4;
1072 else if (cvalue > 0x07ff)
1073 bytes = 3;
1074 else
1075 bytes = 2;
1076
1077 result = (text *) palloc(VARHDRSZ + bytes);
1078 SET_VARSIZE(result, VARHDRSZ + bytes);
1079 wch = (unsigned char *) VARDATA(result);
1080
1081 if (bytes == 2)
1082 {
1083 wch[0] = 0xC0 | ((cvalue >> 6) & 0x1F);
1084 wch[1] = 0x80 | (cvalue & 0x3F);
1085 }
1086 else if (bytes == 3)
1087 {
1088 wch[0] = 0xE0 | ((cvalue >> 12) & 0x0F);
1089 wch[1] = 0x80 | ((cvalue >> 6) & 0x3F);
1090 wch[2] = 0x80 | (cvalue & 0x3F);
1091 }
1092 else
1093 {
1094 wch[0] = 0xF0 | ((cvalue >> 18) & 0x07);
1095 wch[1] = 0x80 | ((cvalue >> 12) & 0x3F);
1096 wch[2] = 0x80 | ((cvalue >> 6) & 0x3F);
1097 wch[3] = 0x80 | (cvalue & 0x3F);
1098 }
1099
1100 /*
1101 * The preceding range check isn't sufficient, because UTF8 excludes
1102 * Unicode "surrogate pair" codes. Make sure what we created is valid
1103 * UTF8.
1104 */
1105 if (!pg_utf8_islegal(wch, bytes))
1106 ereport(ERROR,
1108 errmsg("requested character not valid for encoding: %u",
1109 cvalue)));
1110 }
1111 else
1112 {
1113 bool is_mb;
1114
1116
1117 if ((is_mb && (cvalue > 127)) || (!is_mb && (cvalue > 255)))
1118 ereport(ERROR,
1120 errmsg("requested character too large for encoding: %u",
1121 cvalue)));
1122
1123 result = (text *) palloc(VARHDRSZ + 1);
1124 SET_VARSIZE(result, VARHDRSZ + 1);
1125 *VARDATA(result) = (char) cvalue;
1126 }
1127
1128 PG_RETURN_TEXT_P(result);
1129}
#define VARHDRSZ
Definition c.h:723
uint32_t uint32
Definition c.h:558
Datum arg
Definition elog.c:1322
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
void * palloc(Size size)
Definition mcxt.c:1387
static char * VARDATA(const void *PTR)
Definition varatt.h:305
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432
bool pg_utf8_islegal(const unsigned char *source, int length)
Definition wchar.c:2011

References arg, encoding, ereport, errcode(), errmsg(), ERROR, fb(), GetDatabaseEncoding(), palloc(), pg_encoding_max_length(), PG_GETARG_INT32, PG_RETURN_TEXT_P, PG_UTF8, pg_utf8_islegal(), SET_VARSIZE(), VARDATA(), and VARHDRSZ.

◆ dobyteatrim()

bytea * dobyteatrim ( bytea string,
bytea set,
bool  doltrim,
bool  dortrim 
)
static

Definition at line 555 of file oracle_compat.c.

556{
557 bytea *ret;
558 char *ptr,
559 *end,
560 *ptr2,
561 *ptr2start,
562 *end2;
563 int m,
564 stringlen,
565 setlen;
566
569
570 if (stringlen <= 0 || setlen <= 0)
571 return string;
572
573 m = stringlen;
574 ptr = VARDATA_ANY(string);
575 end = ptr + stringlen - 1;
576 ptr2start = VARDATA_ANY(set);
577 end2 = ptr2start + setlen - 1;
578
579 if (doltrim)
580 {
581 while (m > 0)
582 {
583 ptr2 = ptr2start;
584 while (ptr2 <= end2)
585 {
586 if (*ptr == *ptr2)
587 break;
588 ++ptr2;
589 }
590 if (ptr2 > end2)
591 break;
592 ptr++;
593 m--;
594 }
595 }
596
597 if (dortrim)
598 {
599 while (m > 0)
600 {
601 ptr2 = ptr2start;
602 while (ptr2 <= end2)
603 {
604 if (*end == *ptr2)
605 break;
606 ++ptr2;
607 }
608 if (ptr2 > end2)
609 break;
610 end--;
611 m--;
612 }
613 }
614
615 ret = (bytea *) palloc(VARHDRSZ + m);
616 SET_VARSIZE(ret, VARHDRSZ + m);
617 memcpy(VARDATA(ret), ptr, m);
618 return ret;
619}
char string[11]

References fb(), palloc(), SET_VARSIZE(), VARDATA(), VARDATA_ANY(), VARHDRSZ, and VARSIZE_ANY_EXHDR().

Referenced by bytealtrim(), byteartrim(), and byteatrim().

◆ dotrim()

static text * dotrim ( const char string,
int  stringlen,
const char set,
int  setlen,
bool  doltrim,
bool  dortrim 
)
static

Definition at line 395 of file oracle_compat.c.

398{
399 int i;
400
401 /* Nothing to do if either string or set is empty */
402 if (stringlen > 0 && setlen > 0)
403 {
405 {
406 /*
407 * In the multibyte-encoding case, build arrays of pointers to
408 * character starts, so that we can avoid inefficient checks in
409 * the inner loops.
410 */
411 const char **stringchars;
412 const char **setchars;
413 const char *setend;
414 int *stringmblen;
415 int *setmblen;
416 int stringnchars;
417 int setnchars;
418 int resultndx;
419 int resultnchars;
420 const char *p;
421 const char *pend;
422 int len;
423 int mblen;
424 const char *str_pos;
425 int str_len;
426
427 stringchars = (const char **) palloc(stringlen * sizeof(char *));
428 stringmblen = (int *) palloc(stringlen * sizeof(int));
429 stringnchars = 0;
430 p = string;
431 len = stringlen;
432 pend = p + len;
433 while (len > 0)
434 {
437 stringnchars++;
438 p += mblen;
439 len -= mblen;
440 }
441
442 setchars = (const char **) palloc(setlen * sizeof(char *));
443 setmblen = (int *) palloc(setlen * sizeof(int));
444 setnchars = 0;
445 p = set;
446 len = setlen;
447 setend = set + setlen;
448 while (len > 0)
449 {
450 setchars[setnchars] = p;
452 setnchars++;
453 p += mblen;
454 len -= mblen;
455 }
456
457 resultndx = 0; /* index in stringchars[] */
459
460 if (doltrim)
461 {
462 while (resultnchars > 0)
463 {
466 for (i = 0; i < setnchars; i++)
467 {
468 if (str_len == setmblen[i] &&
470 break;
471 }
472 if (i >= setnchars)
473 break; /* no match here */
474 string += str_len;
476 resultndx++;
477 resultnchars--;
478 }
479 }
480
481 if (dortrim)
482 {
483 while (resultnchars > 0)
484 {
487 for (i = 0; i < setnchars; i++)
488 {
489 if (str_len == setmblen[i] &&
491 break;
492 }
493 if (i >= setnchars)
494 break; /* no match here */
496 resultnchars--;
497 }
498 }
499
504 }
505 else
506 {
507 /*
508 * In the single-byte-encoding case, we don't need such overhead.
509 */
510 if (doltrim)
511 {
512 while (stringlen > 0)
513 {
514 char str_ch = *string;
515
516 for (i = 0; i < setlen; i++)
517 {
518 if (str_ch == set[i])
519 break;
520 }
521 if (i >= setlen)
522 break; /* no match here */
523 string++;
524 stringlen--;
525 }
526 }
527
528 if (dortrim)
529 {
530 while (stringlen > 0)
531 {
532 char str_ch = string[stringlen - 1];
533
534 for (i = 0; i < setlen; i++)
535 {
536 if (str_ch == set[i])
537 break;
538 }
539 if (i >= setlen)
540 break; /* no match here */
541 stringlen--;
542 }
543 }
544 }
545 }
546
547 /* Return selected portion of string */
548 return cstring_to_text_with_len(string, stringlen);
549}
int pg_mblen_range(const char *mbstr, const char *end)
Definition mbutils.c:1084
int pg_database_encoding_max_length(void)
Definition mbutils.c:1674
const void size_t len
const unsigned char * pend
text * cstring_to_text_with_len(const char *s, int len)
Definition varlena.c:194

References cstring_to_text_with_len(), fb(), i, len, palloc(), pend, pfree(), pg_database_encoding_max_length(), and pg_mblen_range().

Referenced by btrim(), btrim1(), ltrim(), ltrim1(), rtrim(), and rtrim1().

◆ initcap()

Datum initcap ( PG_FUNCTION_ARGS  )

Definition at line 114 of file oracle_compat.c.

115{
117 char *out_string;
118 text *result;
119
123 result = cstring_to_text(out_string);
125
126 PG_RETURN_TEXT_P(result);
127}
char * str_initcap(const char *buff, size_t nbytes, Oid collid)

References cstring_to_text(), fb(), pfree(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, str_initcap(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ lower()

Datum lower ( PG_FUNCTION_ARGS  )

Definition at line 49 of file oracle_compat.c.

50{
52 char *out_string;
53 text *result;
54
60
61 PG_RETURN_TEXT_P(result);
62}
char * str_tolower(const char *buff, size_t nbytes, Oid collid)

References cstring_to_text(), fb(), pfree(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, str_tolower(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by _hash_binsearch(), _hash_binsearch_last(), bms_add_range(), calc_hist_selectivity_contained(), calc_hist_selectivity_contained(), calc_hist_selectivity_contains(), calc_hist_selectivity_contains(), check_new_partition_bound(), check_partition_bounds_for_split_range(), compute_range_stats(), create_range_bounds(), daterange_canonical(), enum_range_bounds(), enum_range_internal(), find_simplified_clause(), Generic_Text_IC_like(), geqo_randint(), getQuadrant(), gist_box_picksplit(), hash_multirange(), hash_multirange_extended(), hash_range(), hash_range_extended(), heap_inplace_update_and_unlock(), hyphenate(), int4range_canonical(), int8range_canonical(), iterate_word_similarity(), length_hist_bsearch(), length_hist_bsearch(), make_empty_range(), make_one_partition_rbound(), make_range(), multirange_bsearch_match(), multirange_elem_bsearch_comparison(), multirange_get_bounds(), multirange_get_union_range(), multirange_lower(), multirange_lower_inc(), multirange_lower_inf(), multirange_range_contains_bsearch_comparison(), multirange_range_overlaps_bsearch_comparison(), multirange_upper(), multirange_upper_inc(), multirange_upper_inf(), multirangesel(), PageAddItemExtended(), range_constructor2(), range_constructor3(), range_contains_elem_internal(), range_deserialize(), range_gist_double_sorting_split(), range_in(), range_lower(), range_out(), range_recv(), range_send(), range_serialize(), range_upper(), rangesel(), rbound_bsearch(), rbound_bsearch(), spg_range_quad_inner_consistent(), test_bms_add_range(), test_random_operations(), trim_array(), XLogRecordAssemble(), and XLogSaveBufferForHint().

◆ lpad()

Datum lpad ( PG_FUNCTION_ARGS  )

Definition at line 163 of file oracle_compat.c.

164{
168 text *ret;
169 char *ptr1,
170 *ptr2,
171 *ptr2start,
172 *ptr_ret;
173 const char *ptr2end;
174 int m,
175 s1len,
176 s2len;
177 int bytelen;
178
179 /* Negative len is silently taken as zero */
180 if (len < 0)
181 len = 0;
182
184 if (s1len < 0)
185 s1len = 0; /* shouldn't happen */
186
188 if (s2len < 0)
189 s2len = 0; /* shouldn't happen */
190
192
193 if (s1len > len)
194 s1len = len; /* truncate string1 to len chars */
195
196 if (s2len <= 0)
197 len = s1len; /* nothing to pad with, so don't pad */
198
199 /* compute worst-case output length */
201 &bytelen)) ||
206 errmsg("requested length too large")));
207
208 ret = (text *) palloc(bytelen);
209
210 m = len - s1len;
211
213 ptr2end = ptr2 + s2len;
214 ptr_ret = VARDATA(ret);
215
216 while (m--)
217 {
219
221 ptr_ret += mlen;
222 ptr2 += mlen;
223 if (ptr2 == ptr2end) /* wrap around at end of s2 */
224 ptr2 = ptr2start;
225 }
226
228
229 while (s1len--)
230 {
232
234 ptr_ret += mlen;
235 ptr1 += mlen;
236 }
237
238 SET_VARSIZE(ret, ptr_ret - (char *) ret);
239
240 PG_RETURN_TEXT_P(ret);
241}
#define unlikely(x)
Definition c.h:424
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:187
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:151
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition mbutils.c:1185
int pg_mblen_unbounded(const char *mbstr)
Definition mbutils.c:1137
#define AllocSizeIsValid(size)
Definition memutils.h:42

References AllocSizeIsValid, ereport, errcode(), errmsg(), ERROR, fb(), len, palloc(), pg_add_s32_overflow(), pg_database_encoding_max_length(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, pg_mblen_range(), pg_mblen_unbounded(), pg_mbstrlen_with_len(), pg_mul_s32_overflow(), PG_RETURN_TEXT_P, SET_VARSIZE(), unlikely, VARDATA(), VARDATA_ANY(), VARHDRSZ, and VARSIZE_ANY_EXHDR().

◆ ltrim()

Datum ltrim ( PG_FUNCTION_ARGS  )

Definition at line 719 of file oracle_compat.c.

720{
721 text *string = PG_GETARG_TEXT_PP(0);
722 text *set = PG_GETARG_TEXT_PP(1);
723 text *ret;
724
725 ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
727 true, false);
728
729 PG_RETURN_TEXT_P(ret);
730}

References dotrim(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ ltrim1()

Datum ltrim1 ( PG_FUNCTION_ARGS  )

Definition at line 739 of file oracle_compat.c.

740{
741 text *string = PG_GETARG_TEXT_PP(0);
742 text *ret;
743
744 ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
745 " ", 1,
746 true, false);
747
748 PG_RETURN_TEXT_P(ret);
749}

References dotrim(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ repeat()

Datum repeat ( PG_FUNCTION_ARGS  )

Definition at line 1146 of file oracle_compat.c.

1147{
1148 text *string = PG_GETARG_TEXT_PP(0);
1149 int32 count = PG_GETARG_INT32(1);
1150 text *result;
1151 int slen,
1152 tlen;
1153 int i;
1154 char *cp,
1155 *sp;
1156
1157 if (count < 0)
1158 count = 0;
1159
1160 slen = VARSIZE_ANY_EXHDR(string);
1161
1162 if (unlikely(pg_mul_s32_overflow(count, slen, &tlen)) ||
1165 ereport(ERROR,
1167 errmsg("requested length too large")));
1168
1169 result = (text *) palloc(tlen);
1170
1171 SET_VARSIZE(result, tlen);
1172 cp = VARDATA(result);
1173 sp = VARDATA_ANY(string);
1174 for (i = 0; i < count; i++)
1175 {
1176 memcpy(cp, sp, slen);
1177 cp += slen;
1179 }
1180
1181 PG_RETURN_TEXT_P(result);
1182}
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123

References AllocSizeIsValid, CHECK_FOR_INTERRUPTS, ereport, errcode(), errmsg(), ERROR, fb(), i, palloc(), pg_add_s32_overflow(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, pg_mul_s32_overflow(), PG_RETURN_TEXT_P, SET_VARSIZE(), unlikely, VARDATA(), VARDATA_ANY(), VARHDRSZ, and VARSIZE_ANY_EXHDR().

◆ rpad()

Datum rpad ( PG_FUNCTION_ARGS  )

Definition at line 261 of file oracle_compat.c.

262{
266 text *ret;
267 char *ptr1,
268 *ptr2,
269 *ptr2start,
270 *ptr_ret;
271 const char *ptr2end;
272 int m,
273 s1len,
274 s2len;
275 int bytelen;
276
277 /* Negative len is silently taken as zero */
278 if (len < 0)
279 len = 0;
280
282 if (s1len < 0)
283 s1len = 0; /* shouldn't happen */
284
286 if (s2len < 0)
287 s2len = 0; /* shouldn't happen */
288
290
291 if (s1len > len)
292 s1len = len; /* truncate string1 to len chars */
293
294 if (s2len <= 0)
295 len = s1len; /* nothing to pad with, so don't pad */
296
297 /* compute worst-case output length */
299 &bytelen)) ||
304 errmsg("requested length too large")));
305
306 ret = (text *) palloc(bytelen);
307
308 m = len - s1len;
309
311
312 ptr_ret = VARDATA(ret);
313
314 while (s1len--)
315 {
317
319 ptr_ret += mlen;
320 ptr1 += mlen;
321 }
322
324 ptr2end = ptr2 + s2len;
325
326 while (m--)
327 {
329
331 ptr_ret += mlen;
332 ptr2 += mlen;
333 if (ptr2 == ptr2end) /* wrap around at end of s2 */
334 ptr2 = ptr2start;
335 }
336
337 SET_VARSIZE(ret, ptr_ret - (char *) ret);
338
339 PG_RETURN_TEXT_P(ret);
340}

References AllocSizeIsValid, ereport, errcode(), errmsg(), ERROR, fb(), len, palloc(), pg_add_s32_overflow(), pg_database_encoding_max_length(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, pg_mblen_range(), pg_mblen_unbounded(), pg_mbstrlen_with_len(), pg_mul_s32_overflow(), PG_RETURN_TEXT_P, SET_VARSIZE(), unlikely, VARDATA(), VARDATA_ANY(), VARHDRSZ, and VARSIZE_ANY_EXHDR().

◆ rtrim()

Datum rtrim ( PG_FUNCTION_ARGS  )

Definition at line 767 of file oracle_compat.c.

768{
769 text *string = PG_GETARG_TEXT_PP(0);
770 text *set = PG_GETARG_TEXT_PP(1);
771 text *ret;
772
773 ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
775 false, true);
776
777 PG_RETURN_TEXT_P(ret);
778}

References dotrim(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ rtrim1()

Datum rtrim1 ( PG_FUNCTION_ARGS  )

Definition at line 787 of file oracle_compat.c.

788{
789 text *string = PG_GETARG_TEXT_PP(0);
790 text *ret;
791
792 ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
793 " ", 1,
794 false, true);
795
796 PG_RETURN_TEXT_P(ret);
797}

References dotrim(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ translate()

Datum translate ( PG_FUNCTION_ARGS  )

Definition at line 818 of file oracle_compat.c.

819{
820 text *string = PG_GETARG_TEXT_PP(0);
821 text *from = PG_GETARG_TEXT_PP(1);
822 text *to = PG_GETARG_TEXT_PP(2);
823 text *result;
824 char *from_ptr,
825 *to_ptr,
826 *to_end;
827 char *source,
828 *target;
829 const char *source_end;
830 const char *from_end;
831 int m,
832 fromlen,
833 tolen,
834 retlen,
835 i;
836 int bytelen;
837 int len;
838 int source_len;
839 int from_index;
840
841 m = VARSIZE_ANY_EXHDR(string);
842 if (m <= 0)
843 PG_RETURN_TEXT_P(string);
844 source = VARDATA_ANY(string);
845 source_end = source + m;
846
848 from_ptr = VARDATA_ANY(from);
851 to_ptr = VARDATA_ANY(to);
852 to_end = to_ptr + tolen;
853
854 /*
855 * The worst-case expansion is to substitute a max-length character for a
856 * single-byte character at each position of the string.
857 */
859 &bytelen)) ||
864 errmsg("requested length too large")));
865
866 result = (text *) palloc(bytelen);
867
868 target = VARDATA(result);
869 retlen = 0;
870
871 while (m > 0)
872 {
874 from_index = 0;
875
876 for (i = 0; i < fromlen; i += len)
877 {
879 if (len == source_len &&
880 memcmp(source, &from_ptr[i], len) == 0)
881 break;
882
883 from_index++;
884 }
885 if (i < fromlen)
886 {
887 /* substitute, or delete if no corresponding "to" character */
888 char *p = to_ptr;
889
890 for (i = 0; i < from_index; i++)
891 {
892 if (p >= to_end)
893 break;
894 p += pg_mblen_range(p, to_end);
895 }
896 if (p < to_end)
897 {
899 memcpy(target, p, len);
900 target += len;
901 retlen += len;
902 }
903 }
904 else
905 {
906 /* no match, so copy */
907 memcpy(target, source, source_len);
908 target += source_len;
910 }
911
913 m -= source_len;
914 }
915
916 SET_VARSIZE(result, retlen + VARHDRSZ);
917
918 /*
919 * The function result is probably much bigger than needed, if we're using
920 * a multibyte encoding, but it's not worth reallocating it; the result
921 * probably won't live long anyway.
922 */
923
924 PG_RETURN_TEXT_P(result);
925}
static rewind_source * source
Definition pg_rewind.c:89

References AllocSizeIsValid, ereport, errcode(), errmsg(), ERROR, fb(), i, len, palloc(), pg_add_s32_overflow(), pg_database_encoding_max_length(), PG_GETARG_TEXT_PP, pg_mblen_range(), pg_mul_s32_overflow(), PG_RETURN_TEXT_P, SET_VARSIZE(), source, unlikely, VARDATA(), VARDATA_ANY(), VARHDRSZ, and VARSIZE_ANY_EXHDR().

Referenced by printQuery(), printTableAddCell(), and printTableAddHeader().

◆ upper()

Datum upper ( PG_FUNCTION_ARGS  )

Definition at line 80 of file oracle_compat.c.

81{
83 char *out_string;
84 text *result;
85
91
92 PG_RETURN_TEXT_P(result);
93}
char * str_toupper(const char *buff, size_t nbytes, Oid collid)

References cstring_to_text(), fb(), pfree(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, str_toupper(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by _hash_binsearch(), _hash_binsearch_last(), bms_add_range(), calc_hist_selectivity_contained(), calc_hist_selectivity_contained(), calc_hist_selectivity_contains(), calc_hist_selectivity_contains(), check_new_partition_bound(), check_partition_bounds_for_split_range(), compactify_tuples(), compute_range_stats(), create_range_bounds(), cube_coord_llur(), daterange_canonical(), enum_range_bounds(), enum_range_internal(), find_simplified_clause(), g_cube_distance(), geqo_randint(), getQuadrant(), gist_box_picksplit(), hash_multirange(), hash_multirange_extended(), hash_range(), hash_range_extended(), heap_inplace_update_and_unlock(), hyphenate(), int4range_canonical(), int8range_canonical(), iterate_word_similarity(), length_hist_bsearch(), length_hist_bsearch(), make_empty_range(), make_range(), multirange_bsearch_match(), multirange_elem_bsearch_comparison(), multirange_get_bounds(), multirange_get_union_range(), multirange_lower(), multirange_lower_inc(), multirange_lower_inf(), multirange_range_contains_bsearch_comparison(), multirange_range_overlaps_bsearch_comparison(), multirange_upper(), multirange_upper_inc(), multirange_upper_inf(), multirangesel(), PageAddItemExtended(), range_constructor2(), range_constructor3(), range_contains_elem_internal(), range_deserialize(), range_gist_double_sorting_split(), range_in(), range_lower(), range_out(), range_recv(), range_send(), range_serialize(), range_upper(), rangesel(), rbound_bsearch(), rbound_bsearch(), spg_range_quad_inner_consistent(), test_bms_add_range(), test_random_operations(), trim_array(), XLogRecordAssemble(), and XLogSaveBufferForHint().