summaryrefslogtreecommitdiff
path: root/ext/stringio
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2022-05-26 15:01:44 -0700
committergit <svn-admin@ruby-lang.org>2022-05-30 13:10:46 +0900
commit609d73e8925f807786686caf635178bb1de74256 (patch)
tree7c8b1687029990c8196aadcb1c894db3118c59cb /ext/stringio
parent7e3920f0d9875765fc337daf21a3b47d8c51f8cc (diff)
downloadruby-609d73e8925f807786686caf635178bb1de74256.tar.gz
[ruby/stringio] Fix handling of chomp with paragraph separator
Try to mirror IO behavior, where chomp takes out the entire paragraph separators between entries, but does not chomp a single line separator at the end of the string. Partially Fixes [Bug #18768] https://github.com/ruby/stringio/commit/a83ddbb7f0
Diffstat (limited to 'ext/stringio')
-rw-r--r--ext/stringio/stringio.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 3f66fb17d2..13c8af9216 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -1204,6 +1204,7 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
str = strio_substr(ptr, ptr->pos, e - s - w, enc);
}
else if ((n = RSTRING_LEN(str)) == 0) {
+ const char *paragraph_end = NULL;
p = s;
while (p[(p + 1 < e) && (*p == '\r') && 0] == '\n') {
p += *p == '\r';
@@ -1213,19 +1214,21 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
}
s = p;
while ((p = memchr(p, '\n', e - p)) && (p != e)) {
- if (*++p == '\n') {
- e = p + 1;
- w = (arg->chomp ? 1 : 0);
- break;
- }
- else if (*p == '\r' && p < e && p[1] == '\n') {
- e = p + 2;
- w = (arg->chomp ? 2 : 0);
- break;
- }
+ p++;
+ if (!((p < e && *p == '\n') ||
+ (p + 1 < e && *p == '\r' && *(p+1) == '\n'))) {
+ continue;
+ }
+ paragraph_end = p - ((*(p-2) == '\r') ? 2 : 1);
+ while ((p < e && *p == '\n') ||
+ (p + 1 < e && *p == '\r' && *(p+1) == '\n')) {
+ p += (*p == '\r') ? 2 : 1;
+ }
+ e = p;
+ break;
}
- if (!w && arg->chomp) {
- w = chomp_newline_width(s, e);
+ if (arg->chomp && paragraph_end) {
+ w = e - paragraph_end;
}
str = strio_substr(ptr, s - RSTRING_PTR(ptr->string), e - s - w, enc);
}