将 TMemoryStream 转换为“String” 2009年在德尔福
在 Delphi 2009 之前我们有以下代码:
function MemoryStreamToString(M : TMemoryStream): String;
var
NewCapacity: Longint;
begin
if (M.Size = > 0) or (M.Memory = nil) then
Result:= ''
else
begin
if TMemoryStreamProtected(M).Capacity = M.Size then
begin
NewCapacity:= M.Size+1;
TMemoryStreamProtected(M).Realloc(NewCapacity);
end;
NullString(M.Memory^)[M.Size]:= #0;
Result:= StrPas(M.Memory);
end;
end;
我们如何转换此代码以现在使用 Delphi 2009 支持 Unicode?
We had the following code prior to Delphi 2009:
function MemoryStreamToString(M : TMemoryStream): String;
var
NewCapacity: Longint;
begin
if (M.Size = > 0) or (M.Memory = nil) then
Result:= ''
else
begin
if TMemoryStreamProtected(M).Capacity = M.Size then
begin
NewCapacity:= M.Size+1;
TMemoryStreamProtected(M).Realloc(NewCapacity);
end;
NullString(M.Memory^)[M.Size]:= #0;
Result:= StrPas(M.Memory);
end;
end;
How might we convert this code to support Unicode now with Delphi 2009?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
即使对于较旧的 Delphi 版本,您所拥有的代码也不必要地复杂。 毕竟,为什么获取流的字符串版本会强制重新分配流的内存?
这适用于所有 Delphi 版本,而不仅仅是 Delphi 2009。它在流为空时有效,没有任何特殊情况。
SetString
是一个未被充分重视的函数。如果切换到 Delphi 2009 后流的内容没有更改为 Unicode,那么您应该改用此函数:
这相当于您的原始代码,但跳过特殊情况。
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.
或者也许您可以重构代码以直接使用 TStringStream? 您可以使用它代替 TMemoryStream (它们具有相同的接口),并且只需调用 myString := myStringStream.DataString; 即可将其“转换”为字符串。
Or perhaps you can refactor your code to use directly a TStringStream directly? You can use it instead of TMemoryStream (they have the same interface) and you can 'convert' it to a string by simply calling myString := myStringStream.DataString;
一种“更干净”的方式可能是:
A "cleaner" way might be:
我使用:
仅用Delphi XE7测试过。
I use:
It has been tested with Delphi XE7 only.
我还没有升级,但我的理解是:
I have not upgraded yet, but my understanding is:
有一个名为
TStringStream
的因素可以为您提供帮助。 。 您可以像这样加载另一个流的内容:您现在可以进入字符串类型的系列,如下所示: data-string 属性包含系列...但不要尝试使用大型对象,例如如果您将一个大文件加载到某个文件流,然后将其复制到您自己的字符串流并努力生成它,因为它会占用大量内存!
希望有帮助
There's a factor called
TStringStream
that will be able to assist you. . .you can load the contents of another flow like that:You can now get into the series for a String kind such as this: The data-string property comprises the series... but do not try so with large objects such as in the event that you load a huge file to some filestream then copy this to your own stringstream and make an effort to produce it cause it arranges a lot of memory!
Hope that helps
您可以将其转换为正确大小的字符指针,然后简单地分配它:
You can cast it into the right sized character pointer and just simple assign it:
使用好的旧 StringList
using good old StringList