Skip to content

Commit 53def32

Browse files
committed
Supported mode: option
1 parent d28927b commit 53def32

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

ext/stringio/stringio.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ strio_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash,
3535
VALUE mode = *vmode_p;
3636
VALUE intmode;
3737
int fmode;
38+
int has_enc = 0, has_vmode = 0;
3839

3940
convconfig_p->enc = convconfig_p->enc2 = 0;
41+
42+
vmode_handle:
4043
if (NIL_P(mode)) {
4144
fmode = FMODE_READABLE;
4245
}
@@ -51,6 +54,7 @@ strio_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash,
5154
if (n) {
5255
long len;
5356
char encname[ENCODING_MAXNAMELEN+1];
57+
has_enc = 1;
5458
if (fmode & FMODE_SETENC_BY_BOM) {
5559
n = strchr(n, '|');
5660
}
@@ -72,8 +76,23 @@ strio_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash,
7276

7377
if (!NIL_P(opthash)) {
7478
rb_encoding *extenc = 0, *intenc = 0;
79+
VALUE v;
80+
if (!has_vmode) {
81+
ID id_mode;
82+
CONST_ID(id_mode, "mode");
83+
v = rb_hash_aref(opthash, ID2SYM(id_mode));
84+
if (!NIL_P(v)) {
85+
if (!NIL_P(mode)) {
86+
rb_raise(rb_eArgError, "mode specified twice");
87+
}
88+
has_vmode = 1;
89+
mode = v;
90+
goto vmode_handle;
91+
}
92+
}
93+
7594
if (rb_io_extract_encoding_option(opthash, &extenc, &intenc, &fmode)) {
76-
if (convconfig_p->enc || convconfig_p->enc2) {
95+
if (has_enc) {
7796
rb_raise(rb_eArgError, "encoding specified twice");
7897
}
7998
}

0 commit comments

Comments
 (0)