Skip to content

Commit c4a13d4

Browse files
committed
Added support for older versions
1 parent 7b20075 commit c4a13d4

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

ext/stringio/extconf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# frozen_string_literal: false
22
require 'mkmf'
3+
have_func("rb_io_extract_modeenc", "ruby/io.h")
34
create_makefile('stringio')

ext/stringio/stringio.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,61 @@
2626
# define RB_INTEGER_TYPE_P(c) (FIXNUM_P(c) || RB_TYPE_P(c, T_BIGNUM))
2727
#endif
2828

29+
#ifndef HAVE_RB_IO_EXTRACT_MODEENC
30+
#define rb_io_extract_modeenc strio_extract_modeenc
31+
static void
32+
strio_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash,
33+
int *oflags_p, int *fmode_p, struct rb_io_enc_t *convconfig_p)
34+
{
35+
VALUE mode = *vmode_p;
36+
int fmode;
37+
38+
convconfig_p->enc = convconfig_p->enc2 = 0;
39+
if (NIL_P(mode)) {
40+
fmode = FMODE_READABLE;
41+
}
42+
else if (FIXNUM_P(mode)) {
43+
int flags = FIX2INT(mode);
44+
fmode = rb_io_oflags_fmode(flags);
45+
}
46+
else {
47+
const char *m = StringValueCStr(mode), *n, *e;
48+
fmode = rb_io_modestr_fmode(m);
49+
n = strchr(m, ':');
50+
if (n) {
51+
long len;
52+
char encname[ENCODING_MAXNAMELEN+1];
53+
if (fmode & FMODE_SETENC_BY_BOM) {
54+
n = strchr(n, '|');
55+
}
56+
e = strchr(++n, ':');
57+
len = e ? e - n : strlen(n);
58+
if (len > 0 && len <= ENCODING_MAXNAMELEN) {
59+
if (e) {
60+
memcpy(encname, n, len);
61+
encname[len] = '\0';
62+
n = encname;
63+
}
64+
convconfig_p->enc = rb_enc_find(n);
65+
}
66+
if (e && (len = strlen(++e)) > 0 && len <= ENCODING_MAXNAMELEN) {
67+
convconfig_p->enc2 = rb_enc_find(e);
68+
}
69+
}
70+
}
71+
72+
if (!NIL_P(opthash)) {
73+
rb_encoding *extenc = 0, *intenc = 0;
74+
if (rb_io_extract_encoding_option(opthash, &extenc, &intenc, &fmode)) {
75+
if (convconfig_p->enc || convconfig_p->enc2) {
76+
rb_raise(rb_eArgError, "encoding specified twice");
77+
}
78+
}
79+
}
80+
*fmode_p = fmode;
81+
}
82+
#endif
83+
2984
struct StringIO {
3085
VALUE string;
3186
rb_encoding *enc;

0 commit comments

Comments
 (0)