24-08-2013, 05:16 PM
تحديث للدالة لتفادي Memory Leaks :
PHP كود :
// By SMP3
function SplitStr(const AStr: String; n: integer): string;
var
s, PropStr: PChar;
Len, MemLen, i, D: integer;
c: char;
MemTxt, FTxt, TmpStr: string;
begin
D := 0;
MemTxt := AStr;
Len := Length(MemTxt);
if Len < n then
Result := AStr;
GetMem(PropStr, Len {$IFDEF UNICODE} * 2{$ENDIF});
MemLen := Len {$IFDEF UNICODE} * 2{$ENDIF};
s := PChar(MemTxt);
for i := 0 to Len {$IFDEF UNICODE} * 2{$ENDIF} do
begin
if s^ <> #$D then
begin
inc(s);
if s^ <> #$A then
begin
dec(s);
PropStr^ := s^;
inc(s);
inc(D);
inc(PropStr);
end
else
begin
inc(D);
inc(s, 2);
PropStr^ := ' ';
inc(PropStr);
end;
end
else
begin
inc(D);
inc(s, 2);
PropStr^ := ' ';
inc(PropStr);
end;
end;
dec(PropStr, D);
MemTxt := PropStr;
Len := 0;
FTxt := '';
TmpStr := '';
while 0 <= Length(MemTxt) - Len do
begin
TmpStr := Copy(PropStr, 0, n);
FTxt := FTxt + TmpStr + #13#10;
inc(PropStr, n);
inc(Len, n);
if PropStr^ = ' ' then
begin
inc(PropStr, +1);
inc(Len, +1);
end;
end;
dec(PropStr, Len);
Len := Length(FTxt);
FTxt[Len - 1] := #0;
FTxt[Len - 2] := #0;
Result := FTxt;
FreeMem(PropStr, MemLen{$IFDEF UNICODE} * 2{$ENDIF});
end;
Open Your Source .. Open Your Mind

