Ruby  2.5.0dev(2017-10-22revision60238)
stringio.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  stringio.c -
4 
5  $Author$
6  $RoughId: stringio.c,v 1.13 2002/03/14 03:24:18 nobu Exp $
7  created at: Tue Feb 19 04:10:38 JST 2002
8 
9  All the files in this distribution are covered under the Ruby's
10  license (see the file COPYING).
11 
12 **********************************************************************/
13 
14 #include "ruby.h"
15 #include "ruby/io.h"
16 #include "ruby/encoding.h"
17 #if defined(HAVE_FCNTL_H) || defined(_WIN32)
18 #include <fcntl.h>
19 #elif defined(HAVE_SYS_FCNTL_H)
20 #include <sys/fcntl.h>
21 #endif
22 
23 #ifndef RB_INTEGER_TYPE_P
24 # define RB_INTEGER_TYPE_P(c) (FIXNUM_P(c) || RB_TYPE_P(c, T_BIGNUM))
25 #endif
26 
27 struct StringIO {
30  long pos;
31  long lineno;
32  int flags;
33  int count;
34 };
35 
36 static VALUE strio_init(int, VALUE *, struct StringIO *, VALUE);
37 static VALUE strio_unget_bytes(struct StringIO *, const char *, long);
38 
39 #define IS_STRIO(obj) (rb_typeddata_is_kind_of((obj), &strio_data_type))
40 #define error_inval(msg) (rb_syserr_fail(EINVAL, msg))
41 #define get_enc(ptr) ((ptr)->enc ? (ptr)->enc : rb_enc_get((ptr)->string))
42 
43 static struct StringIO *
44 strio_alloc(void)
45 {
46  struct StringIO *ptr = ALLOC(struct StringIO);
47  ptr->string = Qnil;
48  ptr->pos = 0;
49  ptr->lineno = 0;
50  ptr->flags = 0;
51  ptr->count = 1;
52  return ptr;
53 }
54 
55 static void
56 strio_mark(void *p)
57 {
58  struct StringIO *ptr = p;
59 
60  rb_gc_mark(ptr->string);
61 }
62 
63 static void
64 strio_free(void *p)
65 {
66  struct StringIO *ptr = p;
67  if (--ptr->count <= 0) {
68  xfree(ptr);
69  }
70 }
71 
72 static size_t
73 strio_memsize(const void *p)
74 {
75  return sizeof(struct StringIO);
76 }
77 
78 static const rb_data_type_t strio_data_type = {
79  "strio",
80  {
81  strio_mark,
82  strio_free,
83  strio_memsize,
84  },
86 };
87 
88 #define check_strio(self) ((struct StringIO*)rb_check_typeddata((self), &strio_data_type))
89 
90 static struct StringIO*
91 get_strio(VALUE self)
92 {
93  struct StringIO *ptr = check_strio(rb_io_taint_check(self));
94 
95  if (!ptr) {
96  rb_raise(rb_eIOError, "uninitialized stream");
97  }
98  return ptr;
99 }
100 
101 static VALUE
102 enc_subseq(VALUE str, long pos, long len, rb_encoding *enc)
103 {
104  str = rb_str_subseq(str, pos, len);
105  rb_enc_associate(str, enc);
106  return str;
107 }
108 
109 static VALUE
110 strio_substr(struct StringIO *ptr, long pos, long len, rb_encoding *enc)
111 {
112  VALUE str = ptr->string;
113  long rlen = RSTRING_LEN(str) - pos;
114 
115  if (len > rlen) len = rlen;
116  if (len < 0) len = 0;
117  if (len == 0) return rb_enc_str_new(0, 0, enc);
118  return enc_subseq(str, pos, len, enc);
119 }
120 
121 #define StringIO(obj) get_strio(obj)
122 
123 #define STRIO_READABLE FL_USER4
124 #define STRIO_WRITABLE FL_USER5
125 #define STRIO_READWRITE (STRIO_READABLE|STRIO_WRITABLE)
127 #define STRIO_MODE_SET_P(strio, mode) \
128  ((RBASIC(strio)->flags & STRIO_##mode) && \
129  ((struct StringIO*)DATA_PTR(strio))->flags & FMODE_##mode)
130 #define CLOSED(strio) (!STRIO_MODE_SET_P(strio, READWRITE))
131 #define READABLE(strio) STRIO_MODE_SET_P(strio, READABLE)
132 #define WRITABLE(strio) STRIO_MODE_SET_P(strio, WRITABLE)
133 
134 static VALUE sym_exception;
135 
136 static struct StringIO*
137 readable(VALUE strio)
138 {
139  struct StringIO *ptr = StringIO(strio);
140  if (!READABLE(strio)) {
141  rb_raise(rb_eIOError, "not opened for reading");
142  }
143  return ptr;
144 }
145 
146 static struct StringIO*
147 writable(VALUE strio)
148 {
149  struct StringIO *ptr = StringIO(strio);
150  if (!WRITABLE(strio)) {
151  rb_raise(rb_eIOError, "not opened for writing");
152  }
153  return ptr;
154 }
155 
156 static void
157 check_modifiable(struct StringIO *ptr)
158 {
159  if (OBJ_FROZEN(ptr->string)) {
160  rb_raise(rb_eIOError, "not modifiable string");
161  }
162 }
163 
164 static VALUE
165 strio_s_allocate(VALUE klass)
166 {
167  return TypedData_Wrap_Struct(klass, &strio_data_type, 0);
168 }
169 
170 /*
171  * call-seq: StringIO.new(string=""[, mode])
172  *
173  * Creates new StringIO instance from with _string_ and _mode_.
174  */
175 static VALUE
176 strio_initialize(int argc, VALUE *argv, VALUE self)
177 {
178  struct StringIO *ptr = check_strio(self);
179 
180  if (!ptr) {
181  DATA_PTR(self) = ptr = strio_alloc();
182  }
183  rb_call_super(0, 0);
184  return strio_init(argc, argv, ptr, self);
185 }
186 
187 static VALUE
188 strio_init(int argc, VALUE *argv, struct StringIO *ptr, VALUE self)
189 {
190  VALUE string, mode;
191  int trunc = 0;
192 
193  switch (rb_scan_args(argc, argv, "02", &string, &mode)) {
194  case 2:
195  if (FIXNUM_P(mode)) {
196  int flags = FIX2INT(mode);
197  ptr->flags = rb_io_oflags_fmode(flags);
198  trunc = flags & O_TRUNC;
199  }
200  else {
201  const char *m = StringValueCStr(mode);
202  ptr->flags = rb_io_modestr_fmode(m);
203  trunc = *m == 'w';
204  }
205  StringValue(string);
206  if ((ptr->flags & FMODE_WRITABLE) && OBJ_FROZEN(string)) {
207  rb_syserr_fail(EACCES, 0);
208  }
209  if (trunc) {
210  rb_str_resize(string, 0);
211  }
212  break;
213  case 1:
214  StringValue(string);
215  ptr->flags = OBJ_FROZEN(string) ? FMODE_READABLE : FMODE_READWRITE;
216  break;
217  case 0:
218  string = rb_enc_str_new("", 0, rb_default_external_encoding());
219  ptr->flags = FMODE_READWRITE;
220  break;
221  }
222  ptr->string = string;
223  ptr->enc = 0;
224  ptr->pos = 0;
225  ptr->lineno = 0;
226  RBASIC(self)->flags |= (ptr->flags & FMODE_READWRITE) * (STRIO_READABLE / FMODE_READABLE);
227  return self;
228 }
229 
230 static VALUE
231 strio_finalize(VALUE self)
232 {
233  struct StringIO *ptr = StringIO(self);
234  ptr->string = Qnil;
235  ptr->flags &= ~FMODE_READWRITE;
236  return self;
237 }
238 
239 /*
240  * call-seq: StringIO.open(string=""[, mode]) {|strio| ...}
241  *
242  * Equivalent to StringIO.new except that when it is called with a block, it
243  * yields with the new instance and closes it, and returns the result which
244  * returned from the block.
245  */
246 static VALUE
247 strio_s_open(int argc, VALUE *argv, VALUE klass)
248 {
249  VALUE obj = rb_class_new_instance(argc, argv, klass);
250  if (!rb_block_given_p()) return obj;
251  return rb_ensure(rb_yield, obj, strio_finalize, obj);
252 }
253 
254 /* :nodoc: */
255 static VALUE
256 strio_s_new(int argc, VALUE *argv, VALUE klass)
257 {
258  if (rb_block_given_p()) {
259  VALUE cname = rb_obj_as_string(klass);
260 
261  rb_warn("%"PRIsVALUE"::new() does not take block; use %"PRIsVALUE"::open() instead",
262  cname, cname);
263  }
264  return rb_class_new_instance(argc, argv, klass);
265 }
266 
267 /*
268  * Returns +false+. Just for compatibility to IO.
269  */
270 static VALUE
271 strio_false(VALUE self)
272 {
273  StringIO(self);
274  return Qfalse;
275 }
276 
277 /*
278  * Returns +nil+. Just for compatibility to IO.
279  */
280 static VALUE
281 strio_nil(VALUE self)
282 {
283  StringIO(self);
284  return Qnil;
285 }
286 
287 /*
288  * Returns *strio* itself. Just for compatibility to IO.
289  */
290 static VALUE
291 strio_self(VALUE self)
292 {
293  StringIO(self);
294  return self;
295 }
296 
297 /*
298  * Returns 0. Just for compatibility to IO.
299  */
300 static VALUE
301 strio_0(VALUE self)
302 {
303  StringIO(self);
304  return INT2FIX(0);
305 }
306 
307 /*
308  * Returns the argument unchanged. Just for compatibility to IO.
309  */
310 static VALUE
311 strio_first(VALUE self, VALUE arg)
312 {
313  StringIO(self);
314  return arg;
315 }
316 
317 /*
318  * Raises NotImplementedError.
319  */
320 static VALUE
321 strio_unimpl(int argc, VALUE *argv, VALUE self)
322 {
323  StringIO(self);
324  rb_notimplement();
325 
326  UNREACHABLE;
327 }
328 
329 /*
330  * call-seq: strio.string -> string
331  *
332  * Returns underlying String object, the subject of IO.
333  */
334 static VALUE
335 strio_get_string(VALUE self)
336 {
337  return StringIO(self)->string;
338 }
339 
340 /*
341  * call-seq:
342  * strio.string = string -> string
343  *
344  * Changes underlying String object, the subject of IO.
345  */
346 static VALUE
347 strio_set_string(VALUE self, VALUE string)
348 {
349  struct StringIO *ptr = StringIO(self);
350 
351  rb_io_taint_check(self);
352  ptr->flags &= ~FMODE_READWRITE;
353  StringValue(string);
354  ptr->flags = OBJ_FROZEN(string) ? FMODE_READABLE : FMODE_READWRITE;
355  ptr->pos = 0;
356  ptr->lineno = 0;
357  return ptr->string = string;
358 }
359 
360 /*
361  * call-seq:
362  * strio.close -> nil
363  *
364  * Closes strio. The *strio* is unavailable for any further data
365  * operations; an +IOError+ is raised if such an attempt is made.
366  */
367 static VALUE
368 strio_close(VALUE self)
369 {
370  StringIO(self);
371  RBASIC(self)->flags &= ~STRIO_READWRITE;
372  return Qnil;
373 }
374 
375 /*
376  * call-seq:
377  * strio.close_read -> nil
378  *
379  * Closes the read end of a StringIO. Will raise an +IOError+ if the
380  * *strio* is not readable.
381  */
382 static VALUE
383 strio_close_read(VALUE self)
384 {
385  struct StringIO *ptr = StringIO(self);
386  if (!(ptr->flags & FMODE_READABLE)) {
387  rb_raise(rb_eIOError, "closing non-duplex IO for reading");
388  }
389  RBASIC(self)->flags &= ~STRIO_READABLE;
390  return Qnil;
391 }
392 
393 /*
394  * call-seq:
395  * strio.close_write -> nil
396  *
397  * Closes the write end of a StringIO. Will raise an +IOError+ if the
398  * *strio* is not writeable.
399  */
400 static VALUE
401 strio_close_write(VALUE self)
402 {
403  struct StringIO *ptr = StringIO(self);
404  if (!(ptr->flags & FMODE_WRITABLE)) {
405  rb_raise(rb_eIOError, "closing non-duplex IO for writing");
406  }
407  RBASIC(self)->flags &= ~STRIO_WRITABLE;
408  return Qnil;
409 }
410 
411 /*
412  * call-seq:
413  * strio.closed? -> true or false
414  *
415  * Returns +true+ if *strio* is completely closed, +false+ otherwise.
416  */
417 static VALUE
418 strio_closed(VALUE self)
419 {
420  StringIO(self);
421  if (!CLOSED(self)) return Qfalse;
422  return Qtrue;
423 }
424 
425 /*
426  * call-seq:
427  * strio.closed_read? -> true or false
428  *
429  * Returns +true+ if *strio* is not readable, +false+ otherwise.
430  */
431 static VALUE
432 strio_closed_read(VALUE self)
433 {
434  StringIO(self);
435  if (READABLE(self)) return Qfalse;
436  return Qtrue;
437 }
438 
439 /*
440  * call-seq:
441  * strio.closed_write? -> true or false
442  *
443  * Returns +true+ if *strio* is not writable, +false+ otherwise.
444  */
445 static VALUE
446 strio_closed_write(VALUE self)
447 {
448  StringIO(self);
449  if (WRITABLE(self)) return Qfalse;
450  return Qtrue;
451 }
452 
453 /*
454  * call-seq:
455  * strio.eof -> true or false
456  * strio.eof? -> true or false
457  *
458  * Returns true if *strio* is at end of file. The stringio must be
459  * opened for reading or an +IOError+ will be raised.
460  */
461 static VALUE
462 strio_eof(VALUE self)
463 {
464  struct StringIO *ptr = readable(self);
465  if (ptr->pos < RSTRING_LEN(ptr->string)) return Qfalse;
466  return Qtrue;
467 }
468 
469 /* :nodoc: */
470 static VALUE
471 strio_copy(VALUE copy, VALUE orig)
472 {
473  struct StringIO *ptr;
474 
475  orig = rb_convert_type(orig, T_DATA, "StringIO", "to_strio");
476  if (copy == orig) return copy;
477  ptr = StringIO(orig);
478  if (check_strio(copy)) {
479  strio_free(DATA_PTR(copy));
480  }
481  DATA_PTR(copy) = ptr;
482  OBJ_INFECT(copy, orig);
483  RBASIC(copy)->flags &= ~STRIO_READWRITE;
484  RBASIC(copy)->flags |= RBASIC(orig)->flags & STRIO_READWRITE;
485  ++ptr->count;
486  return copy;
487 }
488 
489 /*
490  * call-seq:
491  * strio.lineno -> integer
492  *
493  * Returns the current line number in *strio*. The stringio must be
494  * opened for reading. +lineno+ counts the number of times +gets+ is
495  * called, rather than the number of newlines encountered. The two
496  * values will differ if +gets+ is called with a separator other than
497  * newline. See also the <code>$.</code> variable.
498  */
499 static VALUE
500 strio_get_lineno(VALUE self)
501 {
502  return LONG2NUM(StringIO(self)->lineno);
503 }
504 
505 /*
506  * call-seq:
507  * strio.lineno = integer -> integer
508  *
509  * Manually sets the current line number to the given value.
510  * <code>$.</code> is updated only on the next read.
511  */
512 static VALUE
513 strio_set_lineno(VALUE self, VALUE lineno)
514 {
515  StringIO(self)->lineno = NUM2LONG(lineno);
516  return lineno;
517 }
518 
519 static VALUE
520 strio_binmode(VALUE self)
521 {
522  struct StringIO *ptr = StringIO(self);
524 
525  ptr->enc = enc;
526  if (WRITABLE(self)) {
527  rb_enc_associate(ptr->string, enc);
528  }
529  return self;
530 }
531 
532 #define strio_fcntl strio_unimpl
533 
534 #define strio_flush strio_self
535 
536 #define strio_fsync strio_0
537 
538 /*
539  * call-seq:
540  * strio.reopen(other_StrIO) -> strio
541  * strio.reopen(string, mode) -> strio
542  *
543  * Reinitializes *strio* with the given <i>other_StrIO</i> or _string_
544  * and _mode_ (see StringIO#new).
545  */
546 static VALUE
547 strio_reopen(int argc, VALUE *argv, VALUE self)
548 {
549  rb_io_taint_check(self);
550  if (argc == 1 && !RB_TYPE_P(*argv, T_STRING)) {
551  return strio_copy(self, *argv);
552  }
553  return strio_init(argc, argv, StringIO(self), self);
554 }
555 
556 /*
557  * call-seq:
558  * strio.pos -> integer
559  * strio.tell -> integer
560  *
561  * Returns the current offset (in bytes) of *strio*.
562  */
563 static VALUE
564 strio_get_pos(VALUE self)
565 {
566  return LONG2NUM(StringIO(self)->pos);
567 }
568 
569 /*
570  * call-seq:
571  * strio.pos = integer -> integer
572  *
573  * Seeks to the given position (in bytes) in *strio*.
574  */
575 static VALUE
576 strio_set_pos(VALUE self, VALUE pos)
577 {
578  struct StringIO *ptr = StringIO(self);
579  long p = NUM2LONG(pos);
580  if (p < 0) {
581  error_inval(0);
582  }
583  ptr->pos = p;
584  return pos;
585 }
586 
587 /*
588  * call-seq:
589  * strio.rewind -> 0
590  *
591  * Positions *strio* to the beginning of input, resetting
592  * +lineno+ to zero.
593  */
594 static VALUE
595 strio_rewind(VALUE self)
596 {
597  struct StringIO *ptr = StringIO(self);
598  ptr->pos = 0;
599  ptr->lineno = 0;
600  return INT2FIX(0);
601 }
602 
603 /*
604  * call-seq:
605  * strio.seek(amount, whence=SEEK_SET) -> 0
606  *
607  * Seeks to a given offset _amount_ in the stream according to
608  * the value of _whence_ (see IO#seek).
609  */
610 static VALUE
611 strio_seek(int argc, VALUE *argv, VALUE self)
612 {
613  VALUE whence;
614  struct StringIO *ptr = StringIO(self);
615  long amount, offset;
616 
617  rb_scan_args(argc, argv, "11", NULL, &whence);
618  amount = NUM2LONG(argv[0]);
619  if (CLOSED(self)) {
620  rb_raise(rb_eIOError, "closed stream");
621  }
622  switch (NIL_P(whence) ? 0 : NUM2LONG(whence)) {
623  case 0:
624  offset = 0;
625  break;
626  case 1:
627  offset = ptr->pos;
628  break;
629  case 2:
630  offset = RSTRING_LEN(ptr->string);
631  break;
632  default:
633  error_inval("invalid whence");
634  }
635  if (amount > LONG_MAX - offset || amount + offset < 0) {
636  error_inval(0);
637  }
638  ptr->pos = amount + offset;
639  return INT2FIX(0);
640 }
641 
642 /*
643  * call-seq:
644  * strio.sync -> true
645  *
646  * Returns +true+ always.
647  */
648 static VALUE
649 strio_get_sync(VALUE self)
650 {
651  StringIO(self);
652  return Qtrue;
653 }
654 
655 #define strio_set_sync strio_first
656 
657 #define strio_tell strio_get_pos
658 
659 /*
660  * call-seq:
661  * strio.each_byte {|byte| block } -> strio
662  * strio.each_byte -> anEnumerator
663  *
664  * See IO#each_byte.
665  */
666 static VALUE
667 strio_each_byte(VALUE self)
668 {
669  struct StringIO *ptr = readable(self);
670 
671  RETURN_ENUMERATOR(self, 0, 0);
672 
673  while (ptr->pos < RSTRING_LEN(ptr->string)) {
674  char c = RSTRING_PTR(ptr->string)[ptr->pos++];
675  rb_yield(CHR2FIX(c));
676  }
677  return self;
678 }
679 
680 /*
681  * This is a deprecated alias for #each_byte.
682  */
683 static VALUE
684 strio_bytes(VALUE self)
685 {
686  rb_warn("StringIO#bytes is deprecated; use #each_byte instead");
687  if (!rb_block_given_p())
688  return rb_enumeratorize(self, ID2SYM(rb_intern("each_byte")), 0, 0);
689  return strio_each_byte(self);
690 }
691 
692 /*
693  * call-seq:
694  * strio.getc -> string or nil
695  *
696  * See IO#getc.
697  */
698 static VALUE
699 strio_getc(VALUE self)
700 {
701  struct StringIO *ptr = readable(self);
702  rb_encoding *enc = get_enc(ptr);
703  VALUE str = ptr->string;
704  long pos = ptr->pos;
705  int len;
706  char *p;
707 
708  if (pos >= RSTRING_LEN(str)) {
709  return Qnil;
710  }
711  p = RSTRING_PTR(str)+pos;
712  len = rb_enc_mbclen(p, RSTRING_END(str), enc);
713  ptr->pos += len;
714  return enc_subseq(str, pos, len, enc);
715 }
716 
717 /*
718  * call-seq:
719  * strio.getbyte -> fixnum or nil
720  *
721  * See IO#getbyte.
722  */
723 static VALUE
724 strio_getbyte(VALUE self)
725 {
726  struct StringIO *ptr = readable(self);
727  int c;
728  if (ptr->pos >= RSTRING_LEN(ptr->string)) {
729  return Qnil;
730  }
731  c = RSTRING_PTR(ptr->string)[ptr->pos++];
732  return CHR2FIX(c);
733 }
734 
735 static void
736 strio_extend(struct StringIO *ptr, long pos, long len)
737 {
738  long olen;
739 
740  if (len > LONG_MAX - pos)
741  rb_raise(rb_eArgError, "string size too big");
742 
743  check_modifiable(ptr);
744  olen = RSTRING_LEN(ptr->string);
745  if (pos + len > olen) {
746  rb_str_resize(ptr->string, pos + len);
747  if (pos > olen)
748  MEMZERO(RSTRING_PTR(ptr->string) + olen, char, pos - olen);
749  }
750  else {
751  rb_str_modify(ptr->string);
752  }
753 }
754 
755 /*
756  * call-seq:
757  * strio.ungetc(string) -> nil
758  *
759  * Pushes back one character (passed as a parameter) onto *strio*
760  * such that a subsequent buffered read will return it. There is no
761  * limitation for multiple pushbacks including pushing back behind the
762  * beginning of the buffer string.
763  */
764 static VALUE
765 strio_ungetc(VALUE self, VALUE c)
766 {
767  struct StringIO *ptr = readable(self);
768  rb_encoding *enc, *enc2;
769 
770  check_modifiable(ptr);
771  if (NIL_P(c)) return Qnil;
772  if (RB_INTEGER_TYPE_P(c)) {
773  int len, cc = NUM2INT(c);
774  char buf[16];
775 
776  enc = rb_enc_get(ptr->string);
777  len = rb_enc_codelen(cc, enc);
778  if (len <= 0) rb_enc_uint_chr(cc, enc);
779  rb_enc_mbcput(cc, buf, enc);
780  return strio_unget_bytes(ptr, buf, len);
781  }
782  else {
783  SafeStringValue(c);
784  enc = rb_enc_get(ptr->string);
785  enc2 = rb_enc_get(c);
786  if (enc != enc2 && enc != rb_ascii8bit_encoding()) {
787  c = rb_str_conv_enc(c, enc2, enc);
788  }
789  strio_unget_bytes(ptr, RSTRING_PTR(c), RSTRING_LEN(c));
790  RB_GC_GUARD(c);
791  return Qnil;
792  }
793 }
794 
795 /*
796  * call-seq:
797  * strio.ungetbyte(fixnum) -> nil
798  *
799  * See IO#ungetbyte
800  */
801 static VALUE
802 strio_ungetbyte(VALUE self, VALUE c)
803 {
804  struct StringIO *ptr = readable(self);
805  char buf[1], *cp = buf;
806  long cl = 1;
807 
808  check_modifiable(ptr);
809  if (NIL_P(c)) return Qnil;
810  if (FIXNUM_P(c)) {
811  buf[0] = (char)FIX2INT(c);
812  return strio_unget_bytes(ptr, buf, 1);
813  }
814  else {
815  SafeStringValue(c);
816  cp = RSTRING_PTR(c);
817  cl = RSTRING_LEN(c);
818  if (cl == 0) return Qnil;
819  strio_unget_bytes(ptr, cp, cl);
820  RB_GC_GUARD(c);
821  return Qnil;
822  }
823 }
824 
825 static VALUE
826 strio_unget_bytes(struct StringIO *ptr, const char *cp, long cl)
827 {
828  long pos = ptr->pos, len, rest;
829  VALUE str = ptr->string;
830  char *s;
831 
832  len = RSTRING_LEN(str);
833  rest = pos - len;
834  if (cl > pos) {
835  long ex = (rest < 0 ? cl-pos : cl+rest);
836  rb_str_modify_expand(str, ex);
837  rb_str_set_len(str, len + ex);
838  s = RSTRING_PTR(str);
839  if (rest < 0) memmove(s + cl, s + pos, -rest);
840  pos = 0;
841  }
842  else {
843  if (rest > 0) {
844  rb_str_modify_expand(str, rest);
845  rb_str_set_len(str, len + rest);
846  }
847  s = RSTRING_PTR(str);
848  if (rest > cl) memset(s + len, 0, rest - cl);
849  pos -= cl;
850  }
851  memcpy(s + pos, cp, cl);
852  ptr->pos = pos;
853  return Qnil;
854 }
855 
856 /*
857  * call-seq:
858  * strio.readchar -> string
859  *
860  * See IO#readchar.
861  */
862 static VALUE
863 strio_readchar(VALUE self)
864 {
865  VALUE c = rb_funcall2(self, rb_intern("getc"), 0, 0);
866  if (NIL_P(c)) rb_eof_error();
867  return c;
868 }
869 
870 /*
871  * call-seq:
872  * strio.readbyte -> fixnum
873  *
874  * See IO#readbyte.
875  */
876 static VALUE
877 strio_readbyte(VALUE self)
878 {
879  VALUE c = rb_funcall2(self, rb_intern("getbyte"), 0, 0);
880  if (NIL_P(c)) rb_eof_error();
881  return c;
882 }
883 
884 /*
885  * call-seq:
886  * strio.each_char {|char| block } -> strio
887  * strio.each_char -> anEnumerator
888  *
889  * See IO#each_char.
890  */
891 static VALUE
892 strio_each_char(VALUE self)
893 {
894  VALUE c;
895 
896  RETURN_ENUMERATOR(self, 0, 0);
897 
898  while (!NIL_P(c = strio_getc(self))) {
899  rb_yield(c);
900  }
901  return self;
902 }
903 
904 /*
905  * This is a deprecated alias for <code>each_char</code>.
906  */
907 static VALUE
908 strio_chars(VALUE self)
909 {
910  rb_warn("StringIO#chars is deprecated; use #each_char instead");
911  if (!rb_block_given_p())
912  return rb_enumeratorize(self, ID2SYM(rb_intern("each_char")), 0, 0);
913  return strio_each_char(self);
914 }
915 
916 /*
917  * call-seq:
918  * strio.each_codepoint {|c| block } -> strio
919  * strio.each_codepoint -> anEnumerator
920  *
921  * See IO#each_codepoint.
922  */
923 static VALUE
924 strio_each_codepoint(VALUE self)
925 {
926  struct StringIO *ptr;
927  rb_encoding *enc;
928  unsigned int c;
929  int n;
930 
931  RETURN_ENUMERATOR(self, 0, 0);
932 
933  ptr = readable(self);
934  enc = get_enc(ptr);
935  for (;;) {
936  if (ptr->pos >= RSTRING_LEN(ptr->string)) {
937  return self;
938  }
939 
941  RSTRING_END(ptr->string), &n, enc);
942  rb_yield(UINT2NUM(c));
943  ptr->pos += n;
944  }
945  return self;
946 }
947 
948 /*
949  * This is a deprecated alias for <code>each_codepoint</code>.
950  */
951 static VALUE
952 strio_codepoints(VALUE self)
953 {
954  rb_warn("StringIO#codepoints is deprecated; use #each_codepoint instead");
955  if (!rb_block_given_p())
956  return rb_enumeratorize(self, ID2SYM(rb_intern("each_codepoint")), 0, 0);
957  return strio_each_codepoint(self);
958 }
959 
960 /* Boyer-Moore search: copied from regex.c */
961 static void
962 bm_init_skip(long *skip, const char *pat, long m)
963 {
964  int c;
965 
966  for (c = 0; c < (1 << CHAR_BIT); c++) {
967  skip[c] = m;
968  }
969  while (--m) {
970  skip[(unsigned char)*pat++] = m;
971  }
972 }
973 
974 static long
975 bm_search(const char *little, long llen, const char *big, long blen, const long *skip)
976 {
977  long i, j, k;
978 
979  i = llen - 1;
980  while (i < blen) {
981  k = i;
982  j = llen - 1;
983  while (j >= 0 && big[k] == little[j]) {
984  k--;
985  j--;
986  }
987  if (j < 0) return k + 1;
988  i += skip[(unsigned char)big[i]];
989  }
990  return -1;
991 }
992 
993 struct getline_arg {
995  long limit;
996  unsigned int chomp: 1;
997 };
998 
999 static struct getline_arg *
1000 prepare_getline_args(struct getline_arg *arg, int argc, VALUE *argv)
1001 {
1002  VALUE str, lim, opts;
1003  long limit = -1;
1004 
1005  argc = rb_scan_args(argc, argv, "02:", &str, &lim, &opts);
1006  switch (argc) {
1007  case 0:
1008  str = rb_rs;
1009  break;
1010 
1011  case 1:
1012  if (!NIL_P(str) && !RB_TYPE_P(str, T_STRING)) {
1013  VALUE tmp = rb_check_string_type(str);
1014  if (NIL_P(tmp)) {
1015  limit = NUM2LONG(str);
1016  str = rb_rs;
1017  }
1018  else {
1019  str = tmp;
1020  }
1021  }
1022  break;
1023 
1024  case 2:
1025  if (!NIL_P(str)) StringValue(str);
1026  if (!NIL_P(lim)) limit = NUM2LONG(lim);
1027  break;
1028  }
1029  arg->rs = str;
1030  arg->limit = limit;
1031  arg->chomp = 0;
1032  if (!NIL_P(opts)) {
1033  static ID keywords[1];
1034  VALUE vchomp;
1035  if (!keywords[0]) {
1036  keywords[0] = rb_intern_const("chomp");
1037  }
1038  rb_get_kwargs(opts, keywords, 0, 1, &vchomp);
1039  arg->chomp = (vchomp != Qundef) && RTEST(vchomp);
1040  }
1041  return arg;
1042 }
1043 
1044 static inline int
1045 chomp_newline_width(const char *s, const char *e)
1046 {
1047  if (e > s && *--e == '\n') {
1048  if (e > s && *--e == '\r') return 2;
1049  return 1;
1050  }
1051  return 0;
1052 }
1053 
1054 static VALUE
1055 strio_getline(struct getline_arg *arg, struct StringIO *ptr)
1056 {
1057  const char *s, *e, *p;
1058  long n, limit = arg->limit;
1059  VALUE str = arg->rs;
1060  int w = 0;
1061  rb_encoding *enc = get_enc(ptr);
1062 
1063  if (ptr->pos >= (n = RSTRING_LEN(ptr->string))) {
1064  return Qnil;
1065  }
1066  s = RSTRING_PTR(ptr->string);
1067  e = s + RSTRING_LEN(ptr->string);
1068  s += ptr->pos;
1069  if (limit > 0 && (size_t)limit < (size_t)(e - s)) {
1070  e = rb_enc_right_char_head(s, s + limit, e, get_enc(ptr));
1071  }
1072  if (NIL_P(str)) {
1073  if (arg->chomp) {
1074  w = chomp_newline_width(s, e);
1075  }
1076  str = strio_substr(ptr, ptr->pos, e - s - w, enc);
1077  }
1078  else if ((n = RSTRING_LEN(str)) == 0) {
1079  p = s;
1080  while (p[(p + 1 < e) && (*p == '\r') && 0] == '\n') {
1081  p += *p == '\r';
1082  if (++p == e) {
1083  return Qnil;
1084  }
1085  }
1086  s = p;
1087  while ((p = memchr(p, '\n', e - p)) && (p != e)) {
1088  if (*++p == '\n') {
1089  e = p + 1;
1090  w = (arg->chomp ? 1 : 0);
1091  break;
1092  }
1093  else if (*p == '\r' && p < e && p[1] == '\n') {
1094  e = p + 2;
1095  w = (arg->chomp ? 2 : 0);
1096  break;
1097  }
1098  }
1099  if (!w && arg->chomp) {
1100  w = chomp_newline_width(s, e);
1101  }
1102  str = strio_substr(ptr, s - RSTRING_PTR(ptr->string), e - s - w, enc);
1103  }
1104  else if (n == 1) {
1105  if ((p = memchr(s, RSTRING_PTR(str)[0], e - s)) != 0) {
1106  e = p + 1;
1107  w = (arg->chomp ? (p > s && *(p-1) == '\r') + 1 : 0);
1108  }
1109  str = strio_substr(ptr, ptr->pos, e - s - w, enc);
1110  }
1111  else {
1112  if (n < e - s) {
1113  if (e - s < 1024) {
1114  for (p = s; p + n <= e; ++p) {
1115  if (MEMCMP(p, RSTRING_PTR(str), char, n) == 0) {
1116  e = p + (arg->chomp ? 0 : n);
1117  break;
1118  }
1119  }
1120  }
1121  else {
1122  long skip[1 << CHAR_BIT], pos;
1123  p = RSTRING_PTR(str);
1124  bm_init_skip(skip, p, n);
1125  if ((pos = bm_search(p, n, s, e - s, skip)) >= 0) {
1126  e = s + pos + (arg->chomp ? 0 : n);
1127  }
1128  }
1129  }
1130  str = strio_substr(ptr, ptr->pos, e - s - w, enc);
1131  }
1132  ptr->pos = e - RSTRING_PTR(ptr->string);
1133  ptr->lineno++;
1134  return str;
1135 }
1136 
1137 /*
1138  * call-seq:
1139  * strio.gets(sep=$/) -> string or nil
1140  * strio.gets(limit) -> string or nil
1141  * strio.gets(sep, limit) -> string or nil
1142  *
1143  * See IO#gets.
1144  */
1145 static VALUE
1146 strio_gets(int argc, VALUE *argv, VALUE self)
1147 {
1148  struct getline_arg arg;
1149  VALUE str;
1150 
1151  if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
1152  struct StringIO *ptr = readable(self);
1153  return rb_enc_str_new(0, 0, get_enc(ptr));
1154  }
1155 
1156  str = strio_getline(&arg, readable(self));
1157  rb_lastline_set(str);
1158  return str;
1159 }
1160 
1161 /*
1162  * call-seq:
1163  * strio.readline(sep=$/) -> string
1164  * strio.readline(limit) -> string or nil
1165  * strio.readline(sep, limit) -> string or nil
1166  *
1167  * See IO#readline.
1168  */
1169 static VALUE
1170 strio_readline(int argc, VALUE *argv, VALUE self)
1171 {
1172  VALUE line = rb_funcall2(self, rb_intern("gets"), argc, argv);
1173  if (NIL_P(line)) rb_eof_error();
1174  return line;
1175 }
1176 
1177 /*
1178  * call-seq:
1179  * strio.each(sep=$/) {|line| block } -> strio
1180  * strio.each(limit) {|line| block } -> strio
1181  * strio.each(sep, limit) {|line| block } -> strio
1182  * strio.each(...) -> anEnumerator
1183  *
1184  * strio.each_line(sep=$/) {|line| block } -> strio
1185  * strio.each_line(limit) {|line| block } -> strio
1186  * strio.each_line(sep,limit) {|line| block } -> strio
1187  * strio.each_line(...) -> anEnumerator
1188  *
1189  * See IO#each.
1190  */
1191 static VALUE
1192 strio_each(int argc, VALUE *argv, VALUE self)
1193 {
1194  VALUE line;
1195  struct getline_arg arg;
1196 
1197  StringIO(self);
1198  RETURN_ENUMERATOR(self, argc, argv);
1199 
1200  if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
1201  rb_raise(rb_eArgError, "invalid limit: 0 for each_line");
1202  }
1203 
1204  while (!NIL_P(line = strio_getline(&arg, readable(self)))) {
1205  rb_yield(line);
1206  }
1207  return self;
1208 }
1209 
1210 /*
1211  * This is a deprecated alias for <code>each_line</code>.
1212  */
1213 static VALUE
1214 strio_lines(int argc, VALUE *argv, VALUE self)
1215 {
1216  rb_warn("StringIO#lines is deprecated; use #each_line instead");
1217  if (!rb_block_given_p())
1218  return rb_enumeratorize(self, ID2SYM(rb_intern("each_line")), argc, argv);
1219  return strio_each(argc, argv, self);
1220 }
1221 
1222 /*
1223  * call-seq:
1224  * strio.readlines(sep=$/) -> array
1225  * strio.readlines(limit) -> array
1226  * strio.readlines(sep,limit) -> array
1227  *
1228  * See IO#readlines.
1229  */
1230 static VALUE
1231 strio_readlines(int argc, VALUE *argv, VALUE self)
1232 {
1233  VALUE ary, line;
1234  struct getline_arg arg;
1235 
1236  StringIO(self);
1237  ary = rb_ary_new();
1238  if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
1239  rb_raise(rb_eArgError, "invalid limit: 0 for readlines");
1240  }
1241 
1242  while (!NIL_P(line = strio_getline(&arg, readable(self)))) {
1243  rb_ary_push(ary, line);
1244  }
1245  return ary;
1246 }
1247 
1248 /*
1249  * call-seq:
1250  * strio.write(string) -> integer
1251  * strio.syswrite(string) -> integer
1252  *
1253  * Appends the given string to the underlying buffer string of *strio*.
1254  * The stream must be opened for writing. If the argument is not a
1255  * string, it will be converted to a string using <code>to_s</code>.
1256  * Returns the number of bytes written. See IO#write.
1257  */
1258 static VALUE
1259 strio_write(VALUE self, VALUE str)
1260 {
1261  struct StringIO *ptr = writable(self);
1262  long len, olen;
1263  rb_encoding *enc, *enc2;
1264  rb_encoding *const ascii8bit = rb_ascii8bit_encoding();
1265 
1266  if (!RB_TYPE_P(str, T_STRING))
1267  str = rb_obj_as_string(str);
1268  enc = get_enc(ptr);
1269  enc2 = rb_enc_get(str);
1270  if (enc != enc2 && enc != ascii8bit) {
1271  str = rb_str_conv_enc(str, enc2, enc);
1272  }
1273  len = RSTRING_LEN(str);
1274  if (len == 0) return INT2FIX(0);
1275  check_modifiable(ptr);
1276  olen = RSTRING_LEN(ptr->string);
1277  if (ptr->flags & FMODE_APPEND) {
1278  ptr->pos = olen;
1279  }
1280  if (ptr->pos == olen) {
1281  if (enc == ascii8bit || enc2 == ascii8bit) {
1282  rb_enc_str_buf_cat(ptr->string, RSTRING_PTR(str), len, enc);
1283  OBJ_INFECT(ptr->string, str);
1284  }
1285  else {
1286  rb_str_buf_append(ptr->string, str);
1287  }
1288  }
1289  else {
1290  strio_extend(ptr, ptr->pos, len);
1291  memmove(RSTRING_PTR(ptr->string)+ptr->pos, RSTRING_PTR(str), len);
1292  OBJ_INFECT(ptr->string, str);
1293  }
1294  OBJ_INFECT(ptr->string, self);
1295  RB_GC_GUARD(str);
1296  ptr->pos += len;
1297  return LONG2NUM(len);
1298 }
1299 
1300 /*
1301  * call-seq:
1302  * strio << obj -> strio
1303  *
1304  * See IO#<<.
1305  */
1306 #define strio_addstr rb_io_addstr
1307 
1308 /*
1309  * call-seq:
1310  * strio.print() -> nil
1311  * strio.print(obj, ...) -> nil
1312  *
1313  * See IO#print.
1314  */
1315 #define strio_print rb_io_print
1316 
1317 /*
1318  * call-seq:
1319  * strio.printf(format_string [, obj, ...] ) -> nil
1320  *
1321  * See IO#printf.
1322  */
1323 #define strio_printf rb_io_printf
1324 
1325 /*
1326  * call-seq:
1327  * strio.putc(obj) -> obj
1328  *
1329  * See IO#putc.
1330  */
1331 static VALUE
1332 strio_putc(VALUE self, VALUE ch)
1333 {
1334  struct StringIO *ptr = writable(self);
1335  VALUE str;
1336 
1337  check_modifiable(ptr);
1338  if (RB_TYPE_P(ch, T_STRING)) {
1339  str = rb_str_substr(ch, 0, 1);
1340  }
1341  else {
1342  char c = NUM2CHR(ch);
1343  str = rb_str_new(&c, 1);
1344  }
1345  strio_write(self, str);
1346  return ch;
1347 }
1348 
1349 /*
1350  * call-seq:
1351  * strio.puts(obj, ...) -> nil
1352  *
1353  * See IO#puts.
1354  */
1355 #define strio_puts rb_io_puts
1356 
1357 /*
1358  * call-seq:
1359  * strio.read([length [, outbuf]]) -> string, outbuf, or nil
1360  *
1361  * See IO#read.
1362  */
1363 static VALUE
1364 strio_read(int argc, VALUE *argv, VALUE self)
1365 {
1366  struct StringIO *ptr = readable(self);
1367  VALUE str = Qnil;
1368  long len;
1369  int binary = 0;
1370 
1371  rb_check_arity(argc, 0, 2);
1372  switch (argc) {
1373  case 2:
1374  str = argv[1];
1375  if (!NIL_P(str)) {
1376  StringValue(str);
1377  rb_str_modify(str);
1378  }
1379  /* fall through */
1380  case 1:
1381  if (!NIL_P(argv[0])) {
1382  len = NUM2LONG(argv[0]);
1383  if (len < 0) {
1384  rb_raise(rb_eArgError, "negative length %ld given", len);
1385  }
1386  if (len > 0 && ptr->pos >= RSTRING_LEN(ptr->string)) {
1387  if (!NIL_P(str)) rb_str_resize(str, 0);
1388  return Qnil;
1389  }
1390  binary = 1;
1391  break;
1392  }
1393  /* fall through */
1394  case 0:
1395  len = RSTRING_LEN(ptr->string);
1396  if (len <= ptr->pos) {
1397  rb_encoding *enc = binary ? rb_ascii8bit_encoding() : get_enc(ptr);
1398  if (NIL_P(str)) {
1399  str = rb_str_new(0, 0);
1400  }
1401  else {
1402  rb_str_resize(str, 0);
1403  }
1404  rb_enc_associate(str, enc);
1405  return str;
1406  }
1407  else {
1408  len -= ptr->pos;
1409  }
1410  break;
1411  }
1412  if (NIL_P(str)) {
1413  rb_encoding *enc = binary ? rb_ascii8bit_encoding() : get_enc(ptr);
1414  str = strio_substr(ptr, ptr->pos, len, enc);
1415  }
1416  else {
1417  long rest = RSTRING_LEN(ptr->string) - ptr->pos;
1418  if (len > rest) len = rest;
1419  rb_str_resize(str, len);
1420  MEMCPY(RSTRING_PTR(str), RSTRING_PTR(ptr->string) + ptr->pos, char, len);
1421  if (binary)
1423  else
1424  rb_enc_copy(str, ptr->string);
1425  }
1426  ptr->pos += RSTRING_LEN(str);
1427  return str;
1428 }
1429 
1430 /*
1431  * call-seq:
1432  * strio.sysread(integer[, outbuf]) -> string
1433  * strio.readpartial(integer[, outbuf]) -> string
1434  *
1435  * Similar to #read, but raises +EOFError+ at end of string instead of
1436  * returning +nil+, as well as IO#sysread does.
1437  */
1438 static VALUE
1439 strio_sysread(int argc, VALUE *argv, VALUE self)
1440 {
1441  VALUE val = rb_funcall2(self, rb_intern("read"), argc, argv);
1442  if (NIL_P(val)) {
1443  rb_eof_error();
1444  }
1445  return val;
1446 }
1447 
1448 /*
1449  * call-seq:
1450  * strio.read_nonblock(integer[, outbuf [, opts]]) -> string
1451  *
1452  * Similar to #read, but raises +EOFError+ at end of string unless the
1453  * +exception: false+ option is passed in.
1454  */
1455 static VALUE
1456 strio_read_nonblock(int argc, VALUE *argv, VALUE self)
1457 {
1458  VALUE opts = Qnil, val;
1459 
1460  rb_scan_args(argc, argv, "11:", NULL, NULL, &opts);
1461 
1462  if (!NIL_P(opts)) {
1463  argc--;
1464  }
1465 
1466  val = strio_read(argc, argv, self);
1467  if (NIL_P(val)) {
1468  if (!NIL_P(opts) &&
1469  rb_hash_lookup2(opts, sym_exception, Qundef) == Qfalse)
1470  return Qnil;
1471  else
1472  rb_eof_error();
1473  }
1474 
1475  return val;
1476 }
1477 
1478 #define strio_syswrite rb_io_write
1479 
1480 static VALUE
1481 strio_syswrite_nonblock(int argc, VALUE *argv, VALUE self)
1482 {
1483  VALUE str;
1484 
1485  rb_scan_args(argc, argv, "10:", &str, NULL);
1486  return strio_syswrite(self, str);
1487 }
1488 
1489 #define strio_isatty strio_false
1490 
1491 #define strio_pid strio_nil
1492 
1493 #define strio_fileno strio_nil
1494 
1495 /*
1496  * call-seq:
1497  * strio.length -> integer
1498  * strio.size -> integer
1499  *
1500  * Returns the size of the buffer string.
1501  */
1502 static VALUE
1503 strio_size(VALUE self)
1504 {
1505  VALUE string = StringIO(self)->string;
1506  if (NIL_P(string)) {
1507  rb_raise(rb_eIOError, "not opened");
1508  }
1509  return ULONG2NUM(RSTRING_LEN(string));
1510 }
1511 
1512 /*
1513  * call-seq:
1514  * strio.truncate(integer) -> 0
1515  *
1516  * Truncates the buffer string to at most _integer_ bytes. The *strio*
1517  * must be opened for writing.
1518  */
1519 static VALUE
1520 strio_truncate(VALUE self, VALUE len)
1521 {
1522  VALUE string = writable(self)->string;
1523  long l = NUM2LONG(len);
1524  long plen = RSTRING_LEN(string);
1525  if (l < 0) {
1526  error_inval("negative length");
1527  }
1528  rb_str_resize(string, l);
1529  if (plen < l) {
1530  MEMZERO(RSTRING_PTR(string) + plen, char, l - plen);
1531  }
1532  return len;
1533 }
1534 
1535 /*
1536  * call-seq:
1537  * strio.external_encoding => encoding
1538  *
1539  * Returns the Encoding object that represents the encoding of the file.
1540  * If strio is write mode and no encoding is specified, returns <code>nil</code>.
1541  */
1542 
1543 static VALUE
1544 strio_external_encoding(VALUE self)
1545 {
1546  struct StringIO *ptr = StringIO(self);
1547  return rb_enc_from_encoding(get_enc(ptr));
1548 }
1549 
1550 /*
1551  * call-seq:
1552  * strio.internal_encoding => encoding
1553  *
1554  * Returns the Encoding of the internal string if conversion is
1555  * specified. Otherwise returns nil.
1556  */
1557 
1558 static VALUE
1559 strio_internal_encoding(VALUE self)
1560 {
1561  return Qnil;
1562 }
1563 
1564 /*
1565  * call-seq:
1566  * strio.set_encoding(ext_enc, [int_enc[, opt]]) => strio
1567  *
1568  * Specify the encoding of the StringIO as <i>ext_enc</i>.
1569  * Use the default external encoding if <i>ext_enc</i> is nil.
1570  * 2nd argument <i>int_enc</i> and optional hash <i>opt</i> argument
1571  * are ignored; they are for API compatibility to IO.
1572  */
1573 
1574 static VALUE
1575 strio_set_encoding(int argc, VALUE *argv, VALUE self)
1576 {
1577  rb_encoding* enc;
1578  struct StringIO *ptr = StringIO(self);
1579  VALUE ext_enc, int_enc, opt;
1580 
1581  argc = rb_scan_args(argc, argv, "11:", &ext_enc, &int_enc, &opt);
1582 
1583  if (NIL_P(ext_enc)) {
1585  }
1586  else {
1587  enc = rb_to_encoding(ext_enc);
1588  }
1589  ptr->enc = enc;
1590  if (WRITABLE(self)) {
1591  rb_enc_associate(ptr->string, enc);
1592  }
1593 
1594  return self;
1595 }
1596 
1597 /*
1598  * Pseudo I/O on String object.
1599  *
1600  * Commonly used to simulate `$stdio` or `$stderr`
1601  *
1602  * === Examples
1603  *
1604  * require 'stringio'
1605  *
1606  * io = StringIO.new
1607  * io.puts "Hello World"
1608  * io.string #=> "Hello World\n"
1609  */
1610 void
1612 {
1613  VALUE StringIO = rb_define_class("StringIO", rb_cData);
1614 
1615  rb_include_module(StringIO, rb_mEnumerable);
1616  rb_define_alloc_func(StringIO, strio_s_allocate);
1617  rb_define_singleton_method(StringIO, "new", strio_s_new, -1);
1618  rb_define_singleton_method(StringIO, "open", strio_s_open, -1);
1619  rb_define_method(StringIO, "initialize", strio_initialize, -1);
1620  rb_define_method(StringIO, "initialize_copy", strio_copy, 1);
1621  rb_define_method(StringIO, "reopen", strio_reopen, -1);
1622 
1623  rb_define_method(StringIO, "string", strio_get_string, 0);
1624  rb_define_method(StringIO, "string=", strio_set_string, 1);
1625  rb_define_method(StringIO, "lineno", strio_get_lineno, 0);
1626  rb_define_method(StringIO, "lineno=", strio_set_lineno, 1);
1627 
1628 
1629  /* call-seq: strio.binmode -> true */
1630  rb_define_method(StringIO, "binmode", strio_binmode, 0);
1631  rb_define_method(StringIO, "close", strio_close, 0);
1632  rb_define_method(StringIO, "close_read", strio_close_read, 0);
1633  rb_define_method(StringIO, "close_write", strio_close_write, 0);
1634  rb_define_method(StringIO, "closed?", strio_closed, 0);
1635  rb_define_method(StringIO, "closed_read?", strio_closed_read, 0);
1636  rb_define_method(StringIO, "closed_write?", strio_closed_write, 0);
1637  rb_define_method(StringIO, "eof", strio_eof, 0);
1638  rb_define_method(StringIO, "eof?", strio_eof, 0);
1639  /* call-seq: strio.fcntl */
1640  rb_define_method(StringIO, "fcntl", strio_fcntl, -1);
1641  /* call-seq: strio.flush -> strio */
1642  rb_define_method(StringIO, "flush", strio_flush, 0);
1643  /* call-seq: strio.fsync -> 0 */
1644  rb_define_method(StringIO, "fsync", strio_fsync, 0);
1645  rb_define_method(StringIO, "pos", strio_get_pos, 0);
1646  rb_define_method(StringIO, "pos=", strio_set_pos, 1);
1647  rb_define_method(StringIO, "rewind", strio_rewind, 0);
1648  rb_define_method(StringIO, "seek", strio_seek, -1);
1649  rb_define_method(StringIO, "sync", strio_get_sync, 0);
1650  /* call-seq: strio.sync = boolean -> boolean */
1651  rb_define_method(StringIO, "sync=", strio_set_sync, 1);
1652  rb_define_method(StringIO, "tell", strio_tell, 0);
1653 
1654  rb_define_method(StringIO, "each", strio_each, -1);
1655  rb_define_method(StringIO, "each_line", strio_each, -1);
1656  rb_define_method(StringIO, "lines", strio_lines, -1);
1657  rb_define_method(StringIO, "each_byte", strio_each_byte, 0);
1658  rb_define_method(StringIO, "bytes", strio_bytes, 0);
1659  rb_define_method(StringIO, "each_char", strio_each_char, 0);
1660  rb_define_method(StringIO, "chars", strio_chars, 0);
1661  rb_define_method(StringIO, "each_codepoint", strio_each_codepoint, 0);
1662  rb_define_method(StringIO, "codepoints", strio_codepoints, 0);
1663  rb_define_method(StringIO, "getc", strio_getc, 0);
1664  rb_define_method(StringIO, "ungetc", strio_ungetc, 1);
1665  rb_define_method(StringIO, "ungetbyte", strio_ungetbyte, 1);
1666  rb_define_method(StringIO, "getbyte", strio_getbyte, 0);
1667  rb_define_method(StringIO, "gets", strio_gets, -1);
1668  rb_define_method(StringIO, "readlines", strio_readlines, -1);
1669  rb_define_method(StringIO, "read", strio_read, -1);
1670 
1671  rb_define_method(StringIO, "write", strio_write, 1);
1672  rb_define_method(StringIO, "putc", strio_putc, 1);
1673 
1674  /*
1675  * call-seq:
1676  * strio.isatty -> nil
1677  * strio.tty? -> nil
1678  *
1679  */
1680  rb_define_method(StringIO, "isatty", strio_isatty, 0);
1681  rb_define_method(StringIO, "tty?", strio_isatty, 0);
1682 
1683  /* call-seq: strio.pid -> nil */
1684  rb_define_method(StringIO, "pid", strio_pid, 0);
1685 
1686  /* call-seq: strio.fileno -> nil */
1687  rb_define_method(StringIO, "fileno", strio_fileno, 0);
1688  rb_define_method(StringIO, "size", strio_size, 0);
1689  rb_define_method(StringIO, "length", strio_size, 0);
1690  rb_define_method(StringIO, "truncate", strio_truncate, 1);
1691 
1692  rb_define_method(StringIO, "external_encoding", strio_external_encoding, 0);
1693  rb_define_method(StringIO, "internal_encoding", strio_internal_encoding, 0);
1694  rb_define_method(StringIO, "set_encoding", strio_set_encoding, -1);
1695 
1696  {
1697  VALUE mReadable = rb_define_module_under(rb_cIO, "generic_readable");
1698  rb_define_method(mReadable, "readchar", strio_readchar, 0);
1699  rb_define_method(mReadable, "readbyte", strio_readbyte, 0);
1700  rb_define_method(mReadable, "readline", strio_readline, -1);
1701  rb_define_method(mReadable, "sysread", strio_sysread, -1);
1702  rb_define_method(mReadable, "readpartial", strio_sysread, -1);
1703  rb_define_method(mReadable, "read_nonblock", strio_read_nonblock, -1);
1704  rb_include_module(StringIO, mReadable);
1705  }
1706  {
1707  VALUE mWritable = rb_define_module_under(rb_cIO, "generic_writable");
1708  rb_define_method(mWritable, "<<", strio_addstr, 1);
1709  rb_define_method(mWritable, "print", strio_print, -1);
1710  rb_define_method(mWritable, "printf", strio_printf, -1);
1711  rb_define_method(mWritable, "puts", strio_puts, -1);
1712  rb_define_method(mWritable, "syswrite", strio_syswrite, 1);
1713  rb_define_method(mWritable, "write_nonblock", strio_syswrite_nonblock, -1);
1714  rb_include_module(StringIO, mWritable);
1715  }
1716 
1717  sym_exception = ID2SYM(rb_intern("exception"));
1718 }
rb_encoding * enc
Definition: stringio.c:29
#define MEMCMP(p1, p2, type, n)
Definition: ruby.h:1663
int rb_enc_codelen(int c, rb_encoding *enc)
Definition: encoding.c:1077
long pos
Definition: stringio.c:30
RUBY_EXTERN VALUE rb_cData
Definition: ruby.h:1902
void rb_warn(const char *fmt,...)
Definition: error.c:246
void rb_syserr_fail(int e, const char *mesg)
Definition: error.c:2391
void rb_enc_copy(VALUE obj1, VALUE obj2)
Definition: encoding.c:978
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1138
#define FMODE_READWRITE
Definition: io.h:104
#define CLOSED(strio)
Definition: stringio.c:130
#define strio_print
Definition: stringio.c:1315
#define NUM2INT(x)
Definition: ruby.h:684
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
#define error_inval(msg)
Definition: stringio.c:40
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:835
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2284
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:1847
#define Qtrue
Definition: ruby.h:437
#define strio_set_sync
Definition: stringio.c:655
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: ruby.h:1162
#define STRIO_WRITABLE
Definition: stringio.c:124
#define FMODE_WRITABLE
Definition: io.h:103
rb_encoding * rb_to_encoding(VALUE enc)
Definition: encoding.c:246
#define FMODE_READABLE
Definition: io.h:102
VALUE rs
Definition: stringio.c:994
VALUE rb_enc_from_encoding(rb_encoding *encoding)
Definition: encoding.c:117
#define rb_check_arity
Definition: intern.h:298
#define UNREACHABLE
Definition: ruby.h:46
#define ULONG2NUM(x)
Definition: ruby.h:1574
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:924
void rb_str_set_len(VALUE, long)
Definition: string.c:2627
int rb_io_modestr_fmode(const char *modestr)
Definition: io.c:5143
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
Definition: encoding.c:1056
char strio_flags_check[(STRIO_READABLE/FMODE_READABLE==STRIO_WRITABLE/FMODE_WRITABLE) *2 - 1]
Definition: stringio.c:126
int flags
Definition: stringio.c:32
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Definition: encoding.c:854
VALUE rb_io_taint_check(VALUE)
Definition: io.c:626
#define RB_GC_GUARD(v)
Definition: ruby.h:552
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
Definition: encoding.c:1008
#define DATA_PTR(dta)
Definition: ruby.h:1106
#define FMODE_APPEND
Definition: io.h:109
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:864
void rb_gc_mark(VALUE ptr)
Definition: gc.c:4464
#define READABLE(strio)
Definition: stringio.c:131
#define strio_pid
Definition: stringio.c:1491
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
An equivalent to ensure clause.
Definition: eval.c:1035
#define FIXNUM_P(f)
Definition: ruby.h:365
VALUE rb_str_buf_append(VALUE, VALUE)
Definition: string.c:2884
#define strio_isatty
Definition: stringio.c:1489
unsigned int chomp
Definition: stringio.c:996
#define StringIO(obj)
Definition: stringio.c:121
#define strio_fsync
Definition: stringio.c:536
RUBY_EXTERN void * memmove(void *, const void *, size_t)
Definition: memmove.c:7
VALUE rb_eArgError
Definition: error.c:802
#define strio_flush
Definition: stringio.c:534
#define WRITABLE(strio)
Definition: stringio.c:132
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
#define strio_puts
Definition: stringio.c:1355
#define MEMZERO(p, type, n)
Definition: ruby.h:1660
rb_encoding * rb_default_external_encoding(void)
Definition: encoding.c:1425
VALUE rb_str_substr(VALUE, long, long)
Definition: string.c:2517
#define val
VALUE string
Definition: stringio.c:28
#define strio_tell
Definition: stringio.c:657
#define RSTRING_END(str)
Definition: ruby.h:979
int rb_io_oflags_fmode(int oflags)
Definition: io.c:5192
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1410
VALUE rb_ary_new(void)
Definition: array.c:499
#define UINT2NUM(x)
Definition: ruby.h:1539
long limit
Definition: stringio.c:995
#define NIL_P(v)
Definition: ruby.h:451
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
void rb_lastline_set(VALUE)
Definition: vm.c:1247
void rb_notimplement(void)
Definition: error.c:2330
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Definition: string.c:1002
int argc
Definition: ruby.c:187
#define Qfalse
Definition: ruby.h:436
#define STRIO_READWRITE
Definition: stringio.c:125
#define LONG_MAX
Definition: ruby.h:189
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1661
#define ALLOC(type)
Definition: ruby.h:1588
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2644
VALUE rb_str_subseq(VALUE, long, long)
Definition: string.c:2406
RUBY_EXTERN VALUE rb_cIO
Definition: ruby.h:1913
#define RSTRING_LEN(str)
Definition: ruby.h:971
void Init_stringio(void)
Definition: stringio.c:1611
VALUE rb_yield(VALUE)
Definition: vm_eval.c:973
#define T_DATA
Definition: ruby.h:506
VALUE rb_mEnumerable
Definition: enum.c:19
#define check_strio(self)
Definition: stringio.c:88
#define strio_addstr
Definition: stringio.c:1306
#define NUM2CHR(x)
Definition: ruby.h:1575
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1908
void rb_str_modify_expand(VALUE, long)
Definition: string.c:2054
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
#define Qnil
Definition: ruby.h:438
VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
Definition: numeric.c:3283
unsigned long VALUE
Definition: ruby.h:85
#define rb_funcall2
Definition: ruby.h:1791
#define RBASIC(obj)
Definition: ruby.h:1197
#define FIX2INT(x)
Definition: ruby.h:686
VALUE rb_call_super(int, const VALUE *)
Definition: vm_eval.c:238
RUBY_EXTERN VALUE rb_rs
Definition: intern.h:516
#define CHAR_BIT
Definition: ruby.h:196
#define LONG2NUM(x)
Definition: ruby.h:1573
register unsigned int len
Definition: zonetab.h:51
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:790
#define StringValueCStr(v)
Definition: ruby.h:571
#define RSTRING_PTR(str)
Definition: ruby.h:975
#define rb_enc_right_char_head(s, p, e, enc)
Definition: encoding.h:217
void rb_str_modify(VALUE)
Definition: string.c:2046
int count
Definition: stringio.c:33
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:860
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
Definition: hash.c:842
#define INT2FIX(i)
Definition: ruby.h:232
#define CHR2FIX(x)
Definition: ruby.h:1576
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Converts an object into another type.
Definition: object.c:2965
VALUE rb_enumeratorize(VALUE obj, VALUE meth, int argc, const VALUE *argv)
Definition: enumerator.c:450
VALUE rb_check_string_type(VALUE)
Definition: string.c:2246
#define RTEST(v)
Definition: ruby.h:450
#define T_STRING
Definition: ruby.h:496
VALUE rb_class_new_instance(int, const VALUE *, VALUE)
Allocates and initializes an instance of klass.
Definition: object.c:2170
#define OBJ_INFECT(x, s)
Definition: ruby.h:1302
#define strio_printf
Definition: stringio.c:1323
#define OBJ_FROZEN(x)
Definition: ruby.h:1304
#define RETURN_ENUMERATOR(obj, argc, argv)
Definition: intern.h:238
#define SafeStringValue(v)
Definition: ruby.h:574
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
Definition: string.c:759
RUBY_EXTERN VALUE rb_eIOError
Definition: ruby.h:1947
#define ID2SYM(x)
Definition: ruby.h:383
rb_encoding * rb_ascii8bit_encoding(void)
Definition: encoding.c:1305
#define rb_intern_const(str)
Definition: ruby.h:1777
void void xfree(void *)
#define strio_fileno
Definition: stringio.c:1493
#define rb_enc_mbcput(c, buf, enc)
Definition: encoding.h:211
#define rb_intern(str)
#define get_enc(ptr)
Definition: stringio.c:41
#define RB_INTEGER_TYPE_P(obj)
Definition: ruby_missing.h:15
#define NULL
Definition: _sdbm.c:102
#define Qundef
Definition: ruby.h:439
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
#define strio_syswrite
Definition: stringio.c:1478
#define STRIO_READABLE
Definition: stringio.c:123
#define NUM2LONG(x)
Definition: ruby.h:648
char ** argv
Definition: ruby.c:188
VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc)
Definition: string.c:2853
#define StringValue(v)
Definition: ruby.h:569
#define strio_fcntl
Definition: stringio.c:532
long lineno
Definition: stringio.c:31
VALUE rb_str_new(const char *, long)
Definition: string.c:737
void rb_eof_error(void)
Definition: io.c:620