mmapscanner 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/ext/mmapscanner.c CHANGED
@@ -95,7 +95,7 @@ static VALUE mmap_unmap(VALUE obj)
95
95
  static void mmapscanner_free(mmapscanner_t *ms)
96
96
  {
97
97
  onig_region_free(&ms->regs, 0);
98
- free(ms);
98
+ xfree(ms);
99
99
  }
100
100
 
101
101
  static void mark(mmapscanner_t *ms)
@@ -107,13 +107,14 @@ static void mark(mmapscanner_t *ms)
107
107
  VALUE allocate(VALUE klass)
108
108
  {
109
109
  mmapscanner_t *ms;
110
- ms = malloc(sizeof *ms);
110
+ ms = xmalloc(sizeof *ms);
111
111
  ms->offset = 0;
112
112
  ms->size = 0;
113
113
  ms->pos = 0;
114
114
  ms->matched = 0;
115
115
  ms->matched_pos = 0;
116
116
  onig_region_init(&ms->regs);
117
+ ms->data = Qnil;
117
118
  ms->dummy_str = Qnil;
118
119
  return Data_Wrap_Struct(klass, mark, mmapscanner_free, ms);
119
120
  }
@@ -170,6 +171,26 @@ static VALUE initialize(int argc, VALUE *argv, VALUE obj)
170
171
  return Qnil;
171
172
  }
172
173
 
174
+ static VALUE create_from_mmapscanner(VALUE src, size_t offset, size_t size)
175
+ {
176
+ mmapscanner_t *ms, *new;
177
+ VALUE obj;
178
+
179
+ Data_Get_Struct(src, mmapscanner_t, ms);
180
+
181
+ if (offset > ms->size)
182
+ rb_raise(rb_eRangeError, "length out of range: %zu > %zu", offset, ms->size);
183
+ if (size > ms->size - offset)
184
+ size = ms->size - offset;
185
+
186
+ obj = allocate(cMmapScanner);
187
+ Data_Get_Struct(obj, mmapscanner_t, new);
188
+ new->offset = ms->offset + offset;
189
+ new->size = size;
190
+ new->data = ms->data;
191
+ return obj;
192
+ }
193
+
173
194
  static VALUE size(VALUE obj)
174
195
  {
175
196
  mmapscanner_t *ms;
@@ -199,7 +220,7 @@ static VALUE to_s(VALUE obj)
199
220
 
200
221
  static VALUE slice(VALUE obj, VALUE pos, VALUE len)
201
222
  {
202
- return rb_funcall(cMmapScanner, rb_intern("new"), 3, obj, pos, len);
223
+ return create_from_mmapscanner(obj, pos, len);
203
224
  }
204
225
 
205
226
  static VALUE inspect(VALUE obj)
@@ -294,7 +315,7 @@ static VALUE scan_sub(VALUE obj, VALUE re, int forward, int headonly, int sizeon
294
315
 
295
316
  if (sizeonly)
296
317
  return SIZET2NUM(matched_len);
297
- return rb_funcall(cMmapScanner, rb_intern("new"), 3, obj, SIZET2NUM(old_pos), SIZET2NUM(matched_len));
318
+ return create_from_mmapscanner(obj, old_pos, matched_len);
298
319
  }
299
320
 
300
321
  static VALUE scan(VALUE obj, VALUE re)
@@ -329,7 +350,7 @@ static VALUE peek(VALUE obj, VALUE size)
329
350
  size_t sz = NUM2SIZET(size);
330
351
  if (sz > ms->size - ms->pos)
331
352
  sz = ms->size - ms->pos;
332
- return rb_funcall(cMmapScanner, rb_intern("new"), 3, obj, SIZET2NUM(ms->pos), SIZET2NUM(sz));
353
+ return create_from_mmapscanner(obj, ms->pos, sz);
333
354
  }
334
355
 
335
356
  static VALUE eos_p(VALUE obj)
@@ -343,10 +364,10 @@ static VALUE rest(VALUE obj)
343
364
  {
344
365
  mmapscanner_t *ms;
345
366
  Data_Get_Struct(obj, mmapscanner_t, ms);
346
- return rb_funcall(cMmapScanner, rb_intern("new"), 2, obj, SIZET2NUM(ms->pos));
367
+ return create_from_mmapscanner(obj, ms->pos, ms->size - ms->pos);
347
368
  }
348
369
 
349
- static int matched_sub(int argc, VALUE *argv, mmapscanner_t *ms, size_t *pos, size_t *len)
370
+ static int matched_sub(int argc, VALUE *argv, mmapscanner_t *ms, int *pos, int *len)
350
371
  {
351
372
  int i = 0;
352
373
  if (ms->matched == 0)
@@ -361,6 +382,8 @@ static int matched_sub(int argc, VALUE *argv, mmapscanner_t *ms, size_t *pos, si
361
382
  return 0;
362
383
  if (i >= ms->regs.num_regs)
363
384
  return 0;
385
+ if (ms->regs.beg[i] < 0 || ms->regs.end[i] < 0)
386
+ return 0;
364
387
  *pos = ms->matched_pos + ms->regs.beg[i];
365
388
  *len = ms->regs.end[i] - ms->regs.beg[i];
366
389
  return 1;
@@ -373,7 +396,7 @@ static VALUE matched(int argc, VALUE *argv, VALUE obj)
373
396
  size_t pos, len;
374
397
  if (matched_sub(argc, argv, ms, &pos, &len) == 0)
375
398
  return Qnil;
376
- return rb_funcall(cMmapScanner, rb_intern("new"), 3, obj, SIZET2NUM(pos), SIZET2NUM(len));
399
+ return create_from_mmapscanner(obj, pos, len);
377
400
  }
378
401
 
379
402
  static VALUE matched_str(int argc, VALUE *argv, VALUE obj)
@@ -142,12 +142,13 @@ describe MmapScanner do
142
142
  end
143
143
  describe '#matched(nth)' do
144
144
  it 'returns nth part of matched string' do
145
- subject.scan(/(..)(..)(..)/)
145
+ subject.scan(/(..)((aa)|..)(..)/)
146
146
  subject.matched(0).to_s.should == '012345'
147
147
  subject.matched(1).to_s.should == '01'
148
148
  subject.matched(2).to_s.should == '23'
149
- subject.matched(3).to_s.should == '45'
150
- subject.matched(4).should be_nil
149
+ subject.matched(3).should be_nil
150
+ subject.matched(4).to_s.should == '45'
151
+ subject.matched(5).should be_nil
151
152
  subject.matched(-1).should be_nil
152
153
  end
153
154
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mmapscanner
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.2
5
+ version: 0.3.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - TOMITA Masahiro
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-23 00:00:00 Z
13
+ date: 2011-07-24 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: