Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit15cd9a3

Browse files
committed
jsonapi: Use const char *
Apply const qualifiers to char * arguments and fields throughout thejsonapi. This allows the top-level APIs such aspg_parse_json_incremental() to declare their input argument as const.It also reduces the number of unconstify() calls.Reviewed-by: Andrew Dunstan <andrew@dunslane.net>Discussion:https://www.postgresql.org/message-id/flat/f732b014-f614-4600-a437-dba5a2c3738b%40eisentraut.org
1 parent0b06bf9 commit15cd9a3

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

‎src/backend/utils/adt/jsonfuncs.c‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ typedef struct GetState
8686
{
8787
JsonLexContext*lex;
8888
text*tresult;
89-
char*result_start;
89+
constchar*result_start;
9090
boolnormalize_results;
9191
boolnext_scalar;
9292
intnpath;/* length of each path-related array */
@@ -111,7 +111,7 @@ typedef struct EachState
111111
Tuplestorestate*tuple_store;
112112
TupleDescret_tdesc;
113113
MemoryContexttmp_cxt;
114-
char*result_start;
114+
constchar*result_start;
115115
boolnormalize_results;
116116
boolnext_scalar;
117117
char*normalized_scalar;
@@ -125,7 +125,7 @@ typedef struct ElementsState
125125
Tuplestorestate*tuple_store;
126126
TupleDescret_tdesc;
127127
MemoryContexttmp_cxt;
128-
char*result_start;
128+
constchar*result_start;
129129
boolnormalize_results;
130130
boolnext_scalar;
131131
char*normalized_scalar;
@@ -138,7 +138,7 @@ typedef struct JHashState
138138
constchar*function_name;
139139
HTAB*hash;
140140
char*saved_scalar;
141-
char*save_json_start;
141+
constchar*save_json_start;
142142
JsonTokenTypesaved_token_type;
143143
}JHashState;
144144

@@ -247,7 +247,7 @@ typedef struct PopulateRecordsetState
247247
constchar*function_name;
248248
HTAB*json_hash;
249249
char*saved_scalar;
250-
char*save_json_start;
250+
constchar*save_json_start;
251251
JsonTokenTypesaved_token_type;
252252
Tuplestorestate*tuple_store;
253253
HeapTupleHeaderrec;
@@ -273,7 +273,7 @@ typedef struct PopulateArrayState
273273
{
274274
JsonLexContext*lex;/* json lexer */
275275
PopulateArrayContext*ctx;/* context */
276-
char*element_start;/* start of the current array element */
276+
constchar*element_start;/* start of the current array element */
277277
char*element_scalar;/* current array element token if it is a
278278
* scalar */
279279
JsonTokenTypeelement_type;/* current array element type */
@@ -295,7 +295,7 @@ typedef struct JsValue
295295
{
296296
struct
297297
{
298-
char*str;/* json string */
298+
constchar*str;/* json string */
299299
intlen;/* json string length or -1 if null-terminated */
300300
JsonTokenTypetype;/* json type */
301301
}json;/* json value */
@@ -390,7 +390,7 @@ static JsonParseErrorType elements_array_element_end(void *state, bool isnull);
390390
staticJsonParseErrorTypeelements_scalar(void*state,char*token,JsonTokenTypetokentype);
391391

392392
/* turn a json object into a hash table */
393-
staticHTAB*get_json_object_as_hash(char*json,intlen,constchar*funcname,
393+
staticHTAB*get_json_object_as_hash(constchar*json,intlen,constchar*funcname,
394394
Node*escontext);
395395

396396
/* semantic actions for populate_array_json */
@@ -456,7 +456,7 @@ static Datum populate_record_field(ColumnIOData *col, Oid typid, int32 typmod,
456456
staticRecordIOData*allocate_record_info(MemoryContextmcxt,intncolumns);
457457
staticboolJsObjectGetField(JsObject*obj,char*field,JsValue*jsv);
458458
staticvoidpopulate_recordset_record(PopulateRecordsetState*state,JsObject*obj);
459-
staticboolpopulate_array_json(PopulateArrayContext*ctx,char*json,intlen);
459+
staticboolpopulate_array_json(PopulateArrayContext*ctx,constchar*json,intlen);
460460
staticboolpopulate_array_dim_jsonb(PopulateArrayContext*ctx,JsonbValue*jbv,
461461
intndim);
462462
staticvoidpopulate_array_report_expected_array(PopulateArrayContext*ctx,intndim);
@@ -1181,7 +1181,7 @@ get_object_end(void *state)
11811181
if (lex_level==0&&_state->npath==0)
11821182
{
11831183
/* Special case: return the entire object */
1184-
char*start=_state->result_start;
1184+
constchar*start=_state->result_start;
11851185
intlen=_state->lex->prev_token_terminator-start;
11861186

11871187
_state->tresult=cstring_to_text_with_len(start,len);
@@ -1275,7 +1275,7 @@ get_object_field_end(void *state, char *fname, bool isnull)
12751275
_state->tresult= (text*)NULL;
12761276
else
12771277
{
1278-
char*start=_state->result_start;
1278+
constchar*start=_state->result_start;
12791279
intlen=_state->lex->prev_token_terminator-start;
12801280

12811281
_state->tresult=cstring_to_text_with_len(start,len);
@@ -1337,7 +1337,7 @@ get_array_end(void *state)
13371337
if (lex_level==0&&_state->npath==0)
13381338
{
13391339
/* Special case: return the entire array */
1340-
char*start=_state->result_start;
1340+
constchar*start=_state->result_start;
13411341
intlen=_state->lex->prev_token_terminator-start;
13421342

13431343
_state->tresult=cstring_to_text_with_len(start,len);
@@ -1426,7 +1426,7 @@ get_array_element_end(void *state, bool isnull)
14261426
_state->tresult= (text*)NULL;
14271427
else
14281428
{
1429-
char*start=_state->result_start;
1429+
constchar*start=_state->result_start;
14301430
intlen=_state->lex->prev_token_terminator-start;
14311431

14321432
_state->tresult=cstring_to_text_with_len(start,len);
@@ -1463,7 +1463,7 @@ get_scalar(void *state, char *token, JsonTokenType tokentype)
14631463
* scalar token, but not whitespace before it. Probably not worth
14641464
* doing our own space-skipping to avoid that.
14651465
*/
1466-
char*start=_state->lex->input;
1466+
constchar*start=_state->lex->input;
14671467
intlen=_state->lex->prev_token_terminator-start;
14681468

14691469
_state->tresult=cstring_to_text_with_len(start,len);
@@ -2782,7 +2782,7 @@ populate_array_scalar(void *_state, char *token, JsonTokenType tokentype)
27822782
* Returns false if an error occurs when parsing.
27832783
*/
27842784
staticbool
2785-
populate_array_json(PopulateArrayContext*ctx,char*json,intlen)
2785+
populate_array_json(PopulateArrayContext*ctx,constchar*json,intlen)
27862786
{
27872787
PopulateArrayStatestate;
27882788
JsonSemActionsem;
@@ -3123,7 +3123,7 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv,
31233123
{
31243124
Datumres;
31253125
char*str=NULL;
3126-
char*json=NULL;
3126+
constchar*json=NULL;
31273127

31283128
if (jsv->is_json)
31293129
{
@@ -3139,7 +3139,10 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv,
31393139
str[len]='\0';
31403140
}
31413141
else
3142-
str=json;/* string is already null-terminated */
3142+
{
3143+
/* string is already null-terminated */
3144+
str=unconstify(char*,json);
3145+
}
31433146

31443147
/* If converting to json/jsonb, make string into valid JSON literal */
31453148
if ((typid==JSONOID||typid==JSONBOID)&&
@@ -3784,7 +3787,7 @@ populate_record_worker(FunctionCallInfo fcinfo, const char *funcname,
37843787
* Returns the hash table if the json is parsed successfully, NULL otherwise.
37853788
*/
37863789
staticHTAB*
3787-
get_json_object_as_hash(char*json,intlen,constchar*funcname,
3790+
get_json_object_as_hash(constchar*json,intlen,constchar*funcname,
37883791
Node*escontext)
37893792
{
37903793
HASHCTLctl;

‎src/common/jsonapi.c‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static td_entry td_parser_table[JSON_NUM_NONTERMINALS][JSON_NUM_TERMINALS] =
211211
staticcharJSON_PROD_GOAL[]= {JSON_TOKEN_END,JSON_NT_JSON,0};
212212

213213
staticinlineJsonParseErrorTypejson_lex_string(JsonLexContext*lex);
214-
staticinlineJsonParseErrorTypejson_lex_number(JsonLexContext*lex,char*s,
214+
staticinlineJsonParseErrorTypejson_lex_number(JsonLexContext*lex,constchar*s,
215215
bool*num_err,size_t*total_len);
216216
staticinlineJsonParseErrorTypeparse_scalar(JsonLexContext*lex,JsonSemAction*sem);
217217
staticJsonParseErrorTypeparse_object_field(JsonLexContext*lex,JsonSemAction*sem);
@@ -290,12 +290,12 @@ IsValidJsonNumber(const char *str, size_t len)
290290
*/
291291
if (*str=='-')
292292
{
293-
dummy_lex.input=unconstify(char*,str)+1;
293+
dummy_lex.input=str+1;
294294
dummy_lex.input_length=len-1;
295295
}
296296
else
297297
{
298-
dummy_lex.input=unconstify(char*,str);
298+
dummy_lex.input=str;
299299
dummy_lex.input_length=len;
300300
}
301301

@@ -323,7 +323,7 @@ IsValidJsonNumber(const char *str, size_t len)
323323
* cleanup.
324324
*/
325325
JsonLexContext*
326-
makeJsonLexContextCstringLen(JsonLexContext*lex,char*json,
326+
makeJsonLexContextCstringLen(JsonLexContext*lex,constchar*json,
327327
size_tlen,intencoding,boolneed_escapes)
328328
{
329329
if (lex==NULL)
@@ -649,7 +649,7 @@ json_count_array_elements(JsonLexContext *lex, int *elements)
649649
JsonParseErrorType
650650
pg_parse_json_incremental(JsonLexContext*lex,
651651
JsonSemAction*sem,
652-
char*json,
652+
constchar*json,
653653
size_tlen,
654654
boolis_last)
655655
{
@@ -1308,8 +1308,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem)
13081308
JsonParseErrorType
13091309
json_lex(JsonLexContext*lex)
13101310
{
1311-
char*s;
1312-
char*constend=lex->input+lex->input_length;
1311+
constchar*s;
1312+
constchar*constend=lex->input+lex->input_length;
13131313
JsonParseErrorTyperesult;
13141314

13151315
if (lex->incremental&&lex->inc_state->partial_completed)
@@ -1593,7 +1593,7 @@ json_lex(JsonLexContext *lex)
15931593
break;
15941594
default:
15951595
{
1596-
char*p;
1596+
constchar*p;
15971597

15981598
/*
15991599
* We're not dealing with a string, number, legal
@@ -1671,8 +1671,8 @@ json_lex(JsonLexContext *lex)
16711671
staticinlineJsonParseErrorType
16721672
json_lex_string(JsonLexContext*lex)
16731673
{
1674-
char*s;
1675-
char*constend=lex->input+lex->input_length;
1674+
constchar*s;
1675+
constchar*constend=lex->input+lex->input_length;
16761676
inthi_surrogate=-1;
16771677

16781678
/* Convenience macros for error exits */
@@ -1689,7 +1689,7 @@ json_lex_string(JsonLexContext *lex)
16891689
} while (0)
16901690
#defineFAIL_AT_CHAR_END(code) \
16911691
do { \
1692-
char *term = s + pg_encoding_mblen(lex->input_encoding, s); \
1692+
constchar *term = s + pg_encoding_mblen(lex->input_encoding, s); \
16931693
lex->token_terminator = (term <= end) ? term : end; \
16941694
return code; \
16951695
} while (0)
@@ -1854,7 +1854,7 @@ json_lex_string(JsonLexContext *lex)
18541854
}
18551855
else
18561856
{
1857-
char*p=s;
1857+
constchar*p=s;
18581858

18591859
if (hi_surrogate!=-1)
18601860
FAIL_AT_CHAR_END(JSON_UNICODE_LOW_SURROGATE);
@@ -1940,7 +1940,7 @@ json_lex_string(JsonLexContext *lex)
19401940
* the distance from lex->input to the token end+1 is returned to *total_len.
19411941
*/
19421942
staticinlineJsonParseErrorType
1943-
json_lex_number(JsonLexContext*lex,char*s,
1943+
json_lex_number(JsonLexContext*lex,constchar*s,
19441944
bool*num_err,size_t*total_len)
19451945
{
19461946
boolerror= false;

‎src/include/common/jsonapi.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,18 @@ typedef struct JsonIncrementalState JsonIncrementalState;
8888
#defineJSONLEX_FREE_STRVAL(1 << 1)
8989
typedefstructJsonLexContext
9090
{
91-
char*input;
91+
constchar*input;
9292
size_tinput_length;
9393
intinput_encoding;
94-
char*token_start;
95-
char*token_terminator;
96-
char*prev_token_terminator;
94+
constchar*token_start;
95+
constchar*token_terminator;
96+
constchar*prev_token_terminator;
9797
boolincremental;
9898
JsonTokenTypetoken_type;
9999
intlex_level;
100100
bits32flags;
101101
intline_number;/* line number, starting from 1 */
102-
char*line_start;/* where that line starts within input */
102+
constchar*line_start;/* where that line starts within input */
103103
JsonParserStack*pstack;
104104
JsonIncrementalState*inc_state;
105105
StringInfostrval;
@@ -157,7 +157,7 @@ extern JsonParseErrorType pg_parse_json(JsonLexContext *lex,
157157

158158
externJsonParseErrorTypepg_parse_json_incremental(JsonLexContext*lex,
159159
JsonSemAction*sem,
160-
char*json,
160+
constchar*json,
161161
size_tlen,
162162
boolis_last);
163163

@@ -192,7 +192,7 @@ extern JsonParseErrorType json_count_array_elements(JsonLexContext *lex,
192192
* cleanup.
193193
*/
194194
externJsonLexContext*makeJsonLexContextCstringLen(JsonLexContext*lex,
195-
char*json,
195+
constchar*json,
196196
size_tlen,
197197
intencoding,
198198
boolneed_escapes);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp