-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrb_u_string_chop.c
More file actions
33 lines (28 loc) · 975 Bytes
/
Copy pathrb_u_string_chop.c
File metadata and controls
33 lines (28 loc) · 975 Bytes
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
#include "rb_includes.h"
/* Returns the receiver, minus its last character, inheriting any taint and
* untrust, unless the receiver is {#empty?} or if the last character is
* invalidly encoded, in which case the receiver is returned.
*
* If the last character is U+000A LINE FEED and the second-to-last character
* is U+000D CARRIAGE RETURN, both characters are removed.
*
* @return [U::String]
* @see #chomp
* @see #lstrip
* @see #rstrip
* @see #strip */
VALUE
rb_u_string_chop(VALUE self)
{
const struct rb_u_string *string = RVAL2USTRING(self);
if (USTRING_LENGTH(string) == 0)
return self;
const char *begin = USTRING_STR(string);
const char *end = USTRING_END(string);
const char *last;
uint32_t c = u_decode_r(&last, begin, end);
if (c == '\n')
if (*(last - 1) == '\r')
last--;
return rb_u_string_new_c(self, begin, last - begin);
}