mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-18 02:39:44 +00:00
Fix bugs in strbuf_ncat(), strbuf_puts() and strbuf_putc()
Were not advancing the 'current' pointer the entire length of the appended text
This commit is contained in:
parent
69ff860b4f
commit
63d1792863
32
strbuf.c
32
strbuf.c
@ -45,9 +45,11 @@ strbuf strbuf_ncat(strbuf sb, const char *text, size_t len)
|
||||
{
|
||||
if (sb->start && sb->current < sb->end) {
|
||||
register size_t n = min(sb->end - sb->current, len);
|
||||
while (n-- && (*sb->current = *text++))
|
||||
++sb->current;
|
||||
char *c;
|
||||
for (c = sb->current; n-- && (*c = *text); ++c, ++text)
|
||||
;
|
||||
}
|
||||
sb->current += len;
|
||||
return sb;
|
||||
}
|
||||
|
||||
@ -55,8 +57,14 @@ strbuf strbuf_puts(strbuf sb, const char *text)
|
||||
{
|
||||
if (sb->start && sb->current < sb->end) {
|
||||
register size_t n = sb->end - sb->current;
|
||||
while (n-- && (*sb->current = *text++))
|
||||
while (n-- && (*sb->current = *text)) {
|
||||
++sb->current;
|
||||
++text;
|
||||
}
|
||||
}
|
||||
while (*text) {
|
||||
++sb->current;
|
||||
++text;
|
||||
}
|
||||
return sb;
|
||||
}
|
||||
@ -66,7 +74,8 @@ strbuf strbuf_putc(strbuf sb, char ch)
|
||||
if (sb->start && sb->current < sb->end) {
|
||||
*sb->current++ = ch;
|
||||
*sb->current = '\0';
|
||||
}
|
||||
} else
|
||||
++sb->current;
|
||||
return sb;
|
||||
}
|
||||
|
||||
@ -111,16 +120,15 @@ char *strbuf_substr(const_strbuf sb, int offset)
|
||||
|
||||
strbuf strbuf_trunc(strbuf sb, int offset)
|
||||
{
|
||||
char *s;
|
||||
if (offset < 0) {
|
||||
s = (sb->current < sb->end ? sb->current : sb->end) + offset;
|
||||
if (s < sb->start)
|
||||
s = sb->start;
|
||||
char *e = sb->current < sb->end ? sb->current : sb->end;
|
||||
sb->current = offset <= sb->start - e ? sb->start : e + offset;
|
||||
} else {
|
||||
s = sb->start + offset;
|
||||
if (s > sb->end)
|
||||
s = sb->end;
|
||||
char *s = sb->start + offset;
|
||||
if (s < sb->current)
|
||||
sb->current = s;
|
||||
}
|
||||
sb->current = s;
|
||||
if (sb->start && sb->current < sb->end)
|
||||
*sb->current = '\0';
|
||||
return sb;
|
||||
}
|
||||
|
17
strbuf.h
17
strbuf.h
@ -318,21 +318,26 @@ char *strbuf_substr(const_strbuf sb, int offset);
|
||||
* and the string's length truncated accordingly. Return a pointer to the
|
||||
* strbuf so that operations can be chained in a single line.
|
||||
*
|
||||
* After the operation:
|
||||
* After the operations:
|
||||
* count = strbuf_count(sb);
|
||||
* len = strbuf_len(sb);
|
||||
* strbuf_trunc(sb, off);
|
||||
* the following invariants hold:
|
||||
* where len <= off, sb is unchanged;
|
||||
* where 0 <= off < len:
|
||||
* if count <= off, sb is unchanged:
|
||||
* strbuf_count(sb) == count
|
||||
* strbuf_len(sb) == len
|
||||
* if len <= off < count:
|
||||
* strbuf_count(sb) == off
|
||||
* strbuf_len(sb) == len
|
||||
* if 0 <= off < len:
|
||||
* strbuf_count(sb) == off
|
||||
* strbuf_len(sb) == off
|
||||
* where -len <= off < 0:
|
||||
* if -len <= off < 0:
|
||||
* strbuf_count(sb) == len + off
|
||||
* strbuf_len(sb) == len + off
|
||||
* where off < -len:
|
||||
* if off < -len:
|
||||
* strbuf_count(sb) == 0
|
||||
* strbuf_len(sb) == 0
|
||||
* strbuf_str(sb)[0] == '\0'
|
||||
*
|
||||
* @author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user