MINOR: buffer: replace buffer_replace2() with b_rep_blk()
This one is more generic and designed to work on a random block. It
may later get a b_rep_ist() variant since many strings are already
available as (ptr,len).
diff --git a/include/common/buf.h b/include/common/buf.h
index 1ad0052..b0b4e64 100644
--- a/include/common/buf.h
+++ b/include/common/buf.h
@@ -484,6 +484,42 @@
return len;
}
+/* b_rep_blk() : writes the block <blk> at position <pos> which must be in
+ * buffer <b>, and moves the part between <end> and the buffer's tail just
+ * after the end of the copy of <blk>. This effectively replaces the part
+ * located between <pos> and <end> with a copy of <blk> of length <len>. The
+ * buffer's length is automatically updated. This is used to replace a block
+ * with another one inside a buffer. The shift value (positive or negative) is
+ * returned. If there's no space left, the move is not done. If <len> is null,
+ * the <blk> pointer is allowed to be null, in order to erase a block.
+ */
+static inline int b_rep_blk(struct buffer *b, char *pos, char *end, const char *blk, size_t len)
+{
+ int delta;
+
+ delta = len - (end - pos);
+
+ if (b_tail(b) + delta > b_wrap(b))
+ return 0; /* no space left */
+
+ if (b_data(b) &&
+ b_tail(b) + delta > b_head(b) &&
+ b_head(b) >= b_tail(b))
+ return 0; /* no space left before wrapping data */
+
+ /* first, protect the end of the buffer */
+ memmove(end + delta, end, b_tail(b) - end);
+
+ /* now, copy blk over pos */
+ if (len)
+ memcpy(pos, blk, len);
+
+ b_add(b, delta);
+ b_realign_if_empty(b);
+
+ return delta;
+}
+
#endif /* _COMMON_BUF_H */
diff --git a/include/common/buffer.h b/include/common/buffer.h
index 3aaca53..1657275 100644
--- a/include/common/buffer.h
+++ b/include/common/buffer.h
@@ -50,7 +50,6 @@
int init_buffer();
void deinit_buffer();
-int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
void buffer_dump(FILE *o, struct buffer *b, int from, int to);
/*****************************************************************/