星期三, 8月 31, 2011
MemoryStream To String
The code you have is unnecessarily complex, even for older Delphi versions. Why should fetching the string version of a stream force the stream's memory to be reallocated, after all?
That works in all Delphi versions, not just Delphi 2009. It works when the stream is empty without any special case. SetString is an under-appreciated function.
If the contents of your stream aren't changing to Unicode with your switch to Delphi 2009, then you should use this function instead:
That's equivalent to your original code, but skips the special cases.
function MemoryStreamToString(M: TMemoryStream): string;
begin
SetString(Result, M.Memory, M.Size div SizeOf(Char));
end;
That works in all Delphi versions, not just Delphi 2009. It works when the stream is empty without any special case. SetString is an under-appreciated function.
If the contents of your stream aren't changing to Unicode with your switch to Delphi 2009, then you should use this function instead:
function MemoryStreamToString(M: TMemoryStream): AnsiString;
begin
SetString(Result, M.Memory, M.Size);
end;
That's equivalent to your original code, but skips the special cases.
訂閱:
文章 (Atom)