Class: MmapScanner
- Inherits:
-
Object
- Object
- MmapScanner
- Defined in:
- ext/mmapscanner.c
Defined Under Namespace
Classes: Mmap
Instance Method Summary collapse
- #check(re) ⇒ Object
- #eos? ⇒ Boolean
- #initialize(*args) ⇒ Object constructor
-
#inspect ⇒ Object
rb_define_method(cMmapScanner, “[]”, slice, 2);.
- #length ⇒ Object
- #match?(re) ⇒ Boolean
- #peek(size) ⇒ Object
- #pos ⇒ Object
- #pos=(pos) ⇒ Object
- #rest ⇒ Object
- #scan(re) ⇒ Object
- #size ⇒ Object
- #skip(re) ⇒ Object
- #slice(pos, len) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*args) ⇒ Object
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 |
# File 'ext/mmapscanner.c', line 36 static VALUE initialize(int argc, VALUE *argv, VALUE obj) { VALUE src, voffset, vsize; size_t offset, size; size_t src_offset, src_size; VALUE src_data; rb_scan_args(argc, argv, "12", &src, &voffset, &vsize); if (voffset != Qnil && NUM2LL(voffset) < 0) rb_raise(rb_eRangeError, "offset out of range: %lld", NUM2LL(voffset)); if (vsize != Qnil && NUM2LL(vsize) < 0) rb_raise(rb_eRangeError, "length out of range: %lld", NUM2LL(vsize)); offset = voffset == Qnil ? 0 : NUM2SIZET(voffset); if (rb_obj_class(src) == cMmapScanner) { src_offset = NUM2SIZET(rb_iv_get(src, "offset")); src_size = NUM2SIZET(rb_iv_get(src, "size")); src_data = rb_iv_get(src, "data"); } else if (TYPE(src) == T_FILE) { int fd; struct stat st; fd = RFILE(src)->fptr->fd; fstat(fd, &st); src_offset = 0; src_size = st.st_size; size = vsize == Qnil ? src_size - offset : NUM2SIZET(vsize); if (size > st.st_size - offset) size = st.st_size - offset; src_data = create_mmap_object(fd, offset, size); } else if (TYPE(src) == T_STRING) { src_offset = 0; src_size = RSTRING_LEN(src); src_data = src; } else { rb_raise(rb_eTypeError, "wrong argument type %s (expected File/String/MmapScanner)", rb_obj_classname(src)); } if (offset >= src_size) rb_raise(rb_eRangeError, "length out of range: %zu >= %zu", offset, src_size); size = vsize == Qnil ? src_size - offset : NUM2SIZET(vsize); if (size > src_size - offset) size = src_size - offset; rb_iv_set(obj, "offset", SIZET2NUM(src_offset + offset)); rb_iv_set(obj, "size", SIZET2NUM(size)); rb_iv_set(obj, "data", src_data); rb_iv_set(obj, "pos", INT2NUM(0)); return Qnil; } |
Instance Method Details
#check(re) ⇒ Object
191 192 193 194 |
# File 'ext/mmapscanner.c', line 191 static VALUE check(VALUE obj, VALUE re) { return scan_sub(obj, re, 0); } |
#eos? ⇒ Boolean
222 223 224 225 226 227 |
# File 'ext/mmapscanner.c', line 222 static VALUE eos_p(VALUE obj) { size_t data_pos = NUM2SIZET(rb_iv_get(obj, "pos")); size_t data_size = NUM2SIZET(rb_iv_get(obj, "size")); return data_pos >= data_size ? Qtrue : Qfalse; } |
#inspect ⇒ Object
rb_define_method(cMmapScanner, “[]”, slice, 2);
106 107 108 109 |
# File 'ext/mmapscanner.c', line 106 static VALUE inspect(VALUE obj) { return rb_str_new2("#<MmapScanner>"); } |
#length ⇒ Object
83 84 85 86 |
# File 'ext/mmapscanner.c', line 83 static VALUE size(VALUE obj) { return rb_iv_get(obj, "size"); } |
#match?(re) ⇒ Boolean
204 205 206 207 208 209 210 |
# File 'ext/mmapscanner.c', line 204 static VALUE match_p(VALUE obj, VALUE re) { VALUE ret = scan_sub(obj, re, 0); if (ret == Qnil) return ret; return rb_iv_get(ret, "size"); } |
#peek(size) ⇒ Object
212 213 214 215 216 217 218 219 220 |
# File 'ext/mmapscanner.c', line 212 static VALUE peek(VALUE obj, VALUE size) { size_t sz = NUM2SIZET(size); size_t data_pos = NUM2SIZET(rb_iv_get(obj, "pos")); size_t data_size = NUM2SIZET(rb_iv_get(obj, "size")); if (sz > data_size - data_pos) sz = data_size - data_pos; return rb_funcall(cMmapScanner, rb_intern("new"), 3, obj, SIZET2NUM(data_pos), SIZET2NUM(sz)); } |
#pos ⇒ Object
111 112 113 114 |
# File 'ext/mmapscanner.c', line 111 static VALUE pos(VALUE obj) { return rb_iv_get(obj, "pos"); } |
#pos=(pos) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'ext/mmapscanner.c', line 116 static VALUE set_pos(VALUE obj, VALUE pos) { size_t p, size; if (NUM2LL(pos) < 0) rb_raise(rb_eRangeError, "out of range: %lld", NUM2LL(pos)); p = NUM2SIZET(pos); size = NUM2SIZET(rb_iv_get(obj, "size")); if (p > size) rb_raise(rb_eRangeError, "out of range: %zu > %zu", p, size); rb_iv_set(obj, "pos", pos); return pos; } |
#rest ⇒ Object
229 230 231 232 |
# File 'ext/mmapscanner.c', line 229 static VALUE rest(VALUE obj) { return rb_funcall(cMmapScanner, rb_intern("new"), 2, obj, rb_iv_get(obj, "pos")); } |
#scan(re) ⇒ Object
186 187 188 189 |
# File 'ext/mmapscanner.c', line 186 static VALUE scan(VALUE obj, VALUE re) { return scan_sub(obj, re, 1); } |
#size ⇒ Object
83 84 85 86 |
# File 'ext/mmapscanner.c', line 83 static VALUE size(VALUE obj) { return rb_iv_get(obj, "size"); } |
#skip(re) ⇒ Object
196 197 198 199 200 201 202 |
# File 'ext/mmapscanner.c', line 196 static VALUE skip(VALUE obj, VALUE re) { VALUE ret = scan_sub(obj, re, 1); if (ret == Qnil) return ret; return rb_iv_get(ret, "size"); } |
#slice(pos, len) ⇒ Object
101 102 103 104 |
# File 'ext/mmapscanner.c', line 101 static VALUE slice(VALUE obj, VALUE pos, VALUE len) { return rb_funcall(cMmapScanner, rb_intern("new"), 3, obj, pos, len); } |
#to_s ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'ext/mmapscanner.c', line 88 static VALUE to_s(VALUE obj) { size_t offset = NUM2SIZET(rb_iv_get(obj, "offset")); size_t size = NUM2SIZET(rb_iv_get(obj, "size")); VALUE data = rb_iv_get(obj, "data"); mmap_data_t *mdata; if (TYPE(data) == T_STRING) return rb_str_new(RSTRING_PTR(data)+offset, size); Data_Get_Struct(data, mmap_data_t, mdata); return rb_str_new(mdata->ptr+offset, size); } |