I would like to use the xbuf_trunclen() and xbuf_truncptr() functions to cut my buffer but the functions do not seem to work.
Here's an example with an example running via char pointer:
char *string = (char*)strdup("---Boundary");
string += 3;
printf("String is '%s'", string);
Result: String is 'Boundary'
Here is what I would like to do but does not seem to work:
xbuf_t buffer;
xbuf_init(&buffer);
xbuf_cat(&buffer, "---Boundary");
xbuf_trunclen(&buffer, 3);
printf("Buffer is '%s'", buffer.ptr);
Result: Buffer is '---Boundary'
while I would like: Buffer is 'Boundary'
and with 'xbuf_trunptr()':
...
char *ptr = (char*)strdup("Boundary");
xbuf_truncptr(&buffer, ptr);
...
Result: Buffer is '---Boundary'
while I would like: Buffer is 'Boundary'
Is this an error of use on my part or a problem of operation of the 2 functions? The goal is to manipulate the beginning of the buffer.
In your code, you duplicate the
xbuffer's buffer so (ptr!=buffer.ptr):Doing so cannot work:
ptrmust belong to the [buffer.ptr,buffer.ptr + buffer.len] memory range. This would work as well: ptr[3] = 0;And, if you merely want to cut a string with xbuf_trunclen() that's faster to do the following:
buffer.ptr[3] = 0;
Hope this helps.
Note: in restrospective, looking at these two function makes me wondering why they were created in the first place. I guess it was for Java and C# APIs and developpers (as both languages don't use C-like "strings" and encapsulate them in hidden structures).