PostgreSQL Source Code  git master
stringinfo_mb.c File Reference
#include "postgres.h"
#include "mb/stringinfo_mb.h"
#include "mb/pg_wchar.h"
Include dependency graph for stringinfo_mb.c:

Go to the source code of this file.

Functions

void appendStringInfoStringQuoted (StringInfo str, const char *s, int maxlen)
 

Function Documentation

◆ appendStringInfoStringQuoted()

void appendStringInfoStringQuoted ( StringInfo  str,
const char *  s,
int  maxlen 
)

Definition at line 34 of file stringinfo_mb.c.

References appendBinaryStringInfoNT(), appendStringInfo(), appendStringInfoCharMacro, Assert, pfree(), pg_mbcliplen(), and pnstrdup().

Referenced by BuildParamLogString(), format_expr_params(), and format_preparedparamsdata().

35 {
36  char *copy = NULL;
37  const char *chunk_search_start,
38  *chunk_copy_start,
39  *chunk_end;
40  int slen;
41  bool ellipsis;
42 
43  Assert(str != NULL);
44 
45  slen = strlen(s);
46  if (maxlen >= 0 && maxlen < slen)
47  {
48  int finallen = pg_mbcliplen(s, slen, maxlen);
49 
50  copy = pnstrdup(s, finallen);
51  chunk_search_start = copy;
52  chunk_copy_start = copy;
53 
54  ellipsis = true;
55  }
56  else
57  {
58  chunk_search_start = s;
59  chunk_copy_start = s;
60 
61  ellipsis = false;
62  }
63 
64  appendStringInfoCharMacro(str, '\'');
65 
66  while ((chunk_end = strchr(chunk_search_start, '\'')) != NULL)
67  {
68  /* copy including the found delimiting ' */
70  chunk_copy_start,
71  chunk_end - chunk_copy_start + 1);
72 
73  /* in order to double it, include this ' into the next chunk as well */
74  chunk_copy_start = chunk_end;
75  chunk_search_start = chunk_end + 1;
76  }
77 
78  /* copy the last chunk and terminate */
79  if (ellipsis)
80  appendStringInfo(str, "%s...'", chunk_copy_start);
81  else
82  appendStringInfo(str, "%s'", chunk_copy_start);
83 
84  if (copy)
85  pfree(copy);
86 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:128
void pfree(void *pointer)
Definition: mcxt.c:1056
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendBinaryStringInfoNT(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:253
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:967
#define Assert(condition)
Definition: c.h:738