PostgreSQL Source Code  git master
ts_locale.h File Reference
#include <ctype.h>
#include <limits.h>
#include <wctype.h>
#include "lib/stringinfo.h"
#include "mb/pg_wchar.h"
#include "utils/pg_locale.h"
Include dependency graph for ts_locale.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  tsearch_readline_state
 

Macros

#define TOUCHAR(x)   (*((const unsigned char *) (x)))
 
#define t_iseq(x, c)   (TOUCHAR(x) == (unsigned char) (c))
 
#define COPYCHAR(d, s)   memcpy(d, s, pg_mblen(s))
 

Functions

int t_isdigit (const char *ptr)
 
int t_isspace (const char *ptr)
 
int t_isalpha (const char *ptr)
 
int t_isalnum (const char *ptr)
 
int t_isprint (const char *ptr)
 
char * lowerstr (const char *str)
 
char * lowerstr_with_len (const char *str, int len)
 
bool tsearch_readline_begin (tsearch_readline_state *stp, const char *filename)
 
char * tsearch_readline (tsearch_readline_state *stp)
 
void tsearch_readline_end (tsearch_readline_state *stp)
 

Macro Definition Documentation

◆ COPYCHAR

#define COPYCHAR (   d,
 
)    memcpy(d, s, pg_mblen(s))

Definition at line 40 of file ts_locale.h.

◆ t_iseq

#define t_iseq (   x,
  c 
)    (TOUCHAR(x) == (unsigned char) (c))

Definition at line 38 of file ts_locale.h.

◆ TOUCHAR

#define TOUCHAR (   x)    (*((const unsigned char *) (x)))

Definition at line 35 of file ts_locale.h.

Function Documentation

◆ lowerstr()

char* lowerstr ( const char *  str)

Definition at line 253 of file ts_locale.c.

254 {
255  return lowerstr_with_len(str, strlen(str));
256 }
const char * str
char * lowerstr_with_len(const char *str, int len)
Definition: ts_locale.c:266

References lowerstr_with_len(), and str.

Referenced by convertPgWchar(), dispell_init(), dsimple_init(), dsnowball_init(), dsynonym_init(), dxsyn_lexize(), lowerstr_ctx(), NIImportAffixes(), and read_dictionary().

◆ lowerstr_with_len()

char* lowerstr_with_len ( const char *  str,
int  len 
)

Definition at line 266 of file ts_locale.c.

267 {
268  char *out;
269  pg_locale_t mylocale = 0; /* TODO */
270 
271  if (len == 0)
272  return pstrdup("");
273 
274  /*
275  * Use wide char code only when max encoding length > 1 and ctype != C.
276  * Some operating systems fail with multi-byte encodings and a C locale.
277  * Also, for a C locale there is no need to process as multibyte. From
278  * backend/utils/adt/oracle_compat.c Teodor
279  */
281  {
282  wchar_t *wstr,
283  *wptr;
284  int wlen;
285 
286  /*
287  * alloc number of wchar_t for worst case, len contains number of
288  * bytes >= number of characters and alloc 1 wchar_t for 0, because
289  * wchar2char wants zero-terminated string
290  */
291  wptr = wstr = (wchar_t *) palloc(sizeof(wchar_t) * (len + 1));
292 
293  wlen = char2wchar(wstr, len + 1, str, len, mylocale);
294  Assert(wlen <= len);
295 
296  while (*wptr)
297  {
298  *wptr = towlower((wint_t) *wptr);
299  wptr++;
300  }
301 
302  /*
303  * Alloc result string for worst case + '\0'
304  */
305  len = pg_database_encoding_max_length() * wlen + 1;
306  out = (char *) palloc(len);
307 
308  wlen = wchar2char(out, wstr, len, mylocale);
309 
310  pfree(wstr);
311 
312  if (wlen < 0)
313  ereport(ERROR,
314  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
315  errmsg("conversion from wchar_t to server encoding failed: %m")));
316  Assert(wlen < len);
317  }
318  else
319  {
320  const char *ptr = str;
321  char *outptr;
322 
323  outptr = out = (char *) palloc(sizeof(char) * (len + 1));
324  while ((ptr - str) < len && *ptr)
325  {
326  *outptr++ = tolower(TOUCHAR(ptr));
327  ptr++;
328  }
329  *outptr = '\0';
330  }
331 
332  return out;
333 }
#define Assert(condition)
Definition: c.h:858
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1546
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void pfree(void *pointer)
Definition: mcxt.c:1520
void * palloc(Size size)
Definition: mcxt.c:1316
const void size_t len
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:3065
bool database_ctype_is_c
Definition: pg_locale.c:117
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:3121
#define TOUCHAR(x)
Definition: ts_locale.h:35

References Assert, char2wchar(), database_ctype_is_c, ereport, errcode(), errmsg(), ERROR, len, palloc(), pfree(), pg_database_encoding_max_length(), pstrdup(), str, TOUCHAR, and wchar2char().

Referenced by dispell_lexize(), dsimple_lexize(), dsnowball_lexize(), dsynonym_lexize(), generate_trgm_only(), generate_wildcard_trgm(), and lowerstr().

◆ t_isalnum()

int t_isalnum ( const char *  ptr)

Definition at line 80 of file ts_locale.c.

81 {
82  int clen = pg_mblen(ptr);
83  wchar_t character[WC_BUF_LEN];
84  pg_locale_t mylocale = 0; /* TODO */
85 
86  if (clen == 1 || database_ctype_is_c)
87  return isalnum(TOUCHAR(ptr));
88 
89  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
90 
91  return iswalnum((wint_t) character[0]);
92 }
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1023
#define WC_BUF_LEN
Definition: ts_locale.c:32

References char2wchar(), database_ctype_is_c, pg_mblen(), TOUCHAR, and WC_BUF_LEN.

Referenced by parse_or_operator().

◆ t_isalpha()

int t_isalpha ( const char *  ptr)

Definition at line 65 of file ts_locale.c.

66 {
67  int clen = pg_mblen(ptr);
68  wchar_t character[WC_BUF_LEN];
69  pg_locale_t mylocale = 0; /* TODO */
70 
71  if (clen == 1 || database_ctype_is_c)
72  return isalpha(TOUCHAR(ptr));
73 
74  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
75 
76  return iswalpha((wint_t) character[0]);
77 }

References char2wchar(), database_ctype_is_c, pg_mblen(), TOUCHAR, and WC_BUF_LEN.

Referenced by parse_affentry(), RS_compile(), and RS_isRegis().

◆ t_isdigit()

int t_isdigit ( const char *  ptr)

Definition at line 35 of file ts_locale.c.

36 {
37  int clen = pg_mblen(ptr);
38  wchar_t character[WC_BUF_LEN];
39  pg_locale_t mylocale = 0; /* TODO */
40 
41  if (clen == 1 || database_ctype_is_c)
42  return isdigit(TOUCHAR(ptr));
43 
44  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
45 
46  return iswdigit((wint_t) character[0]);
47 }

References char2wchar(), database_ctype_is_c, pg_mblen(), TOUCHAR, and WC_BUF_LEN.

Referenced by getNextFlagFromString(), gettoken_tsvector(), NISortDictionary(), parse_lquery(), and parse_phrase_operator().

◆ t_isprint()

int t_isprint ( const char *  ptr)

Definition at line 95 of file ts_locale.c.

96 {
97  int clen = pg_mblen(ptr);
98  wchar_t character[WC_BUF_LEN];
99  pg_locale_t mylocale = 0; /* TODO */
100 
101  if (clen == 1 || database_ctype_is_c)
102  return isprint(TOUCHAR(ptr));
103 
104  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
105 
106  return iswprint((wint_t) character[0]);
107 }

References char2wchar(), database_ctype_is_c, pg_mblen(), TOUCHAR, and WC_BUF_LEN.

Referenced by NIImportDictionary().

◆ t_isspace()

int t_isspace ( const char *  ptr)

Definition at line 50 of file ts_locale.c.

51 {
52  int clen = pg_mblen(ptr);
53  wchar_t character[WC_BUF_LEN];
54  pg_locale_t mylocale = 0; /* TODO */
55 
56  if (clen == 1 || database_ctype_is_c)
57  return isspace(TOUCHAR(ptr));
58 
59  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
60 
61  return iswspace((wint_t) character[0]);
62 }

References char2wchar(), database_ctype_is_c, pg_mblen(), TOUCHAR, and WC_BUF_LEN.

Referenced by addCompoundAffixFlagValue(), find_word(), findwrd(), get_nextfield(), getNextFlagFromString(), gettoken_query(), gettoken_query_standard(), gettoken_query_websearch(), gettoken_tsvector(), initTrie(), NIImportAffixes(), NIImportDictionary(), NIImportOOAffixes(), NISortDictionary(), parse_affentry(), parse_or_operator(), readstoplist(), and thesaurusRead().

◆ tsearch_readline()

char* tsearch_readline ( tsearch_readline_state stp)

Definition at line 157 of file ts_locale.c.

158 {
159  char *recoded;
160 
161  /* Advance line number to use in error reports */
162  stp->lineno++;
163 
164  /* Clear curline, it's no longer relevant */
165  if (stp->curline)
166  {
167  if (stp->curline != stp->buf.data)
168  pfree(stp->curline);
169  stp->curline = NULL;
170  }
171 
172  /* Collect next line, if there is one */
173  if (!pg_get_line_buf(stp->fp, &stp->buf))
174  return NULL;
175 
176  /* Validate the input as UTF-8, then convert to DB encoding if needed */
177  recoded = pg_any_to_server(stp->buf.data, stp->buf.len, PG_UTF8);
178 
179  /* Save the correctly-encoded string for possible error reports */
180  stp->curline = recoded; /* might be equal to buf.data */
181 
182  /*
183  * We always return a freshly pstrdup'd string. This is clearly necessary
184  * if pg_any_to_server() returned buf.data, and we need a second copy even
185  * if encoding conversion did occur. The caller is entitled to pfree the
186  * returned string at any time, which would leave curline pointing to
187  * recycled storage, causing problems if an error occurs after that point.
188  * (It's preferable to return the result of pstrdup instead of the output
189  * of pg_any_to_server, because the conversion result tends to be
190  * over-allocated. Since callers might save the result string directly
191  * into a long-lived dictionary structure, we don't want it to be a larger
192  * palloc chunk than necessary. We'll reclaim the conversion result on
193  * the next call.)
194  */
195  return pstrdup(recoded);
196 }
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:676
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:95
@ PG_UTF8
Definition: pg_wchar.h:232
StringInfoData buf
Definition: ts_locale.h:29

References tsearch_readline_state::buf, tsearch_readline_state::curline, StringInfoData::data, tsearch_readline_state::fp, StringInfoData::len, tsearch_readline_state::lineno, pfree(), pg_any_to_server(), pg_get_line_buf(), PG_UTF8, and pstrdup().

Referenced by dsynonym_init(), initTrie(), NIImportAffixes(), NIImportDictionary(), NIImportOOAffixes(), read_dictionary(), readstoplist(), and thesaurusRead().

◆ tsearch_readline_begin()

bool tsearch_readline_begin ( tsearch_readline_state stp,
const char *  filename 
)

Definition at line 134 of file ts_locale.c.

136 {
137  if ((stp->fp = AllocateFile(filename, "r")) == NULL)
138  return false;
139  stp->filename = filename;
140  stp->lineno = 0;
141  initStringInfo(&stp->buf);
142  stp->curline = NULL;
143  /* Setup error traceback support for ereport() */
145  stp->cb.arg = (void *) stp;
147  error_context_stack = &stp->cb;
148  return true;
149 }
ErrorContextCallback * error_context_stack
Definition: elog.c:94
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2583
static char * filename
Definition: pg_dumpall.c:119
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
struct ErrorContextCallback * previous
Definition: elog.h:295
void(* callback)(void *arg)
Definition: elog.h:296
ErrorContextCallback cb
Definition: ts_locale.h:32
const char * filename
Definition: ts_locale.h:27
static void tsearch_readline_callback(void *arg)
Definition: ts_locale.c:225

References AllocateFile(), ErrorContextCallback::arg, tsearch_readline_state::buf, ErrorContextCallback::callback, tsearch_readline_state::cb, tsearch_readline_state::curline, error_context_stack, filename, tsearch_readline_state::filename, tsearch_readline_state::fp, initStringInfo(), tsearch_readline_state::lineno, ErrorContextCallback::previous, and tsearch_readline_callback().

Referenced by dsynonym_init(), initTrie(), NIImportAffixes(), NIImportDictionary(), NIImportOOAffixes(), read_dictionary(), readstoplist(), and thesaurusRead().

◆ tsearch_readline_end()

void tsearch_readline_end ( tsearch_readline_state stp)

Definition at line 202 of file ts_locale.c.

203 {
204  /* Suppress use of curline in any error reported below */
205  if (stp->curline)
206  {
207  if (stp->curline != stp->buf.data)
208  pfree(stp->curline);
209  stp->curline = NULL;
210  }
211 
212  /* Release other resources */
213  pfree(stp->buf.data);
214  FreeFile(stp->fp);
215 
216  /* Pop the error context stack */
218 }
int FreeFile(FILE *file)
Definition: fd.c:2781

References tsearch_readline_state::buf, tsearch_readline_state::cb, tsearch_readline_state::curline, StringInfoData::data, error_context_stack, tsearch_readline_state::fp, FreeFile(), pfree(), and ErrorContextCallback::previous.

Referenced by dsynonym_init(), initTrie(), NIImportAffixes(), NIImportDictionary(), NIImportOOAffixes(), read_dictionary(), readstoplist(), and thesaurusRead().