Method: StringScanner#peek

Defined in:
strscan.c

#peek(len) ⇒ Object

Extracts a string corresponding to string[pos,len], without advancing the scan pointer.

s = StringScanner.new('test string')
s.peek(7)          # => "test st"
s.peek(7)          # => "test st"


739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'strscan.c', line 739

static VALUE
strscan_peek(VALUE self, VALUE vlen)
{
    struct strscanner *p;
    long len;

    GET_SCANNER(self, p);
    len = NUM2LONG(vlen);
    if (EOS_P(p)) {
        return infect(rb_str_new("", 0), p);
    }
    if (p->curr + len > S_LEN(p)) {
        len = S_LEN(p) - p->curr;
    }
    return extract_beg_len(p, p->curr, len);
}