xbuf_repl没有替换所有出现的事件

时间:2016-02-03 14:12:43

标签: api servlets g-wan

正如g-wan所记录的那样,xbuf_repl正在替换所有出现的事件。但我安装的g-wan,运行以下代码,只替换了匹配的第一次出现。

#include "gwan.h"

int main(int argc, char* argv[]){

    xbuf_t *reply = get_reply(argv);

    char str[ ] = "kjfdkkkkfldjfjfldkjdkkklfjworhg8kkkugpugulrghkkkr8g";
    xbuf_ncat(reply, str, sizeof(str)-1);
    xbuf_repl(reply, "kkk", "((()))");

    return 200;
}

输出为:kjfd((()))kfldjfjfldkjdkkklfjworhg8kkkugpugulrghkkkr8g

我的代码出了什么问题?如何解决它?

2 个答案:

答案 0 :(得分:0)

根据G-WAN API documentation。 。

// replace the first occurence of the 'old' string by the 'new' string in the buffer
char *xbuf_repl    (xbuf_t *ctx, char *old, char *new);

G-WAN也有这个API,(我刚刚从Gil的答案中学到的)也只是替换了第一次出现,但是在缓冲区的范围内,而不是从缓冲区的开头首次出现。 。 。

// same as above but using a range in the buffer
char *xbuf_replfrto(xbuf_t *ctx, char *beg, char *end, char *old, char *new);

Gil的答案显示了如何利用它来替换缓冲区中的所有事件。

答案 1 :(得分:0)

entity.c G-WAN示例中,您可以看到:

  // escape '<' because it cuts the text
  while(xbuf_replfrto(reply, pos, reply->ptr + reply->len - 13, "<", "&lt;"));

while()显示一次更换一个实例,这由G-WAN documentation确认:

// replace the first occurence of the 'old' string by the 'new' string in the buffer
char *xbuf_repl    (xbuf_t *ctx, char *old, char *new);