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

Commitd5f139c

Browse files
committed
Constify fields and parameters in spell.c
I started by marking VoidString as const, and fixing the fallout bymarking more fields and function arguments as const. It proliferatedquite a lot, but all within spell.c and spell.h.A more narrow patch to get rid of the static VoidString buffer wouldbe to replace it with '#define VoidString ""', as C99 allows assigning"" to a non-const pointer, even though you're not allowed to modifyit. But it seems like good hygiene to mark all these as const. In thestructs, the pointers can point to the constant VoidString, or abuffer allocated with palloc(), or with compact_palloc(), so youshould not modify them.Reviewed-by: Andres FreundDiscussion:https://www.postgresql.org/message-id/54c29fb0-edf2-48ea-9814-44e918bbd6e8@iki.fi
1 parentfe8dd65 commitd5f139c

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

‎src/backend/tsearch/spell.c

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ lowerstr_ctx(IspellDict *Conf, const char *src)
191191
#defineGETWCHAR(W,L,N,T) ( ((const uint8*)(W))[ ((T)==FF_PREFIX) ? (N) : ( (L) - 1 - (N) ) ] )
192192
#defineGETCHAR(A,N,T) GETWCHAR( (A)->repl, (A)->replen, N, T )
193193

194-
staticchar*VoidString="";
194+
staticconstchar*VoidString="";
195195

196196
staticint
197197
cmpspell(constvoid*s1,constvoid*s2)
@@ -346,11 +346,11 @@ cmpaffix(const void *s1, const void *s2)
346346
* sflag: returns an affix flag from sflagset.
347347
*/
348348
staticvoid
349-
getNextFlagFromString(IspellDict*Conf,char**sflagset,char*sflag)
349+
getNextFlagFromString(IspellDict*Conf,constchar**sflagset,char*sflag)
350350
{
351351
int32s;
352-
char*next,
353-
*sbuf=*sflagset;
352+
char*next;
353+
constchar*sbuf=*sflagset;
354354
intmaxstep;
355355
boolstop= false;
356356
boolmet_comma= false;
@@ -453,7 +453,7 @@ getNextFlagFromString(IspellDict *Conf, char **sflagset, char *sflag)
453453
staticbool
454454
IsAffixFlagInUse(IspellDict*Conf,intaffix,constchar*affixflag)
455455
{
456-
char*flagcur;
456+
constchar*flagcur;
457457
charflag[BUFSIZ];
458458

459459
if (*affixflag==0)
@@ -1120,13 +1120,13 @@ addCompoundAffixFlagValue(IspellDict *Conf, char *s, uint32 val)
11201120
* flags s.
11211121
*/
11221122
staticint
1123-
getCompoundAffixFlagValue(IspellDict*Conf,char*s)
1123+
getCompoundAffixFlagValue(IspellDict*Conf,constchar*s)
11241124
{
11251125
uint32flag=0;
11261126
CompoundAffixFlag*found,
11271127
key;
11281128
charsflag[BUFSIZ];
1129-
char*flagcur;
1129+
constchar*flagcur;
11301130

11311131
if (Conf->nCompoundAffixFlag==0)
11321132
return0;
@@ -1155,7 +1155,7 @@ getCompoundAffixFlagValue(IspellDict *Conf, char *s)
11551155
* Conf->AffixData array and function returns its entry.
11561156
* Else function returns the s parameter.
11571157
*/
1158-
staticchar*
1158+
staticconstchar*
11591159
getAffixFlagSet(IspellDict*Conf,char*s)
11601160
{
11611161
if (Conf->useFlagAliases&&*s!='\0')
@@ -1323,7 +1323,7 @@ NIImportOOAffixes(IspellDict *Conf, const char *filename)
13231323
/* Also reserve place for empty flag set */
13241324
naffix++;
13251325

1326-
Conf->AffixData= (char**)palloc0(naffix*sizeof(char*));
1326+
Conf->AffixData= (constchar**)palloc0(naffix*sizeof(char*));
13271327
Conf->lenAffixData=Conf->nAffixData=naffix;
13281328

13291329
/* Add empty flag set into AffixData */
@@ -1571,7 +1571,7 @@ NIImportAffixes(IspellDict *Conf, const char *filename)
15711571
staticint
15721572
MergeAffix(IspellDict*Conf,inta1,inta2)
15731573
{
1574-
char**ptr;
1574+
constchar**ptr;
15751575

15761576
Assert(a1<Conf->nAffixData&&a2<Conf->nAffixData);
15771577

@@ -1585,24 +1585,28 @@ MergeAffix(IspellDict *Conf, int a1, int a2)
15851585
if (Conf->nAffixData+1 >=Conf->lenAffixData)
15861586
{
15871587
Conf->lenAffixData *=2;
1588-
Conf->AffixData= (char**)repalloc(Conf->AffixData,
1589-
sizeof(char*)*Conf->lenAffixData);
1588+
Conf->AffixData= (constchar**)repalloc(Conf->AffixData,
1589+
sizeof(char*)*Conf->lenAffixData);
15901590
}
15911591

15921592
ptr=Conf->AffixData+Conf->nAffixData;
15931593
if (Conf->flagMode==FM_NUM)
15941594
{
1595-
*ptr=cpalloc(strlen(Conf->AffixData[a1])+
1596-
strlen(Conf->AffixData[a2])+
1597-
1/* comma */+1/* \0 */ );
1598-
sprintf(*ptr,"%s,%s",Conf->AffixData[a1],Conf->AffixData[a2]);
1595+
char*p=cpalloc(strlen(Conf->AffixData[a1])+
1596+
strlen(Conf->AffixData[a2])+
1597+
1/* comma */+1/* \0 */ );
1598+
1599+
sprintf(p,"%s,%s",Conf->AffixData[a1],Conf->AffixData[a2]);
1600+
*ptr=p;
15991601
}
16001602
else
16011603
{
1602-
*ptr=cpalloc(strlen(Conf->AffixData[a1])+
1603-
strlen(Conf->AffixData[a2])+
1604-
1/* \0 */ );
1605-
sprintf(*ptr,"%s%s",Conf->AffixData[a1],Conf->AffixData[a2]);
1604+
char*p=cpalloc(strlen(Conf->AffixData[a1])+
1605+
strlen(Conf->AffixData[a2])+
1606+
1/* \0 */ );
1607+
1608+
sprintf(p,"%s%s",Conf->AffixData[a1],Conf->AffixData[a2]);
1609+
*ptr=p;
16061610
}
16071611
ptr++;
16081612
*ptr=NULL;
@@ -1785,7 +1789,7 @@ NISortDictionary(IspellDict *Conf)
17851789
* dictionary. Replace textual flag-field of Conf->Spell entries with
17861790
* indexes into Conf->AffixData array.
17871791
*/
1788-
Conf->AffixData= (char**)palloc0(naffix*sizeof(char*));
1792+
Conf->AffixData= (constchar**)palloc0(naffix*sizeof(constchar*));
17891793

17901794
curaffix=-1;
17911795
for (i=0;i<Conf->nspell;i++)
@@ -1954,7 +1958,7 @@ mkVoidAffix(IspellDict *Conf, bool issuffix, int startsuffix)
19541958
* returns false.
19551959
*/
19561960
staticbool
1957-
isAffixInUse(IspellDict*Conf,char*affixflag)
1961+
isAffixInUse(IspellDict*Conf,constchar*affixflag)
19581962
{
19591963
inti;
19601964

@@ -2169,7 +2173,7 @@ addToResult(char **forms, char **cur, char *word)
21692173
}
21702174

21712175
staticchar**
2172-
NormalizeSubWord(IspellDict*Conf,char*word,intflag)
2176+
NormalizeSubWord(IspellDict*Conf,constchar*word,intflag)
21732177
{
21742178
AffixNodeData*suffix=NULL,
21752179
*prefix=NULL;
@@ -2255,7 +2259,7 @@ NormalizeSubWord(IspellDict *Conf, char *word, int flag)
22552259
if (CheckAffix(newword,swrdlen,prefix->aff[j],flag,pnewword,&baselen))
22562260
{
22572261
/* prefix success */
2258-
char*ff= (prefix->aff[j]->flagflags&suffix->aff[i]->flagflags&FF_CROSSPRODUCT) ?
2262+
constchar*ff= (prefix->aff[j]->flagflags&suffix->aff[i]->flagflags&FF_CROSSPRODUCT) ?
22592263
VoidString :prefix->aff[j]->flag;
22602264

22612265
if (FindWord(Conf,pnewword,ff,flag))
@@ -2287,7 +2291,7 @@ typedef struct SplitVar
22872291
}SplitVar;
22882292

22892293
staticint
2290-
CheckCompoundAffixes(CMPDAffix**ptr,char*word,intlen,boolCheckInPlace)
2294+
CheckCompoundAffixes(CMPDAffix**ptr,constchar*word,intlen,boolCheckInPlace)
22912295
{
22922296
boolissuffix;
22932297

@@ -2367,7 +2371,7 @@ AddStem(SplitVar *v, char *word)
23672371
}
23682372

23692373
staticSplitVar*
2370-
SplitToVariants(IspellDict*Conf,SPNode*snode,SplitVar*orig,char*word,intwordlen,intstartpos,intminpos)
2374+
SplitToVariants(IspellDict*Conf,SPNode*snode,SplitVar*orig,constchar*word,intwordlen,intstartpos,intminpos)
23712375
{
23722376
SplitVar*var=NULL;
23732377
SPNodeData*StopLow,
@@ -2533,7 +2537,7 @@ addNorm(TSLexeme **lres, TSLexeme **lcur, char *word, int flags, uint16 NVariant
25332537
}
25342538

25352539
TSLexeme*
2536-
NINormalizeWord(IspellDict*Conf,char*word)
2540+
NINormalizeWord(IspellDict*Conf,constchar*word)
25372541
{
25382542
char**res;
25392543
TSLexeme*lcur=NULL,

‎src/include/tsearch/dicts/spell.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ typedef struct spell_struct
6666
* flag is filled in by NIImportDictionary(). After
6767
* NISortDictionary(), d is used instead of flag.
6868
*/
69-
char*flag;
69+
constchar*flag;
7070
/* d is used in mkSPNode() */
7171
struct
7272
{
@@ -86,15 +86,15 @@ typedef struct spell_struct
8686
*/
8787
typedefstructaff_struct
8888
{
89-
char*flag;
89+
constchar*flag;
9090
/* FF_SUFFIX or FF_PREFIX */
9191
uint32type:1,
9292
flagflags:7,
9393
issimple:1,
9494
isregis:1,
9595
replen:14;
96-
char*find;
97-
char*repl;
96+
constchar*find;
97+
constchar*repl;
9898
union
9999
{
100100
/*
@@ -146,7 +146,7 @@ typedef struct AffixNode
146146

147147
typedefstruct
148148
{
149-
char*affix;
149+
constchar*affix;
150150
intlen;
151151
boolissuffix;
152152
}CMPDAffix;
@@ -170,7 +170,7 @@ typedef struct CompoundAffixFlag
170170
union
171171
{
172172
/* Flag name if flagMode is FM_CHAR or FM_LONG */
173-
char*s;
173+
constchar*s;
174174
/* Flag name if flagMode is FM_NUM */
175175
uint32i;
176176
}flag;
@@ -192,7 +192,7 @@ typedef struct
192192

193193
SPNode*Dictionary;
194194
/* Array of sets of affixes */
195-
char**AffixData;
195+
constchar**AffixData;
196196
intlenAffixData;
197197
intnAffixData;
198198
booluseFlagAliases;
@@ -229,7 +229,7 @@ typedef struct
229229
size_tavail;/* free space remaining at firstfree */
230230
}IspellDict;
231231

232-
externTSLexeme*NINormalizeWord(IspellDict*Conf,char*word);
232+
externTSLexeme*NINormalizeWord(IspellDict*Conf,constchar*word);
233233

234234
externvoidNIStartBuild(IspellDict*Conf);
235235
externvoidNIImportAffixes(IspellDict*Conf,constchar*filename);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp