-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrb_u_string_rindex.c
More file actions
104 lines (89 loc) · 3.63 KB
/
rb_u_string_rindex.c
File metadata and controls
104 lines (89 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "rb_includes.h"
long
rb_u_string_rindex(VALUE self, VALUE rbsubstring, long offset)
{
const struct rb_u_string *string = RVAL2USTRING(self);
const struct rb_u_string *substring = RVAL2USTRING_ANY(rbsubstring);
if (USTRING_LENGTH(string) < USTRING_LENGTH(substring))
return -1;
const char *s = rb_u_string_begin_from_offset(string, offset);
if (s == NULL)
return -1;
if (USTRING_LENGTH(substring) == 0)
return offset;
const char *begin = USTRING_STR(string);
const char *t = USTRING_STR(substring);
long t_length = USTRING_LENGTH(substring);
while (s >= begin) {
if (rb_memcmp(s, t, t_length) == 0)
return u_pointer_to_offset(begin, s);
s--;
}
return -1;
}
/* @overload rindex(pattern, offset = -1)
*
* Returns the maximal index of the receiver where PATTERN matches, equal to
* or less than _i_, where _i_ = OFFSET if OFFSET ≥ 0, _i_ = {#length} -
* abs(OFFSET) otherwise, or nil if there is no match.
*
* If PATTERN is a Regexp, the Regexp special variables `$&`, `$'`,
* <code>$\`</code>, `$1`, `$2`, …, `$`_n_ are updated accordingly.
*
* If PATTERN responds to `#to_str`, the matching is performed by a byte
* comparison.
*
* @param [Regexp, #to_str] pattern
* @param [#to_int] offset
* @return [Integer, nil]
* @see #index */
VALUE
rb_u_string_rindex_m(int argc, VALUE *argv, VALUE self)
{
const struct rb_u_string *string = RVAL2USTRING(self);
VALUE sub, rboffset;
long offset;
if (rb_scan_args(argc, argv, "11", &sub, &rboffset) == 2)
offset = NUM2LONG(rboffset);
else
/* TODO: Why not simply use -1? Benchmark which is faster. */
offset = u_n_chars_n(USTRING_STR(string), USTRING_LENGTH(string));
const char *begin = rb_u_string_begin_from_offset(string, offset);
const char *end = USTRING_END(string);
if (begin == NULL) {
if (offset <= 0) {
if (TYPE(sub) == T_REGEXP)
rb_backref_set(Qnil);
return Qnil;
}
begin = end;
/* TODO: this converting back and forward can be optimized away
* if rb_u_string_index_regexp() and rb_u_string_rindex() were split up
* into two additional functions, adding
* rb_u_string_index_regexp_pointer() and rb_u_string_rindex_pointer(),
* so that one can pass a pointer to start at immediately
* instead of an offset that gets calculated into a pointer. */
offset = u_n_chars_n(USTRING_STR(string), USTRING_LENGTH(string));
}
switch (TYPE(sub)) {
case T_REGEXP:
/* TODO: What’s this first test for, exactly? */
if (RREGEXP(sub)->ptr == NULL || RREGEXP_SRC_LEN(sub) > 0)
offset = rb_u_string_index_regexp(self, begin, sub, true);
break;
default: {
VALUE tmp = rb_check_string_type(sub);
if (NIL_P(tmp))
rb_u_raise(rb_eTypeError, "type mismatch: %s given",
rb_obj_classname(sub));
sub = tmp;
}
/* fall through */
case T_STRING:
offset = rb_u_string_rindex(self, sub, offset);
break;
}
if (offset < 0)
return Qnil;
return LONG2NUM(offset);
}