Created
November 24, 2009 19:43
-
-
Save tmountain/242134 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static VALUE | |
rb_str_squeeze_bang(int argc, VALUE *argv, VALUE str) | |
{ | |
char squeez[256]; | |
rb_encoding *enc = 0; | |
VALUE del = 0, nodel = 0; | |
char *s, *send, *t; | |
int save, modify = 0; | |
int i; | |
int ascompat, singlebyte = single_byte_optimizable(str); | |
if (argc == 0) { | |
enc = STR_ENC_GET(str); | |
} | |
else { | |
for (i=0; i<argc; i++) { | |
VALUE s = argv[i]; | |
StringValue(s); | |
enc = rb_enc_check(str, s); | |
if (singlebyte && !single_byte_optimizable(s)) | |
singlebyte = 0; | |
tr_setup_table(s, squeez, i==0, &del, &nodel, enc); | |
} | |
} | |
str_modify_keep_cr(str); | |
s = t = RSTRING_PTR(str); | |
if (!s || RSTRING_LEN(str) == 0) return Qnil; | |
send = RSTRING_END(str); | |
save = -1; | |
ascompat = rb_enc_asciicompat(enc); | |
if (singlebyte) { | |
while (s < send) { | |
unsigned int c = *(unsigned char*)s++; | |
if (c != save || (argc > 0 && !squeez[c])) { | |
*t++ = save = c; | |
} | |
} | |
} else { | |
while (s < send) { | |
unsigned int c; | |
int clen; | |
if (ascompat && (c = *(unsigned char*)s) < 0x80) { | |
if (c != save || (argc > 0 && !squeez[c])) { | |
*t++ = save = c; | |
} | |
s++; | |
} | |
else { | |
c = rb_enc_codepoint(s, send, enc); | |
clen = rb_enc_codelen(c, enc); | |
if (c != save || (argc > 0 && !tr_find(c, squeez, del, nodel))) { | |
if (t != s) rb_enc_mbcput(c, t, enc); | |
save = c; | |
t += clen; | |
} | |
s += clen; | |
} | |
} | |
} | |
*t = '\0'; | |
if (t - RSTRING_PTR(str) != RSTRING_LEN(str)) { | |
STR_SET_LEN(str, t - RSTRING_PTR(str)); | |
modify = 1; | |
} | |
if (modify) return str; | |
return Qnil; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment