PostgreSQL Source Code  git master
string.c File Reference
#include "postgres.h"
#include "common/string.h"
#include "lib/stringinfo.h"
Include dependency graph for string.c:

Go to the source code of this file.

Functions

bool pg_str_endswith (const char *str, const char *end)
 
int strtoint (const char *pg_restrict str, char **pg_restrict endptr, int base)
 
char * pg_clean_ascii (const char *str, int alloc_flags)
 
bool pg_is_ascii (const char *str)
 
int pg_strip_crlf (char *str)
 

Function Documentation

◆ pg_clean_ascii()

char* pg_clean_ascii ( const char *  str,
int  alloc_flags 
)

Definition at line 86 of file string.c.

87 {
88  size_t dstlen;
89  char *dst;
90  const char *p;
91  size_t i = 0;
92 
93  /* Worst case, each byte can become four bytes, plus a null terminator. */
94  dstlen = strlen(str) * 4 + 1;
95 
96 #ifdef FRONTEND
97  dst = malloc(dstlen);
98 #else
99  dst = palloc_extended(dstlen, alloc_flags);
100 #endif
101 
102  if (!dst)
103  return NULL;
104 
105  for (p = str; *p != '\0'; p++)
106  {
107 
108  /* Only allow clean ASCII chars in the string */
109  if (*p < 32 || *p > 126)
110  {
111  Assert(i < (dstlen - 3));
112  snprintf(&dst[i], dstlen - i, "\\x%02x", (unsigned char) *p);
113  i += 4;
114  }
115  else
116  {
117  Assert(i < dstlen);
118  dst[i] = *p;
119  i++;
120  }
121  }
122 
123  Assert(i < dstlen);
124  dst[i] = '\0';
125  return dst;
126 }
#define Assert(condition)
Definition: c.h:861
const char * str
#define malloc(a)
Definition: header.h:50
int i
Definition: isn.c:73
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1368
#define snprintf
Definition: port.h:238

References Assert, i, malloc, palloc_extended(), snprintf, and str.

Referenced by check_application_name(), check_cluster_name(), prepare_cert_name(), and ProcessStartupPacket().

◆ pg_is_ascii()

bool pg_is_ascii ( const char *  str)

Definition at line 133 of file string.c.

134 {
135  while (*str)
136  {
137  if (IS_HIGHBIT_SET(*str))
138  return false;
139  str++;
140  }
141  return true;
142 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1158

References IS_HIGHBIT_SET, and str.

Referenced by check_locale(), check_locale_name(), parse_key_value_arrays(), pg_import_system_collations(), and pg_saslprep().

◆ pg_str_endswith()

bool pg_str_endswith ( const char *  str,
const char *  end 
)

Definition at line 32 of file string.c.

33 {
34  size_t slen = strlen(str);
35  size_t elen = strlen(end);
36 
37  /* can't be a postfix if longer */
38  if (elen > slen)
39  return false;
40 
41  /* compare the end of the strings */
42  str += slen - elen;
43  return strcmp(str, end) == 0;
44 }

References str.

Referenced by decide_file_action(), and StartupReplicationSlots().

◆ pg_strip_crlf()

int pg_strip_crlf ( char *  str)

Definition at line 155 of file string.c.

156 {
157  int len = strlen(str);
158 
159  while (len > 0 && (str[len - 1] == '\n' ||
160  str[len - 1] == '\r'))
161  str[--len] = '\0';
162 
163  return len;
164 }
const void size_t len

References len, and str.

Referenced by adjust_data_dir(), check_exec(), CheckDataVersion(), get_control_data(), get_prompt(), get_sock_dir(), get_su_pwd(), getRestoreCommand(), passwordFromFile(), run_ssl_passphrase_command(), simple_prompt_extended(), and tokenize_auth_file().

◆ strtoint()

int strtoint ( const char *pg_restrict  str,
char **pg_restrict  endptr,
int  base 
)

Definition at line 51 of file string.c.

52 {
53  long val;
54 
55  val = strtol(str, endptr, base);
56  if (val != (int) val)
57  errno = ERANGE;
58  return (int) val;
59 }
long val
Definition: informix.c:689

References str, and val.

Referenced by buildDefItem(), DecodeDateTime(), DecodeInterval(), DecodeNumber(), DecodeNumberField(), DecodeTime(), DecodeTimeCommon(), DecodeTimeOnly(), DecodeTimezone(), exec_command_watch(), get_path_all(), jsonb_get_element(), nodeTokenType(), option_parse_int(), push_path(), and setPathArray().