squeeze!(...)
public
Squeezes str in place, returning either str, or
nil if no changes were made.
Show source
/*
* call-seq:
* str.squeeze!([other_str]*) => str or nil
*
* Squeezes <i>str</i> in place, returning either <i>str</i>, or
* <code>nil</code> if no changes were made.
*/
static VALUE
rb_str_squeeze_bang(argc, argv, str)
int argc;
VALUE *argv;
VALUE str;
{
char squeez[256];
char *s, *send, *t;
int c, save, modify = 0;
int init = 1;
int i;
if (argc == 0) {
for (i=0; i<256; i++) {
squeez[i] = 1;
}
}
else {
for (i=0; i<argc; i++) {
VALUE s = argv[i];
StringValue(s);
tr_setup_table(s, squeez, init);
init = 0;
}
}
rb_str_modify(str);
s = t = RSTRING(str)->ptr;
if (!s || RSTRING(str)->len == 0) return Qnil;
send = s + RSTRING(str)->len;
save = -1;
while (s < send) {
c = *s++ & 0xff;
if (c != save || !squeez[c]) {
*t++ = save = c;
}
}
*t = '\0';
if (t - RSTRING(str)->ptr != RSTRING(str)->len) {
RSTRING(str)->len = t - RSTRING(str)->ptr;
modify = 1;
}
if (modify) return str;
return Qnil;
}