PostgreSQL Source Code  git master
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 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 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 919 of file oracle_compat.c.

References Assert, encoding, ereport, errcode(), errmsg(), ERROR, 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().

920 {
921  text *string = PG_GETARG_TEXT_PP(0);
923  unsigned char *data;
924 
925  if (VARSIZE_ANY_EXHDR(string) <= 0)
926  PG_RETURN_INT32(0);
927 
928  data = (unsigned char *) VARDATA_ANY(string);
929 
930  if (encoding == PG_UTF8 && *data > 127)
931  {
932  /* return the code point for Unicode */
933 
934  int result = 0,
935  tbytes = 0,
936  i;
937 
938  if (*data >= 0xF0)
939  {
940  result = *data & 0x07;
941  tbytes = 3;
942  }
943  else if (*data >= 0xE0)
944  {
945  result = *data & 0x0F;
946  tbytes = 2;
947  }
948  else
949  {
950  Assert(*data > 0xC0);
951  result = *data & 0x1f;
952  tbytes = 1;
953  }
954 
955  Assert(tbytes > 0);
956 
957  for (i = 1; i <= tbytes; i++)
958  {
959  Assert((data[i] & 0xC0) == 0x80);
960  result = (result << 6) + (data[i] & 0x3f);
961  }
962 
963  PG_RETURN_INT32(result);
964  }
965  else
966  {
967  if (pg_encoding_max_length(encoding) > 1 && *data > 127)
968  ereport(ERROR,
969  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
970  errmsg("requested character too large")));
971 
972 
973  PG_RETURN_INT32((int32) *data);
974  }
975 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
int pg_encoding_max_length(int encoding)
Definition: wchar.c:1972
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
int32 encoding
Definition: pg_database.h:41
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
Definition: c.h:621

◆ btrim()

Datum btrim ( PG_FUNCTION_ARGS  )

Definition at line 338 of file oracle_compat.c.

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

339 {
340  text *string = PG_GETARG_TEXT_PP(0);
341  text *set = PG_GETARG_TEXT_PP(1);
342  text *ret;
343 
344  ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
345  VARDATA_ANY(set), VARSIZE_ANY_EXHDR(set),
346  true, true);
347 
348  PG_RETURN_TEXT_P(ret);
349 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
static text * dotrim(const char *string, int stringlen, const char *set, int setlen, bool doltrim, bool dortrim)
Definition: c.h:621

◆ btrim1()

Datum btrim1 ( PG_FUNCTION_ARGS  )

Definition at line 358 of file oracle_compat.c.

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

359 {
360  text *string = PG_GETARG_TEXT_PP(0);
361  text *ret;
362 
363  ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
364  " ", 1,
365  true, true);
366 
367  PG_RETURN_TEXT_P(ret);
368 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
static text * dotrim(const char *string, int stringlen, const char *set, int setlen, bool doltrim, bool dortrim)
Definition: c.h:621

◆ bytealtrim()

Datum bytealtrim ( PG_FUNCTION_ARGS  )

Definition at line 640 of file oracle_compat.c.

References dobyteatrim(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

641 {
642  bytea *string = PG_GETARG_BYTEA_PP(0);
643  bytea *set = PG_GETARG_BYTEA_PP(1);
644  bytea *ret;
645 
646  ret = dobyteatrim(string, set, true, false);
647 
648  PG_RETURN_BYTEA_P(ret);
649 }
static bytea * dobyteatrim(bytea *string, bytea *set, bool doltrim, bool dortrim)
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
Definition: c.h:621

◆ byteartrim()

Datum byteartrim ( PG_FUNCTION_ARGS  )

Definition at line 667 of file oracle_compat.c.

References dobyteatrim(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

668 {
669  bytea *string = PG_GETARG_BYTEA_PP(0);
670  bytea *set = PG_GETARG_BYTEA_PP(1);
671  bytea *ret;
672 
673  ret = dobyteatrim(string, set, false, true);
674 
675  PG_RETURN_BYTEA_P(ret);
676 }
static bytea * dobyteatrim(bytea *string, bytea *set, bool doltrim, bool dortrim)
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
Definition: c.h:621

◆ byteatrim()

Datum byteatrim ( PG_FUNCTION_ARGS  )

Definition at line 613 of file oracle_compat.c.

References dobyteatrim(), PG_GETARG_BYTEA_PP, and PG_RETURN_BYTEA_P.

614 {
615  bytea *string = PG_GETARG_BYTEA_PP(0);
616  bytea *set = PG_GETARG_BYTEA_PP(1);
617  bytea *ret;
618 
619  ret = dobyteatrim(string, set, true, true);
620 
621  PG_RETURN_BYTEA_P(ret);
622 }
static bytea * dobyteatrim(bytea *string, bytea *set, bool doltrim, bool dortrim)
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
Definition: c.h:621

◆ chr()

Definition at line 1000 of file oracle_compat.c.

References generate_unaccent_rules::bytes(), encoding, ereport, errcode(), errmsg(), ERROR, GetDatabaseEncoding(), palloc(), pg_encoding_max_length(), PG_GETARG_UINT32, PG_RETURN_TEXT_P, PG_UTF8, pg_utf8_islegal(), SET_VARSIZE, VARDATA, and VARHDRSZ.

1001 {
1002  uint32 cvalue = PG_GETARG_UINT32(0);
1003  text *result;
1004  int encoding = GetDatabaseEncoding();
1005 
1006  if (encoding == PG_UTF8 && cvalue > 127)
1007  {
1008  /* for Unicode we treat the argument as a code point */
1009  int bytes;
1010  unsigned char *wch;
1011 
1012  /*
1013  * We only allow valid Unicode code points; per RFC3629 that stops at
1014  * U+10FFFF, even though 4-byte UTF8 sequences can hold values up to
1015  * U+1FFFFF.
1016  */
1017  if (cvalue > 0x0010ffff)
1018  ereport(ERROR,
1019  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1020  errmsg("requested character too large for encoding: %d",
1021  cvalue)));
1022 
1023  if (cvalue > 0xffff)
1024  bytes = 4;
1025  else if (cvalue > 0x07ff)
1026  bytes = 3;
1027  else
1028  bytes = 2;
1029 
1030  result = (text *) palloc(VARHDRSZ + bytes);
1031  SET_VARSIZE(result, VARHDRSZ + bytes);
1032  wch = (unsigned char *) VARDATA(result);
1033 
1034  if (bytes == 2)
1035  {
1036  wch[0] = 0xC0 | ((cvalue >> 6) & 0x1F);
1037  wch[1] = 0x80 | (cvalue & 0x3F);
1038  }
1039  else if (bytes == 3)
1040  {
1041  wch[0] = 0xE0 | ((cvalue >> 12) & 0x0F);
1042  wch[1] = 0x80 | ((cvalue >> 6) & 0x3F);
1043  wch[2] = 0x80 | (cvalue & 0x3F);
1044  }
1045  else
1046  {
1047  wch[0] = 0xF0 | ((cvalue >> 18) & 0x07);
1048  wch[1] = 0x80 | ((cvalue >> 12) & 0x3F);
1049  wch[2] = 0x80 | ((cvalue >> 6) & 0x3F);
1050  wch[3] = 0x80 | (cvalue & 0x3F);
1051  }
1052 
1053  /*
1054  * The preceding range check isn't sufficient, because UTF8 excludes
1055  * Unicode "surrogate pair" codes. Make sure what we created is valid
1056  * UTF8.
1057  */
1058  if (!pg_utf8_islegal(wch, bytes))
1059  ereport(ERROR,
1060  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1061  errmsg("requested character not valid for encoding: %d",
1062  cvalue)));
1063  }
1064  else
1065  {
1066  bool is_mb;
1067 
1068  /*
1069  * Error out on arguments that make no sense or that we can't validly
1070  * represent in the encoding.
1071  */
1072  if (cvalue == 0)
1073  ereport(ERROR,
1074  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1075  errmsg("null character not permitted")));
1076 
1077  is_mb = pg_encoding_max_length(encoding) > 1;
1078 
1079  if ((is_mb && (cvalue > 127)) || (!is_mb && (cvalue > 255)))
1080  ereport(ERROR,
1081  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1082  errmsg("requested character too large for encoding: %d",
1083  cvalue)));
1084 
1085  result = (text *) palloc(VARHDRSZ + 1);
1086  SET_VARSIZE(result, VARHDRSZ + 1);
1087  *VARDATA(result) = (char) cvalue;
1088  }
1089 
1090  PG_RETURN_TEXT_P(result);
1091 }
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:270
#define VARDATA(PTR)
Definition: postgres.h:315
bool pg_utf8_islegal(const unsigned char *source, int length)
Definition: wchar.c:1797
#define VARHDRSZ
Definition: c.h:627
def bytes(source, encoding='ascii', errors='strict')
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
int pg_encoding_max_length(int encoding)
Definition: wchar.c:1972
unsigned int uint32
Definition: c.h:441
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
int32 encoding
Definition: pg_database.h:41
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ dobyteatrim()

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

Definition at line 530 of file oracle_compat.c.

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

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

531 {
532  bytea *ret;
533  char *ptr,
534  *end,
535  *ptr2,
536  *ptr2start,
537  *end2;
538  int m,
539  stringlen,
540  setlen;
541 
542  stringlen = VARSIZE_ANY_EXHDR(string);
543  setlen = VARSIZE_ANY_EXHDR(set);
544 
545  if (stringlen <= 0 || setlen <= 0)
546  return string;
547 
548  m = stringlen;
549  ptr = VARDATA_ANY(string);
550  end = ptr + stringlen - 1;
551  ptr2start = VARDATA_ANY(set);
552  end2 = ptr2start + setlen - 1;
553 
554  if (doltrim)
555  {
556  while (m > 0)
557  {
558  ptr2 = ptr2start;
559  while (ptr2 <= end2)
560  {
561  if (*ptr == *ptr2)
562  break;
563  ++ptr2;
564  }
565  if (ptr2 > end2)
566  break;
567  ptr++;
568  m--;
569  }
570  }
571 
572  if (dortrim)
573  {
574  while (m > 0)
575  {
576  ptr2 = ptr2start;
577  while (ptr2 <= end2)
578  {
579  if (*end == *ptr2)
580  break;
581  ++ptr2;
582  }
583  if (ptr2 > end2)
584  break;
585  end--;
586  m--;
587  }
588  }
589 
590  ret = (bytea *) palloc(VARHDRSZ + m);
591  SET_VARSIZE(ret, VARHDRSZ + m);
592  memcpy(VARDATA(ret), ptr, m);
593  return ret;
594 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
char string[11]
Definition: preproc-type.c:46
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ dotrim()

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

Definition at line 374 of file oracle_compat.c.

References cstring_to_text_with_len(), i, palloc(), pfree(), pg_database_encoding_max_length(), and pg_mblen().

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

377 {
378  int i;
379 
380  /* Nothing to do if either string or set is empty */
381  if (stringlen > 0 && setlen > 0)
382  {
384  {
385  /*
386  * In the multibyte-encoding case, build arrays of pointers to
387  * character starts, so that we can avoid inefficient checks in
388  * the inner loops.
389  */
390  const char **stringchars;
391  const char **setchars;
392  int *stringmblen;
393  int *setmblen;
394  int stringnchars;
395  int setnchars;
396  int resultndx;
397  int resultnchars;
398  const char *p;
399  int len;
400  int mblen;
401  const char *str_pos;
402  int str_len;
403 
404  stringchars = (const char **) palloc(stringlen * sizeof(char *));
405  stringmblen = (int *) palloc(stringlen * sizeof(int));
406  stringnchars = 0;
407  p = string;
408  len = stringlen;
409  while (len > 0)
410  {
411  stringchars[stringnchars] = p;
412  stringmblen[stringnchars] = mblen = pg_mblen(p);
413  stringnchars++;
414  p += mblen;
415  len -= mblen;
416  }
417 
418  setchars = (const char **) palloc(setlen * sizeof(char *));
419  setmblen = (int *) palloc(setlen * sizeof(int));
420  setnchars = 0;
421  p = set;
422  len = setlen;
423  while (len > 0)
424  {
425  setchars[setnchars] = p;
426  setmblen[setnchars] = mblen = pg_mblen(p);
427  setnchars++;
428  p += mblen;
429  len -= mblen;
430  }
431 
432  resultndx = 0; /* index in stringchars[] */
433  resultnchars = stringnchars;
434 
435  if (doltrim)
436  {
437  while (resultnchars > 0)
438  {
439  str_pos = stringchars[resultndx];
440  str_len = stringmblen[resultndx];
441  for (i = 0; i < setnchars; i++)
442  {
443  if (str_len == setmblen[i] &&
444  memcmp(str_pos, setchars[i], str_len) == 0)
445  break;
446  }
447  if (i >= setnchars)
448  break; /* no match here */
449  string += str_len;
450  stringlen -= str_len;
451  resultndx++;
452  resultnchars--;
453  }
454  }
455 
456  if (dortrim)
457  {
458  while (resultnchars > 0)
459  {
460  str_pos = stringchars[resultndx + resultnchars - 1];
461  str_len = stringmblen[resultndx + resultnchars - 1];
462  for (i = 0; i < setnchars; i++)
463  {
464  if (str_len == setmblen[i] &&
465  memcmp(str_pos, setchars[i], str_len) == 0)
466  break;
467  }
468  if (i >= setnchars)
469  break; /* no match here */
470  stringlen -= str_len;
471  resultnchars--;
472  }
473  }
474 
475  pfree(stringchars);
476  pfree(stringmblen);
477  pfree(setchars);
478  pfree(setmblen);
479  }
480  else
481  {
482  /*
483  * In the single-byte-encoding case, we don't need such overhead.
484  */
485  if (doltrim)
486  {
487  while (stringlen > 0)
488  {
489  char str_ch = *string;
490 
491  for (i = 0; i < setlen; i++)
492  {
493  if (str_ch == set[i])
494  break;
495  }
496  if (i >= setlen)
497  break; /* no match here */
498  string++;
499  stringlen--;
500  }
501  }
502 
503  if (dortrim)
504  {
505  while (stringlen > 0)
506  {
507  char str_ch = string[stringlen - 1];
508 
509  for (i = 0; i < setlen; i++)
510  {
511  if (str_ch == set[i])
512  break;
513  }
514  if (i >= setlen)
515  break; /* no match here */
516  stringlen--;
517  }
518  }
519  }
520  }
521 
522  /* Return selected portion of string */
523  return cstring_to_text_with_len(string, stringlen);
524 }
void pfree(void *pointer)
Definition: mcxt.c:1169
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:201
char string[11]
Definition: preproc-type.c:46
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1495
void * palloc(Size size)
Definition: mcxt.c:1062
int i

◆ initcap()

Datum initcap ( PG_FUNCTION_ARGS  )

Definition at line 111 of file oracle_compat.c.

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

112 {
113  text *in_string = PG_GETARG_TEXT_PP(0);
114  char *out_string;
115  text *result;
116 
117  out_string = str_initcap(VARDATA_ANY(in_string),
118  VARSIZE_ANY_EXHDR(in_string),
119  PG_GET_COLLATION());
120  result = cstring_to_text(out_string);
121  pfree(out_string);
122 
123  PG_RETURN_TEXT_P(result);
124 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
char * str_initcap(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1886
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
void pfree(void *pointer)
Definition: mcxt.c:1169
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:189
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621

◆ lower()

Datum lower ( PG_FUNCTION_ARGS  )

Definition at line 46 of file oracle_compat.c.

References cstring_to_text(), 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(), check_new_partition_bound(), compute_range_stats(), create_range_bounds(), daterange_canonical(), enum_range_bounds(), Generic_Text_IC_like(), getQuadrant(), gist_box_picksplit(), hash_multirange(), hash_multirange_extended(), hash_range(), hash_range_extended(), hyphenate(), int4range_canonical(), int8range_canonical(), iterate_word_similarity(), length_hist_bsearch(), make_empty_range(), make_one_partition_rbound(), multirange_bsearch_match(), multirange_get_union_range(), multirange_lower(), multirange_lower_inc(), multirange_lower_inf(), multirange_upper(), multirange_upper_inc(), multirange_upper_inf(), multirangesel(), PageAddItemExtended(), range_constructor2(), range_constructor3(), range_contains_elem_internal(), range_gist_double_sorting_split(), range_in(), range_lower(), range_out(), range_recv(), range_send(), range_upper(), rangesel(), rbound_bsearch(), spg_range_quad_inner_consistent(), trim_array(), XLogRecordAssemble(), and XLogSaveBufferForHint().

47 {
48  text *in_string = PG_GETARG_TEXT_PP(0);
49  char *out_string;
50  text *result;
51 
52  out_string = str_tolower(VARDATA_ANY(in_string),
53  VARSIZE_ANY_EXHDR(in_string),
55  result = cstring_to_text(out_string);
56  pfree(out_string);
57 
58  PG_RETURN_TEXT_P(result);
59 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1638
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
void pfree(void *pointer)
Definition: mcxt.c:1169
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:189
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621

◆ lpad()

Datum lpad ( PG_FUNCTION_ARGS  )

Definition at line 144 of file oracle_compat.c.

References ereport, errcode(), errmsg(), ERROR, palloc(), pg_database_encoding_max_length(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, pg_mblen(), pg_mbstrlen_with_len(), PG_RETURN_TEXT_P, SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

145 {
146  text *string1 = PG_GETARG_TEXT_PP(0);
147  int32 len = PG_GETARG_INT32(1);
148  text *string2 = PG_GETARG_TEXT_PP(2);
149  text *ret;
150  char *ptr1,
151  *ptr2,
152  *ptr2start,
153  *ptr2end,
154  *ptr_ret;
155  int m,
156  s1len,
157  s2len;
158 
159  int bytelen;
160 
161  /* Negative len is silently taken as zero */
162  if (len < 0)
163  len = 0;
164 
165  s1len = VARSIZE_ANY_EXHDR(string1);
166  if (s1len < 0)
167  s1len = 0; /* shouldn't happen */
168 
169  s2len = VARSIZE_ANY_EXHDR(string2);
170  if (s2len < 0)
171  s2len = 0; /* shouldn't happen */
172 
173  s1len = pg_mbstrlen_with_len(VARDATA_ANY(string1), s1len);
174 
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 */
180 
181  bytelen = pg_database_encoding_max_length() * len;
182 
183  /* check for integer overflow */
184  if (len != 0 && bytelen / pg_database_encoding_max_length() != len)
185  ereport(ERROR,
186  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
187  errmsg("requested length too large")));
188 
189  ret = (text *) palloc(VARHDRSZ + bytelen);
190 
191  m = len - s1len;
192 
193  ptr2 = ptr2start = VARDATA_ANY(string2);
194  ptr2end = ptr2 + s2len;
195  ptr_ret = VARDATA(ret);
196 
197  while (m--)
198  {
199  int mlen = pg_mblen(ptr2);
200 
201  memcpy(ptr_ret, ptr2, mlen);
202  ptr_ret += mlen;
203  ptr2 += mlen;
204  if (ptr2 == ptr2end) /* wrap around at end of s2 */
205  ptr2 = ptr2start;
206  }
207 
208  ptr1 = VARDATA_ANY(string1);
209 
210  while (s1len--)
211  {
212  int mlen = pg_mblen(ptr1);
213 
214  memcpy(ptr_ret, ptr1, mlen);
215  ptr_ret += mlen;
216  ptr1 += mlen;
217  }
218 
219  SET_VARSIZE(ret, ptr_ret - (char *) ret);
220 
221  PG_RETURN_TEXT_P(ret);
222 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1000
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1495
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ ltrim()

Datum ltrim ( PG_FUNCTION_ARGS  )

Definition at line 694 of file oracle_compat.c.

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

695 {
696  text *string = PG_GETARG_TEXT_PP(0);
697  text *set = PG_GETARG_TEXT_PP(1);
698  text *ret;
699 
700  ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
701  VARDATA_ANY(set), VARSIZE_ANY_EXHDR(set),
702  true, false);
703 
704  PG_RETURN_TEXT_P(ret);
705 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
static text * dotrim(const char *string, int stringlen, const char *set, int setlen, bool doltrim, bool dortrim)
Definition: c.h:621

◆ ltrim1()

Datum ltrim1 ( PG_FUNCTION_ARGS  )

Definition at line 714 of file oracle_compat.c.

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

715 {
716  text *string = PG_GETARG_TEXT_PP(0);
717  text *ret;
718 
719  ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
720  " ", 1,
721  true, false);
722 
723  PG_RETURN_TEXT_P(ret);
724 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
static text * dotrim(const char *string, int stringlen, const char *set, int setlen, bool doltrim, bool dortrim)
Definition: c.h:621

◆ repeat()

Datum repeat ( PG_FUNCTION_ARGS  )

Definition at line 1108 of file oracle_compat.c.

References CHECK_FOR_INTERRUPTS, ereport, errcode(), errmsg(), ERROR, 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.

1109 {
1110  text *string = PG_GETARG_TEXT_PP(0);
1111  int32 count = PG_GETARG_INT32(1);
1112  text *result;
1113  int slen,
1114  tlen;
1115  int i;
1116  char *cp,
1117  *sp;
1118 
1119  if (count < 0)
1120  count = 0;
1121 
1122  slen = VARSIZE_ANY_EXHDR(string);
1123 
1124  if (unlikely(pg_mul_s32_overflow(count, slen, &tlen)) ||
1125  unlikely(pg_add_s32_overflow(tlen, VARHDRSZ, &tlen)))
1126  ereport(ERROR,
1127  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1128  errmsg("requested length too large")));
1129 
1130  result = (text *) palloc(tlen);
1131 
1132  SET_VARSIZE(result, tlen);
1133  cp = VARDATA(result);
1134  sp = VARDATA_ANY(string);
1135  for (i = 0; i < count; i++)
1136  {
1137  memcpy(cp, sp, slen);
1138  cp += slen;
1140  }
1141 
1142  PG_RETURN_TEXT_P(result);
1143 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARDATA(PTR)
Definition: postgres.h:315
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140
#define VARHDRSZ
Definition: c.h:627
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
#define unlikely(x)
Definition: c.h:273
Definition: c.h:621
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ rpad()

Datum rpad ( PG_FUNCTION_ARGS  )

Definition at line 242 of file oracle_compat.c.

References ereport, errcode(), errmsg(), ERROR, palloc(), pg_database_encoding_max_length(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, pg_mblen(), pg_mbstrlen_with_len(), PG_RETURN_TEXT_P, SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

243 {
244  text *string1 = PG_GETARG_TEXT_PP(0);
245  int32 len = PG_GETARG_INT32(1);
246  text *string2 = PG_GETARG_TEXT_PP(2);
247  text *ret;
248  char *ptr1,
249  *ptr2,
250  *ptr2start,
251  *ptr2end,
252  *ptr_ret;
253  int m,
254  s1len,
255  s2len;
256 
257  int bytelen;
258 
259  /* Negative len is silently taken as zero */
260  if (len < 0)
261  len = 0;
262 
263  s1len = VARSIZE_ANY_EXHDR(string1);
264  if (s1len < 0)
265  s1len = 0; /* shouldn't happen */
266 
267  s2len = VARSIZE_ANY_EXHDR(string2);
268  if (s2len < 0)
269  s2len = 0; /* shouldn't happen */
270 
271  s1len = pg_mbstrlen_with_len(VARDATA_ANY(string1), s1len);
272 
273  if (s1len > len)
274  s1len = len; /* truncate string1 to len chars */
275 
276  if (s2len <= 0)
277  len = s1len; /* nothing to pad with, so don't pad */
278 
279  bytelen = pg_database_encoding_max_length() * len;
280 
281  /* Check for integer overflow */
282  if (len != 0 && bytelen / pg_database_encoding_max_length() != len)
283  ereport(ERROR,
284  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
285  errmsg("requested length too large")));
286 
287  ret = (text *) palloc(VARHDRSZ + bytelen);
288  m = len - s1len;
289 
290  ptr1 = VARDATA_ANY(string1);
291  ptr_ret = VARDATA(ret);
292 
293  while (s1len--)
294  {
295  int mlen = pg_mblen(ptr1);
296 
297  memcpy(ptr_ret, ptr1, mlen);
298  ptr_ret += mlen;
299  ptr1 += mlen;
300  }
301 
302  ptr2 = ptr2start = VARDATA_ANY(string2);
303  ptr2end = ptr2 + s2len;
304 
305  while (m--)
306  {
307  int mlen = pg_mblen(ptr2);
308 
309  memcpy(ptr_ret, ptr2, mlen);
310  ptr_ret += mlen;
311  ptr2 += mlen;
312  if (ptr2 == ptr2end) /* wrap around at end of s2 */
313  ptr2 = ptr2start;
314  }
315 
316  SET_VARSIZE(ret, ptr_ret - (char *) ret);
317 
318  PG_RETURN_TEXT_P(ret);
319 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1000
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1495
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ rtrim()

Datum rtrim ( PG_FUNCTION_ARGS  )

Definition at line 742 of file oracle_compat.c.

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

743 {
744  text *string = PG_GETARG_TEXT_PP(0);
745  text *set = PG_GETARG_TEXT_PP(1);
746  text *ret;
747 
748  ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
749  VARDATA_ANY(set), VARSIZE_ANY_EXHDR(set),
750  false, true);
751 
752  PG_RETURN_TEXT_P(ret);
753 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
static text * dotrim(const char *string, int stringlen, const char *set, int setlen, bool doltrim, bool dortrim)
Definition: c.h:621

◆ rtrim1()

Datum rtrim1 ( PG_FUNCTION_ARGS  )

Definition at line 762 of file oracle_compat.c.

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

Referenced by gbt_bpchar_compress(), and gbt_bpchar_consistent().

763 {
764  text *string = PG_GETARG_TEXT_PP(0);
765  text *ret;
766 
767  ret = dotrim(VARDATA_ANY(string), VARSIZE_ANY_EXHDR(string),
768  " ", 1,
769  false, true);
770 
771  PG_RETURN_TEXT_P(ret);
772 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
static text * dotrim(const char *string, int stringlen, const char *set, int setlen, bool doltrim, bool dortrim)
Definition: c.h:621

◆ translate()

Datum translate ( PG_FUNCTION_ARGS  )

Definition at line 793 of file oracle_compat.c.

References ereport, errcode(), errmsg(), ERROR, i, palloc(), pg_database_encoding_max_length(), PG_GETARG_TEXT_PP, pg_mblen(), PG_RETURN_TEXT_P, SET_VARSIZE, source, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by printQuery().

794 {
795  text *string = PG_GETARG_TEXT_PP(0);
796  text *from = PG_GETARG_TEXT_PP(1);
797  text *to = PG_GETARG_TEXT_PP(2);
798  text *result;
799  char *from_ptr,
800  *to_ptr;
801  char *source,
802  *target;
803  int m,
804  fromlen,
805  tolen,
806  retlen,
807  i;
808  int worst_len;
809  int len;
810  int source_len;
811  int from_index;
812 
813  m = VARSIZE_ANY_EXHDR(string);
814  if (m <= 0)
815  PG_RETURN_TEXT_P(string);
816  source = VARDATA_ANY(string);
817 
818  fromlen = VARSIZE_ANY_EXHDR(from);
819  from_ptr = VARDATA_ANY(from);
820  tolen = VARSIZE_ANY_EXHDR(to);
821  to_ptr = VARDATA_ANY(to);
822 
823  /*
824  * The worst-case expansion is to substitute a max-length character for a
825  * single-byte character at each position of the string.
826  */
827  worst_len = pg_database_encoding_max_length() * m;
828 
829  /* check for integer overflow */
830  if (worst_len / pg_database_encoding_max_length() != m)
831  ereport(ERROR,
832  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
833  errmsg("requested length too large")));
834 
835  result = (text *) palloc(worst_len + VARHDRSZ);
836  target = VARDATA(result);
837  retlen = 0;
838 
839  while (m > 0)
840  {
841  source_len = pg_mblen(source);
842  from_index = 0;
843 
844  for (i = 0; i < fromlen; i += len)
845  {
846  len = pg_mblen(&from_ptr[i]);
847  if (len == source_len &&
848  memcmp(source, &from_ptr[i], len) == 0)
849  break;
850 
851  from_index++;
852  }
853  if (i < fromlen)
854  {
855  /* substitute */
856  char *p = to_ptr;
857 
858  for (i = 0; i < from_index; i++)
859  {
860  p += pg_mblen(p);
861  if (p >= (to_ptr + tolen))
862  break;
863  }
864  if (p < (to_ptr + tolen))
865  {
866  len = pg_mblen(p);
867  memcpy(target, p, len);
868  target += len;
869  retlen += len;
870  }
871 
872  }
873  else
874  {
875  /* no match, so copy */
876  memcpy(target, source, source_len);
877  target += source_len;
878  retlen += source_len;
879  }
880 
881  source += source_len;
882  m -= source_len;
883  }
884 
885  SET_VARSIZE(result, retlen + VARHDRSZ);
886 
887  /*
888  * The function result is probably much bigger than needed, if we're using
889  * a multibyte encoding, but it's not worth reallocating it; the result
890  * probably won't live long anyway.
891  */
892 
893  PG_RETURN_TEXT_P(result);
894 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
static rewind_source * source
Definition: pg_rewind.c:79
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1495
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ upper()

Datum upper ( PG_FUNCTION_ARGS  )

Definition at line 77 of file oracle_compat.c.

References cstring_to_text(), 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(), check_new_partition_bound(), compactify_tuples(), compute_range_stats(), create_range_bounds(), cube_coord_llur(), daterange_canonical(), enum_range_bounds(), g_cube_distance(), getQuadrant(), gist_box_picksplit(), hash_multirange(), hash_multirange_extended(), hash_range(), hash_range_extended(), hyphenate(), int4range_canonical(), int8range_canonical(), iterate_word_similarity(), length_hist_bsearch(), make_empty_range(), multirange_bsearch_match(), multirange_get_union_range(), multirange_lower(), multirange_lower_inc(), multirange_lower_inf(), multirange_upper(), multirange_upper_inc(), multirange_upper_inf(), multirangesel(), PageAddItemExtended(), range_constructor2(), range_constructor3(), range_contains_elem_internal(), range_gist_double_sorting_split(), range_in(), range_lower(), range_out(), range_recv(), range_send(), range_upper(), rangesel(), rbound_bsearch(), spg_range_quad_inner_consistent(), trim_array(), XLogRecordAssemble(), and XLogSaveBufferForHint().

78 {
79  text *in_string = PG_GETARG_TEXT_PP(0);
80  char *out_string;
81  text *result;
82 
83  out_string = str_toupper(VARDATA_ANY(in_string),
84  VARSIZE_ANY_EXHDR(in_string),
86  result = cstring_to_text(out_string);
87  pfree(out_string);
88 
89  PG_RETURN_TEXT_P(result);
90 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
void pfree(void *pointer)
Definition: mcxt.c:1169
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:189
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
char * str_toupper(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1762
Definition: c.h:621