16 #define STRSCAN_VERSION "0.7.0" 22 static VALUE StringScanner;
23 static VALUE ScanError;
24 static ID id_byteslice;
30 #define FLAG_MATCHED (1 << 0) 46 #define MATCHED_P(s) ((s)->flags & FLAG_MATCHED) 47 #define MATCHED(s) (s)->flags |= FLAG_MATCHED 48 #define CLEAR_MATCH_STATUS(s) (s)->flags &= ~FLAG_MATCHED 50 #define S_PBEG(s) (RSTRING_PTR((s)->str)) 51 #define S_LEN(s) (RSTRING_LEN((s)->str)) 52 #define S_PEND(s) (S_PBEG(s) + S_LEN(s)) 53 #define CURPTR(s) (S_PBEG(s) + (s)->curr) 54 #define S_RESTLEN(s) (S_LEN(s) - (s)->curr) 56 #define EOS_P(s) ((s)->curr >= RSTRING_LEN(p->str)) 58 #define GET_SCANNER(obj,var) do {\ 59 (var) = check_strscan(obj);\ 60 if (NIL_P((var)->str)) rb_raise(rb_eArgError, "uninitialized StringScanner object");\ 67 static inline long minl
_((
const long n,
const long x));
69 static VALUE extract_range
_((
struct strscanner *p,
long beg_i,
long end_i));
73 static void strscan_mark
_((
void *p));
74 static void strscan_free
_((
void *p));
75 static size_t strscan_memsize
_((
const void *p));
89 int succptr,
int getstr,
int headonly));
101 static void adjust_registers_to_matched
_((
struct strscanner *p));
145 minl(
const long x,
const long y)
147 return (x < y) ? x : y;
151 extract_range(
struct strscanner *p,
long beg_i,
long end_i)
154 end_i = minl(end_i,
S_LEN(p));
155 return infect(str_new(p,
S_PBEG(p) + beg_i, end_i - beg_i), p);
162 len = minl(len,
S_LEN(p) - beg_i);
163 return infect(str_new(p,
S_PBEG(p) + beg_i, len), p);
171 strscan_mark(
void *ptr)
178 strscan_free(
void *ptr)
186 strscan_memsize(
const void *ptr)
194 {strscan_mark, strscan_free, strscan_memsize},
199 strscan_s_allocate(
VALUE klass)
222 p = check_strscan(
self);
231 check_strscan(
VALUE obj)
248 self = check_strscan(vself);
249 orig = check_strscan(vorig);
251 self->flags = orig->
flags;
252 self->str = orig->
str;
253 self->prev = orig->
prev;
254 self->curr = orig->
curr;
273 strscan_s_mustc(
VALUE self)
282 strscan_reset(
VALUE self)
300 strscan_terminate(
VALUE self)
315 strscan_clear(
VALUE self)
317 rb_warning(
"StringScanner#clear is obsolete; use #terminate instead");
318 return strscan_terminate(
self);
325 strscan_get_string(
VALUE self)
391 strscan_get_pos(
VALUE self)
413 strscan_get_charpos(
VALUE self)
442 if (i < 0) i +=
S_LEN(p);
450 strscan_do_scan(
VALUE self,
VALUE regex,
int succptr,
int getstr,
int headonly)
469 if (!tmpreg)
RREGEXP(regex)->usecnt++;
482 if (!tmpreg)
RREGEXP(regex)->usecnt--;
493 if (ret == -2)
rb_raise(ScanError,
"regexp buffer overflow");
505 return extract_beg_len(p, p->
prev, p->
regs.
end[0]);
530 return strscan_do_scan(
self, re, 1, 1, 1);
547 return strscan_do_scan(
self, re, 0, 0, 1);
570 return strscan_do_scan(
self, re, 1, 0, 1);
591 return strscan_do_scan(
self, re, 0, 1, 1);
607 return strscan_do_scan(
self, re,
RTEST(s),
RTEST(f), 1);
625 return strscan_do_scan(
self, re, 1, 1, 0);
644 return strscan_do_scan(
self, re, 0, 0, 0);
666 return strscan_do_scan(
self, re, 1, 0, 0);
685 return strscan_do_scan(
self, re, 0, 1, 0);
700 return strscan_do_scan(
self, re,
RTEST(s),
RTEST(f), 0);
704 adjust_registers_to_matched(
struct strscanner *p)
725 strscan_getch(
VALUE self)
740 adjust_registers_to_matched(p);
762 strscan_get_byte(
VALUE self)
774 adjust_registers_to_matched(p);
784 strscan_getbyte(
VALUE self)
786 rb_warning(
"StringScanner#getbyte is obsolete; use #get_byte instead");
787 return strscan_get_byte(
self);
811 return infect(str_new(p,
"", 0), p);
814 return extract_beg_len(p, p->
curr, len);
824 rb_warning(
"StringScanner#peep is obsolete; use #peek instead");
825 return strscan_peek(
self, vlen);
840 strscan_unscan(
VALUE self)
846 rb_raise(ScanError,
"unscan failed: previous match record not exist");
865 strscan_bol_p(
VALUE self)
886 strscan_eos_p(
VALUE self)
899 strscan_empty_p(
VALUE self)
901 rb_warning(
"StringScanner#empty? is obsolete; use #eos? instead");
902 return strscan_eos_p(
self);
914 strscan_rest_p(
VALUE self)
932 strscan_matched_p(
VALUE self)
948 strscan_matched(
VALUE self)
969 strscan_matched_size(
VALUE self)
984 (
const unsigned char* )name, (
const unsigned char* )name_end, regs);
1032 switch (
TYPE(idx)) {
1047 if (i < 0)
return Qnil;
1065 strscan_pre_match(
VALUE self)
1071 return extract_range(p, 0, p->
prev + p->
regs.
beg[0]);
1084 strscan_post_match(
VALUE self)
1098 strscan_rest(
VALUE self)
1104 return infect(str_new(p,
"", 0), p);
1106 return extract_range(p, p->
curr,
S_LEN(p));
1113 strscan_rest_size(
VALUE self)
1131 strscan_restsize(
VALUE self)
1133 rb_warning(
"StringScanner#restsize is obsolete; use #rest_size instead");
1134 return strscan_rest_size(
self);
1137 #define INSPECT_LENGTH 5 1151 strscan_inspect(
VALUE self)
1156 p = check_strscan(
self);
1159 return infect(a, p);
1163 return infect(a, p);
1171 return infect(a, p);
1179 return infect(a, p);
VALUE rb_str_length(VALUE)
void rb_enc_copy(VALUE obj1, VALUE obj2)
#define RUBY_TYPED_FREE_IMMEDIATELY
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
ONIG_EXTERN void onig_region_clear(OnigRegion *region)
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_str_cat(VALUE, const char *, long)
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
#define CLEAR_MATCH_STATUS(s)
ONIG_EXTERN void onig_region_free(OnigRegion *region, int free_self)
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
int rb_const_defined(VALUE, ID)
ONIG_EXTERN void onig_free(OnigRegex)
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
void rb_gc_mark(VALUE ptr)
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
RUBY_EXTERN VALUE rb_cObject
VALUE rb_str_cat2(VALUE, const char *)
ONIG_EXTERN int onig_name_to_backref_number(OnigRegex reg, const OnigUChar *name, const OnigUChar *name_end, const OnigRegion *region)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
size_t onig_region_memsize(const OnigRegion *regs)
ONIG_EXTERN int onig_region_set(OnigRegion *region, int at, int beg, int end)
VALUE rb_obj_freeze(VALUE)
call-seq: obj.freeze -> obj
VALUE rb_sprintf(const char *format,...)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
void rb_const_set(VALUE, ID, VALUE)
ONIG_EXTERN OnigPosition onig_search(OnigRegex, const OnigUChar *str, const OnigUChar *end, const OnigUChar *start, const OnigUChar *range, OnigRegion *region, OnigOptionType option)
void rb_alias(VALUE, ID, ID)
VALUE rb_str_new_cstr(const char *)
#define GET_SCANNER(obj, var)
register unsigned int len
rb_encoding * rb_enc_get(VALUE obj)
void rb_warning(const char *fmt,...)
regex_t * rb_reg_prepare_re(VALUE re, VALUE str)
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
#define TypedData_Make_Struct(klass, type, data_type, sval)
int rb_reg_region_copy(struct re_registers *, const struct re_registers *)
ONIG_EXTERN OnigPosition onig_match(OnigRegex, const OnigUChar *str, const OnigUChar *end, const OnigUChar *at, OnigRegion *region, OnigOptionType option)
ONIG_EXTERN void onig_region_init(OnigRegion *region)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_str_append(VALUE, VALUE)
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
VALUE rb_str_new(const char *, long)