Class: ZScan

Inherits:
Object
  • Object
show all
Defined in:
lib/zscan/bspec.rb,
lib/zscan.rb,
ext/zscan.c

Overview

generated by rake gen

Defined Under Namespace

Classes: BSpec

Constant Summary collapse

VERSION =
'2.0.9'

Instance Method Summary collapse

Constructor Details

#initialize(s, dup = false) ⇒ ZScan

Returns a new instance of ZScan.



8
9
10
11
12
13
14
15
# File 'lib/zscan.rb', line 8

def initialize s, dup=false
  if s.encoding.ascii_compatible?
    s = dup ? s.dup : s
  else
    s = s.encode 'utf-8'
  end
  _internal_init s
end

Instance Method Details

#<<(substring) ⇒ Object



90
91
92
# File 'lib/zscan.rb', line 90

def << substring
  _internal_string << substring
end

#[]=(range, substring) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/zscan.rb', line 94

def []= range, substring
  start = range.start
  if start < 0
    start = _internal_string.size + start
  end
  if start < pos
    self.pos = start
  end
  _internal_string[range] = substring
end

#_internal_init(v_s) ⇒ Object



36
37
38
39
40
# File 'ext/zscan.c', line 36

static VALUE zscan_internal_init(VALUE self, VALUE v_s) {
  P;
  p->s = v_s;
  return self;
}

#_internal_stringObject



42
43
44
45
# File 'ext/zscan.c', line 42

static VALUE zscan_internal_string(VALUE self) {
  P;
  return p->s;
}

#_try(r) ⇒ Object

optimized version without pushing and block



318
319
320
321
322
323
324
325
# File 'ext/zscan.c', line 318

static VALUE zscan__try(VALUE self, VALUE r) {
  if (RTEST(r)) {
    zscan_drop(self);
  } else {
    zscan_pop(self);
  }
  return r;
}

#advance(v_diff) ⇒ Object



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
# File 'ext/zscan.c', line 52

static VALUE zscan_advance(VALUE self, VALUE v_diff) {
  P;
  long n = p->pos + NUM2LONG(v_diff);
  if (n < 0) {
    p->pos = 0;
    p->bytepos = 0;
    return self;
  }

  // because there's no "reverse scan" API, we have a O(n) routine :(
  if (n < p->pos) {
    p->pos = 0;
    p->bytepos = 0;
  }

  if (n > p->pos) {
    rb_encoding* enc = rb_enc_get(p->s);
    long byteend = RSTRING_LEN(p->s);
    char* ptr = RSTRING_PTR(p->s);
    for (; p->pos < n && p->bytepos < byteend;) {
      int m = rb_enc_mbclen(ptr + p->bytepos, ptr + byteend, enc);
      if (m) {
        p->pos++;
        p->bytepos += m;
      } else {
        break;
      }
    }
  }
  return self;
}

#byteposObject



84
85
86
87
# File 'ext/zscan.c', line 84

static VALUE zscan_bytepos(VALUE self) {
  P;
  return LONG2NUM(p->bytepos);
}

#bytepos=(v_bytepos) ⇒ Object



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
# File 'ext/zscan.c', line 89

VALUE zscan_bytepos_eq(VALUE self, VALUE v_bytepos) {
  P;
  long signed_bytepos = NUM2LONG(v_bytepos);
  long from, to, bytepos;

  if (signed_bytepos > RSTRING_LEN(p->s)) {
    bytepos = RSTRING_LEN(p->s);
  } else if (signed_bytepos < 0) {
    bytepos = 0;
  } else {
    bytepos = signed_bytepos;
  }

  if (bytepos > p->bytepos) {
    from = p->bytepos;
    to = bytepos;
  } else if (bytepos < p->bytepos) {
    from = bytepos;
    to = p->bytepos;
  } else {
    return v_bytepos;
  }

  rb_encoding* enc = rb_enc_get(p->s);
  char* ptr = RSTRING_PTR(p->s);
  long diff = 0;
  for (; from < to;) {
    int n = rb_enc_mbclen(ptr + from, ptr + to, enc);
    if (n) {
      diff++;
      from += n;
    } else {
      if (from < to) {
        rb_raise(rb_eRuntimeError, "the given bytepos splits character");
        return v_bytepos;
      }
      break;
    }
  }

  if (bytepos > p->bytepos) {
    p->pos += diff;
  } else if (bytepos < p->bytepos) {
    p->pos -= diff;
  }
  p->bytepos = bytepos;

  return v_bytepos;
}

#bytesizeObject



109
110
111
# File 'lib/zscan.rb', line 109

def bytesize
  _internal_string.bytesize
end

#byteslice(bytesize) ⇒ Object



123
124
125
126
127
# File 'lib/zscan.rb', line 123

def byteslice bytesize
  r = _internal_string.byteslice bytepos, bytesize
  self.bytepos += bytesize
  r
end

#cleanupObject



173
174
175
176
177
178
179
180
# File 'ext/zscan.c', line 173

static VALUE zscan_cleanup(VALUE self) {
  P;
  long rest_len = RSTRING_LEN(p->s) - p->bytepos;
  p->s = rb_funcall(p->s, rb_intern("byteslice"), 2, LONG2NUM(p->bytepos), LONG2NUM(rest_len));
  p->bytepos = 0;
  p->pos = 0;
  return self;
}

#clear_pos_stackObject



293
294
295
296
297
# File 'ext/zscan.c', line 293

static VALUE zscan_clear_pos_stack(VALUE self) {
  P;
  p->stack_i = 0;
  return self;
}

#dropObject



276
277
278
279
280
281
282
# File 'ext/zscan.c', line 276

static VALUE zscan_drop(VALUE self) {
  P;
  if (p->stack_i) {
    p->stack_i--;
  }
  return self;
}

#eos?Boolean

Returns:

  • (Boolean)


139
140
141
142
# File 'ext/zscan.c', line 139

static VALUE zscan_eos_p(VALUE self) {
  P;
  return (p->bytepos == RSTRING_LEN(p->s) ? Qtrue : Qfalse);
}

#line_indexObject



113
114
115
# File 'lib/zscan.rb', line 113

def line_index
  _internal_string.byteslice(0, bytepos).count "\n"
end

#match_bytesize(pattern) ⇒ Object



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
# File 'ext/zscan.c', line 183

static VALUE zscan_match_bytesize(VALUE self, VALUE pattern) {
  P;
  if (TYPE(pattern) == T_STRING) {
    volatile VALUE ss = rb_funcall(p->s, rb_intern("byteslice"), 2, LONG2NUM(p->bytepos), LONG2NUM(RSTRING_LEN(p->s) - p->bytepos));
    if (RTEST(rb_funcall(ss, rb_intern("start_with?"), 1, pattern))) {
      return LONG2NUM(RSTRING_LEN(pattern));
    }
  } else if (TYPE(pattern) == T_REGEXP) {
    regex_t *re = rb_reg_prepare_re(pattern, p->s); // prepare with compatible encoding
    int tmpreg = re != RREGEXP(pattern)->ptr;
    if (!tmpreg) {
      RREGEXP(pattern)->usecnt++;
    }

    char* ptr = RSTRING_PTR(p->s);
    UChar* ptr_end = (UChar*)(ptr + RSTRING_LEN(p->s));
    UChar* ptr_match_from = (UChar*)(ptr + p->bytepos);
    long ret = onig_match(re, (UChar*)ptr, ptr_end, ptr_match_from, NULL, ONIG_OPTION_NONE);

    if (tmpreg) {
      if (RREGEXP(pattern)->usecnt) {
        onig_free(re);
      } else {
        onig_free(RREGEXP(pattern)->ptr);
        RREGEXP(pattern)->ptr = re;
      }
    } else {
      RREGEXP(pattern)->usecnt--;
    }

    if (ret == -2) {
      rb_raise(rb_eRuntimeError, "regexp buffer overflow");
    } else if (ret >= 0) {
      return LONG2NUM(ret);
    }
  } else {
    rb_raise(rb_eTypeError, "expect String or Regexp");
  }

  return Qnil;
}

#one_or_more(*args) ⇒ Object



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
# File 'ext/zscan.c', line 371

static VALUE zscan_one_or_more(int argc, VALUE* argv, VALUE self) {
  REQUIRE_BLOCK;
  volatile VALUE a = Qnil;
  volatile VALUE r;

  r = rb_yield(Qnil);
  if (RTEST(r)) {
    long backpos;
    P;
    rb_scan_args(argc, argv, "01", (VALUE*)&a);
    if (a == Qnil) {
      a = rb_ary_new();
    }

    rb_funcall(a, rb_intern("<<"), 1, r);
    for (;;) {
      zscan_push(self);
      backpos = p->bytepos;
      r = rb_yield(Qnil);
      if (RTEST(r) && backpos != p->bytepos) {
        rb_funcall(a, rb_intern("<<"), 1, r);
        zscan_drop(self);
      } else {
        zscan_pop(self);
        break;
      }
    }
    return a;
  } else {
    return Qnil;
  }
}

#popObject



263
264
265
266
267
268
269
270
271
272
273
274
# File 'ext/zscan.c', line 263

static VALUE zscan_pop(VALUE self) {
  P;
  if (p->stack_i) {
    p->pos = p->stack[p->stack_i].pos;
    p->bytepos = p->stack[p->stack_i].bytepos;
    p->stack_i--;
  } else {
    p->pos = 0;
    p->bytepos = 0;
  }
  return self;
}

#posObject



47
48
49
50
# File 'ext/zscan.c', line 47

static VALUE zscan_pos(VALUE self) {
  P;
  return LONG2NUM(p->pos);
}

#pos=(new_pos) ⇒ Object



77
78
79
# File 'lib/zscan.rb', line 77

def pos= new_pos
  advance new_pos - pos
end

#pushObject



252
253
254
255
256
257
258
259
260
261
# File 'ext/zscan.c', line 252

static VALUE zscan_push(VALUE self) {
  P;
  if (p->stack_i + 1 == p->stack_cap) {
    p->stack_cap = p->stack_cap * 1.4 + 3;
    p->stack = (Pos*)realloc(p->stack, sizeof(Pos) * p->stack_cap);
  }
  Pos e = {p->pos, p->bytepos};
  p->stack[++p->stack_i] = e;
  return self;
}

#resetObject



81
82
83
84
# File 'lib/zscan.rb', line 81

def reset
  self.pos = 0
  self
end

#restObject



144
145
146
147
# File 'ext/zscan.c', line 144

static VALUE zscan_rest(VALUE self) {
  P;
  return rb_funcall(p->s, rb_intern("byteslice"), 2, LONG2NUM(p->bytepos), LONG2NUM(RSTRING_LEN(p->s)));
}

#rest_bytesizeObject



168
169
170
171
# File 'ext/zscan.c', line 168

static VALUE zscan_rest_bytesize(VALUE self) {
  P;
  return LONG2NUM(RSTRING_LEN(p->s) - p->bytepos);
}

#rest_sizeObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'ext/zscan.c', line 149

static VALUE zscan_rest_size(VALUE self) {
  P;
  rb_encoding* enc = rb_enc_get(p->s);
  char* ptr = RSTRING_PTR(p->s) + p->bytepos;
  long len = RSTRING_LEN(p->s) - p->bytepos;

  long sz = 0;
  for (long i = 0; i < len;) {
    long n = rb_enc_mbclen(ptr + i, ptr + len, enc);
    if (n) {
      sz++;
      i += n;
    } else {
      rb_raise(rb_eRuntimeError, "failed to scan char");
    }
  }
  return LONG2NUM(sz);
}

#restoreObject



284
285
286
287
288
289
290
291
# File 'ext/zscan.c', line 284

static VALUE zscan_restore(VALUE self) {
  P;
  if (p->stack_i) {
    p->pos = p->stack[p->stack_i].pos;
    p->bytepos = p->stack[p->stack_i].bytepos;
  }
  return self;
}

#scan(pattern) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'ext/zscan.c', line 225

static VALUE zscan_scan(VALUE self, VALUE pattern) {
  VALUE v_bytelen = zscan_match_bytesize(self, pattern);
  if (v_bytelen == Qnil) {
    return Qnil;
  } else {
    P;
    long bytelen = NUM2LONG(v_bytelen);
    volatile VALUE ret = rb_funcall(p->s, rb_intern("byteslice"), 2, LONG2NUM(p->bytepos), v_bytelen);
    VALUE v_len = rb_str_length(ret);
    p->bytepos += bytelen;
    p->pos += NUM2LONG(v_len);
    return ret;
  }
}

#scan_date(format, start = Date::ITALY) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zscan.rb', line 60

def scan_date format, start=Date::ITALY
  s = rest
  d = DateTime._strptime s, format
  if d
    # XXX need 2 parses because the handling is very complex ...
    dt = DateTime.strptime s, format, start rescue return nil

    len = s.bytesize
    if leftover = d[:leftover]
      len -= leftover.bytesize
    end
    self.bytepos += len

    dt
  end
end

#scan_floatObject



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'ext/zscan.c', line 404

VALUE zscan_scan_float(VALUE self) {
  P;
  if (RSTRING_LEN(p->s) == p->bytepos) {
    return Qnil;
  }

  char* s = RSTRING_PTR(p->s) + p->bytepos;
  if (isspace(s[0])) {
    return Qnil;
  }
  char* e;
  double d = strtod(s, &e);
  if (e == s || e - s > RSTRING_LEN(p->s) - p->bytepos) {
    return Qnil;
  } else {
    // it ok to use advance because the source is ascii compatible
    zscan_advance(self, LONG2NUM(e - s));
    return DBL2NUM(d);
  }
}

#scan_int(radix = nil) ⇒ Object



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
# File 'lib/zscan.rb', line 21

def scan_int radix=nil
  negative = false
  r = try do
    negative = (scan(/[+\-]/) == '-')
    if radix.nil?
      radix =
        if scan(/0b/i)
          2
        elsif scan(/0x/i)
          16
        elsif scan('0')
          8
        else
          10
        end
    end
    scan \
      case radix
      when 2;  /[01]+/
      when 8;  /[0-7]+/
      when 10; /[0-9]+/ # don't use \d because it matches unicode numbers
      when 16; /\h+/i
      else
        if radix < 10
          /[0-#{radix}]+/
        elsif radix > 36
          raise ArgumentError, "invalid radix #{radix}"
        else
          end_char = ('a'.ord + (radix - 11)).chr
          /[0-9a-#{end_char}]+/i
        end
      end
  end
  if r
    r = r.to_i radix
    negative ? -r : r
  end
end

#sizeObject



105
106
107
# File 'lib/zscan.rb', line 105

def size
  _internal_string.size
end

#skip(pattern) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
# File 'ext/zscan.c', line 240

static VALUE zscan_skip(VALUE self, VALUE pattern) {
  volatile VALUE v_bytelen = zscan_match_bytesize(self, pattern);
  if (v_bytelen == Qnil) {
    return Qnil;
  } else {
    P;
    long bytepos = p->bytepos + NUM2LONG(v_bytelen);
    zscan_bytepos_eq(self, LONG2NUM(bytepos));
    return LONG2NUM(bytepos);
  }
}

#slice(size) ⇒ Object



117
118
119
120
121
# File 'lib/zscan.rb', line 117

def slice size
  r = _internal_string.slice pos, size
  advance size
  r
end

#stringObject



17
18
19
# File 'lib/zscan.rb', line 17

def string
  _internal_string.dup
end

#terminateObject



86
87
88
# File 'lib/zscan.rb', line 86

def terminate
  self.pos = _internal_string.size
end

#tryObject



304
305
306
307
308
309
310
311
312
313
314
315
# File 'ext/zscan.c', line 304

static VALUE zscan_try(VALUE self) {
  REQUIRE_BLOCK;
  VALUE r;
  zscan_push(self);
  r = rb_yield(Qnil);
  if (RTEST(r)) {
    zscan_drop(self);
  } else {
    zscan_pop(self);
  }
  return r;
}

#unpack(fmt) ⇒ Object



425
426
427
428
429
430
431
432
# File 'ext/zscan.c', line 425

VALUE zscan_unpack(VALUE self, VALUE fmt) {
  P;
  long parsed_len = 0;
  volatile VALUE str = zscan_rest(self);
  VALUE r = zscan_internal_unpack(str, fmt, &parsed_len);
  zscan_bytepos_eq(self, LONG2NUM(p->bytepos + parsed_len));
  return r;
}

#zero_or_more(*args) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'ext/zscan.c', line 346

static VALUE zscan_zero_or_more(int argc, VALUE* argv, VALUE self) {
  REQUIRE_BLOCK;
  volatile VALUE a = Qnil;
  volatile VALUE r;
  long backpos;
  P;
  rb_scan_args(argc, argv, "01", (VALUE*)&a);
  if (a == Qnil) {
    a = rb_ary_new();
  }
  for (;;) {
    zscan_push(self);
    backpos = p->bytepos;
    r = rb_yield(Qnil);
    if (RTEST(r) && backpos != p->bytepos) {
      rb_funcall(a, rb_intern("<<"), 1, r);
      zscan_drop(self);
    } else {
      zscan_pop(self);
      break;
    }
  }
  return a;
}

#zero_or_one(*args) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'ext/zscan.c', line 327

static VALUE zscan_zero_or_one(int argc, VALUE* argv, VALUE self) {
  REQUIRE_BLOCK;
  volatile VALUE a = Qnil;
  volatile VALUE r;
  rb_scan_args(argc, argv, "01", (VALUE*)&a);
  if (a == Qnil) {
    a = rb_ary_new();
  }
  zscan_push(self);
  r = rb_yield(Qnil);
  if (RTEST(r)) {
    rb_funcall(a, rb_intern("<<"), 1, r);
    zscan_drop(self);
  } else {
    zscan_pop(self);
  }
  return a;
}