PostgreSQL Source Code  git master
ts_locale.h File Reference
#include <ctype.h>
#include <limits.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_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)
 
char * t_readline (FILE *fp)
 

Macro Definition Documentation

◆ COPYCHAR

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

◆ t_iseq

◆ TOUCHAR

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

Definition at line 42 of file ts_locale.h.

Referenced by lowerstr_with_len(), t_isalpha(), t_isdigit(), t_isprint(), and t_isspace().

Function Documentation

◆ lowerstr()

char* lowerstr ( const char *  str)

Definition at line 239 of file ts_locale.c.

References lowerstr_with_len().

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

240 {
241  return lowerstr_with_len(str, strlen(str));
242 }
char * lowerstr_with_len(const char *str, int len)
Definition: ts_locale.c:252

◆ lowerstr_with_len()

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

Definition at line 252 of file ts_locale.c.

References Assert, char2wchar(), ereport, errcode(), errmsg(), ERROR, lc_ctype_is_c(), palloc(), pfree(), pg_database_encoding_max_length(), pstrdup(), generate_unaccent_rules::str, TOUCHAR, and wchar2char().

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

253 {
254  char *out;
255  Oid collation = DEFAULT_COLLATION_OID; /* TODO */
256  pg_locale_t mylocale = 0; /* TODO */
257 
258  if (len == 0)
259  return pstrdup("");
260 
261  /*
262  * Use wide char code only when max encoding length > 1 and ctype != C.
263  * Some operating systems fail with multi-byte encodings and a C locale.
264  * Also, for a C locale there is no need to process as multibyte. From
265  * backend/utils/adt/oracle_compat.c Teodor
266  */
267  if (pg_database_encoding_max_length() > 1 && !lc_ctype_is_c(collation))
268  {
269  wchar_t *wstr,
270  *wptr;
271  int wlen;
272 
273  /*
274  * alloc number of wchar_t for worst case, len contains number of
275  * bytes >= number of characters and alloc 1 wchar_t for 0, because
276  * wchar2char wants zero-terminated string
277  */
278  wptr = wstr = (wchar_t *) palloc(sizeof(wchar_t) * (len + 1));
279 
280  wlen = char2wchar(wstr, len + 1, str, len, mylocale);
281  Assert(wlen <= len);
282 
283  while (*wptr)
284  {
285  *wptr = towlower((wint_t) *wptr);
286  wptr++;
287  }
288 
289  /*
290  * Alloc result string for worst case + '\0'
291  */
292  len = pg_database_encoding_max_length() * wlen + 1;
293  out = (char *) palloc(len);
294 
295  wlen = wchar2char(out, wstr, len, mylocale);
296 
297  pfree(wstr);
298 
299  if (wlen < 0)
300  ereport(ERROR,
301  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
302  errmsg("conversion from wchar_t to server encoding failed: %m")));
303  Assert(wlen < len);
304  }
305  else
306  {
307  const char *ptr = str;
308  char *outptr;
309 
310  outptr = out = (char *) palloc(sizeof(char) * (len + 1));
311  while ((ptr - str) < len && *ptr)
312  {
313  *outptr++ = tolower(TOUCHAR(ptr));
314  ptr++;
315  }
316  *outptr = '\0';
317  }
318 
319  return out;
320 }
char * pstrdup(const char *in)
Definition: mcxt.c:1186
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
#define ereport(elevel, rest)
Definition: elog.h:141
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:1836
#define Assert(condition)
Definition: c.h:739
#define TOUCHAR(x)
Definition: ts_locale.h:42
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:1765
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1226

◆ t_isalpha()

int t_isalpha ( const char *  ptr)

Definition at line 68 of file ts_locale.c.

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

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

69 {
70  int clen = pg_mblen(ptr);
71  wchar_t character[WC_BUF_LEN];
72  Oid collation = DEFAULT_COLLATION_OID; /* TODO */
73  pg_locale_t mylocale = 0; /* TODO */
74 
75  if (clen == 1 || lc_ctype_is_c(collation))
76  return isalpha(TOUCHAR(ptr));
77 
78  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
79 
80  return iswalpha((wint_t) character[0]);
81 }
#define WC_BUF_LEN
Definition: ts_locale.c:33
unsigned int Oid
Definition: postgres_ext.h:31
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:1836
#define TOUCHAR(x)
Definition: ts_locale.h:42
int pg_mblen(const char *mbstr)
Definition: mbutils.c:802
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1226

◆ t_isdigit()

int t_isdigit ( const char *  ptr)

Definition at line 36 of file ts_locale.c.

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

Referenced by getNextFlagFromString(), gettoken_tsvector(), lquery_in(), NISortDictionary(), parse_or_operator(), and parse_phrase_operator().

37 {
38  int clen = pg_mblen(ptr);
39  wchar_t character[WC_BUF_LEN];
40  Oid collation = DEFAULT_COLLATION_OID; /* TODO */
41  pg_locale_t mylocale = 0; /* TODO */
42 
43  if (clen == 1 || lc_ctype_is_c(collation))
44  return isdigit(TOUCHAR(ptr));
45 
46  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
47 
48  return iswdigit((wint_t) character[0]);
49 }
#define WC_BUF_LEN
Definition: ts_locale.c:33
unsigned int Oid
Definition: postgres_ext.h:31
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:1836
#define TOUCHAR(x)
Definition: ts_locale.h:42
int pg_mblen(const char *mbstr)
Definition: mbutils.c:802
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1226

◆ t_isprint()

int t_isprint ( const char *  ptr)

Definition at line 84 of file ts_locale.c.

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

Referenced by NIImportDictionary().

85 {
86  int clen = pg_mblen(ptr);
87  wchar_t character[WC_BUF_LEN];
88  Oid collation = DEFAULT_COLLATION_OID; /* TODO */
89  pg_locale_t mylocale = 0; /* TODO */
90 
91  if (clen == 1 || lc_ctype_is_c(collation))
92  return isprint(TOUCHAR(ptr));
93 
94  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
95 
96  return iswprint((wint_t) character[0]);
97 }
#define WC_BUF_LEN
Definition: ts_locale.c:33
unsigned int Oid
Definition: postgres_ext.h:31
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:1836
#define TOUCHAR(x)
Definition: ts_locale.h:42
int pg_mblen(const char *mbstr)
Definition: mbutils.c:802
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1226

◆ t_isspace()

int t_isspace ( const char *  ptr)

Definition at line 52 of file ts_locale.c.

References char2wchar(), lc_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().

53 {
54  int clen = pg_mblen(ptr);
55  wchar_t character[WC_BUF_LEN];
56  Oid collation = DEFAULT_COLLATION_OID; /* TODO */
57  pg_locale_t mylocale = 0; /* TODO */
58 
59  if (clen == 1 || lc_ctype_is_c(collation))
60  return isspace(TOUCHAR(ptr));
61 
62  char2wchar(character, WC_BUF_LEN, ptr, clen, mylocale);
63 
64  return iswspace((wint_t) character[0]);
65 }
#define WC_BUF_LEN
Definition: ts_locale.c:33
unsigned int Oid
Definition: postgres_ext.h:31
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:1836
#define TOUCHAR(x)
Definition: ts_locale.h:42
int pg_mblen(const char *mbstr)
Definition: mbutils.c:802
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1226

◆ t_readline()

char* t_readline ( FILE *  fp)

Definition at line 205 of file ts_locale.c.

References buf, pg_any_to_server(), PG_UTF8, pg_verify_mbstr(), and pnstrdup().

Referenced by tsearch_readline().

206 {
207  int len;
208  char *recoded;
209  char buf[4096]; /* lines must not be longer than this */
210 
211  if (fgets(buf, sizeof(buf), fp) == NULL)
212  return NULL;
213 
214  len = strlen(buf);
215 
216  /* Make sure the input is valid UTF-8 */
217  (void) pg_verify_mbstr(PG_UTF8, buf, len, false);
218 
219  /* And convert */
220  recoded = pg_any_to_server(buf, len, PG_UTF8);
221  if (recoded == buf)
222  {
223  /*
224  * conversion didn't pstrdup, so we must. We can use the length of the
225  * original string, because no conversion was done.
226  */
227  recoded = pnstrdup(recoded, len);
228  }
229 
230  return recoded;
231 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
static char * buf
Definition: pg_test_fsync.c:67
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: wchar.c:1925
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:581

◆ tsearch_readline()

char* tsearch_readline ( tsearch_readline_state stp)

Definition at line 146 of file ts_locale.c.

References tsearch_readline_state::curline, tsearch_readline_state::fp, tsearch_readline_state::lineno, and t_readline().

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

147 {
148  char *result;
149 
150  stp->lineno++;
151  stp->curline = NULL;
152  result = t_readline(stp->fp);
153  stp->curline = result;
154  return result;
155 }
char * t_readline(FILE *fp)
Definition: ts_locale.c:205

◆ tsearch_readline_begin()

bool tsearch_readline_begin ( tsearch_readline_state stp,
const char *  filename 
)

Definition at line 124 of file ts_locale.c.

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

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

126 {
127  if ((stp->fp = AllocateFile(filename, "r")) == NULL)
128  return false;
129  stp->filename = filename;
130  stp->lineno = 0;
131  stp->curline = NULL;
132  /* Setup error traceback support for ereport() */
134  stp->cb.arg = (void *) stp;
136  error_context_stack = &stp->cb;
137  return true;
138 }
static void tsearch_readline_callback(void *arg)
Definition: ts_locale.c:173
void(* callback)(void *arg)
Definition: elog.h:256
struct ErrorContextCallback * previous
Definition: elog.h:255
ErrorContextCallback * error_context_stack
Definition: elog.c:91
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2242
ErrorContextCallback cb
Definition: ts_locale.h:39
const char * filename
Definition: ts_locale.h:36
static char * filename
Definition: pg_dumpall.c:90

◆ tsearch_readline_end()

void tsearch_readline_end ( tsearch_readline_state stp)

Definition at line 161 of file ts_locale.c.

References tsearch_readline_state::cb, error_context_stack, tsearch_readline_state::fp, FreeFile(), and ErrorContextCallback::previous.

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

162 {
163  FreeFile(stp->fp);
164  /* Pop the error context stack */
166 }
struct ErrorContextCallback * previous
Definition: elog.h:255
ErrorContextCallback * error_context_stack
Definition: elog.c:91
ErrorContextCallback cb
Definition: ts_locale.h:39
int FreeFile(FILE *file)
Definition: fd.c:2441