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
105
106
107
|
# File 'ext/u/rb_u_string_chomp.c', line 65
VALUE
rb_u_string_chomp(int argc, VALUE *argv, VALUE self)
{
const struct rb_u_string *string = RVAL2USTRING(self);
long length = USTRING_LENGTH(string);
if (length == 0)
return Qnil;
VALUE rs;
if (argc == 0) {
rs = rb_rs;
if (rs == rb_default_rs)
return rb_u_string_chomp_default(self);
} else {
rb_scan_args(argc, argv, "01", &rs);
}
if (NIL_P(rs))
return self;
const struct rb_u_string *separator = RVAL2USTRING_ANY(rs);
long separator_length = USTRING_LENGTH(separator);
if (separator_length == 0)
return rb_u_string_chomp_newlines(self);
if (separator_length > length)
return self;
char last_char = USTRING_STR(separator)[separator_length - 1];
if (separator_length == 1 && last_char == '\n')
return rb_u_string_chomp_default(self);
if (!u_valid(USTRING_STR(separator), separator_length, NULL) ||
USTRING_STR(string)[length - 1] != last_char ||
(separator_length > 1 &&
rb_memcmp(USTRING_STR(separator),
USTRING_END(string) - separator_length,
separator_length) != 0))
return self;
return rb_u_string_new_c(self, USTRING_STR(string), length - separator_length);
}
|