StringReplace 的二进制版本
我正在尝试在保存大量数据的 RawByteString 上运行 AnsiStrings.StringReplace,其中一些数据需要替换。它会起作用,除了在 StringReplace 内部它将我的字符串转换为 PAnsiChar,因此搜索一旦到达 blob 内的第一个 #0 字节就会结束。
我正在寻找一个与 StringReplace 类似的例程,但可以安全地在可能包含空字节的 blob 上使用。有人知道其中一个吗?
I'm trying to run AnsiStrings.StringReplace on a RawByteString holding a blob of data, some of which needs to be replaced. It would work, except that inside StringReplace it converts my string to a PAnsiChar, and so the search ends up bailing out as soon as it hits the first #0 byte inside the blob.
I'm looking for a routine that works just like StringReplace, but is safe to use on blobs that may contain null bytes. Does anyone know of one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜想 StringReplace 中的“违规”函数是 AnsiPos->AnsiStrPos
所以...我想缺少一个已经有效的解决方案,我会复制/粘贴 StringReplace 代码并将 AnsiPos 更改为其他内容。 (即 AnsiStrings.PosEx)
I'd guess the "Offending" function in StringReplace is AnsiPos->AnsiStrPos
So... I guess short of an already working solution, I'd copy/paste the StringReplace code and change AnsiPos for something else. (i.e. AnsiStrings.PosEx)
我没有进行广泛的测试,但我认为这段代码是有效的。
要测试它:
I have not performed extensive testing, but I think that this code works.
To test it:
唔。看来自己写一个并不太难。只需迭代缓冲区,直到在第一个字节上找到匹配项。然后查看后续字节是否匹配。如果是这样,您已找到它,请立即更换。继续或退出,取决于您的需要。如果尺寸相同,显然更简单。如果没有,那么您可以设置第二个缓冲区并将字节从基本缓冲区复制到新缓冲区中。
Hmm. Seems like it couldn't be too hard to write your own. Just iterate through the buffer until you find a match on the first byte. Then see if the subsequent bytes match. If so, you found it, now replace. Keep going or quit, depending on what you need. Obviously simpler if the sizes are the same. If not, then you can set up a second buffer and copy bytes from the base buffer into the new buffer.