@@ -1143,38 +1143,57 @@ struct getline_arg {
1143
1143
};
1144
1144
1145
1145
static struct getline_arg *
1146
- prepare_getline_args (struct getline_arg * arg , int argc , VALUE * argv )
1146
+ prepare_getline_args (struct StringIO * ptr , struct getline_arg * arg , int argc , VALUE * argv )
1147
1147
{
1148
- VALUE str , lim , opts ;
1148
+ VALUE rs , lim , opts ;
1149
1149
long limit = -1 ;
1150
1150
int respect_chomp ;
1151
1151
1152
- argc = rb_scan_args (argc , argv , "02:" , & str , & lim , & opts );
1153
- respect_chomp = argc == 0 || !NIL_P (str );
1152
+ argc = rb_scan_args (argc , argv , "02:" , & rs , & lim , & opts );
1153
+ respect_chomp = argc == 0 || !NIL_P (rs );
1154
1154
switch (argc ) {
1155
1155
case 0 :
1156
- str = rb_rs ;
1156
+ rs = rb_rs ;
1157
1157
break ;
1158
1158
1159
1159
case 1 :
1160
- if (!NIL_P (str ) && !RB_TYPE_P (str , T_STRING )) {
1161
- VALUE tmp = rb_check_string_type (str );
1160
+ if (!NIL_P (rs ) && !RB_TYPE_P (rs , T_STRING )) {
1161
+ VALUE tmp = rb_check_string_type (rs );
1162
1162
if (NIL_P (tmp )) {
1163
- limit = NUM2LONG (str );
1164
- str = rb_rs ;
1163
+ limit = NUM2LONG (rs );
1164
+ rs = rb_rs ;
1165
1165
}
1166
1166
else {
1167
- str = tmp ;
1167
+ rs = tmp ;
1168
1168
}
1169
1169
}
1170
1170
break ;
1171
1171
1172
1172
case 2 :
1173
- if (!NIL_P (str )) StringValue (str );
1173
+ if (!NIL_P (rs )) StringValue (rs );
1174
1174
if (!NIL_P (lim )) limit = NUM2LONG (lim );
1175
1175
break ;
1176
1176
}
1177
- arg -> rs = str ;
1177
+ if (!NIL_P (rs )) {
1178
+ rb_encoding * enc_rs , * enc_io ;
1179
+ enc_rs = rb_enc_get (rs );
1180
+ enc_io = get_enc (ptr );
1181
+ if (enc_rs != enc_io &&
1182
+ (rb_enc_str_coderange (rs ) != ENC_CODERANGE_7BIT ||
1183
+ (RSTRING_LEN (rs ) > 0 && !rb_enc_asciicompat (enc_io )))) {
1184
+ if (rs == rb_rs ) {
1185
+ rs = rb_enc_str_new (0 , 0 , enc_io );
1186
+ rb_str_buf_cat_ascii (rs , "\n" );
1187
+ rs = rs ;
1188
+ }
1189
+ else {
1190
+ rb_raise (rb_eArgError , "encoding mismatch: %s IO with %s RS" ,
1191
+ rb_enc_name (enc_io ),
1192
+ rb_enc_name (enc_rs ));
1193
+ }
1194
+ }
1195
+ }
1196
+ arg -> rs = rs ;
1178
1197
arg -> limit = limit ;
1179
1198
arg -> chomp = 0 ;
1180
1199
if (!NIL_P (opts )) {
@@ -1302,15 +1321,15 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
1302
1321
static VALUE
1303
1322
strio_gets (int argc , VALUE * argv , VALUE self )
1304
1323
{
1324
+ struct StringIO * ptr = readable (self );
1305
1325
struct getline_arg arg ;
1306
1326
VALUE str ;
1307
1327
1308
- if (prepare_getline_args (& arg , argc , argv )-> limit == 0 ) {
1309
- struct StringIO * ptr = readable (self );
1328
+ if (prepare_getline_args (ptr , & arg , argc , argv )-> limit == 0 ) {
1310
1329
return rb_enc_str_new (0 , 0 , get_enc (ptr ));
1311
1330
}
1312
1331
1313
- str = strio_getline (& arg , readable ( self ) );
1332
+ str = strio_getline (& arg , ptr );
1314
1333
rb_lastline_set (str );
1315
1334
return str ;
1316
1335
}
@@ -1347,16 +1366,16 @@ static VALUE
1347
1366
strio_each (int argc , VALUE * argv , VALUE self )
1348
1367
{
1349
1368
VALUE line ;
1369
+ struct StringIO * ptr = readable (self );
1350
1370
struct getline_arg arg ;
1351
1371
1352
- StringIO (self );
1353
1372
RETURN_ENUMERATOR (self , argc , argv );
1354
1373
1355
- if (prepare_getline_args (& arg , argc , argv )-> limit == 0 ) {
1374
+ if (prepare_getline_args (ptr , & arg , argc , argv )-> limit == 0 ) {
1356
1375
rb_raise (rb_eArgError , "invalid limit: 0 for each_line" );
1357
1376
}
1358
1377
1359
- while (!NIL_P (line = strio_getline (& arg , readable ( self ) ))) {
1378
+ while (!NIL_P (line = strio_getline (& arg , ptr ))) {
1360
1379
rb_yield (line );
1361
1380
}
1362
1381
return self ;
@@ -1374,15 +1393,15 @@ static VALUE
1374
1393
strio_readlines (int argc , VALUE * argv , VALUE self )
1375
1394
{
1376
1395
VALUE ary , line ;
1396
+ struct StringIO * ptr = readable (self );
1377
1397
struct getline_arg arg ;
1378
1398
1379
- StringIO (self );
1380
- ary = rb_ary_new ();
1381
- if (prepare_getline_args (& arg , argc , argv )-> limit == 0 ) {
1399
+ if (prepare_getline_args (ptr , & arg , argc , argv )-> limit == 0 ) {
1382
1400
rb_raise (rb_eArgError , "invalid limit: 0 for readlines" );
1383
1401
}
1384
1402
1385
- while (!NIL_P (line = strio_getline (& arg , readable (self )))) {
1403
+ ary = rb_ary_new ();
1404
+ while (!NIL_P (line = strio_getline (& arg , ptr ))) {
1386
1405
rb_ary_push (ary , line );
1387
1406
}
1388
1407
return ary ;
0 commit comments