stringio 3.0.7 → 3.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/ext/stringio/stringio.c +94 -24
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f7f9c928c654ab73856e461f75e7f6171252633858472336a8900cb37c06d15
4
- data.tar.gz: eb1583a64b9d4f4fdd46e3bffba21edb124074b7f1ce8f6c9cf25e5c9c45ae64
3
+ metadata.gz: 5c756222b86fbf0eb9e39360a3541b2877a85da13658be6119fa5582205a103e
4
+ data.tar.gz: 82da787881d2e0a49e134202f68ec0bba3e23976227bdfac04a58813dd20d7db
5
5
  SHA512:
6
- metadata.gz: f8a13e0f7352087b716f1516829275c1ca5765cd3587c49f0c8bcd7ec3ff718b4fd09b3280159bb2fd1b4ab487e960468b361eda6121c6b45ed78b868b897bc2
7
- data.tar.gz: 805c032b64a74d49c2583230a2116ea955e7be9ad52f635fbfcefdfaf7586b0ccc51463dbc3f7e7ece15db8abc069fb74d2616dd6e165bf6bf6ba4d6070a17da
6
+ metadata.gz: f49902b5785864fbd00c4e12b3f62b1ab0d4595943f256af7e7b4bf23647d4ef9e98d3637bd20eb89cd7030c1dcb8e1995d2b299904d4b33cb5b1d78326f5a18
7
+ data.tar.gz: '08a58465652dbdf23df245db28129c5d2416ae6fc91d00376894754a896b526736d964d466630e62427b4b56dc07e9ca9242e1f49b1648d2eb0e4f8df09a4b66'
data/README.md CHANGED
@@ -34,7 +34,7 @@ Or install it yourself as:
34
34
 
35
35
  Run `bundle install` to install dependencies and then `bundle exec rake test` to run the tests.
36
36
 
37
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, author a NEWS.md section, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
38
 
39
39
  ## Contributing
40
40
 
@@ -12,7 +12,8 @@
12
12
 
13
13
  **********************************************************************/
14
14
 
15
- #define STRINGIO_VERSION "3.0.7"
15
+ static const char *const
16
+ STRINGIO_VERSION = "3.0.9";
16
17
 
17
18
  #include "ruby.h"
18
19
  #include "ruby/io.h"
@@ -603,7 +604,7 @@ strio_to_read(VALUE self)
603
604
  * eof? -> true or false
604
605
  *
605
606
  * Returns +true+ if positioned at end-of-stream, +false+ otherwise;
606
- * see {Position}[rdoc-ref:File@Position].
607
+ * see {Position}[rdoc-ref:IO@Position].
607
608
  *
608
609
  * Raises IOError if the stream is not opened for reading.
609
610
  */
@@ -1142,38 +1143,57 @@ struct getline_arg {
1142
1143
  };
1143
1144
 
1144
1145
  static struct getline_arg *
1145
- 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)
1146
1147
  {
1147
- VALUE str, lim, opts;
1148
+ VALUE rs, lim, opts;
1148
1149
  long limit = -1;
1149
1150
  int respect_chomp;
1150
1151
 
1151
- argc = rb_scan_args(argc, argv, "02:", &str, &lim, &opts);
1152
- 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);
1153
1154
  switch (argc) {
1154
1155
  case 0:
1155
- str = rb_rs;
1156
+ rs = rb_rs;
1156
1157
  break;
1157
1158
 
1158
1159
  case 1:
1159
- if (!NIL_P(str) && !RB_TYPE_P(str, T_STRING)) {
1160
- 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);
1161
1162
  if (NIL_P(tmp)) {
1162
- limit = NUM2LONG(str);
1163
- str = rb_rs;
1163
+ limit = NUM2LONG(rs);
1164
+ rs = rb_rs;
1164
1165
  }
1165
1166
  else {
1166
- str = tmp;
1167
+ rs = tmp;
1167
1168
  }
1168
1169
  }
1169
1170
  break;
1170
1171
 
1171
1172
  case 2:
1172
- if (!NIL_P(str)) StringValue(str);
1173
+ if (!NIL_P(rs)) StringValue(rs);
1173
1174
  if (!NIL_P(lim)) limit = NUM2LONG(lim);
1174
1175
  break;
1175
1176
  }
1176
- 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;
1177
1197
  arg->limit = limit;
1178
1198
  arg->chomp = 0;
1179
1199
  if (!NIL_P(opts)) {
@@ -1301,15 +1321,15 @@ strio_getline(struct getline_arg *arg, struct StringIO *ptr)
1301
1321
  static VALUE
1302
1322
  strio_gets(int argc, VALUE *argv, VALUE self)
1303
1323
  {
1324
+ struct StringIO *ptr = readable(self);
1304
1325
  struct getline_arg arg;
1305
1326
  VALUE str;
1306
1327
 
1307
- if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
1308
- struct StringIO *ptr = readable(self);
1328
+ if (prepare_getline_args(ptr, &arg, argc, argv)->limit == 0) {
1309
1329
  return rb_enc_str_new(0, 0, get_enc(ptr));
1310
1330
  }
1311
1331
 
1312
- str = strio_getline(&arg, readable(self));
1332
+ str = strio_getline(&arg, ptr);
1313
1333
  rb_lastline_set(str);
1314
1334
  return str;
1315
1335
  }
@@ -1346,16 +1366,16 @@ static VALUE
1346
1366
  strio_each(int argc, VALUE *argv, VALUE self)
1347
1367
  {
1348
1368
  VALUE line;
1369
+ struct StringIO *ptr = readable(self);
1349
1370
  struct getline_arg arg;
1350
1371
 
1351
- StringIO(self);
1352
1372
  RETURN_ENUMERATOR(self, argc, argv);
1353
1373
 
1354
- if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
1374
+ if (prepare_getline_args(ptr, &arg, argc, argv)->limit == 0) {
1355
1375
  rb_raise(rb_eArgError, "invalid limit: 0 for each_line");
1356
1376
  }
1357
1377
 
1358
- while (!NIL_P(line = strio_getline(&arg, readable(self)))) {
1378
+ while (!NIL_P(line = strio_getline(&arg, ptr))) {
1359
1379
  rb_yield(line);
1360
1380
  }
1361
1381
  return self;
@@ -1373,15 +1393,15 @@ static VALUE
1373
1393
  strio_readlines(int argc, VALUE *argv, VALUE self)
1374
1394
  {
1375
1395
  VALUE ary, line;
1396
+ struct StringIO *ptr = readable(self);
1376
1397
  struct getline_arg arg;
1377
1398
 
1378
- StringIO(self);
1379
- ary = rb_ary_new();
1380
- if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
1399
+ if (prepare_getline_args(ptr, &arg, argc, argv)->limit == 0) {
1381
1400
  rb_raise(rb_eArgError, "invalid limit: 0 for readlines");
1382
1401
  }
1383
1402
 
1384
- while (!NIL_P(line = strio_getline(&arg, readable(self)))) {
1403
+ ary = rb_ary_new();
1404
+ while (!NIL_P(line = strio_getline(&arg, ptr))) {
1385
1405
  rb_ary_push(ary, line);
1386
1406
  }
1387
1407
  return ary;
@@ -1583,6 +1603,55 @@ strio_read(int argc, VALUE *argv, VALUE self)
1583
1603
  return str;
1584
1604
  }
1585
1605
 
1606
+ /*
1607
+ * call-seq:
1608
+ * pread(maxlen, offset) -> string
1609
+ * pread(maxlen, offset, out_string) -> string
1610
+ *
1611
+ * See IO#pread.
1612
+ */
1613
+ static VALUE
1614
+ strio_pread(int argc, VALUE *argv, VALUE self)
1615
+ {
1616
+ VALUE rb_len, rb_offset, rb_buf;
1617
+ rb_scan_args(argc, argv, "21", &rb_len, &rb_offset, &rb_buf);
1618
+ long len = NUM2LONG(rb_len);
1619
+ long offset = NUM2LONG(rb_offset);
1620
+
1621
+ if (len < 0) {
1622
+ rb_raise(rb_eArgError, "negative string size (or size too big): %" PRIsVALUE, rb_len);
1623
+ }
1624
+
1625
+ if (len == 0) {
1626
+ if (NIL_P(rb_buf)) {
1627
+ return rb_str_new("", 0);
1628
+ }
1629
+ return rb_buf;
1630
+ }
1631
+
1632
+ if (offset < 0) {
1633
+ rb_syserr_fail_str(EINVAL, rb_sprintf("pread: Invalid offset argument: %" PRIsVALUE, rb_offset));
1634
+ }
1635
+
1636
+ struct StringIO *ptr = readable(self);
1637
+
1638
+ if (offset >= RSTRING_LEN(ptr->string)) {
1639
+ rb_eof_error();
1640
+ }
1641
+
1642
+ if (NIL_P(rb_buf)) {
1643
+ return strio_substr(ptr, offset, len, rb_ascii8bit_encoding());
1644
+ }
1645
+
1646
+ long rest = RSTRING_LEN(ptr->string) - offset;
1647
+ if (len > rest) len = rest;
1648
+ rb_str_resize(rb_buf, len);
1649
+ rb_enc_associate(rb_buf, rb_ascii8bit_encoding());
1650
+ MEMCPY(RSTRING_PTR(rb_buf), RSTRING_PTR(ptr->string) + offset, char, len);
1651
+ return rb_buf;
1652
+ }
1653
+
1654
+
1586
1655
  /*
1587
1656
  * call-seq:
1588
1657
  * strio.sysread(integer[, outbuf]) -> string
@@ -1843,6 +1912,7 @@ Init_stringio(void)
1843
1912
  rb_define_method(StringIO, "gets", strio_gets, -1);
1844
1913
  rb_define_method(StringIO, "readlines", strio_readlines, -1);
1845
1914
  rb_define_method(StringIO, "read", strio_read, -1);
1915
+ rb_define_method(StringIO, "pread", strio_pread, -1);
1846
1916
 
1847
1917
  rb_define_method(StringIO, "write", strio_write_m, -1);
1848
1918
  rb_define_method(StringIO, "putc", strio_putc, 1);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stringio
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.7
4
+ version: 3.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nobu Nakada
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-06-02 00:00:00.000000000 Z
12
+ date: 2023-11-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Pseudo `IO` class from/to `String`.
15
15
  email:
@@ -43,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
43
  - !ruby/object:Gem::Version
44
44
  version: '0'
45
45
  requirements: []
46
- rubygems_version: 3.5.0.dev
46
+ rubygems_version: 3.4.10
47
47
  signing_key:
48
48
  specification_version: 4
49
49
  summary: Pseudo IO on String