PostgreSQL Source Code git master
string.c File Reference
#include "postgres.h"
#include "common/string.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 85 of file string.c.

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

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 132 of file string.c.

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

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 31 of file string.c.

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

References str.

Referenced by decide_file_action(), and StartupReplicationSlots().

◆ pg_strip_crlf()

int pg_strip_crlf ( char *  str)

Definition at line 154 of file string.c.

155{
156 int len = strlen(str);
157
158 while (len > 0 && (str[len - 1] == '\n' ||
159 str[len - 1] == '\r'))
160 str[--len] = '\0';
161
162 return len;
163}
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 50 of file string.c.

51{
52 long val;
53
54 val = strtol(str, endptr, base);
55 if (val != (int) val)
56 errno = ERANGE;
57 return (int) val;
58}
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(), process_integer_literal(), push_path(), and setPathArray().