Ruby
2.7.2p137(2020-10-01revision5445e0435260b449decf2ac16f9d09bae3cafe72)
|
Go to the documentation of this file.
14 #define STRINGIO_VERSION "0.1.0"
19 #if defined(HAVE_FCNTL_H) || defined(_WIN32)
21 #elif defined(HAVE_SYS_FCNTL_H)
22 #include <sys/fcntl.h>
25 #ifndef RB_INTEGER_TYPE_P
26 # define RB_INTEGER_TYPE_P(c) (FIXNUM_P(c) || RB_TYPE_P(c, T_BIGNUM))
29 #ifndef RB_PASS_CALLED_KEYWORDS
30 # define rb_funcallv_kw(recv, mid, arg, argv, kw_splat) rb_funcallv(recv, mid, arg, argv)
31 # define rb_class_new_instance_kw(argc, argv, klass, kw_splat) rb_class_new_instance(argc, argv, klass)
34 #ifndef HAVE_RB_IO_EXTRACT_MODEENC
35 #define rb_io_extract_modeenc strio_extract_modeenc
38 int *oflags_p,
int *fmode_p,
struct rb_io_enc_t *convconfig_p)
40 VALUE mode = *vmode_p;
43 int has_enc = 0, has_vmode = 0;
45 convconfig_p->enc = convconfig_p->enc2 = 0;
82 if (!
NIL_P(opthash)) {
119 static VALUE strio_unget_bytes(
struct StringIO *,
const char *,
long);
122 #define IS_STRIO(obj) (rb_typeddata_is_kind_of((obj), &strio_data_type))
123 #define error_inval(msg) (rb_syserr_fail(EINVAL, msg))
124 #define get_enc(ptr) ((ptr)->enc ? (ptr)->enc : rb_enc_get((ptr)->string))
150 if (--
ptr->count <= 0) {
156 strio_memsize(
const void *p)
171 #define check_strio(self) ((struct StringIO*)rb_check_typeddata((self), &strio_data_type))
174 get_strio(
VALUE self)
198 if (
len > rlen)
len = rlen;
204 #define StringIO(obj) get_strio(obj)
206 #define STRIO_READABLE FL_USER4
207 #define STRIO_WRITABLE FL_USER5
208 #define STRIO_READWRITE (STRIO_READABLE|STRIO_WRITABLE)
210 #define STRIO_MODE_SET_P(strio, mode) \
211 ((RBASIC(strio)->flags & STRIO_##mode) && \
212 ((struct StringIO*)DATA_PTR(strio))->flags & FMODE_##mode)
213 #define CLOSED(strio) (!STRIO_MODE_SET_P(strio, READWRITE))
214 #define READABLE(strio) STRIO_MODE_SET_P(strio, READABLE)
215 #define WRITABLE(strio) STRIO_MODE_SET_P(strio, WRITABLE)
217 static VALUE sym_exception;
220 readable(
VALUE strio)
230 writable(
VALUE strio)
277 if (
len < 1)
return 0;
278 switch ((
unsigned char)p[0]) {
281 if ((
unsigned char)p[1] == 0xBB &&
len > 2) {
282 if ((
unsigned char)p[2] == 0xBF) {
291 if ((
unsigned char)p[1] == 0xFF) {
299 if ((
unsigned char)p[1] == 0xFE) {
300 if (
len >= 4 && (
unsigned char)p[2] == 0 && (
unsigned char)p[3] == 0) {
311 if ((
unsigned char)p[1] == 0 && (
unsigned char)p[2] == 0xFE && (
unsigned char)p[3] == 0xFF) {
323 int bomlen, idx = detect_bom(
ptr->string, &bomlen);
365 ptr->string = string;
370 ptr->enc = convconfig.enc;
380 strio_finalize(
VALUE self)
420 strio_false(
VALUE self)
430 strio_nil(
VALUE self)
440 strio_self(
VALUE self)
484 strio_get_string(
VALUE self)
517 strio_close(
VALUE self)
532 strio_close_read(
VALUE self)
550 strio_close_write(
VALUE self)
567 strio_closed(
VALUE self)
581 strio_closed_read(
VALUE self)
595 strio_closed_write(
VALUE self)
611 strio_eof(
VALUE self)
625 if (copy == orig)
return copy;
648 strio_get_lineno(
VALUE self)
675 strio_binmode(
VALUE self)
687 #define strio_fcntl strio_unimpl
689 #define strio_flush strio_self
691 #define strio_fsync strio_0
706 return strio_copy(
self, *
argv);
719 strio_get_pos(
VALUE self)
750 strio_rewind(
VALUE self)
790 if (amount >
LONG_MAX - offset || amount + offset < 0) {
793 ptr->pos = amount + offset;
804 strio_get_sync(
VALUE self)
810 #define strio_set_sync strio_first
812 #define strio_tell strio_get_pos
822 strio_each_byte(
VALUE self)
839 strio_bytes(
VALUE self)
841 rb_warn(
"StringIO#bytes is deprecated; use #each_byte instead");
844 return strio_each_byte(
self);
854 strio_getc(
VALUE self)
879 strio_getbyte(
VALUE self)
898 check_modifiable(
ptr);
925 check_modifiable(
ptr);
961 check_modifiable(
ptr);
967 strio_unget_bytes(
ptr, &
cc, 1);
982 strio_unget_bytes(
struct StringIO *
ptr,
const char *cp,
long cl)
991 long ex = (rest < 0 ? cl-
pos : cl+rest);
995 if (rest < 0)
memmove(s + cl, s +
pos, -rest);
1004 if (rest > cl)
memset(s +
len, 0, rest - cl);
1019 strio_readchar(
VALUE self)
1033 strio_readbyte(
VALUE self)
1048 strio_each_char(
VALUE self)
1054 while (!
NIL_P(c = strio_getc(
self))) {
1064 strio_chars(
VALUE self)
1066 rb_warn(
"StringIO#chars is deprecated; use #each_char instead");
1069 return strio_each_char(
self);
1080 strio_each_codepoint(
VALUE self)
1089 ptr = readable(
self);
1108 strio_codepoints(
VALUE self)
1110 rb_warn(
"StringIO#codepoints is deprecated; use #each_codepoint instead");
1113 return strio_each_codepoint(
self);
1118 bm_init_skip(
long *skip,
const char *pat,
long m)
1122 for (c = 0; c < (1 <<
CHAR_BIT); c++) {
1126 skip[(
unsigned char)*pat++] = m;
1131 bm_search(
const char *little,
long llen,
const char *big,
long blen,
const long *skip)
1139 while (j >= 0 && big[k] == little[j]) {
1143 if (j < 0)
return k + 1;
1144 i += skip[(
unsigned char)big[
i]];
1189 static ID keywords[1];
1201 chomp_newline_width(
const char *s,
const char *e)
1203 if (e > s && *--e ==
'\n') {
1204 if (e > s && *--e ==
'\r')
return 2;
1213 const char *s, *e, *p;
1225 if (
limit > 0 && (
size_t)
limit < (
size_t)(e - s)) {
1230 w = chomp_newline_width(s, e);
1232 str = strio_substr(
ptr,
ptr->pos, e - s - w, enc);
1236 while (p[(p + 1 < e) && (*p ==
'\r') && 0] ==
'\n') {
1243 while ((p =
memchr(p,
'\n', e - p)) && (p != e)) {
1246 w = (
arg->chomp ? 1 : 0);
1249 else if (*p ==
'\r' && p < e && p[1] ==
'\n') {
1251 w = (
arg->chomp ? 2 : 0);
1255 if (!w &&
arg->chomp) {
1256 w = chomp_newline_width(s, e);
1263 w = (
arg->chomp ? (p > s && *(p-1) ==
'\r') + 1 : 0);
1265 str = strio_substr(
ptr,
ptr->pos, e - s - w, enc);
1270 for (p = s; p +
n <= e; ++p) {
1272 e = p + (
arg->chomp ? 0 :
n);
1280 bm_init_skip(skip, p,
n);
1281 if ((pos = bm_search(p,
n, s, e - s, skip)) >= 0) {
1282 e = s + pos + (
arg->chomp ? 0 :
n);
1286 str = strio_substr(
ptr,
ptr->pos, e - s - w, enc);
1312 str = strio_getline(&
arg, readable(
self));
1360 while (!
NIL_P(line = strio_getline(&
arg, readable(
self)))) {
1372 rb_warn(
"StringIO#lines is deprecated; use #each_line instead");
1375 return strio_each(
argc,
argv,
self);
1398 while (!
NIL_P(line = strio_getline(&
arg, readable(
self)))) {
1418 while (
argc-- > 0) {
1420 len += strio_write(
self, *
argv++);
1437 if (
enc != enc2 &&
enc != ascii8bit) {
1441 if (
len == 0)
return 0;
1442 check_modifiable(
ptr);
1447 if (
ptr->pos == olen) {
1448 if (
enc == ascii8bit || enc2 == ascii8bit) {
1470 #define strio_addstr rb_io_addstr
1479 #define strio_print rb_io_print
1487 #define strio_printf rb_io_printf
1501 check_modifiable(
ptr);
1509 strio_write(
self,
str);
1519 #define strio_puts rb_io_puts
1560 if (len <= ptr->
pos) {
1582 if (
len > rest)
len = rest;
1630 val = strio_read(
argc,
argv,
self);
1642 #define strio_syswrite rb_io_write
1653 #define strio_isatty strio_false
1655 #define strio_pid strio_nil
1657 #define strio_fileno strio_nil
1667 strio_size(
VALUE self)
1670 if (
NIL_P(
string)) {
1709 strio_external_encoding(
VALUE self)
1724 strio_internal_encoding(
VALUE self)
1744 VALUE ext_enc, int_enc, opt;
1748 if (
NIL_P(ext_enc)) {
1763 strio_set_encoding_by_bom(
VALUE self)
1767 if (!set_encoding_by_bom(
ptr))
return Qnil;
1898 rb_define_method(mWritable,
"write_nonblock", strio_syswrite_nonblock, -1);
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
void rb_include_module(VALUE klass, VALUE module)
VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc)
RUBY_EXTERN VALUE rb_cData
#define RB_PASS_CALLED_KEYWORDS
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
#define rb_enc_mbcput(c, buf, enc)
VALUE rb_define_module_under(VALUE outer, const char *name)
void rb_warn(const char *fmt,...)
int rb_block_given_p(void)
Determines if the current method is given a block.
VALUE rb_funcallv_kw(VALUE, ID, int, const VALUE *, int)
int rb_io_oflags_fmode(int oflags)
char * strchr(char *, char)
VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
VALUE rb_str_substr(VALUE, long, long)
VALUE rb_class_new_instance_kw(int, const VALUE *, VALUE, int)
VALUE rb_hash_aref(VALUE hash, VALUE key)
int rb_utf8_encindex(void)
rb_encoding * rb_default_external_encoding(void)
VALUE rb_obj_as_string(VALUE)
#define RB_TYPE_P(obj, type)
#define rb_intern_const(str)
#define rb_io_extract_modeenc
rb_encoding * rb_enc_get(VALUE obj)
VALUE rb_call_super(int, const VALUE *)
void rb_lastline_set(VALUE)
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
VALUE rb_check_string_type(VALUE)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
void rb_str_modify(VALUE)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
RUBY_EXTERN VALUE rb_eIOError
struct rb_io_enc_t rb_io_enc_t
VALUE rb_io_taint_check(VALUE)
#define rb_enc_right_char_head(s, p, e, enc)
#define SafeStringValue(v)
VALUE rb_enc_from_encoding(rb_encoding *encoding)
size_t strlen(const char *)
#define ENCODING_MAXNAMELEN
VALUE rb_str_resize(VALUE, long)
void rb_raise(VALUE exc, const char *fmt,...)
void rb_notimplement(void)
void rb_syserr_fail(int e, const char *mesg)
rb_encoding * rb_ascii8bit_encoding(void)
rb_encoding * rb_enc_from_index(int index)
VALUE rb_check_to_integer(VALUE, const char *)
Tries to convert val into Integer.
VALUE rb_ary_push(VALUE ary, VALUE item)
#define FMODE_SETENC_BY_BOM
#define TypedData_Wrap_Struct(klass, data_type, sval)
#define RETURN_ENUMERATOR(obj, argc, argv)
void rb_enc_copy(VALUE obj1, VALUE obj2)
void rb_str_set_len(VALUE, long)
char strio_flags_check[(STRIO_READABLE/FMODE_READABLE==STRIO_WRITABLE/FMODE_WRITABLE) *2 - 1]
#define MEMZERO(p, type, n)
#define MEMCMP(p1, p2, type, n)
#define CONST_ID(var, str)
#define StringValueCStr(v)
VALUE rb_str_buf_append(VALUE, VALUE)
unsigned char buf[MIME_BUF_SIZE]
rb_encoding * rb_to_encoding(VALUE enc)
int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
rb_encoding * rb_enc_find(const char *name)
VALUE rb_str_subseq(VALUE, long, long)
char str[HTML_ESCAPE_MAX_LEN+1]
int rb_enc_find_index(const char *name)
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Converts an object into another type.
#define RUBY_TYPED_FREE_IMMEDIATELY
#define check_strio(self)
#define MEMCPY(p1, p2, type, n)
int rb_io_modestr_fmode(const char *modestr)
void rb_str_modify_expand(VALUE, long)
void rb_define_const(VALUE, const char *, VALUE)
#define OBJ_FROZEN_RAW(x)
void rb_gc_mark(VALUE ptr)
VALUE rb_enumeratorize(VALUE obj, VALUE meth, int argc, const VALUE *argv)
int rb_enc_codelen(int c, rb_encoding *enc)
#define RB_INTEGER_TYPE_P(obj)
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_enc_associate_index(VALUE obj, int idx)