MASM str 和 substr?
我目前正在用 asm 编写一个 irc 机器人 我已经在 C++ 中做过一次,所以我知道如何解决我遇到的大多数问题,但我需要一个像 C++ 中那样的 substr()[*] 函数。我需要 substr 函数从 PING 请求接收服务器名称,以便我可以响应相应的 PONG 响应
,但我不知道如何在 MASM 中实现它,我听说过一种叫做宏组装的东西,似乎 substr 经常用于这些函数
有谁知道如何让我的 substr 函数工作
[*] string substr ( size_t pos = 0, size_t n = npos )
这就是我在中使用 substr() 函数的方式C++:
if(data.find("PING :") != std::string::npos){
string pong = "PONG :" + data.substr( (data.find_last_of(":")+1), (data.find_last_of("\r")-1) );
SCHiMBot.Pong(pong); // Keep the connection alive!
}
其中 data 是保存服务器发送给我的所有信息的字符串,SCHiMBot 是我用来与服务器通信的类 这段代码是直接从我编码的机器人中编译出来的,所以它应该是完美的
I'm currently coding an irc bot in asm
I have already done this once in C++, so I know how to solve most problems I encounter, but I need a substr()[*] function like the one seen in C++. I need the substr function to receive the server name from a PING request so I can respond with the corresponding PONG response
But I don't know how to implent it in MASM, I heard of something called macroassembling, It seems substr is often used in those functions
Does anyone have any idea how I can get my substr function to work
[*] string substr ( size_t pos = 0, size_t n = npos )
This is how I use the substr() funcion in C++:
if(data.find("PING :") != std::string::npos){
string pong = "PONG :" + data.substr( (data.find_last_of(":")+1), (data.find_last_of("\r")-1) );
SCHiMBot.Pong(pong); // Keep the connection alive!
}
Where data is a string holding all the information the server sends me, and SCHiMBot is a class I use to talk with the server
This code is c&p'ed directly out of a bot I coded, so it should be flawless
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上并不像最初看起来那么容易回答。问题非常简单:像
substr
这样的函数并不是真正孤立存在的——它是字符串库的一部分,为了使其有用,您至少需要概述一下如何库作为一个整体组合在一起,如何表示数据等。即,substr
创建一个字符串,但要做到这一点,您需要确定字符串是什么。为了避免这个问题,我将忽略您实际询问的内容,并给出一个更适合汇编语言的更简单的答案。您真正需要的是从一个数据缓冲区开始,在该缓冲区中找到几个“标记”,然后将这些标记之间的内容复制到另一个缓冲区中的指定位置。首先我们需要代码来执行“find_last”:
现在要将子字符串复制到传输缓冲区,我们执行如下操作:
This really isn't nearly as easy to answer is it might initially seem. The problem is pretty simple: a function like
substr
doesn't really exist in isolation -- it's part of a string library, and to make it useful, you just about need to at least sketch out how the library as a whole fits together, how you represent your data, etc. I.e.,substr
creates a string, but to do so you need to decide what a string is.To avoid that problem, I'm going to sort of ignore what you actually asked, and give a somewhat simpler answer that's more suited to assembly language. What you really need is to start with one buffer of data, find a couple of "markers" in that buffer, and copy what's in between those markers to a designated position in another buffer. First we need the code to do the "find_last":
Now to copy the substring to the transmission buffer, we do something like this:
substr
的第一个参数是起始位置。如果它是传递字符串最后一个元素的值,则会抛出 out_of_range 异常。您应该验证这种情况没有发生。The first parameter of
substr
is the starting position. If it is a value passed the last element of the string, out_of_range exception will be thrown. You should verify that this is not happening.