mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-13 12:59:15 +02:00
* fpc_ansistr_setlength: fixed/refactored:
* Handle codepage the same way in all control flow branches (was different for the cases of allocating a new string and reallocating an existing one) * Don't assign intermediate values directly to var-parameter, use local pointer instead. * Setting string pointer to nil after fpc_ansistr_decr_ref is no longer necessary. - fpc_ansistr_copy: removed repeating index check and unecessary typecasts. git-svn-id: trunk@20209 -
This commit is contained in:
parent
2cfb20db60
commit
a2233d7991
@ -672,37 +672,25 @@ begin
|
|||||||
if (l>0) then
|
if (l>0) then
|
||||||
begin
|
begin
|
||||||
if Pointer(S)=nil then
|
if Pointer(S)=nil then
|
||||||
begin
|
begin
|
||||||
GetMem(Pointer(S),AnsiRecLen+L);
|
Pointer(S):=NewAnsiString(L);
|
||||||
PAnsiRec(S)^.Ref:=1;
|
end
|
||||||
{$ifdef FPC_HAS_CPSTRING}
|
|
||||||
if (cp=CP_ACP) then
|
|
||||||
cp:=DefaultSystemCodePage;
|
|
||||||
PAnsiRec(S)^.CodePage:=cp;
|
|
||||||
{$else}
|
|
||||||
PAnsiRec(S)^.CodePage:=DefaultSystemCodePage;
|
|
||||||
{$endif FPC_HAS_CPSTRING}
|
|
||||||
PAnsiRec(S)^.ElementSize:=1;
|
|
||||||
inc(Pointer(S),AnsiFirstOff);
|
|
||||||
end
|
|
||||||
else if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref=1 then
|
else if PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref=1 then
|
||||||
begin
|
begin
|
||||||
Dec(Pointer(S),AnsiFirstOff);
|
Temp:=Pointer(s)-AnsiFirstOff;
|
||||||
lens:=MemSize(Pointer(s));
|
lens:=MemSize(Temp);
|
||||||
lena:=AnsiRecLen+L;
|
lena:=AnsiRecLen+L;
|
||||||
{ allow shrinking string if that saves at least half of current size }
|
{ allow shrinking string if that saves at least half of current size }
|
||||||
if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
|
if (lena>lens) or ((lens>32) and (lena<=(lens div 2))) then
|
||||||
reallocmem(pointer(S),AnsiRecLen+L);
|
begin
|
||||||
Inc(Pointer(S),AnsiFirstOff);
|
reallocmem(Temp,AnsiRecLen+L);
|
||||||
|
Pointer(S):=Temp+AnsiFirstOff;
|
||||||
|
end;
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
{ Reallocation is needed... }
|
{ Reallocation is needed... }
|
||||||
Temp:=Pointer(NewAnsiString(L));
|
Temp:=NewAnsiString(L);
|
||||||
{$ifdef FPC_HAS_CPSTRING}
|
|
||||||
PAnsiRec(Pointer(Temp)-AnsiFirstOff)^.CodePage:=cp;
|
|
||||||
{$endif FPC_HAS_CPSTRING}
|
|
||||||
|
|
||||||
{ also move terminating null }
|
{ also move terminating null }
|
||||||
lens:=succ(length(s));
|
lens:=succ(length(s));
|
||||||
if l<lens then
|
if l<lens then
|
||||||
@ -710,23 +698,22 @@ begin
|
|||||||
else
|
else
|
||||||
movelen:=lens;
|
movelen:=lens;
|
||||||
Move(Pointer(S)^,Temp^,movelen);
|
Move(Pointer(S)^,Temp^,movelen);
|
||||||
{ ref count dropped to zero in the mean time? }
|
fpc_ansistr_decr_ref(Pointer(s));
|
||||||
If (PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref>0) and
|
|
||||||
declocked(PAnsiRec(Pointer(S)-AnsiFirstOff)^.Ref) then
|
|
||||||
freemem(PAnsiRec(Pointer(s)-AnsiFirstOff));
|
|
||||||
Pointer(S):=Temp;
|
Pointer(S):=Temp;
|
||||||
end;
|
end;
|
||||||
|
{$ifdef FPC_HAS_CPSTRING}
|
||||||
|
if (cp=CP_ACP) then
|
||||||
|
cp:=DefaultSystemCodePage;
|
||||||
|
PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage:=cp;
|
||||||
|
{$else}
|
||||||
|
PAnsiRec(Pointer(S)-AnsiFirstOff)^.CodePage:=DefaultSystemCodePage;
|
||||||
|
{$endif FPC_HAS_CPSTRING}
|
||||||
{ Force nil termination in case it gets shorter }
|
{ Force nil termination in case it gets shorter }
|
||||||
PByte(Pointer(S)+l)^:=0;
|
PByte(Pointer(S)+l)^:=0;
|
||||||
PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len:=l;
|
PAnsiRec(Pointer(S)-AnsiFirstOff)^.Len:=l;
|
||||||
end
|
end
|
||||||
else
|
else { length=0, deallocate the string }
|
||||||
begin
|
fpc_ansistr_decr_ref (Pointer(S));
|
||||||
{ Length=0 }
|
|
||||||
if Pointer(S)<>nil then
|
|
||||||
fpc_ansistr_decr_ref (Pointer(S));
|
|
||||||
Pointer(S):=Nil;
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{$ifdef EXTRAANSISHORT}
|
{$ifdef EXTRAANSISHORT}
|
||||||
@ -808,9 +795,7 @@ begin
|
|||||||
Size:=Length(S)-Index;
|
Size:=Length(S)-Index;
|
||||||
If Size>0 then
|
If Size>0 then
|
||||||
begin
|
begin
|
||||||
If Index<0 Then
|
ResultAddress:=NewAnsiString(Size);
|
||||||
Index:=0;
|
|
||||||
ResultAddress:=Pointer(NewAnsiString (Size));
|
|
||||||
if ResultAddress<>Nil then
|
if ResultAddress<>Nil then
|
||||||
begin
|
begin
|
||||||
Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
|
Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
|
||||||
|
Loading…
Reference in New Issue
Block a user