-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
Copy pathstringio.c
1965 lines (1776 loc) · 45.1 KB
/
stringio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: c; indent-tabs-mode: t -*- */
/**********************************************************************
stringio.c -
$Author$
$RoughId: stringio.c,v 1.13 2002/03/14 03:24:18 nobu Exp $
created at: Tue Feb 19 04:10:38 JST 2002
All the files in this distribution are covered under the Ruby's
license (see the file COPYING).
**********************************************************************/
static const char *const
STRINGIO_VERSION = "3.1.1";
#include "ruby.h"
#include "ruby/io.h"
#include "ruby/encoding.h"
#if defined(HAVE_FCNTL_H) || defined(_WIN32)
#include <fcntl.h>
#elif defined(HAVE_SYS_FCNTL_H)
#include <sys/fcntl.h>
#endif
#ifndef RB_INTEGER_TYPE_P
# define RB_INTEGER_TYPE_P(c) (FIXNUM_P(c) || RB_TYPE_P(c, T_BIGNUM))
#endif
#ifndef RB_PASS_CALLED_KEYWORDS
# define rb_funcallv_kw(recv, mid, arg, argv, kw_splat) rb_funcallv(recv, mid, arg, argv)
# define rb_class_new_instance_kw(argc, argv, klass, kw_splat) rb_class_new_instance(argc, argv, klass)
#endif
struct StringIO {
VALUE string;
rb_encoding *enc;
long pos;
long lineno;
int flags;
int count;
};
static VALUE strio_init(int, VALUE *, struct StringIO *, VALUE);
static VALUE strio_unget_bytes(struct StringIO *, const char *, long);
static long strio_write(VALUE self, VALUE str);
#define IS_STRIO(obj) (rb_typeddata_is_kind_of((obj), &strio_data_type))
#define error_inval(msg) (rb_syserr_fail(EINVAL, msg))
#define get_enc(ptr) ((ptr)->enc ? (ptr)->enc : rb_enc_get((ptr)->string))
static struct StringIO *
strio_alloc(void)
{
struct StringIO *ptr = ALLOC(struct StringIO);
ptr->string = Qnil;
ptr->pos = 0;
ptr->lineno = 0;
ptr->flags = 0;
ptr->count = 1;
return ptr;
}
static void
strio_mark(void *p)
{
struct StringIO *ptr = p;
rb_gc_mark(ptr->string);
}
static void
strio_free(void *p)
{
struct StringIO *ptr = p;
if (--ptr->count <= 0) {
xfree(ptr);
}
}
static size_t
strio_memsize(const void *p)
{
return sizeof(struct StringIO);
}
static const rb_data_type_t strio_data_type = {
"strio",
{
strio_mark,
strio_free,
strio_memsize,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};
#define check_strio(self) ((struct StringIO*)rb_check_typeddata((self), &strio_data_type))
static struct StringIO*
get_strio(VALUE self)
{
struct StringIO *ptr = check_strio(rb_io_taint_check(self));
if (!ptr) {
rb_raise(rb_eIOError, "uninitialized stream");
}
return ptr;
}
static VALUE
enc_subseq(VALUE str, long pos, long len, rb_encoding *enc)
{
str = rb_str_subseq(str, pos, len);
rb_enc_associate(str, enc);
return str;
}
static VALUE
strio_substr(struct StringIO *ptr, long pos, long len, rb_encoding *enc)
{
VALUE str = ptr->string;
long rlen = RSTRING_LEN(str) - pos;
if (len > rlen) len = rlen;
if (len < 0) len = 0;
if (len == 0) return rb_enc_str_new(0, 0, enc);
return enc_subseq(str, pos, len, enc);
}
#define StringIO(obj) get_strio(obj)
#define STRIO_READABLE FL_USER4
#define STRIO_WRITABLE FL_USER5
#define STRIO_READWRITE (STRIO_READABLE|STRIO_WRITABLE)
typedef char strio_flags_check[(STRIO_READABLE/FMODE_READABLE == STRIO_WRITABLE/FMODE_WRITABLE) * 2 - 1];
#define STRIO_MODE_SET_P(strio, mode) \
((RBASIC(strio)->flags & STRIO_##mode) && \
((struct StringIO*)DATA_PTR(strio))->flags & FMODE_##mode)
#define CLOSED(strio) (!STRIO_MODE_SET_P(strio, READWRITE))
#define READABLE(strio) STRIO_MODE_SET_P(strio, READABLE)
#define WRITABLE(strio) STRIO_MODE_SET_P(strio, WRITABLE)
static VALUE sym_exception;
static struct StringIO*
readable(VALUE strio)
{
struct StringIO *ptr = StringIO(strio);
if (!READABLE(strio)) {
rb_raise(rb_eIOError, "not opened for reading");
}
return ptr;
}
static struct StringIO*
writable(VALUE strio)
{
struct StringIO *ptr = StringIO(strio);
if (!WRITABLE(strio)) {
rb_raise(rb_eIOError, "not opened for writing");
}
return ptr;
}
static void
check_modifiable(struct StringIO *ptr)
{
if (OBJ_FROZEN(ptr->string)) {
rb_raise(rb_eIOError, "not modifiable string");
}
}
static VALUE
strio_s_allocate(VALUE klass)
{
return TypedData_Wrap_Struct(klass, &strio_data_type, 0);
}
/*
* call-seq:
* StringIO.new(string = '', mode = 'r+') -> new_stringio
*
* Note that +mode+ defaults to <tt>'r'</tt> if +string+ is frozen.
*
* Returns a new \StringIO instance formed from +string+ and +mode+;
* see {Access Modes}[rdoc-ref:File@Access+Modes]:
*
* strio = StringIO.new # => #<StringIO>
* strio.close
*
* The instance should be closed when no longer needed.
*
* Related: StringIO.open (accepts block; closes automatically).
*/
static VALUE
strio_initialize(int argc, VALUE *argv, VALUE self)
{
struct StringIO *ptr = check_strio(self);
if (!ptr) {
DATA_PTR(self) = ptr = strio_alloc();
}
rb_call_super(0, 0);
return strio_init(argc, argv, ptr, self);
}
static int
detect_bom(VALUE str, int *bomlen)
{
const char *p;
long len;
RSTRING_GETMEM(str, p, len);
if (len < 1) return 0;
switch ((unsigned char)p[0]) {
case 0xEF:
if (len < 2) break;
if ((unsigned char)p[1] == 0xBB && len > 2) {
if ((unsigned char)p[2] == 0xBF) {
*bomlen = 3;
return rb_utf8_encindex();
}
}
break;
case 0xFE:
if (len < 2) break;
if ((unsigned char)p[1] == 0xFF) {
*bomlen = 2;
return rb_enc_find_index("UTF-16BE");
}
break;
case 0xFF:
if (len < 2) break;
if ((unsigned char)p[1] == 0xFE) {
if (len >= 4 && (unsigned char)p[2] == 0 && (unsigned char)p[3] == 0) {
*bomlen = 4;
return rb_enc_find_index("UTF-32LE");
}
*bomlen = 2;
return rb_enc_find_index("UTF-16LE");
}
break;
case 0:
if (len < 4) break;
if ((unsigned char)p[1] == 0 && (unsigned char)p[2] == 0xFE && (unsigned char)p[3] == 0xFF) {
*bomlen = 4;
return rb_enc_find_index("UTF-32BE");
}
break;
}
return 0;
}
static rb_encoding *
set_encoding_by_bom(struct StringIO *ptr)
{
int bomlen, idx = detect_bom(ptr->string, &bomlen);
rb_encoding *extenc = NULL;
if (idx) {
extenc = rb_enc_from_index(idx);
ptr->pos = bomlen;
if (ptr->flags & FMODE_WRITABLE) {
rb_enc_associate_index(ptr->string, idx);
}
}
ptr->enc = extenc;
return extenc;
}
static VALUE
strio_init(int argc, VALUE *argv, struct StringIO *ptr, VALUE self)
{
VALUE string, vmode, opt;
int oflags;
rb_io_enc_t convconfig;
argc = rb_scan_args(argc, argv, "02:", &string, &vmode, &opt);
rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &ptr->flags, &convconfig);
if (argc) {
StringValue(string);
}
else {
string = rb_enc_str_new("", 0, rb_default_external_encoding());
}
if (OBJ_FROZEN_RAW(string)) {
if (ptr->flags & FMODE_WRITABLE) {
rb_syserr_fail(EACCES, 0);
}
}
else {
if (NIL_P(vmode)) {
ptr->flags |= FMODE_WRITABLE;
}
}
if (ptr->flags & FMODE_TRUNC) {
rb_str_resize(string, 0);
}
RB_OBJ_WRITE(self, &ptr->string, string);
if (argc == 1) {
ptr->enc = rb_enc_get(string);
}
else {
ptr->enc = convconfig.enc;
}
ptr->pos = 0;
ptr->lineno = 0;
if (ptr->flags & FMODE_SETENC_BY_BOM) set_encoding_by_bom(ptr);
RBASIC(self)->flags |= (ptr->flags & FMODE_READWRITE) * (STRIO_READABLE / FMODE_READABLE);
return self;
}
static VALUE
strio_finalize(VALUE self)
{
struct StringIO *ptr = StringIO(self);
RB_OBJ_WRITE(self, &ptr->string, Qnil);
ptr->flags &= ~FMODE_READWRITE;
return self;
}
/*
* call-seq:
* StringIO.open(string = '', mode = 'r+') {|strio| ... }
*
* Note that +mode+ defaults to <tt>'r'</tt> if +string+ is frozen.
*
* Creates a new \StringIO instance formed from +string+ and +mode+;
* see {Access Modes}[rdoc-ref:File@Access+Modes].
*
* With no block, returns the new instance:
*
* strio = StringIO.open # => #<StringIO>
*
* With a block, calls the block with the new instance
* and returns the block's value;
* closes the instance on block exit.
*
* StringIO.open {|strio| p strio }
* # => #<StringIO>
*
* Related: StringIO.new.
*/
static VALUE
strio_s_open(int argc, VALUE *argv, VALUE klass)
{
VALUE obj = rb_class_new_instance_kw(argc, argv, klass, RB_PASS_CALLED_KEYWORDS);
if (!rb_block_given_p()) return obj;
return rb_ensure(rb_yield, obj, strio_finalize, obj);
}
/* :nodoc: */
static VALUE
strio_s_new(int argc, VALUE *argv, VALUE klass)
{
if (rb_block_given_p()) {
VALUE cname = rb_obj_as_string(klass);
rb_warn("%"PRIsVALUE"::new() does not take block; use %"PRIsVALUE"::open() instead",
cname, cname);
}
return rb_class_new_instance_kw(argc, argv, klass, RB_PASS_CALLED_KEYWORDS);
}
/*
* Returns +false+. Just for compatibility to IO.
*/
static VALUE
strio_false(VALUE self)
{
StringIO(self);
return Qfalse;
}
/*
* Returns +nil+. Just for compatibility to IO.
*/
static VALUE
strio_nil(VALUE self)
{
StringIO(self);
return Qnil;
}
/*
* Returns an object itself. Just for compatibility to IO.
*/
static VALUE
strio_self(VALUE self)
{
StringIO(self);
return self;
}
/*
* Returns 0. Just for compatibility to IO.
*/
static VALUE
strio_0(VALUE self)
{
StringIO(self);
return INT2FIX(0);
}
/*
* Returns the argument unchanged. Just for compatibility to IO.
*/
static VALUE
strio_first(VALUE self, VALUE arg)
{
StringIO(self);
return arg;
}
/*
* Raises NotImplementedError.
*/
static VALUE
strio_unimpl(int argc, VALUE *argv, VALUE self)
{
StringIO(self);
rb_notimplement();
UNREACHABLE;
}
/*
* call-seq:
* string -> string
*
* Returns underlying string:
*
* StringIO.open('foo') do |strio|
* p strio.string
* strio.string = 'bar'
* p strio.string
* end
*
* Output:
*
* "foo"
* "bar"
*
* Related: StringIO#string= (assigns the underlying string).
*/
static VALUE
strio_get_string(VALUE self)
{
return StringIO(self)->string;
}
/*
* call-seq:
* string = other_string -> other_string
*
* Assigns the underlying string as +other_string+, and sets position to zero;
* returns +other_string+:
*
* StringIO.open('foo') do |strio|
* p strio.string
* strio.string = 'bar'
* p strio.string
* end
*
* Output:
*
* "foo"
* "bar"
*
* Related: StringIO#string (returns the underlying string).
*/
static VALUE
strio_set_string(VALUE self, VALUE string)
{
struct StringIO *ptr = StringIO(self);
rb_io_taint_check(self);
ptr->flags &= ~FMODE_READWRITE;
StringValue(string);
ptr->flags = OBJ_FROZEN(string) ? FMODE_READABLE : FMODE_READWRITE;
ptr->pos = 0;
ptr->lineno = 0;
RB_OBJ_WRITE(self, &ptr->string, string);
return string;
}
/*
* call-seq:
* close -> nil
*
* Closes +self+ for both reading and writing.
*
* Raises IOError if reading or writing is attempted.
*
* Related: StringIO#close_read, StringIO#close_write.
*/
static VALUE
strio_close(VALUE self)
{
StringIO(self);
RBASIC(self)->flags &= ~STRIO_READWRITE;
return Qnil;
}
/*
* call-seq:
* close_read -> nil
*
* Closes +self+ for reading; closed-write setting remains unchanged.
*
* Raises IOError if reading is attempted.
*
* Related: StringIO#close, StringIO#close_write.
*/
static VALUE
strio_close_read(VALUE self)
{
struct StringIO *ptr = StringIO(self);
if (!(ptr->flags & FMODE_READABLE)) {
rb_raise(rb_eIOError, "closing non-duplex IO for reading");
}
RBASIC(self)->flags &= ~STRIO_READABLE;
return Qnil;
}
/*
* call-seq:
* close_write -> nil
*
* Closes +self+ for writing; closed-read setting remains unchanged.
*
* Raises IOError if writing is attempted.
*
* Related: StringIO#close, StringIO#close_read.
*/
static VALUE
strio_close_write(VALUE self)
{
struct StringIO *ptr = StringIO(self);
if (!(ptr->flags & FMODE_WRITABLE)) {
rb_raise(rb_eIOError, "closing non-duplex IO for writing");
}
RBASIC(self)->flags &= ~STRIO_WRITABLE;
return Qnil;
}
/*
* call-seq:
* closed? -> true or false
*
* Returns +true+ if +self+ is closed for both reading and writing,
* +false+ otherwise.
*/
static VALUE
strio_closed(VALUE self)
{
StringIO(self);
if (!CLOSED(self)) return Qfalse;
return Qtrue;
}
/*
* call-seq:
* closed_read? -> true or false
*
* Returns +true+ if +self+ is closed for reading, +false+ otherwise.
*/
static VALUE
strio_closed_read(VALUE self)
{
StringIO(self);
if (READABLE(self)) return Qfalse;
return Qtrue;
}
/*
* call-seq:
* closed_write? -> true or false
*
* Returns +true+ if +self+ is closed for writing, +false+ otherwise.
*/
static VALUE
strio_closed_write(VALUE self)
{
StringIO(self);
if (WRITABLE(self)) return Qfalse;
return Qtrue;
}
static struct StringIO *
strio_to_read(VALUE self)
{
struct StringIO *ptr = readable(self);
if (ptr->pos < RSTRING_LEN(ptr->string)) return ptr;
return NULL;
}
/*
* call-seq:
* eof? -> true or false
*
* Returns +true+ if positioned at end-of-stream, +false+ otherwise;
* see {Position}[rdoc-ref:IO@Position].
*
* Raises IOError if the stream is not opened for reading.
*/
static VALUE
strio_eof(VALUE self)
{
if (strio_to_read(self)) return Qfalse;
return Qtrue;
}
/* :nodoc: */
static VALUE
strio_copy(VALUE copy, VALUE orig)
{
struct StringIO *ptr, *old_ptr;
VALUE old_string = Qundef;
orig = rb_convert_type(orig, T_DATA, "StringIO", "to_strio");
if (copy == orig) return copy;
ptr = StringIO(orig);
old_ptr = check_strio(copy);
if (old_ptr) {
old_string = old_ptr->string;
strio_free(old_ptr);
}
DATA_PTR(copy) = ptr;
RB_OBJ_WRITTEN(copy, old_string, ptr->string);
RBASIC(copy)->flags &= ~STRIO_READWRITE;
RBASIC(copy)->flags |= RBASIC(orig)->flags & STRIO_READWRITE;
++ptr->count;
return copy;
}
/*
* call-seq:
* lineno -> current_line_number
*
* Returns the current line number in +self+;
* see {Line Number}[rdoc-ref:IO@Line+Number].
*/
static VALUE
strio_get_lineno(VALUE self)
{
return LONG2NUM(StringIO(self)->lineno);
}
/*
* call-seq:
* lineno = new_line_number -> new_line_number
*
* Sets the current line number in +self+ to the given +new_line_number+;
* see {Line Number}[rdoc-ref:IO@Line+Number].
*/
static VALUE
strio_set_lineno(VALUE self, VALUE lineno)
{
StringIO(self)->lineno = NUM2LONG(lineno);
return lineno;
}
/*
* call-seq:
* binmode -> self
*
* Sets the data mode in +self+ to binary mode;
* see {Data Mode}[rdoc-ref:File@Data+Mode].
*
*/
static VALUE
strio_binmode(VALUE self)
{
struct StringIO *ptr = StringIO(self);
rb_encoding *enc = rb_ascii8bit_encoding();
ptr->enc = enc;
if (WRITABLE(self)) {
rb_enc_associate(ptr->string, enc);
}
return self;
}
#define strio_fcntl strio_unimpl
#define strio_flush strio_self
#define strio_fsync strio_0
/*
* call-seq:
* reopen(other, mode = 'r+') -> self
*
* Reinitializes the stream with the given +other+ (string or StringIO) and +mode+;
* see IO.new:
*
* StringIO.open('foo') do |strio|
* p strio.string
* strio.reopen('bar')
* p strio.string
* other_strio = StringIO.new('baz')
* strio.reopen(other_strio)
* p strio.string
* other_strio.close
* end
*
* Output:
*
* "foo"
* "bar"
* "baz"
*
*/
static VALUE
strio_reopen(int argc, VALUE *argv, VALUE self)
{
rb_io_taint_check(self);
if (argc == 1 && !RB_TYPE_P(*argv, T_STRING)) {
return strio_copy(self, *argv);
}
return strio_init(argc, argv, StringIO(self), self);
}
/*
* call-seq:
* pos -> stream_position
*
* Returns the current position (in bytes);
* see {Position}[rdoc-ref:IO@Position].
*/
static VALUE
strio_get_pos(VALUE self)
{
return LONG2NUM(StringIO(self)->pos);
}
/*
* call-seq:
* pos = new_position -> new_position
*
* Sets the current position (in bytes);
* see {Position}[rdoc-ref:IO@Position].
*/
static VALUE
strio_set_pos(VALUE self, VALUE pos)
{
struct StringIO *ptr = StringIO(self);
long p = NUM2LONG(pos);
if (p < 0) {
error_inval(0);
}
ptr->pos = p;
return pos;
}
/*
* call-seq:
* rewind -> 0
*
* Sets the current position and line number to zero;
* see {Position}[rdoc-ref:IO@Position]
* and {Line Number}[rdoc-ref:IO@Line+Number].
*/
static VALUE
strio_rewind(VALUE self)
{
struct StringIO *ptr = StringIO(self);
ptr->pos = 0;
ptr->lineno = 0;
return INT2FIX(0);
}
/*
* call-seq:
* seek(offset, whence = SEEK_SET) -> 0
*
* Sets the current position to the given integer +offset+ (in bytes),
* with respect to a given constant +whence+;
* see {Position}[rdoc-ref:IO@Position].
*/
static VALUE
strio_seek(int argc, VALUE *argv, VALUE self)
{
VALUE whence;
struct StringIO *ptr = StringIO(self);
long amount, offset;
rb_scan_args(argc, argv, "11", NULL, &whence);
amount = NUM2LONG(argv[0]);
if (CLOSED(self)) {
rb_raise(rb_eIOError, "closed stream");
}
switch (NIL_P(whence) ? 0 : NUM2LONG(whence)) {
case 0:
offset = 0;
break;
case 1:
offset = ptr->pos;
break;
case 2:
offset = RSTRING_LEN(ptr->string);
break;
default:
error_inval("invalid whence");
}
if (amount > LONG_MAX - offset || amount + offset < 0) {
error_inval(0);
}
ptr->pos = amount + offset;
return INT2FIX(0);
}
/*
* call-seq:
* sync -> true
*
* Returns +true+; implemented only for compatibility with other stream classes.
*/
static VALUE
strio_get_sync(VALUE self)
{
StringIO(self);
return Qtrue;
}
#define strio_set_sync strio_first
#define strio_tell strio_get_pos
/*
* call-seq:
* each_byte {|byte| ... } -> self
*
* With a block given, calls the block with each remaining byte in the stream;
* see {Byte IO}[rdoc-ref:IO@Byte+IO].
*
* With no block given, returns an enumerator.
*/
static VALUE
strio_each_byte(VALUE self)
{
struct StringIO *ptr;
RETURN_ENUMERATOR(self, 0, 0);
while ((ptr = strio_to_read(self)) != NULL) {
char c = RSTRING_PTR(ptr->string)[ptr->pos++];
rb_yield(CHR2FIX(c));
}
return self;
}
/*
* call-seq:
* getc -> character or nil
*
* Reads and returns the next character from the stream;
* see {Character IO}[rdoc-ref:IO@Character+IO].
*/
static VALUE
strio_getc(VALUE self)
{
struct StringIO *ptr = readable(self);
rb_encoding *enc = get_enc(ptr);
VALUE str = ptr->string;
long pos = ptr->pos;
int len;
char *p;
if (pos >= RSTRING_LEN(str)) {
return Qnil;
}
p = RSTRING_PTR(str)+pos;
len = rb_enc_mbclen(p, RSTRING_END(str), enc);
ptr->pos += len;
return enc_subseq(str, pos, len, enc);
}
/*
* call-seq:
* getbyte -> byte or nil
*
* Reads and returns the next 8-bit byte from the stream;
* see {Byte IO}[rdoc-ref:IO@Byte+IO].
*/
static VALUE
strio_getbyte(VALUE self)
{
struct StringIO *ptr = readable(self);
int c;
if (ptr->pos >= RSTRING_LEN(ptr->string)) {
return Qnil;
}
c = RSTRING_PTR(ptr->string)[ptr->pos++];
return CHR2FIX(c);
}
static void
strio_extend(struct StringIO *ptr, long pos, long len)
{
long olen;
if (len > LONG_MAX - pos)
rb_raise(rb_eArgError, "string size too big");
check_modifiable(ptr);
olen = RSTRING_LEN(ptr->string);
if (pos + len > olen) {
rb_str_resize(ptr->string, pos + len);
if (pos > olen)
MEMZERO(RSTRING_PTR(ptr->string) + olen, char, pos - olen);
}
else {
rb_str_modify(ptr->string);
}
}
/*
* call-seq:
* ungetc(character) -> nil
*
* Pushes back ("unshifts") a character or integer onto the stream;
* see {Character IO}[rdoc-ref:IO@Character+IO].
*/
static VALUE
strio_ungetc(VALUE self, VALUE c)
{
struct StringIO *ptr = readable(self);
rb_encoding *enc, *enc2;
check_modifiable(ptr);
if (NIL_P(c)) return Qnil;
if (RB_INTEGER_TYPE_P(c)) {
int len, cc = NUM2INT(c);
char buf[16];
enc = rb_enc_get(ptr->string);
len = rb_enc_codelen(cc, enc);
if (len <= 0) rb_enc_uint_chr(cc, enc);
rb_enc_mbcput(cc, buf, enc);
return strio_unget_bytes(ptr, buf, len);
}
else {
SafeStringValue(c);
enc = rb_enc_get(ptr->string);
enc2 = rb_enc_get(c);
if (enc != enc2 && enc != rb_ascii8bit_encoding()) {
c = rb_str_conv_enc(c, enc2, enc);
}
strio_unget_bytes(ptr, RSTRING_PTR(c), RSTRING_LEN(c));
RB_GC_GUARD(c);
return Qnil;
}
}
/*
* call-seq:
* ungetbyte(byte) -> nil
*
* Pushes back ("unshifts") an 8-bit byte onto the stream;
* see {Byte IO}[rdoc-ref:IO@Byte+IO].
*/
static VALUE
strio_ungetbyte(VALUE self, VALUE c)
{
struct StringIO *ptr = readable(self);
check_modifiable(ptr);
if (NIL_P(c)) return Qnil;
if (RB_INTEGER_TYPE_P(c)) {
/* rb_int_and() not visible from exts */
VALUE v = rb_funcall(c, '&', 1, INT2FIX(0xff));
const char cc = NUM2INT(v) & 0xFF;
strio_unget_bytes(ptr, &cc, 1);
}
else {
long cl;
SafeStringValue(c);
cl = RSTRING_LEN(c);
if (cl > 0) {
strio_unget_bytes(ptr, RSTRING_PTR(c), cl);
RB_GC_GUARD(c);
}
}
return Qnil;
}
static VALUE
strio_unget_bytes(struct StringIO *ptr, const char *cp, long cl)
{
long pos = ptr->pos, len, rest;
VALUE str = ptr->string;
char *s;
len = RSTRING_LEN(str);