summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'string.c')
-rw-r--r--string.c29
1 files changed, 20 insertions, 9 deletions
diff --git a/string.c b/string.c
index 36f6888778..e6f72bc5e7 100644
--- a/string.c
+++ b/string.c
@@ -6059,21 +6059,25 @@ rb_str_count(int argc, VALUE *argv, VALUE str)
{
char table[TR_TABLE_SIZE];
rb_encoding *enc = 0;
- VALUE del = 0, nodel = 0;
+ VALUE del = 0, nodel = 0, tstr;
char *s, *send;
int i;
int ascompat;
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
- for (i=0; i<argc; i++) {
- VALUE tstr = argv[i];
- unsigned char c;
- StringValue(tstr);
- enc = rb_enc_check(str, tstr);
- if (argc == 1 && RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
- (c = RSTRING_PTR(tstr)[0]) < 0x80 && !is_broken_string(str)) {
+ tstr = argv[0];
+ StringValue(tstr);
+ enc = rb_enc_check(str, tstr);
+ if (argc == 1) {
+ const char *ptstr;
+ if (RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
+ (ptstr = RSTRING_PTR(tstr),
+ ONIGENC_IS_ALLOWED_REVERSE_MATCH(enc, (const unsigned char *)ptstr, (const unsigned char *)ptstr+1)) &&
+ !is_broken_string(str)) {
int n = 0;
+ int clen;
+ unsigned char c = rb_enc_codepoint_len(ptstr, ptstr+1, &clen, enc);
s = RSTRING_PTR(str);
if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
@@ -6083,7 +6087,14 @@ rb_str_count(int argc, VALUE *argv, VALUE str)
}
return INT2NUM(n);
}
- tr_setup_table(tstr, table, i==0, &del, &nodel, enc);
+ }
+
+ tr_setup_table(tstr, table, TRUE, &del, &nodel, enc);
+ for (i=1; i<argc; i++) {
+ tstr = argv[i];
+ StringValue(tstr);
+ enc = rb_enc_check(str, tstr);
+ tr_setup_table(tstr, table, FALSE, &del, &nodel, enc);
}
s = RSTRING_PTR(str);