14#define STRINGIO_VERSION "0.1.0"
19#if defined(HAVE_FCNTL_H) || defined(_WIN32)
21#elif defined(HAVE_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)) {
119static 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) {
156strio_memsize(
const void *p)
171#define check_strio(self) ((struct StringIO*)rb_check_typeddata((self), &strio_data_type))
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)
217static VALUE sym_exception;
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;
380strio_finalize(
VALUE self)
420strio_false(
VALUE self)
440strio_self(
VALUE self)
484strio_get_string(
VALUE self)
517strio_close(
VALUE self)
520 RBASIC(
self)->flags &= ~STRIO_READWRITE;
532strio_close_read(
VALUE self)
538 RBASIC(
self)->flags &= ~STRIO_READABLE;
550strio_close_write(
VALUE self)
556 RBASIC(
self)->flags &= ~STRIO_WRITABLE;
567strio_closed(
VALUE self)
581strio_closed_read(
VALUE self)
595strio_closed_write(
VALUE self)
625 if (copy == orig)
return copy;
631 RBASIC(copy)->flags &= ~STRIO_READWRITE;
648strio_get_lineno(
VALUE self)
675strio_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);
719strio_get_pos(
VALUE self)
750strio_rewind(
VALUE self)
790 if (amount >
LONG_MAX - offset || amount + offset < 0) {
793 ptr->pos = amount + offset;
804strio_get_sync(
VALUE self)
810#define strio_set_sync strio_first
812#define strio_tell strio_get_pos
822strio_each_byte(
VALUE self)
839strio_bytes(
VALUE self)
841 rb_warn(
"StringIO#bytes is deprecated; use #each_byte instead");
844 return strio_each_byte(
self);
854strio_getc(
VALUE self)
879strio_getbyte(
VALUE self)
898 check_modifiable(
ptr);
925 check_modifiable(
ptr);
961 check_modifiable(
ptr);
967 strio_unget_bytes(
ptr, &
cc, 1);
982strio_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);
1019strio_readchar(
VALUE self)
1033strio_readbyte(
VALUE self)
1048strio_each_char(
VALUE self)
1054 while (!
NIL_P(c = strio_getc(
self))) {
1064strio_chars(
VALUE self)
1066 rb_warn(
"StringIO#chars is deprecated; use #each_char instead");
1069 return strio_each_char(
self);
1080strio_each_codepoint(
VALUE self)
1089 ptr = readable(
self);
1108strio_codepoints(
VALUE self)
1110 rb_warn(
"StringIO#codepoints is deprecated; use #each_codepoint instead");
1113 return strio_each_codepoint(
self);
1118bm_init_skip(
long *skip,
const char *pat,
long m)
1122 for (c = 0; c < (1 <<
CHAR_BIT); c++) {
1126 skip[(
unsigned char)*pat++] = m;
1131bm_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];
1201chomp_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
1667strio_size(
VALUE self)
1670 if (
NIL_P(
string)) {
1709strio_external_encoding(
VALUE self)
1724strio_internal_encoding(
VALUE self)
1744 VALUE ext_enc, int_enc, opt;
1748 if (
NIL_P(ext_enc)) {
1763strio_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);
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
rb_encoding * rb_ascii8bit_encoding(void)
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
rb_encoding * rb_enc_from_index(int index)
int rb_utf8_encindex(void)
rb_encoding * rb_enc_get(VALUE obj)
void rb_enc_copy(VALUE obj1, VALUE obj2)
rb_encoding * rb_enc_find(const char *name)
rb_encoding * rb_default_external_encoding(void)
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
rb_encoding * rb_to_encoding(VALUE enc)
VALUE rb_enc_from_encoding(rb_encoding *encoding)
VALUE rb_enc_associate_index(VALUE obj, int idx)
int rb_enc_find_index(const char *name)
int rb_enc_codelen(int c, rb_encoding *enc)
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
#define rb_enc_mbcput(c, buf, enc)
VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
#define ENCODING_MAXNAMELEN
VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc)
#define rb_enc_right_char_head(s, p, e, enc)
char str[HTML_ESCAPE_MAX_LEN+1]
void rb_include_module(VALUE, VALUE)
VALUE rb_define_class(const char *, VALUE)
Defines a top-level class.
VALUE rb_define_module_under(VALUE, const char *)
int rb_block_given_p(void)
Determines if the current method is given a block.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *)
VALUE rb_cData
Data class.
void rb_notimplement(void)
void rb_syserr_fail(int e, const char *mesg)
void rb_raise(VALUE exc, const char *fmt,...)
void rb_warn(const char *fmt,...)
VALUE rb_ensure(VALUE(*)(VALUE), VALUE, VALUE(*)(VALUE), VALUE)
An equivalent to ensure clause.
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Converts an object into another type.
VALUE rb_class_new_instance_kw(int, const VALUE *, VALUE, int)
VALUE rb_check_to_integer(VALUE, const char *)
Tries to convert val into Integer.
int rb_io_oflags_fmode(int oflags)
int rb_io_modestr_fmode(const char *modestr)
#define FMODE_SETENC_BY_BOM
int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
struct rb_io_enc_t rb_io_enc_t
VALUE rb_io_taint_check(VALUE)
#define rb_io_extract_modeenc
char strio_flags_check[(STRIO_READABLE/FMODE_READABLE==STRIO_WRITABLE/FMODE_WRITABLE) *2 - 1]
#define check_strio(self)