0

As g-wan documented, xbuf_repl is replacing all occurrences. But my installed g-wan, running the following code, only replaced the first occurrence of the matched.

#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;
}

output is : kjfd((()))kfldjfjfldkjdkkklfjworhg8kkkugpugulrghkkkr8g

What's wrong of my code? How to work around it?

Gil
  • 3,279
  • 1
  • 15
  • 25
k.k. lou
  • 1,805
  • 2
  • 13
  • 16
  • What version of G-WAN are you using? – Kenigmatic Feb 04 '16 at 07:28
  • what i meant the "document" is in xbuffer.h, line 85: `// replace all occurences of the 'old' string by the 'new' string in the buffer.` `char *xbuf_repl (xbuf_t *ctx, char *old, char *newstr); ` sorry, for i have not read the document in the official website. Pls update the line in xbuffer.h accordingly. – k.k. lou Feb 12 '16 at 16:18
  • Ah! That explains why you were expecting it to replace all occurrences. Sadly, If I correct the comment in xbuffer.h, it won't help anyone else. I am a user, just like you, and this isn't open source. :) – Kenigmatic Feb 12 '16 at 19:46

2 Answers2

0

According to the 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 also has this API, which (as I just learned from Gil's answer) also only replaces the first occurrence but within a range of the buffer rather than first occurrence from the beginning of the buffer . . . .

// 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's answer shows how you can make use of this to replace ALL occurrences within the buffer.

Ken

Kenigmatic
  • 448
  • 6
  • 16
0

In the entity.c G-WAN example, you can see:

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

This while() makes it obvious that one instance is replaced at a time, which is confirmed by the 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);
Gil
  • 3,279
  • 1
  • 15
  • 25
  • Hi Gil. It looks like I guessed wrong about xbuf_replfrto(...). Doh! I'll update my answer (and upvote yours). – Kenigmatic Feb 04 '16 at 18:39
  • what i meant the "document" is in xbuffer.h, line 85: // replace all occurences of the 'old' string by the 'new' string in the buffer char *xbuf_repl (xbuf_t *ctx, char *old, char *newstr); sorry, for i have not read the document in the official website. Pls update the line in xbuffer.h accordingly. – k.k. lou Feb 12 '16 at 15:16
  • Done in the codebase; will be published with the next release. Note that the Web site documented it 'correctly': http://www.gwan.ch/api states "replace the first occurence of the 'old' string by the 'new' string in the buffer". – Gil Feb 22 '16 at 15:25
  • Like many other users, including myself, he didn´t look at the website but he looked at the .h file comments. – Sir Rogers Nov 01 '16 at 19:46