summaryrefslogtreecommitdiff
path: root/util.h
blob: d7d879564e1902c8ef45cf719d0ac98631fac6e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*-------------------------------------------------------------------------
 *
 * util.h
 *	  General purpose routines used by JSON data type support.
 *
 * Copyright (c) 2010, PostgreSQL Global Development Group
 * Written by Joey Adams <[email protected]>.
 *
 *-------------------------------------------------------------------------
 */

#ifndef JSON_UTIL_H
#define JSON_UTIL_H

#include "postgres.h"

#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "utils/lsyscache.h"

typedef struct
{
	Oid			type;
	IOFuncSelector which_func;
	MemoryContext mcxt;

	int16		typlen;
	bool		typbyval;
	char		typalign;
	char		typdelim;
	Oid			typioparam;
	Oid			typiofunc;
	FmgrInfo	proc;

	char		typcategory;
	bool		typispreferred;
} TypeInfo;

typedef struct
{
	int			index;
	const char *label;
}	EnumLabel;


void		getTypeInfo(TypeInfo *d, Oid type, IOFuncSelector which_func, MemoryContext mcxt);

void		getEnumLabelOids(const char *typname, EnumLabel labels[], Oid oid_out[], int count);

size_t utf8_substring(const char *src, size_t srcbytes,
			   size_t start, size_t length,
			   const char **out_start, size_t *out_bytes);
pg_wchar	utf8_decode_char(const char **sp);
bool		utf8_validate(const char *str, size_t length);

/*
 * Adaptations of pg_do_encoding_conversion for simplifying UTF-8 conversions.
 *
 * These are used frequently in the JSON code because JSON nodes are encoded
 * in UTF-8.  The reason they are encoded in UTF-8 is because we need to
 * be able to handle Unicode escapes, but there is
 * no simple and efficient way to do that with the server encoding.
 *
 * Just like pg_do_encoding_conversion, if no conversion is done, the original
 * pointer given is returned.
 */
char	   *server_to_utf8(const char *str, int len);
char	   *utf8_to_server(const char *str, int len);

/*
 * Variants of text_to_cstring and cstring_to_text for simplifying UTF-8 conversions.
 *
 * Just like text_to_cstring, text_to_utf8_cstring will always return a palloc'd,
 * null-terminated C-string.
 */
char	   *text_to_utf8_cstring(const text *t);
text	   *utf8_cstring_to_text(const char *s);
text	   *utf8_cstring_to_text_with_len(const char *s, int len);


#endif