Archived
1
0
Fork 0

common/strbuf.c: fixing up squeeze.

This commit is contained in:
Henrik Hautakoski 2010-10-04 22:20:54 +02:00
parent 73ac9837ff
commit 03c830be75
2 changed files with 16 additions and 9 deletions

View file

@ -163,19 +163,21 @@ void strbuf_squeeze(strbuf_t *s, char ch) {
size_t p;
if (s->len <= 1)
return;
for(p=s->len; p; p--) {
for(p=s->len-1; p; p--) {
if (s->buf[p] != ch)
if (s->buf[p-1] != ch)
continue;
size_t np = p, of = 0;
for(; np && s->buf[np-1] == ch; np--)
for(; np-1 && s->buf[np-2] == ch; np--)
of++;
for(s->len -= of; np <= s->len; np++)
s->buf[np] = s->buf[np + of];
if (of) {
p = np;
for(s->len -= of; np <= s->len; np++)
s->buf[np] = s->buf[np + of];
}
}
}

View file

@ -31,7 +31,7 @@ void test_squeeze() {
strbuf_squeeze(&b, 'X');
strbuf_append_str(&b, "aaabXXXcdefXXXXghijklXXmmmnopXXXXXqrstuXXvwxyXXzXX");
strbuf_append_str(&b, "aaabXXXcdefXXXXghijklXXmmmnopXXXXXqrXstuXXvwxyXXzXX");
strbuf_squeeze(&b, 'X');
print_strbuf(&b);
strbuf_free(&b);
@ -40,6 +40,11 @@ void test_squeeze() {
strbuf_squeeze(&b, 'X');
print_strbuf(&b);
strbuf_free(&b);
strbuf_append_str(&b, "A");
strbuf_squeeze(&b, 'X');
print_strbuf(&b);
strbuf_free(&b);
}
void test_chop() {