Skip to content

Commit 5ece00a

Browse files
committed
Use memspn for safe EOL stepping after block replacements
1 parent ef6c8dd commit 5ece00a

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

SRC/NPPTextFX.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,25 +3666,26 @@ EXTERNC unsigned extendblockspaces(char **dest,unsigned *destsz,unsigned *destle
36663666
#undef lold
36673667
}
36683668

3669-
EXTERNC unsigned trimtrailingspace(char *dest,unsigned *destlen) {
3670-
#define lnew 0
3671-
unsigned n=0,lold;
3672-
char *d,*dp,*end;
3673-
if (dest) {
3674-
for(d=dest,end=dest+*destlen;d<end; ) {
3675-
dp=d;
3676-
d=memcspn(d,end,"\r\n",2);
3677-
for(d--,lold=0; d>=dp && *d==' '; d--, lold++);
3678-
d++;
3679-
if (lnew != lold) {
3680-
memmovetest(d+lnew,d+lold,*destlen-(d-dest)-lold+1);
3681-
*destlen += lnew-lold;
3682-
end += lnew-lold;
3683-
n++;
3684-
}
3685-
d=memspn(d,end,"\r\n",2);
3686-
}
3687-
}
3669+
EXTERNC unsigned trimtrailingspace(char *dest,unsigned *destlen) {
3670+
#define lnew 0
3671+
unsigned n=0,lold;
3672+
char *d,*lineend,*trimstart,*end;
3673+
if (dest) {
3674+
for(d=dest,end=dest+*destlen;d<end; ) {
3675+
lineend=memcspn(d,end,"\r\n",2);
3676+
trimstart=lineend;
3677+
while(trimstart>d && trimstart[-1]==' ') trimstart--;
3678+
lold=(unsigned)(lineend-trimstart);
3679+
if (lnew != lold) {
3680+
memmove(trimstart+lnew,lineend,*destlen-(unsigned)(lineend-dest));
3681+
*destlen += lnew-lold;
3682+
end += lnew-lold;
3683+
n++;
3684+
lineend=trimstart;
3685+
}
3686+
d=memspn(lineend,end,"\r\n",2);
3687+
}
3688+
}
36883689
return(n);
36893690
#undef lnew
36903691
}
@@ -4954,16 +4955,16 @@ EXTERNC void convertall(char cmd,unsigned flags,const char *s1,const char *s2,co
49544955
SENDMSGTOCED(currentEdit, SCI_REPLACETARGET, chn, d);
49554956
}
49564957
d += chn;
4957-
if ((d[0]=='\r' && d[1]=='\n') || (d[0]=='\n' && d[1]=='\r')) d+=2;
4958-
else if (d[0]=='\r' || d[0]=='\n') d++;
4958+
d = memspn(d,end,"\r\n",2);
49594959
}
49604960
/*curpos = lpe[blocklines-1]+SENDMSGTOCED(currentEdit, SCI_POSITIONFROMLINE, p1line+blocklines-1, 0);
49614961
if (curpos<anchor) curpos=anchor+1; // carefully constructed cases can push anchor ahead of curpos
49624962
SENDMSGTOCED(currentEdit, SCI_SETANCHOR,(flags&CAFLAG_LESS)?curpos:anchor, 0);
49634963
SENDMSGTOCED(currentEdit, SCI_SETSELECTIONMODE,SC_SEL_RECTANGLE, 0);
49644964
SENDMSGTOCED(currentEdit, SCI_SETCURRENTPOS,(flags&CAFLAG_LESS)?anchor:curpos, 0);*/
49654965
} else {
4966-
p2 += sln-sellen;
4966+
if (sln>=sellen) p2 += sln-sellen;
4967+
else p2 -= sellen-sln;
49674968
if (rv) {
49684969
if (flags&CAFLAG_GETALLWHENNOSELECTION) {
49694970
SENDMSGTOCED(currentEdit, SCI_SETTEXT, 0, "");
@@ -5862,7 +5863,11 @@ EXTERNC void detectprplist(void) {
58625863
EXTERNC void adjustprplist(INT_CURRENTEDIT,unsigned curpos) {
58635864
unsigned t1=SENDMSGTOCED(currentEdit, SCI_GETLENGTH,0,0);
58645865
if (g_PopLists[g_uPopListNo].poptextlen) {
5865-
unsigned i; for(i=0; i<g_PopLists[g_uPopListNo].popcount; i++) if (g_PopLists[g_uPopListNo].poplist[i]>curpos) g_PopLists[g_uPopListNo].poplist[i]+=t1-g_PopLists[g_uPopListNo].poptextlen;
5866+
unsigned i; for(i=0; i<g_PopLists[g_uPopListNo].popcount; i++) if (g_PopLists[g_uPopListNo].poplist[i]>curpos) {
5867+
if (t1>=g_PopLists[g_uPopListNo].poptextlen) g_PopLists[g_uPopListNo].poplist[i]+=t1-g_PopLists[g_uPopListNo].poptextlen;
5868+
else if (g_PopLists[g_uPopListNo].poplist[i]>=g_PopLists[g_uPopListNo].poptextlen-t1) g_PopLists[g_uPopListNo].poplist[i]-=g_PopLists[g_uPopListNo].poptextlen-t1;
5869+
else g_PopLists[g_uPopListNo].poplist[i]=0;
5870+
}
58665871
}
58675872
g_PopLists[g_uPopListNo].poptextlen=t1;
58685873
}

0 commit comments

Comments
 (0)