common/strbuf.c: fixing up squeeze.
This commit is contained in:
parent
73ac9837ff
commit
03c830be75
2 changed files with 16 additions and 9 deletions
|
|
@ -163,19 +163,21 @@ void strbuf_squeeze(strbuf_t *s, char ch) {
|
||||||
|
|
||||||
size_t p;
|
size_t p;
|
||||||
|
|
||||||
if (s->len <= 1)
|
for(p=s->len; p; p--) {
|
||||||
return;
|
|
||||||
|
|
||||||
for(p=s->len-1; p; p--) {
|
if (s->buf[p-1] != ch)
|
||||||
|
|
||||||
if (s->buf[p] != ch)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
size_t np = p, of = 0;
|
size_t np = p, of = 0;
|
||||||
|
|
||||||
for(; np && s->buf[np-1] == ch; np--)
|
for(; np-1 && s->buf[np-2] == ch; np--)
|
||||||
of++;
|
of++;
|
||||||
|
|
||||||
|
if (of) {
|
||||||
|
|
||||||
|
p = np;
|
||||||
for(s->len -= of; np <= s->len; np++)
|
for(s->len -= of; np <= s->len; np++)
|
||||||
s->buf[np] = s->buf[np + of];
|
s->buf[np] = s->buf[np + of];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ void test_squeeze() {
|
||||||
|
|
||||||
strbuf_squeeze(&b, 'X');
|
strbuf_squeeze(&b, 'X');
|
||||||
|
|
||||||
strbuf_append_str(&b, "aaabXXXcdefXXXXghijklXXmmmnopXXXXXqrstuXXvwxyXXzXX");
|
strbuf_append_str(&b, "aaabXXXcdefXXXXghijklXXmmmnopXXXXXqrXstuXXvwxyXXzXX");
|
||||||
strbuf_squeeze(&b, 'X');
|
strbuf_squeeze(&b, 'X');
|
||||||
print_strbuf(&b);
|
print_strbuf(&b);
|
||||||
strbuf_free(&b);
|
strbuf_free(&b);
|
||||||
|
|
@ -40,6 +40,11 @@ void test_squeeze() {
|
||||||
strbuf_squeeze(&b, 'X');
|
strbuf_squeeze(&b, 'X');
|
||||||
print_strbuf(&b);
|
print_strbuf(&b);
|
||||||
strbuf_free(&b);
|
strbuf_free(&b);
|
||||||
|
|
||||||
|
strbuf_append_str(&b, "A");
|
||||||
|
strbuf_squeeze(&b, 'X');
|
||||||
|
print_strbuf(&b);
|
||||||
|
strbuf_free(&b);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_chop() {
|
void test_chop() {
|
||||||
|
|
|
||||||
Reference in a new issue