Skip to content

Commit 70b49b6

Browse files
YO4nobu
authored andcommitted
refactor io_each_codepoint
1 parent f1765cd commit 70b49b6

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

io.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,8 +3156,6 @@ io_enc_str(VALUE str, rb_io_t *fptr)
31563156
return str;
31573157
}
31583158

3159-
static rb_encoding *io_read_encoding(rb_io_t *fptr);
3160-
31613159
static void
31623160
make_readconv(rb_io_t *fptr, int size)
31633161
{
@@ -4900,7 +4898,7 @@ static VALUE
49004898
rb_io_each_codepoint(VALUE io)
49014899
{
49024900
rb_io_t *fptr;
4903-
rb_encoding *enc, *read_enc;
4901+
rb_encoding *enc;
49044902
unsigned int c;
49054903
int r, n;
49064904

@@ -4909,20 +4907,17 @@ rb_io_each_codepoint(VALUE io)
49094907
rb_io_check_char_readable(fptr);
49104908

49114909
READ_CHECK(fptr);
4910+
enc = io_read_encoding(fptr);
49124911
if (NEED_READCONV(fptr)) {
49134912
SET_BINARY_MODE(fptr);
49144913
r = 1; /* no invalid char yet */
49154914
for (;;) {
49164915
make_readconv(fptr, 0);
4917-
read_enc = io_read_encoding(fptr);
49184916
for (;;) {
49194917
if (fptr->cbuf.len) {
4920-
if (read_enc)
4921-
r = rb_enc_precise_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
4922-
fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
4923-
read_enc);
4924-
else
4925-
r = ONIGENC_CONSTRUCT_MBCLEN_CHARFOUND(1);
4918+
r = rb_enc_precise_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
4919+
fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
4920+
enc);
49264921
if (!MBCLEN_NEEDMORE_P(r))
49274922
break;
49284923
if (fptr->cbuf.len == fptr->cbuf.capa) {
@@ -4932,33 +4927,25 @@ rb_io_each_codepoint(VALUE io)
49324927
if (more_char(fptr) == MORE_CHAR_FINISHED) {
49334928
clear_readconv(fptr);
49344929
if (!MBCLEN_CHARFOUND_P(r)) {
4935-
enc = read_enc;
49364930
goto invalid;
49374931
}
49384932
return io;
49394933
}
49404934
}
49414935
if (MBCLEN_INVALID_P(r)) {
4942-
enc = read_enc;
49434936
goto invalid;
49444937
}
49454938
n = MBCLEN_CHARFOUND_LEN(r);
4946-
if (read_enc) {
4947-
c = rb_enc_codepoint(fptr->cbuf.ptr+fptr->cbuf.off,
4948-
fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
4949-
read_enc);
4950-
}
4951-
else {
4952-
c = (unsigned char)fptr->cbuf.ptr[fptr->cbuf.off];
4953-
}
4939+
c = rb_enc_codepoint(fptr->cbuf.ptr+fptr->cbuf.off,
4940+
fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
4941+
enc);
49544942
fptr->cbuf.off += n;
49554943
fptr->cbuf.len -= n;
49564944
rb_yield(UINT2NUM(c));
49574945
rb_io_check_char_readable(fptr);
49584946
}
49594947
}
49604948
NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
4961-
enc = io_input_encoding(fptr);
49624949
while (io_fillbuf(fptr) >= 0) {
49634950
r = rb_enc_precise_mbclen(fptr->rbuf.ptr+fptr->rbuf.off,
49644951
fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);

0 commit comments

Comments
 (0)