diff options
| author | mimaki <[email protected]> | 2012-04-23 11:51:47 +0900 |
|---|---|---|
| committer | mimaki <[email protected]> | 2012-04-23 11:51:47 +0900 |
| commit | c80487561f291e8239541687341b4d64a840c108 (patch) | |
| tree | d4f7777cc91597cf880bed364df26066b9aaee8f /src | |
| parent | 835443614d21b13b27af3674d7f8cb9bf49c298b (diff) | |
| parent | 3f0b98762d4a49beb7cc3f9cc8a8dfcee4aa5f6f (diff) | |
| download | mruby-c80487561f291e8239541687341b4d64a840c108.tar.gz mruby-c80487561f291e8239541687341b4d64a840c108.zip | |
Merge branch 'master' of github.com:mruby/mruby
Conflicts:
src/variable.c
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 3 | ||||
| -rw-r--r-- | src/array.c | 12 | ||||
| -rw-r--r-- | src/cdump.c | 2 | ||||
| -rw-r--r-- | src/compile.h | 12 | ||||
| -rw-r--r-- | src/encoding.c | 25 | ||||
| -rw-r--r-- | src/gc.c | 4 | ||||
| -rw-r--r-- | src/kernel.c | 138 | ||||
| -rw-r--r-- | src/numeric.c | 173 | ||||
| -rw-r--r-- | src/parse.y | 64 | ||||
| -rw-r--r-- | src/proc.c | 18 | ||||
| -rw-r--r-- | src/re.c | 49 | ||||
| -rw-r--r-- | src/regint.h | 4 | ||||
| -rw-r--r-- | src/st.c | 867 | ||||
| -rw-r--r-- | src/st.h | 72 | ||||
| -rw-r--r-- | src/string.c | 183 | ||||
| -rw-r--r-- | src/transcode.c | 8 | ||||
| -rw-r--r-- | src/variable.c | 1 | ||||
| -rw-r--r-- | src/variable.h | 1 |
18 files changed, 282 insertions, 1354 deletions
diff --git a/src/Makefile b/src/Makefile index ea3681dce..fb5483a72 100644 --- a/src/Makefile +++ b/src/Makefile @@ -40,7 +40,7 @@ YACC = bison DEBUG_MODE = 1 ifeq ($(DEBUG_MODE),1) -CFLAGS = -g +CFLAGS = -g -O3 else CFLAGS = -O3 endif @@ -86,5 +86,6 @@ clean : $(MAKE) clean -C ../mrblib $(MAKE_FLAGS) -rm -f $(LIB) $(OBJS) $(OBJY) $(YC) -rm -f $(OBJS:.o=.d) $(OBJY:.o=.d) + -rm -f $(patsubst %.c,%.o,$(EXCEPT1)) $(patsubst %.c,%.d,$(EXCEPT1)) @echo "make: removing targets, objects and depend files of `pwd`" diff --git a/src/array.c b/src/array.c index 035feead5..1b92be560 100644 --- a/src/array.c +++ b/src/array.c @@ -40,10 +40,8 @@ mrb_value mrb_ary_new_capa(mrb_state *mrb, size_t capa) { struct RArray *a; + size_t blen; - if (capa < 0) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "negative ary size (or size too big)"); - } #ifdef LONG_MAX if (capa > ARY_MAX_SIZE) { mrb_raise(mrb, E_ARGUMENT_ERROR, "ary size too big"); @@ -52,10 +50,14 @@ mrb_ary_new_capa(mrb_state *mrb, size_t capa) if (capa < ARY_DEFAULT_LEN) { capa = ARY_DEFAULT_LEN; } + blen = capa * sizeof(mrb_value) ; + if (blen < capa) { + mrb_raise(mrb, E_ARGUMENT_ERROR, "ary size too big"); + } a = mrb_obj_alloc(mrb, MRB_TT_ARRAY, mrb->array_class); - a->buf = mrb_malloc(mrb, sizeof(mrb_value) * capa); - memset(a->buf, 0, sizeof(mrb_value) * capa); + a->buf = mrb_malloc(mrb, blen); + memset(a->buf, 0, blen); a->capa = capa; a->len = 0; diff --git a/src/cdump.c b/src/cdump.c index 400f446ef..d48139664 100644 --- a/src/cdump.c +++ b/src/cdump.c @@ -146,7 +146,7 @@ make_cdump_irep(mrb_state *mrb, int irep_no, FILE *f) } } memset(buf, 0, buf_len); - SOURCE_CODE(" irep->pool[%d] = mrb_str_new(mrb, \"%s\", %d);", n, str_to_format(irep->pool[n], buf), RSTRING_LEN(irep->pool[n])); break; + SOURCE_CODE(" irep->pool[%d] = mrb_str_new(mrb, \"%s\", %zd);", n, str_to_format(irep->pool[n], buf), RSTRING_LEN(irep->pool[n])); break; /* TODO MRB_TT_REGEX */ default: break; } diff --git a/src/compile.h b/src/compile.h index 49c8e9cf8..6e6975cbf 100644 --- a/src/compile.h +++ b/src/compile.h @@ -31,6 +31,12 @@ enum mrb_lex_state_enum { EXPR_MAX_STATE }; +struct mrb_parser_message { + int lineno; + int column; + char* message; +}; + struct mrb_parser_state { mrb_state *mrb; struct mrb_pool *pool; @@ -61,14 +67,20 @@ struct mrb_parser_state { void *ylval; int nerr; + int nwarn; mrb_ast_node *tree, *begin_tree; + int capture_errors; + struct mrb_parser_message error_buffer[10]; + struct mrb_parser_message warn_buffer[10]; + jmp_buf jmp; }; struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*); struct mrb_parser_state* mrb_parse_string(mrb_state*,char*); struct mrb_parser_state* mrb_parse_nstring(mrb_state*,char*,size_t); +struct mrb_parser_state* mrb_parse_nstring_ext(mrb_state*,char*,size_t); int mrb_generate_code(mrb_state*, mrb_ast_node*); int mrb_compile_file(mrb_state*,FILE*); diff --git a/src/encoding.c b/src/encoding.c index 2df294e20..ed30a222a 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -917,6 +917,31 @@ enc_name(mrb_state *mrb, mrb_value self) return mrb_usascii_str_new2(mrb, mrb_enc_name((mrb_encoding*)DATA_PTR(self))); } +struct fn_arg { + mrb_state *mrb; + int (*func)(ANYARGS); + void *a; +}; + +static int +fn_i(st_data_t key, st_data_t val, st_data_t arg) { + struct fn_arg *a = (struct fn_arg*)arg; + + return (*a->func)(a->mrb, key, val, a->a); +} + +static int +st_foreachNew(mrb_state *mrb, st_table *tbl, int (*func)(ANYARGS), void *a) +{ + struct fn_arg arg = { + mrb, + func, + a, + }; + + return st_foreach(tbl, fn_i, (st_data_t)&arg); +} + static int enc_names_i(mrb_state *mrb, st_data_t name, st_data_t idx, st_data_t args) { @@ -407,8 +407,8 @@ obj_free(mrb_state *mrb, struct RBasic *obj) struct REnv *e = (struct REnv *)obj; if (e->cioff < 0) { - mrb_free(mrb, mrb->stack); - mrb->stack = 0; + mrb_free(mrb, e->stack); + e->stack = 0; } } break; diff --git a/src/kernel.c b/src/kernel.c index dbc17940e..40db88b12 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -37,85 +37,12 @@ KHASH_MAP_INIT_INT(iv, mrb_value); #define TRUE 1 #endif -static mrb_value tst_setconst(mrb_state *mrb, mrb_value obj); -int kiv_lookup(khash_t(iv) *table, mrb_sym key, mrb_value *value); - struct obj_ivar_tag { mrb_value obj; int (*func)(mrb_sym key, mrb_value val, void * arg); void * arg; }; -static int -obj_ivar_i(mrb_sym key, int index, struct obj_ivar_tag *arg) -{ - enum st_retval {ST_CONTINUE, ST_STOP, ST_DELETE, ST_CHECK}; - struct obj_ivar_tag *data = (struct obj_ivar_tag *)arg; - if ((long)index < ROBJECT_NUMIV(data->obj)) { - mrb_value val = ROBJECT_IVPTR(data->obj)->vals[(long)index]; - if (val.tt != MRB_TT_FREE) { - return (data->func)((mrb_sym)key, val, data->arg); - } - } - return ST_CONTINUE; -} - -void -mrb_ivar_foreach(mrb_state *mrb, mrb_value obj, int (*func)(ANYARGS), void* arg) -{ - struct obj_ivar_tag data; - switch (mrb_type(obj)) { - case MRB_TT_OBJECT: - //obj_ivar_each(mrb, obj, func, arg); - if (RCLASS_IV_TBL(obj)) { - data.obj = obj; - data.func = (int (*)(mrb_sym key, mrb_value val, void * arg))func; - data.arg = arg; - st_foreach_safe(mrb, RCLASS_IV_TBL(obj), obj_ivar_i, (void *)&data); - } - break; - case MRB_TT_CLASS: - case MRB_TT_MODULE: - if (RCLASS_IV_TBL(obj)) { - st_foreach_safe(mrb, RCLASS_IV_TBL(obj), func, arg); - } - break; - default: - if (!ROBJECT_IVPTR(obj)/*generic_iv_tbl*/) break; - if (/*FL_TEST(obj, FL_EXIVAR) ||*/ mrb_special_const_p(obj)) { - mrb_value *tbl=0; - if (kiv_lookup(ROBJECT_IVPTR(obj)/*generic_iv_tbl*/, SYM2ID(obj), tbl)) { - st_foreach_safe(mrb, (void *)tbl, func, arg); - } - } - break; - } -} - -static int -inspect_i(mrb_state *mrb, mrb_sym id, mrb_value value, mrb_value str) -{ - enum st_retval {ST_CONTINUE, ST_STOP, ST_DELETE, ST_CHECK}; - mrb_value str2; - const char *ivname; - /* need not to show internal data */ - if (RSTRING_PTR(str)[0] == '-') { /* first element */ - RSTRING_PTR(str)[0] = '#'; - mrb_str_cat2(mrb, str, " "); - } - else { - mrb_str_cat2(mrb, str, ", "); - } - ivname = mrb_sym2name(mrb, id); - mrb_str_cat2(mrb, str, ivname); - mrb_str_cat2(mrb, str, "="); - str2 = mrb_inspect(mrb, value); - mrb_str_append(mrb, str, str2); - //OBJ_INFECT(str, str2); - - return ST_CONTINUE; -} - static mrb_value inspect_obj(mrb_state *mrb, mrb_value obj, mrb_value str, int recur) { @@ -123,11 +50,32 @@ inspect_obj(mrb_state *mrb, mrb_value obj, mrb_value str, int recur) mrb_str_cat2(mrb, str, " ..."); } else { - mrb_ivar_foreach(mrb, obj, inspect_i, &str); + khiter_t k; + kh_iv_t *h = RCLASS_IV_TBL(obj); + + if (h) { + for (k = kh_begin(h); k != kh_end(h); k++) { + if (kh_exist(h, k)){ + mrb_sym id = kh_key(h, k); + mrb_value value = kh_value(h, k); + + /* need not to show internal data */ + if (RSTRING_PTR(str)[0] == '-') { /* first element */ + RSTRING_PTR(str)[0] = '#'; + mrb_str_cat2(mrb, str, " "); + } + else { + mrb_str_cat2(mrb, str, ", "); + } + mrb_str_cat2(mrb, str, mrb_sym2name(mrb, id)); + mrb_str_cat2(mrb, str, "="); + mrb_str_append(mrb, str, mrb_inspect(mrb, value)); + } + } + } } mrb_str_cat2(mrb, str, ">"); RSTRING_PTR(str)[0] = '#'; - //OBJ_INFECT(str, obj); return str; } @@ -162,19 +110,9 @@ mrb_value mrb_obj_inspect(mrb_state *mrb, mrb_value obj) { if ((mrb_type(obj) == MRB_TT_OBJECT) && mrb_obj_basic_to_s_p(mrb, obj)) { - int has_ivar = 0; - mrb_value *ptr = (mrb_value *)ROBJECT_IVPTR(obj); long len = ROBJECT_NUMIV(obj); - long i; - - for (i = 0; i < len; i++) { - if (ptr[i].tt != MRB_TT_FREE) { - has_ivar = 1; - break; - } - } - if (has_ivar) { + if (len > 0) { mrb_value str; const char *c = mrb_obj_classname(mrb, obj); @@ -429,7 +367,6 @@ mrb_singleton_class_clone(mrb_state *mrb, mrb_value obj) clone->super = klass->super; if (klass->iv) { - //clone->iv = st_copy(klass->iv); clone->iv = klass->iv; } clone->mt = kh_init(mt, mrb); @@ -455,11 +392,9 @@ init_copy(mrb_state *mrb, mrb_value dest, mrb_value obj) case MRB_TT_CLASS: case MRB_TT_MODULE: if (ROBJECT(dest)->iv) { - //st_free_table(ROBJECT(dest)->iv); ROBJECT(dest)->iv = 0; } if (ROBJECT(obj)->iv) { - //ROBJECT(dest)->iv = st_copy((st_table *)ROBJECT(obj)->iv); ROBJECT(dest)->iv = ROBJECT(obj)->iv; } } @@ -1000,7 +935,6 @@ class_instance_method_list(mrb_state *mrb, int argc, mrb_value *argv, struct RCl { mrb_value ary; int recur; - //st_table *list; struct RClass* oldklass; if (argc == 0) { @@ -1012,25 +946,19 @@ class_instance_method_list(mrb_state *mrb, int argc, mrb_value *argv, struct RCl recur = mrb_test(r); } - //list = st_init_numtable(); ary = mrb_ary_new(mrb); - //for (; mod; mod = RCLASS_SUPER(mod)) { oldklass = 0; while (klass && (klass != oldklass)) { - //st_foreach(RCLASS_M_TBL(mod), method_entry, (st_data_t)list); method_entry_loop(mrb, klass, ary); if ((klass->tt == MRB_TT_ICLASS) || (klass->tt == MRB_TT_SCLASS)) { } - else - { + else { if (!recur) break; } oldklass = klass; klass = klass->super; } - //st_foreach(list, func, ary); - //st_free_table(list); return ary; } @@ -1039,7 +967,6 @@ mrb_value mrb_obj_singleton_methods(mrb_state *mrb, int argc, mrb_value *argv, mrb_value obj) { mrb_value recur, ary; - //st_table *list; struct RClass* klass; if (argc == 0) { @@ -1050,22 +977,17 @@ mrb_obj_singleton_methods(mrb_state *mrb, int argc, mrb_value *argv, mrb_value o recur = argv[0]; } klass = mrb_class(mrb, obj); - //list = st_init_numtable(); ary = mrb_ary_new(mrb); if (klass && (klass->tt == MRB_TT_SCLASS)) { - //st_foreach(RCLASS_M_TBL(klass), method_entry, (st_data_t)list); method_entry_loop(mrb, klass, ary); klass = klass->super; } if (RTEST(recur)) { while (klass && ((klass->tt == MRB_TT_SCLASS) || (klass->tt == MRB_TT_ICLASS))) { - //st_foreach(RCLASS_M_TBL(klass), method_entry, (st_data_t)list); method_entry_loop(mrb, klass, ary); klass = klass->super; } } - //st_foreach(list, ins_methods_i, ary); - //st_free_table(list); return ary; } @@ -1332,14 +1254,8 @@ mrb_obj_remove_instance_variable(mrb_state *mrb, mrb_value self) } } break; - //default: - // if (mrb_special_const_p(obj)) { - // v = val; - // if (generic_ivar_remove(obj, (st_data_t)id, &v)) { - // return (VALUE)v; - // } - // } - // break; + default: + break; } mrb_name_error(mrb, sym, "instance variable %s not defined", mrb_sym2name(mrb, sym)); return mrb_nil_value(); /* not reached */ diff --git a/src/numeric.c b/src/numeric.c index d30e683fd..2462e63df 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -83,34 +83,9 @@ #define mrb_rational_raw1(x) mrb_rational_raw(x, INT2FIX(1)) -#if SIZEOF_LONG_LONG > 0 -# define LONG_LONG long long -#elif SIZEOF___INT64 > 0 -# define HAVE_LONG_LONG 1 -# define LONG_LONG __int64 -# undef SIZEOF_LONG_LONG -# define SIZEOF_LONG_LONG SIZEOF___INT64 -#endif - -#if defined HAVE_UINTPTR_T && 0 typedef uintptr_t VALUE; typedef uintptr_t ID; -# define SIGNED_VALUE intptr_t -# define SIZEOF_VALUE SIZEOF_UINTPTR_T -#elif SIZEOF_LONG == SIZEOF_VOIDP -//typedef unsigned long VALUE; -//typedef unsigned long ID; -# define SIGNED_VALUE long long -# define SIZEOF_VALUE SIZEOF_LONG -#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP -typedef unsigned LONG_LONG VALUE; -typedef unsigned LONG_LONG ID; -# define SIGNED_VALUE LONG_LONG -# define LONG_LONG_VALUE 1 -# define SIZEOF_VALUE SIZEOF_LONG_LONG -#else -# error ---->> ruby requires sizeof(void*) == sizeof(long) to be compiled. <<---- -#endif +#define SIGNED_VALUE intptr_t #ifdef HAVE_INFINITY #elif BYTE_ORDER == LITTLE_ENDIAN @@ -975,80 +950,6 @@ mrb_num2ulong(mrb_state *mrb, mrb_value val) } } -#if SIZEOF_INT < SIZEOF_VALUE -void -mrb_out_of_int(mrb_state *mrb, SIGNED_VALUE num) -{ - mrb_raise(mrb, E_RANGE_ERROR, "integer %"PRIdVALUE " too %s to convert to `int'", - num, num < 0 ? "small" : "big"); -} - -static void -check_int(SIGNED_VALUE num) -{ - if ((SIGNED_VALUE)(int)num != num) { - mrb_out_of_int(num); - } -} - -static void -check_uint(mrb_state *mrb, mrb_value num, mrb_value sign) -{ - static const mrb_value mask = ~(mrb_value)UINT_MAX; - - if (RTEST(sign)) { - /* minus */ - if ((num & mask) != mask || (num & ~mask) <= INT_MAX + 1UL) - mrb_raise(mrb, E_RANGE_ERROR, "integer %"PRIdVALUE " too small to convert to `unsigned int'", num); - } - else { - /* plus */ - if ((num & mask) != 0) - mrb_raise(mrb, E_RANGE_ERROR, "integer %"PRIuVALUE " too big to convert to `unsigned int'", num); - } -} - -long -mrb_num2int(mrb_value val) -{ - long num = mrb_num2long(mrb, val); - - check_int(num); - return num; -} - -long -mrb_fix2int(mrb_state *mrb, mrb_value val) -{ - long num = FIXNUM_P(val)?mrb_fixnum(val):mrb_num2long(mrb, val); - - check_int(num); - return num; -} - -unsigned long -mrb_num2uint(mrb_value val) -{ - unsigned long num = mrb_num2ulong(val); - - check_uint(num, mrb_funcall(mrb, val, "<", 1, mrb_fixnum_value(0))); - return num; -} - -unsigned long -mrb_fix2uint(mrb_state *mrb, mrb_value val) -{ - unsigned long num; - - if (!FIXNUM_P(val)) { - return mrb_num2uint(mrb, val); - } - num = FIX2ULONG(val); - - check_uint(num, mrb_funcall(mrb, val, "<", 1, mrb_fixnum_value(0))); - return num; -} -#else long mrb_num2int(mrb_state *mrb, mrb_value val) { @@ -1060,7 +961,6 @@ mrb_fix2int(mrb_value val) { return mrb_fixnum(val); } -#endif mrb_value mrb_num2fix(mrb_state *mrb, mrb_value val) @@ -1071,59 +971,10 @@ mrb_num2fix(mrb_state *mrb, mrb_value val) v = mrb_num2long(mrb, val); if (!FIXABLE(v)) - mrb_raise(mrb, E_RANGE_ERROR, "integer %"PRIdVALUE " out of range of fixnum", v); + mrb_raise(mrb, E_RANGE_ERROR, "integer %ld out of range of fixnum", v); return mrb_fixnum_value(v); } -#if HAVE_LONG_LONG - -LONG_LONG -mrb_num2ll(mrb_state *mrb, mrb_value val) -{ - if (mrb_nil_p(val)) { - mrb_raise(mrb, E_TYPE_ERROR, "no implicit conversion from nil"); - } - - if (FIXNUM_P(val)) return (LONG_LONG)mrb_fixnum(val); - - switch (mrb_type(val)) { - case MRB_TT_FLOAT: - if (mrb_float(val) <= (double)LLONG_MAX - && mrb_float(val) >= (double)LLONG_MIN) { - return (LONG_LONG)(mrb_float(val)); - } - else { - char buf[24]; - char *s; - - snprintf(buf, sizeof(buf), "%-.10g", mrb_float(val)); - if ((s = strchr(buf, ' ')) != 0) *s = '\0'; - mrb_raise(mrb, E_RANGE_ERROR, "float %s out of range of long long", buf); - } - - case MRB_TT_STRING: - mrb_raise(mrb, E_TYPE_ERROR, "no implicit conversion from string"); - return mrb_nil_value(); /* not reached */ - - case MRB_TT_TRUE: - case MRB_TT_FALSE: - mrb_raise(mrb, E_TYPE_ERROR, "no implicit conversion from boolean"); - return mrb_nil_value(); /* not reached */ - - default: - val = mrb_to_int(mrb, val); - return NUM2LL(val); - } -} - -unsigned LONG_LONG -mrb_num2ull(mrb_state *mrb, mrb_value val) -{ - return (unsigned LONG_LONG)mrb_num2ll(mrb, val); -} - -#endif /* HAVE_LONG_LONG */ - /* * Document-class: Integer * @@ -1199,7 +1050,7 @@ mrb_value rb_fix2str(mrb_state *mrb, mrb_value x, int base) { extern const char ruby_digitmap[]; - char buf[SIZEOF_VALUE*CHAR_BIT + 2], *b = buf + sizeof buf; + char buf[sizeof(mrb_int)*CHAR_BIT + 2], *b = buf + sizeof buf; long val = mrb_fixnum(x); int neg = 0; @@ -1224,7 +1075,7 @@ rb_fix2str(mrb_state *mrb, mrb_value x, int base) return mrb_usascii_str_new2(mrb, b); } -#define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2)) +#define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((sizeof(intptr_t)*CHAR_BIT-1)/2)) /*tests if N*N would overflow*/ #define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX)) @@ -1250,21 +1101,12 @@ fix_mul(mrb_state *mrb, mrb_value x) volatile #endif long a, b; -#if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG - LONG_LONG d; -#else long c; mrb_value r; -#endif a = mrb_fixnum(x); b = mrb_fixnum(y); -#if SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG - d = (LONG_LONG)a * b; - if (FIXABLE(d)) return mrb_fixnum_value(d); - return mrb_nil_value();// rb_ll2inum(d); -#else if (FIT_SQRT_LONG(a) && FIT_SQRT_LONG(b)) return mrb_fixnum_value(a*b); c = a * b; @@ -1276,7 +1118,6 @@ fix_mul(mrb_state *mrb, mrb_value x) r = mrb_fixnum_value(a*b); } return r; -#endif } switch (mrb_type(y)) { case MRB_TT_FLOAT: @@ -1610,9 +1451,9 @@ mrb_fix_lshift(mrb_state *mrb, mrb_value x) static mrb_value fix_lshift(mrb_state *mrb, long val, unsigned long width) { - if (width > (SIZEOF_LONG*CHAR_BIT-1) - || ((unsigned long)abs(val))>>(SIZEOF_LONG*CHAR_BIT-1-width) > 0) { - mrb_raise(mrb, E_RANGE_ERROR, "width(%d) > (SIZEOF_LONG*CHAR_BIT-1)", width); + if (width > (sizeof(intptr_t)*CHAR_BIT-1) + || ((unsigned long)abs(val))>>(sizeof(intptr_t)*CHAR_BIT-1-width) > 0) { + mrb_raise(mrb, E_RANGE_ERROR, "width(%d) > (sizeof(intptr_t)*CHAR_BIT-1)", width); } val = val << width; return mrb_fixnum_value(val); diff --git a/src/parse.y b/src/parse.y index ef49338ac..2c02a31cb 100644 --- a/src/parse.y +++ b/src/parse.y @@ -281,7 +281,7 @@ new_true(parser_state *p) return list1((node*)NODE_TRUE); } -// (:true) +// (:false) static node* new_false(parser_state *p) { @@ -2904,8 +2904,21 @@ none : /* none */ static void yyerror(parser_state *p, const char *s) { - fputs(s, stderr); - fputs("\n", stderr); + char* c; + size_t n; + + if (! p->capture_errors) { + fputs(s, stderr); + fputs("\n", stderr); + } + else if (p->nerr < sizeof(p->error_buffer) / sizeof(p->error_buffer[0])) { + n = strlen(s); + c = parser_palloc(p, n + 1); + memcpy(c, s, n + 1); + p->error_buffer[p->nerr].message = c; + p->error_buffer[p->nerr].lineno = p->lineno; + p->error_buffer[p->nerr].column = p->column; + } p->nerr++; } @@ -2921,8 +2934,22 @@ yyerror_i(parser_state *p, const char *fmt, int i) static void yywarn(parser_state *p, const char *s) { - fputs(s, stderr); - fputs("\n", stderr); + char* c; + size_t n; + + if (! p->capture_errors) { + fputs(s, stderr); + fputs("\n", stderr); + } + else if (p->nerr < sizeof(p->warn_buffer) / sizeof(p->warn_buffer[0])) { + n = strlen(s); + c = parser_palloc(p, n + 1); + memcpy(c, s, n + 1); + p->error_buffer[p->nwarn].message = c; + p->error_buffer[p->nwarn].lineno = p->lineno; + p->error_buffer[p->nwarn].column = p->column; + } + p->nwarn++; } static void @@ -2982,8 +3009,13 @@ nextc(parser_state *p) c = *p->s++; } if (c == '\n') { - p->lineno++; - p->column = 0; + if (p->column < 0) { + p->column++; // pushback caused an underflow + } + else { + p->lineno++; + p->column = 0; + } // must understand heredoc } else { @@ -4610,6 +4642,8 @@ parser_new(mrb_state *mrb) p->cmd_start = TRUE; p->in_def = p->in_single = FALSE; + p->capture_errors = NULL; + p->lineno = 1; #if defined(PARSER_TEST) || defined(PARSER_DEBUG) yydebug = 1; @@ -4648,6 +4682,22 @@ mrb_parse_nstring(mrb_state *mrb, char *s, size_t len) } parser_state* +mrb_parse_nstring_ext(mrb_state *mrb, char *s, size_t len) +{ + parser_state *p; + + p = parser_new(mrb); + if (!p) return 0; + p->s = s; + p->send = s + len; + p->f = NULL; + p->capture_errors = 1; + + start_parser(p); + return p; +} + +parser_state* mrb_parse_string(mrb_state *mrb, char *s) { return mrb_parse_nstring(mrb, s, strlen(s)); diff --git a/src/proc.c b/src/proc.c index 94a8d8465..d4d57c6aa 100644 --- a/src/proc.c +++ b/src/proc.c @@ -56,6 +56,22 @@ mrb_proc_new_cfunc(mrb_state *mrb, mrb_func_t func) return p; } +static mrb_value +mrb_proc_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value blk = mrb->stack[mrb->ci->argc+1]; + + if (!mrb_nil_p(blk)) { + *mrb_proc_ptr(self) = *mrb_proc_ptr(blk); + } + else { + /* Calling Proc.new without a block is not implemented yet */ + mrb_raise(mrb, E_ARGUMENT_ERROR, "tried to create Proc object without a block"); + } + + return self; +} + int mrb_proc_cfunc_p(struct RProc *p) { @@ -92,6 +108,8 @@ mrb_init_proc(mrb_state *mrb) mrb->proc_class = mrb_define_class(mrb, "Proc", mrb->object_class); + mrb_define_method(mrb, mrb->proc_class, "initialize", mrb_proc_initialize, ARGS_NONE()); + m = mrb_proc_new(mrb, call_irep); mrb_define_method_raw(mrb, mrb->proc_class, mrb_intern(mrb, "call"), m); mrb_define_method_raw(mrb, mrb->proc_class, mrb_intern(mrb, "[]"), m); @@ -1778,11 +1778,7 @@ static int pair_byte_cmp(const void *pair1, const void *pair2) { long diff = ((pair_t*)pair1)->byte_pos - ((pair_t*)pair2)->byte_pos; -#if SIZEOF_LONG > SIZEOF_INT return diff ? diff > 0 ? 1 : -1 : 0; -#else - return (int)diff; -#endif } static void @@ -2964,48 +2960,6 @@ mrb_backref_set(mrb_state *mrb, mrb_value val) #ifdef INCLUDE_ENCODING static inline long -mrb_memsearch_ss(const unsigned char *xs, long m, const unsigned char *ys, long n) -{ - const unsigned char *x = xs, *xe = xs + m; - const unsigned char *y = ys, *ye = ys + n; -#define SIZEOF_VOIDP 4 -#define SIZEOF_LONG 4 - -#ifndef VALUE_MAX -# if SIZEOF_VALUE == 8 -# define VALUE_MAX 0xFFFFFFFFFFFFFFFFULL -# elif SIZEOF_VALUE == 4 -# define VALUE_MAX 0xFFFFFFFFUL -# elif SIZEOF_LONG == SIZEOF_VOIDP -# define SIZEOF_VALUE 4 -# define VALUE_MAX 0xFFFFFFFFUL -# endif -#endif - int hx, hy, mask = VALUE_MAX >> ((SIZEOF_VALUE - m) * CHAR_BIT); - - if (m > SIZEOF_VALUE) - mrb_bug("!!too long pattern string!!"); - - /* Prepare hash value */ - for (hx = *x++, hy = *y++; x < xe; ++x, ++y) { - hx <<= CHAR_BIT; - hy <<= CHAR_BIT; - hx |= *x; - hy |= *y; - } - /* Searching */ - while (hx != hy) { - if (y == ye) - return -1; - hy <<= CHAR_BIT; - hy |= *y; - hy &= mask; - y++; - } - return y - ys - m; -} - -static inline long mrb_memsearch_qs(const unsigned char *xs, long m, const unsigned char *ys, long n) { const unsigned char *x = xs, *xe = xs + m; @@ -3099,9 +3053,6 @@ mrb_memsearch(mrb_state *mrb, const void *x0, int m, const void *y0, int n, mrb_ } return -1; } - else if (m <= SIZEOF_VALUE) { - return mrb_memsearch_ss(x0, m, y0, n); - } else if (enc == mrb_utf8_encoding(mrb)) { return mrb_memsearch_qs_utf8(x0, m, y0, n); } diff --git a/src/regint.h b/src/regint.h index e86a95f27..bf19eee1a 100644 --- a/src/regint.h +++ b/src/regint.h @@ -246,11 +246,11 @@ } while(0) /* sizeof(OnigCodePoint) */ -#define WORD_ALIGNMENT_SIZE SIZEOF_LONG +#define WORD_ALIGNMENT_SIZE sizeof(uintptr_t) #define GET_ALIGNMENT_PAD_SIZE(addr,pad_size) do {\ (pad_size) = WORD_ALIGNMENT_SIZE \ - - ((uintptr_t )(addr) % WORD_ALIGNMENT_SIZE);\ + - ((uintptr_t)(addr) % WORD_ALIGNMENT_SIZE);\ if ((pad_size) == WORD_ALIGNMENT_SIZE) (pad_size) = 0;\ } while (0) @@ -1,19 +1,13 @@ /* This is a public domain general purpose hash table package written by Peter Moore @ UCB. */ /* static char sccsid[] = "@(#) st.c 5.1 89/12/14 Crucible"; */ -#define NOT_RUBY -#ifdef NOT_RUBY -#include "regint.h" -#include "st.h" -#else -#include "ruby/ruby.h" -#endif #include <stdio.h> #ifdef HAVE_STDLIB_H #include <stdlib.h> #endif #include <string.h> +#include "st.h" #define ST_DEFAULT_MAX_DENSITY 5 #define ST_DEFAULT_INIT_TABLE_SIZE 11 @@ -27,15 +21,16 @@ * allocated initially * */ - -static const struct st_hash_type type_numhash = { - st_numcmp, - st_numhash, +static int numcmp(long, long); +static st_index_t numhash(long); +static struct st_hash_type type_numhash = { + numcmp, + numhash, }; /* extern int strcmp(const char *, const char *); */ -static st_index_t strhash(st_data_t); -static const struct st_hash_type type_strhash = { +static st_index_t strhash(const char *); +static struct st_hash_type type_strhash = { strcmp, strhash, }; @@ -51,18 +46,14 @@ static void rehash(st_table *); #ifdef RUBY #define malloc xmalloc #define calloc xcalloc -#define free(x) xfree(x) #endif -#define numberof(array) (int)(sizeof(array) / sizeof((array)[0])) - -#define alloc(type) (type*)malloc((size_t)sizeof(type)) +#define alloc(type) (type*)malloc((unsigned)sizeof(type)) #define Calloc(n,s) (char*)calloc((n),(s)) #define EQUAL(table,x,y) ((x)==(y) || (*table->type->compare)((x),(y)) == 0) -/* remove cast to unsigned int in the future */ -#define do_hash(key,table) (unsigned int)(st_index_t)(*(table)->type->hash)((key)) +#define do_hash(key,table) (unsigned int)(*(table)->type->hash)((key)) #define do_hash_bin(key,table) (do_hash(key, table)%(table)->num_bins) /* @@ -74,7 +65,7 @@ static void rehash(st_table *); /* Table of prime numbers 2^n+a, 2<=n<=30. */ -static const unsigned int primes[] = { +static long primes[] = { 8 + 3, 16 + 3, 32 + 5, @@ -106,27 +97,32 @@ static const unsigned int primes[] = { 0 }; -static st_index_t -new_size(st_index_t size) +static int +new_size(int size) { int i; - st_index_t newsize; +#if 0 + for (i=3; i<31; i++) { + if ((1<<i) > size) return 1<<i; + } + return -1; +#else + int newsize; - for (i = 0, newsize = MINSIZE; i < numberof(primes); i++, newsize <<= 1) { + for (i = 0, newsize = MINSIZE; + i < sizeof(primes)/sizeof(primes[0]); + i++, newsize <<= 1) + { if (newsize > size) return primes[i]; } /* Ran out of polynomials */ -#ifndef NOT_RUBY - rb_raise(rb_eRuntimeError, "st_table too big"); -#endif return -1; /* should raise exception */ +#endif } -#define MAX_PACKED_NUMHASH (ST_DEFAULT_INIT_TABLE_SIZE/2) - st_table* -st_init_table_with_size(const struct st_hash_type *type, st_index_t size) +st_init_table_with_size(const struct st_hash_type *type, int size) { st_table *tbl; @@ -135,7 +131,6 @@ st_init_table_with_size(const struct st_hash_type *type, st_index_t size) tbl = alloc(st_table); tbl->type = type; tbl->num_entries = 0; - tbl->entries_packed = type == &type_numhash && size/2 <= MAX_PACKED_NUMHASH; tbl->num_bins = size; tbl->bins = (st_table_entry **)Calloc(size, sizeof(st_table_entry*)); tbl->head = 0; @@ -157,7 +152,7 @@ st_init_numtable(void) } st_table* -st_init_numtable_with_size(st_index_t size) +st_init_numtable_with_size(int size) { return st_init_table_with_size(&type_numhash, size); } @@ -169,7 +164,7 @@ st_init_strtable(void) } st_table* -st_init_strtable_with_size(st_index_t size) +st_init_strtable_with_size(int size) { return st_init_table_with_size(&type_strhash, size); } @@ -192,11 +187,6 @@ st_clear(st_table *table) register st_table_entry *ptr, *next; st_index_t i; - if (table->entries_packed) { - table->num_entries = 0; - return; - } - for(i = 0; i < table->num_bins; i++) { ptr = table->bins[i]; table->bins[i] = 0; @@ -219,29 +209,13 @@ st_free_table(st_table *table) free(table); } -size_t -st_memsize(const st_table *table) -{ - if (table->entries_packed) { - return table->num_bins * sizeof (void *) + sizeof(st_table); - } - else { - return table->num_entries * sizeof(struct st_table_entry) + table->num_bins * sizeof (void *) + sizeof(st_table); - } -} - #define PTR_NOT_EQUAL(table, ptr, hash_val, key) \ ((ptr) != 0 && (ptr->hash != (hash_val) || !EQUAL((table), (key), (ptr)->key))) -#define COLLISION -#define FOUND_ENTRY - #define FIND_ENTRY(table, ptr, hash_val, bin_pos) do {\ bin_pos = hash_val%(table)->num_bins;\ ptr = (table)->bins[bin_pos];\ - FOUND_ENTRY;\ if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {\ - COLLISION;\ while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {\ ptr = ptr->next;\ }\ @@ -249,25 +223,12 @@ st_memsize(const st_table *table) }\ } while (0) -#define collision_check 0 - int -st_lookup(st_table *table, register st_data_t key, st_data_t *value) +st_lookup(st_table *table, st_data_t key, st_data_t *value) { - st_index_t hash_val, bin_pos; + unsigned int hash_val, bin_pos; register st_table_entry *ptr; - if (table->entries_packed) { - st_index_t i; - for (i = 0; i < table->num_entries; i++) { - if ((st_data_t)table->bins[i*2] == key) { - if (value !=0) *value = (st_data_t)table->bins[i*2+1]; - return 1; - } - } - return 0; - } - hash_val = do_hash(key, table); FIND_ENTRY(table, ptr, hash_val, bin_pos); @@ -280,46 +241,10 @@ st_lookup(st_table *table, register st_data_t key, st_data_t *value) } } -int -st_get_key(st_table *table, register st_data_t key, st_data_t *result) -{ - st_index_t hash_val, bin_pos; - register st_table_entry *ptr; - - if (table->entries_packed) { - st_index_t i; - for (i = 0; i < table->num_entries; i++) { - if ((st_data_t)table->bins[i*2] == key) { - if (result !=0) *result = (st_data_t)table->bins[i*2]; - return 1; - } - } - return 0; - } - - hash_val = do_hash(key, table); - FIND_ENTRY(table, ptr, hash_val, bin_pos); - - if (ptr == 0) { - return 0; - } - else { - if (result != 0) *result = ptr->key; - return 1; - } -} - -#undef collision_check -#define collision_check 1 - -#define MORE_PACKABLE_P(table) \ - ((st_index_t)((table)->num_entries+1) * 2 <= (table)->num_bins && \ - (table)->num_entries+1 <= MAX_PACKED_NUMHASH) - #define ADD_DIRECT(table, key, value, hash_val, bin_pos)\ do {\ st_table_entry *entry;\ - if (table->num_entries > ST_DEFAULT_MAX_DENSITY * table->num_bins) {\ + if (table->num_entries/(table->num_bins) > ST_DEFAULT_MAX_DENSITY) {\ rehash(table);\ bin_pos = hash_val % table->num_bins;\ }\ @@ -343,49 +268,12 @@ do {\ table->num_entries++;\ } while (0) -static void -unpack_entries(register st_table *table) -{ - st_index_t i; - struct st_table_entry *packed_bins[MAX_PACKED_NUMHASH*2]; - st_table tmp_table = *table; - - memcpy(packed_bins, table->bins, sizeof(struct st_table_entry *) * table->num_entries*2); - table->bins = packed_bins; - tmp_table.entries_packed = 0; - tmp_table.num_entries = 0; - memset(tmp_table.bins, 0, sizeof(struct st_table_entry *) * tmp_table.num_bins); - for (i = 0; i < table->num_entries; i++) { - st_insert(&tmp_table, (st_data_t)packed_bins[i*2], (st_data_t)packed_bins[i*2+1]); - } - *table = tmp_table; -} - int -st_insert(register st_table *table, register st_data_t key, st_data_t value) +st_insert(st_table *table, st_data_t key, st_data_t value) { - st_index_t hash_val, bin_pos; + unsigned int hash_val, bin_pos; register st_table_entry *ptr; - if (table->entries_packed) { - st_index_t i; - for (i = 0; i < table->num_entries; i++) { - if ((st_data_t)table->bins[i*2] == key) { - table->bins[i*2+1] = (struct st_table_entry*)value; - return 1; - } - } - if (MORE_PACKABLE_P(table)) { - i = table->num_entries++; - table->bins[i*2] = (struct st_table_entry*)key; - table->bins[i*2+1] = (struct st_table_entry*)value; - return 0; - } - else { - unpack_entries(table); - } - } - hash_val = do_hash(key, table); FIND_ENTRY(table, ptr, hash_val, bin_pos); @@ -399,63 +287,10 @@ st_insert(register st_table *table, register st_data_t key, st_data_t value) } } -int -st_insert2(register st_table *table, register st_data_t key, st_data_t value, - st_data_t (*func)(st_data_t)) -{ - st_index_t hash_val, bin_pos; - register st_table_entry *ptr; - - if (table->entries_packed) { - st_index_t i; - for (i = 0; i < table->num_entries; i++) { - if ((st_data_t)table->bins[i*2] == key) { - table->bins[i*2+1] = (struct st_table_entry*)value; - return 1; - } - } - if (MORE_PACKABLE_P(table)) { - i = table->num_entries++; - table->bins[i*2] = (struct st_table_entry*)key; - table->bins[i*2+1] = (struct st_table_entry*)value; - return 0; - } - else { - unpack_entries(table); - } - } - - hash_val = do_hash(key, table); - FIND_ENTRY(table, ptr, hash_val, bin_pos); - - if (ptr == 0) { - key = (*func)(key); - ADD_DIRECT(table, key, value, hash_val, bin_pos); - return 0; - } - else { - ptr->record = value; - return 1; - } -} - void st_add_direct(st_table *table, st_data_t key, st_data_t value) { - st_index_t hash_val, bin_pos; - - if (table->entries_packed) { - int i; - if (MORE_PACKABLE_P(table)) { - i = table->num_entries++; - table->bins[i*2] = (struct st_table_entry*)key; - table->bins[i*2+1] = (struct st_table_entry*)value; - return; - } - else { - unpack_entries(table); - } - } + unsigned int hash_val, bin_pos; hash_val = do_hash(key, table); bin_pos = hash_val % table->num_bins; @@ -470,7 +305,7 @@ rehash(register st_table *table) new_num_bins = new_size(table->num_bins+1); new_bins = (st_table_entry**) - xrealloc(table->bins, new_num_bins * sizeof(st_table_entry*)); + realloc(table->bins, new_num_bins * sizeof(st_table_entry*)); for (i = 0; i < new_num_bins; ++i) new_bins[i] = 0; table->num_bins = new_num_bins; table->bins = new_bins; @@ -506,11 +341,6 @@ st_copy(st_table *old_table) return 0; } - if (old_table->entries_packed) { - memcpy(new_table->bins, old_table->bins, sizeof(struct st_table_entry *) * old_table->num_bins); - return new_table; - } - if ((ptr = old_table->head) != 0) { prev = 0; tail = &new_table->head; @@ -557,23 +387,7 @@ st_delete(register st_table *table, register st_data_t *key, st_data_t *value) st_table_entry **prev; register st_table_entry *ptr; - if (table->entries_packed) { - st_index_t i; - for (i = 0; i < table->num_entries; i++) { - if ((st_data_t)table->bins[i*2] == *key) { - if (value != 0) *value = (st_data_t)table->bins[i*2+1]; - table->num_entries--; - memmove(&table->bins[i*2], &table->bins[(i+1)*2], - sizeof(struct st_table_entry*) * 2*(table->num_entries-i)); - return 1; - } - } - if (value != 0) *value = 0; - return 0; - } - hash_val = do_hash_bin(*key, table); - for (prev = &table->bins[hash_val]; (ptr = *prev) != 0; prev = &ptr->next) { if (EQUAL(table, *key, ptr->key)) { *prev = ptr->next; @@ -590,118 +404,12 @@ st_delete(register st_table *table, register st_data_t *key, st_data_t *value) } int -st_delete_safe(register st_table *table, register st_data_t *key, st_data_t *value, st_data_t never) -{ - st_index_t hash_val; - register st_table_entry *ptr; - - if (table->entries_packed) { - st_index_t i; - for (i = 0; i < table->num_entries; i++) { - if ((st_data_t)table->bins[i*2] == *key) { - if (value != 0) *value = (st_data_t)table->bins[i*2+1]; - table->bins[i*2] = (void *)never; - return 1; - } - } - if (value != 0) *value = 0; - return 0; - } - - hash_val = do_hash_bin(*key, table); - ptr = table->bins[hash_val]; - - for (; ptr != 0; ptr = ptr->next) { - if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) { - REMOVE_ENTRY(table, ptr); - *key = ptr->key; - if (value != 0) *value = ptr->record; - ptr->key = ptr->record = never; - return 1; - } - } - - if (value != 0) *value = 0; - return 0; -} - -void -st_cleanup_safe(st_table *table, st_data_t never) -{ - st_table_entry *ptr, **last, *tmp; - st_index_t i; - - if (table->entries_packed) { - st_index_t i = 0, j = 0; - while ((st_data_t)table->bins[i*2] != never) { - if (i++ == table->num_entries) return; - } - for (j = i; ++i < table->num_entries;) { - if ((st_data_t)table->bins[i*2] == never) continue; - table->bins[j*2] = table->bins[i*2]; - table->bins[j*2+1] = table->bins[i*2+1]; - j++; - } - table->num_entries = j; - return; - } - - for (i = 0; i < table->num_bins; i++) { - ptr = *(last = &table->bins[i]); - while (ptr != 0) { - if (ptr->key == never) { - tmp = ptr; - *last = ptr = ptr->next; - free(tmp); - } - else { - ptr = *(last = &ptr->next); - } - } - } -} - -int st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg) { st_table_entry *ptr, **last, *tmp; enum st_retval retval; st_index_t i; - if (table->entries_packed) { - for (i = 0; i < table->num_entries; i++) { - st_index_t j; - st_data_t key, val; - key = (st_data_t)table->bins[i*2]; - val = (st_data_t)table->bins[i*2+1]; - retval = (*func)(key, val, arg); - switch (retval) { - case ST_CHECK: /* check if hash is modified during iteration */ - for (j = 0; j < table->num_entries; j++) { - if ((st_data_t)table->bins[j*2] == key) - break; - } - if (j == table->num_entries) { - /* call func with error notice */ - retval = (*func)(0, 0, arg, 1); - return 1; - } - /* fall through */ - case ST_CONTINUE: - break; - case ST_STOP: - return 0; - case ST_DELETE: - table->num_entries--; - memmove(&table->bins[i*2], &table->bins[(i+1)*2], - sizeof(struct st_table_entry*) * 2*(table->num_entries-i)); - i--; - break; - } - } - return 0; - } - if ((ptr = table->head) != 0) { do { i = ptr->hash % table->num_bins; @@ -716,6 +424,7 @@ st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg) } } /* fall through */ + default: case ST_CONTINUE: ptr = ptr->fore; break; @@ -740,466 +449,46 @@ st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg) return 0; } -typedef int st_foreach_func(mrb_sym, void*, void *); - -struct foreach_safe_arg { - st_table *tbl; - st_foreach_func *func; - void *arg; -}; - -static int -foreach_safe_i(mrb_state *mrb, mrb_sym key, void* value, struct foreach_safe_arg *arg) -{ - int status; - - if (key == 0xffffffff/*key == Qundef*/) return ST_CONTINUE; - status = (*arg->func)(key, value, arg->arg); - if (status == ST_CONTINUE) { - return ST_CHECK; - } - return status; -} - -void -st_foreach_safe(mrb_state *mrb, void *table, int (*func)(ANYARGS), void* a) -{ - struct foreach_safe_arg arg; - - arg.tbl = table; - arg.func = (st_foreach_func *)func; - arg.arg = a; - if (st_foreach(table, foreach_safe_i, (st_data_t)&arg)) { - mrb_raise(mrb, mrb->eRuntimeError_class, "hash modified during iteration"); - } -} - -int -st_foreachNew(mrb_state *mrb, st_table *table, int (*func)(ANYARGS), void* arg) -{ - st_table_entry *ptr, **last, *tmp; - enum st_retval retval; - st_index_t i; - - if (table->entries_packed) { - for (i = 0; i < table->num_entries; i++) { - st_index_t j; - st_data_t key, val; - key = (st_data_t)table->bins[i*2]; - val = (st_data_t)table->bins[i*2+1]; - retval = (*func)(mrb, key, val, arg); - switch (retval) { - case ST_CHECK: /* check if hash is modified during iteration */ - for (j = 0; j < table->num_entries; j++) { - if ((st_data_t)table->bins[j*2] == key) - break; - } - if (j == table->num_entries) { - /* call func with error notice */ - retval = (*func)(0, 0, arg, 1); - return 1; - } - /* fall through */ - case ST_CONTINUE: - break; - case ST_STOP: - return 0; - case ST_DELETE: - table->num_entries--; - memmove(&table->bins[i*2], &table->bins[(i+1)*2], - sizeof(struct st_table_entry*) * 2*(table->num_entries-i)); - i--; - break; - } - } - return 0; - } - - if ((ptr = table->head) != 0) { - do { - i = ptr->hash % table->num_bins; - retval = (*func)(mrb, ptr->key, ptr->record, arg); - switch (retval) { - case ST_CHECK: /* check if hash is modified during iteration */ - for (tmp = table->bins[i]; tmp != ptr; tmp = tmp->next) { - if (!tmp) { - /* call func with error notice */ - retval = (*func)(0, 0, arg, 1); - return 1; - } - } - /* fall through */ - case ST_CONTINUE: - ptr = ptr->fore; - break; - case ST_STOP: - return 0; - case ST_DELETE: - last = &table->bins[ptr->hash % table->num_bins]; - for (; (tmp = *last) != 0; last = &tmp->next) { - if (ptr == tmp) { - tmp = ptr->fore; - *last = ptr->next; - REMOVE_ENTRY(table, ptr); - free(ptr); - if (ptr == tmp) return 0; - ptr = tmp; - break; - } - } - } - } while (ptr && table->head); - } - return 0; -} - -/* - * hash_32 - 32 bit Fowler/Noll/Vo FNV-1a hash code - * - * @(#) $Hash32: Revision: 1.1 $ - * @(#) $Hash32: Id: hash_32a.c,v 1.1 2003/10/03 20:38:53 chongo Exp $ - * @(#) $Hash32: Source: /usr/local/src/cmd/fnv/RCS/hash_32a.c,v $ - * - *** - * - * Fowler/Noll/Vo hash - * - * The basis of this hash algorithm was taken from an idea sent - * as reviewer comments to the IEEE POSIX P1003.2 committee by: - * - * Phong Vo (http://www.research.att.com/info/kpv/) - * Glenn Fowler (http://www.research.att.com/~gsf/) - * - * In a subsequent ballot round: - * - * Landon Curt Noll (http://www.isthe.com/chongo/) - * - * improved on their algorithm. Some people tried this hash - * and found that it worked rather well. In an EMail message - * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash. - * - * FNV hashes are designed to be fast while maintaining a low - * collision rate. The FNV speed allows one to quickly hash lots - * of data while maintaining a reasonable collision rate. See: - * - * http://www.isthe.com/chongo/tech/comp/fnv/index.html - * - * for more details as well as other forms of the FNV hash. - *** - * - * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the - * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str(). - * - *** - * - * Please do not copyright this code. This code is in the public domain. - * - * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, - * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO - * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR - * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF - * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR - * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - * - * By: - * chongo <Landon Curt Noll> /\oo/\ - * http://www.isthe.com/chongo/ - * - * Share and Enjoy! :-) - */ - -/* - * 32 bit FNV-1 and FNV-1a non-zero initial basis - * - * The FNV-1 initial basis is the FNV-0 hash of the following 32 octets: - * - * chongo <Landon Curt Noll> /\../\ - * - * NOTE: The \'s above are not back-slashing escape characters. - * They are literal ASCII backslash 0x5c characters. - * - * NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition. - */ -#define FNV1_32A_INIT 0x811c9dc5 - -/* - * 32 bit magic FNV-1a prime - */ -#define FNV_32_PRIME 0x01000193 - -#ifdef ST_USE_FNV1 static st_index_t -strhash(st_data_t arg) +strhash(const char *string) { - register const char *string = (const char *)arg; - register st_index_t hval = FNV1_32A_INIT; + register int c; - /* - * FNV-1a hash each octet in the buffer - */ - while (*string) { - /* xor the bottom with the current octet */ - hval ^= (unsigned int)*string++; +#ifdef HASH_ELFHASH + register unsigned int h = 0, g; - /* multiply by the 32 bit FNV magic prime mod 2^32 */ - hval *= FNV_32_PRIME; + while ((c = *string++) != '\0') { + h = ( h << 4 ) + c; + if ( g = h & 0xF0000000 ) + h ^= g >> 24; + h &= ~g; } - return hval; -} -#else - -#ifndef UNALIGNED_WORD_ACCESS -# if defined __i386__ || defined _M_IX86 -# define UNALIGNED_WORD_ACCESS 1 -# endif -#endif -#ifndef UNALIGNED_WORD_ACCESS -# define UNALIGNED_WORD_ACCESS 0 -#endif - -/* MurmurHash described in http://murmurhash.googlepages.com/ */ -#ifndef MURMUR -#define MURMUR 2 -#endif - -#if MURMUR == 1 -#define MurmurMagic 0xc6a4a793 -#elif MURMUR == 2 -#if SIZEOF_ST_INDEX_T > 4 -#define MurmurMagic 0xc6a4a7935bd1e995 -#else -#define MurmurMagic 0x5bd1e995 -#endif -#endif - -static inline st_index_t -murmur(st_index_t h, st_index_t k, int r) -{ - const st_index_t m = MurmurMagic; -#if MURMUR == 1 - h += k; - h *= m; - h ^= h >> r; -#elif MURMUR == 2 - k *= m; - k ^= k >> r; - k *= m; - - h *= m; - h ^= k; -#endif return h; -} - -static inline st_index_t -murmur_finish(st_index_t h) -{ -#if MURMUR == 1 - h = murmur(h, 0, 10); - h = murmur(h, 0, 17); -#elif MURMUR == 2 - h ^= h >> 13; - h *= MurmurMagic; - h ^= h >> 15; -#endif - return h; -} - -#define murmur_step(h, k) murmur(h, k, 16) - -#if MURMUR == 1 -#define murmur1(h) murmur_step(h, 16) -#else -#define murmur1(h) murmur_step(h, 24) -#endif - -st_index_t -st_hash(const void *ptr, size_t len, st_index_t h) -{ - const char *data = ptr; - st_index_t t = 0; - - h += 0xdeadbeef; - -#define data_at(n) (st_index_t)((unsigned char)data[n]) -#define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0) -#if SIZEOF_ST_INDEX_T > 4 -#define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4 -#if SIZEOF_ST_INDEX_T > 8 -#define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \ - UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8 -#define UNALIGNED_ADD_ALL UNALIGNED_ADD_16 -#endif -#define UNALIGNED_ADD_ALL UNALIGNED_ADD_8 -#else -#define UNALIGNED_ADD_ALL UNALIGNED_ADD_4 -#endif - if (len >= sizeof(st_index_t)) { -#if !UNALIGNED_WORD_ACCESS - int align = (int)((st_data_t)data % sizeof(st_index_t)); - if (align) { - st_index_t d = 0; - int sl, sr, pack; - - switch (align) { -#ifdef WORDS_BIGENDIAN -# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \ - t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2) -#else -# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \ - t |= data_at(n) << CHAR_BIT*(n) -#endif - UNALIGNED_ADD_ALL; -#undef UNALIGNED_ADD - } - -#ifdef WORDS_BIGENDIAN - t >>= (CHAR_BIT * align) - CHAR_BIT; -#else - t <<= (CHAR_BIT * align); -#endif +#elif defined(HASH_PERL) + register int val = 0; - data += sizeof(st_index_t)-align; - len -= sizeof(st_index_t)-align; - - sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align); - sr = CHAR_BIT * align; - - while (len >= sizeof(st_index_t)) { - d = *(st_index_t *)data; -#ifdef WORDS_BIGENDIAN - t = (t << sr) | (d >> sl); -#else - t = (t >> sr) | (d << sl); -#endif - h = murmur_step(h, t); - t = d; - data += sizeof(st_index_t); - len -= sizeof(st_index_t); - } - - pack = len < (size_t)align ? (int)len : align; - d = 0; - switch (pack) { -#ifdef WORDS_BIGENDIAN -# define UNALIGNED_ADD(n) case (n) + 1: \ - d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1) -#else -# define UNALIGNED_ADD(n) case (n) + 1: \ - d |= data_at(n) << CHAR_BIT*(n) -#endif - UNALIGNED_ADD_ALL; -#undef UNALIGNED_ADD - } -#ifdef WORDS_BIGENDIAN - t = (t << sr) | (d >> sl); -#else - t = (t >> sr) | (d << sl); -#endif - -#if MURMUR == 2 - if (len < (size_t)align) goto skip_tail; -#endif - h = murmur_step(h, t); - data += pack; - len -= pack; - } - else -#endif - { - do { - h = murmur_step(h, *(st_index_t *)data); - data += sizeof(st_index_t); - len -= sizeof(st_index_t); - } while (len >= sizeof(st_index_t)); - } + while ((c = *string++) != '\0') { + val += c; + val += (val << 10); + val ^= (val >> 6); } + val += (val << 3); + val ^= (val >> 11); - t = 0; - switch (len) { -#ifdef WORDS_BIGENDIAN -# define UNALIGNED_ADD(n) case (n) + 1: \ - t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1) + return val + (val << 15); #else -# define UNALIGNED_ADD(n) case (n) + 1: \ - t |= data_at(n) << CHAR_BIT*(n) -#endif - UNALIGNED_ADD_ALL; -#undef UNALIGNED_ADD -#if MURMUR == 1 - h = murmur_step(h, t); -#elif MURMUR == 2 -# if !UNALIGNED_WORD_ACCESS - skip_tail: -# endif - h ^= t; - h *= MurmurMagic; -#endif - } - - return murmur_finish(h); -} + register int val = 0; -st_index_t -st_hash_uint32(st_index_t h, uint32_t i) -{ - return murmur_step(h + i, 16); -} + while ((c = *string++) != '\0') { + val = val*997 + c; + } -st_index_t -st_hash_uint(st_index_t h, st_index_t i) -{ - st_index_t v = 0; - h += i; -#ifdef WORDS_BIGENDIAN -#if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8 - v = murmur1(v + (h >> 12*8)); -#endif -#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8 - v = murmur1(v + (h >> 8*8)); -#endif -#if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8 - v = murmur1(v + (h >> 4*8)); -#endif -#endif - v = murmur1(v + h); -#ifndef WORDS_BIGENDIAN -#if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8 - v = murmur1(v + (h >> 4*8)); -#endif -#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8 - v = murmur1(v + (h >> 8*8)); -#endif -#if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8 - v = murmur1(v + (h >> 12*8)); + return val + (val>>5); #endif -#endif - return v; -} - -st_index_t -st_hash_end(st_index_t h) -{ - h = murmur_step(h, 10); - h = murmur_step(h, 17); - return h; } -#undef st_hash_start -st_index_t -st_hash_start(st_index_t h) -{ - return h; -} - -static st_index_t -strhash(st_data_t arg) -{ - register const char *string = (const char *)arg; - return st_hash(string, strlen(string), FNV1_32A_INIT); -} -#endif +#define FNV1_32A_INIT 0x811c9dc5 +#define FNV_32_PRIME 0x01000193 int st_strcasecmp(const char *s1, const char *s2) @@ -1270,14 +559,38 @@ strcasehash(st_data_t arg) return hval; } -int -st_numcmp(st_data_t x, st_data_t y) +static int +numcmp(long x, long y) { return x != y; } -st_index_t -st_numhash(st_data_t n) +static st_index_t +numhash(long n) +{ + return n; +} + +#if 0 +static int +f(st_data_t key, st_data_t val, st_data_t a) +{ + printf("tbl=%p key=%s val=%s\n", (st_table*)a, (char*)key, (char*)val); + // return ST_CONTINUE; +} + +void +main(int argc, char **argv) { - return (st_index_t)n; + st_table *tbl = st_init_strtable(); + int i; + + for (i = 1; i<argc; i+=2) { + st_insert(tbl, (st_data_t)argv[i], (st_data_t)argv[i+1]); + } + st_foreach(tbl, f, (st_data_t)tbl); + st_delete(tbl, (st_data_t*)&argv[1], 0); + st_foreach(tbl, f, (st_data_t)tbl); } +#endif + @@ -9,23 +9,8 @@ extern "C" { #endif -#ifndef RUBY_LIB_PREFIX - -#ifdef RUBY_EXTCONF_H -#include RUBY_EXTCONF_H -#endif -#endif - -#if defined STDC_HEADERS -#include <stddef.h> -#elif defined HAVE_STDLIB_H #include <stdlib.h> -#endif - -#ifdef HAVE_STDINT_H -# include <stdint.h> -#endif -#include <inttypes.h> +#include <stdint.h> #ifndef CHAR_BIT # ifdef HAVE_LIMITS_H @@ -35,10 +20,6 @@ extern "C" { # endif #endif -#ifndef _ -# define _(args) args -#endif - #ifndef ANYARGS # ifdef __cplusplus # define ANYARGS ... @@ -64,27 +45,15 @@ struct st_table_entry { st_table_entry *fore, *back; }; -#ifndef SIZEOF_VOIDP -#define SIZEOF_VOIDP 4 -#endif - -#define SIZEOF_ST_INDEX_T SIZEOF_VOIDP - struct st_hash_type { int (*compare)(ANYARGS /*st_data_t, st_data_t*/); /* st_compare_func* */ st_index_t (*hash)(ANYARGS /*st_data_t*/); /* st_hash_func* */ }; -#define ST_INDEX_BITS (sizeof(st_index_t) * CHAR_BIT) - struct st_table { const struct st_hash_type *type; - st_index_t num_bins; - unsigned int entries_packed : 1; -#ifdef __GNUC__ - __extension__ -#endif - st_index_t num_entries : ST_INDEX_BITS - 1; + int num_bins; + int num_entries; struct st_table_entry **bins; struct st_table_entry *head, *tail; }; @@ -94,46 +63,37 @@ struct st_table { enum st_retval {ST_CONTINUE, ST_STOP, ST_DELETE, ST_CHECK}; st_table *st_init_table(const struct st_hash_type *); -st_table *st_init_table_with_size(const struct st_hash_type *, st_index_t); +st_table *st_init_table_with_size(const struct st_hash_type *, int); st_table *st_init_numtable(void); -st_table *st_init_numtable_with_size(st_index_t); +st_table *st_init_numtable_with_size(int); st_table *st_init_strtable(void); -st_table *st_init_strtable_with_size(st_index_t); +st_table *st_init_strtable_with_size(int); st_table *st_init_strcasetable(void); st_table *st_init_strcasetable_with_size(st_index_t); -int st_delete(st_table *, st_data_t *, st_data_t *); /* returns 0:notfound 1:deleted */ +int st_delete(st_table *, st_data_t *, st_data_t *); int st_delete_safe(st_table *, st_data_t *, st_data_t *, st_data_t); int st_insert(st_table *, st_data_t, st_data_t); -int st_insert2(st_table *, st_data_t, st_data_t, st_data_t (*)(st_data_t)); int st_lookup(st_table *, st_data_t, st_data_t *); -int st_get_key(st_table *, st_data_t, st_data_t *); int st_foreach(st_table *, int (*)(ANYARGS), st_data_t); -int st_foreachNew(mrb_state *mrb, st_table *, int (*)(ANYARGS), void*); -int st_reverse_foreach(st_table *, int (*)(ANYARGS), st_data_t); void st_add_direct(st_table *, st_data_t, st_data_t); void st_free_table(st_table *); void st_cleanup_safe(st_table *, st_data_t); -void st_clear(st_table *); st_table *st_copy(st_table *); -int st_numcmp(st_data_t, st_data_t); -st_index_t st_numhash(st_data_t); -int st_strcasecmp(const char *s1, const char *s2); -int st_strncasecmp(const char *s1, const char *s2, size_t n); -size_t st_memsize(const st_table *); -st_index_t st_hash(const void *ptr, size_t len, st_index_t h); -st_index_t st_hash_uint32(st_index_t h, uint32_t i); -st_index_t st_hash_uint(st_index_t h, st_index_t i); -st_index_t st_hash_end(st_index_t h); -st_index_t st_hash_start(st_index_t h); -#define st_hash_start(h) ((st_index_t)(h)) - int st_strcasecmp(const char *s1, const char *s2); int st_strncasecmp(const char *s1, const char *s2, size_t n); #define STRCASECMP(s1, s2) (st_strcasecmp(s1, s2)) #define STRNCASECMP(s1, s2, n) (st_strncasecmp(s1, s2, n)) +#define ST_NUMCMP ((int (*)()) 0) +#define ST_NUMHASH ((int (*)()) -2) + +#define st_numcmp ST_NUMCMP +#define st_numhash ST_NUMHASH + +int st_strhash(); + #if defined(__cplusplus) } /* extern "C" { */ #endif -#endif /* RUBY_ST_H */ +#endif /* ST_INCLUDED */ diff --git a/src/string.c b/src/string.c index b9c8796e6..e774b7032 100644 --- a/src/string.c +++ b/src/string.c @@ -217,38 +217,12 @@ single_byte_optimizable(mrb_state *mrb, mrb_value str) static inline const char * search_nonascii(const char *p, const char *e) { -#if SIZEOF_VALUE == 8 -# define NONASCII_MASK 0x8080808080808080ULL -#elif SIZEOF_VALUE == 4 -# define NONASCII_MASK 0x80808080UL -#endif -#ifdef NONASCII_MASK - if ((int)sizeof(intptr_t) * 2 < e - p) { - const intptr_t *s, *t; - const intptr_t lowbits = sizeof(intptr_t) - 1; - s = (const intptr_t*)(~lowbits & ((intptr_t)p + lowbits)); - while (p < (const char *)s) { - if (!ISASCII(*p)) - return p; - p++; - } - t = (const intptr_t*)(~lowbits & (intptr_t)e); - while (s < t) { - if (*s & (intptr_t)NONASCII_MASK) { - t = s; - break; - } - s++; - } - p = (const char *)t; - } -#endif - while (p < e) { - if (!ISASCII(*p)) - return p; - p++; - } - return NULL; + while (p < e) { + if (!ISASCII(*p)) + return p; + p++; + } + return NULL; } #endif //INCLUDE_ENCODING @@ -1173,63 +1147,6 @@ mrb_str_match(mrb_state *mrb, mrb_value self/* x */) } } /* ---------------------------------- */ -#ifdef INCLUDE_ENCODING -#ifdef NONASCII_MASK -#define is_utf8_lead_byte(c) (((c)&0xC0) != 0x80) -static inline int -count_utf8_lead_bytes_with_word(const intptr_t *s) -{ - int d = *s; - d |= ~(d>>1); - d >>= 6; - d &= NONASCII_MASK >> 7; - d += (d>>8); - d += (d>>16); -#if SIZEOF_VALUE == 8 - d += (d>>32); -#endif - return (d&0xF); -} -#endif - -#ifdef NONASCII_MASK -static char * -str_utf8_nth(const char *p, const char *e, long nth) -{ - if ((int)SIZEOF_VALUE < e - p && (int)SIZEOF_VALUE * 2 < nth) { - const intptr_t *s, *t; - const intptr_t lowbits = sizeof(int) - 1; - s = (const intptr_t*)(~lowbits & ((intptr_t)p + lowbits)); - t = (const intptr_t*)(~lowbits & (intptr_t)e); - while (p < (const char *)s) { - if (is_utf8_lead_byte(*p)) nth--; - p++; - } - do { - nth -= count_utf8_lead_bytes_with_word(s); - s++; - } while (s < t && (int)sizeof(intptr_t) <= nth); - p = (char *)s; - } - while (p < e) { - if (is_utf8_lead_byte(*p)) { - if (nth == 0) break; - nth--; - } - p++; - } - return (char *)p; -} - -static long -str_utf8_offset(const char *p, const char *e, long nth) -{ - const char *pp = str_utf8_nth(p, e, nth); - return pp - p; -} -#endif -#endif //INCLUDE_ENCODING - mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, int len) { @@ -1289,13 +1206,6 @@ mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, int len) if (len == 0) { p = 0; } -#ifdef NONASCII_MASK - else if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID && - enc == mrb_utf8_encoding(mrb)) { - p = str_utf8_nth(s, e, beg); - len = str_utf8_offset(p, e, len); - } -#endif else if (mrb_enc_mbmaxlen(enc) == mrb_enc_mbminlen(enc)) { int char_sz = mrb_enc_mbmaxlen(enc); @@ -1412,46 +1322,6 @@ mrb_enc_strlen_cr(mrb_state *mrb, const char *p, const char *e, mrb_encoding *en #ifndef INCLUDE_ENCODING static inline long -mrb_memsearch_ss(const unsigned char *xs, long m, const unsigned char *ys, long n) -{ - const unsigned char *x = xs, *xe = xs + m; - const unsigned char *y = ys, *ye = ys + n; -//2011/06/30 #define SIZEOF_VALUE 4 -#ifndef VALUE_MAX -# if SIZEOF_VALUE == 8 -# define VALUE_MAX 0xFFFFFFFFFFFFFFFFULL -# elif SIZEOF_VALUE == 4 -# define VALUE_MAX 0xFFFFFFFFUL -# elif SIZEOF_LONG == SIZEOF_VOIDP -# define SIZEOF_VALUE 4 -# define VALUE_MAX 0xFFFFFFFFUL -# endif -#endif - int hx, hy, mask = VALUE_MAX >> ((SIZEOF_VALUE - m) * CHAR_BIT); - - if (m > SIZEOF_VALUE) - mrb_bug("!!too long pattern string!!"); - - /* Prepare hash value */ - for (hx = *x++, hy = *y++; x < xe; ++x, ++y) { - hx <<= CHAR_BIT; - hy <<= CHAR_BIT; - hx |= *x; - hy |= *y; - } - /* Searching */ - while (hx != hy) { - if (y == ye) - return -1; - hy <<= CHAR_BIT; - hy |= *y; - hy &= mask; - y++; - } - return y - ys - m; -} - -static inline long mrb_memsearch_qs(const unsigned char *xs, long m, const unsigned char *ys, long n) { const unsigned char *x = xs, *xe = xs + m; @@ -1470,6 +1340,7 @@ mrb_memsearch_qs(const unsigned char *xs, long m, const unsigned char *ys, long } return -1; } + int mrb_memsearch(const void *x0, int m, const void *y0, int n) { @@ -1490,12 +1361,7 @@ mrb_memsearch(const void *x0, int m, const void *y0, int n) } return -1; } - else if (m <= SIZEOF_VALUE) { - return mrb_memsearch_ss(x0, m, y0, n); - } - else { - return mrb_memsearch_qs(x0, m, y0, n); - } + return mrb_memsearch_qs(x0, m, y0, n); } #endif //INCLUDE_ENCODING @@ -1513,33 +1379,6 @@ str_strlen(mrb_state *mrb, mrb_value str, mrb_encoding *enc) p = RSTRING_PTR(str); e = RSTRING_END(str); cr = ENC_CODERANGE(str); -#ifdef NONASCII_MASK - if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID && - enc == mrb_utf8_encoding(mrb)) { - - int len = 0; - if ((int)sizeof(intptr_t) * 2 < e - p) { - const intptr_t *s, *t; - const intptr_t lowbits = sizeof(int) - 1; - s = (const intptr_t*)(~lowbits & ((intptr_t)p + lowbits)); - t = (const intptr_t*)(~lowbits & (intptr_t)e); - while (p < (const char *)s) { - if (is_utf8_lead_byte(*p)) len++; - p++; - } - while (s < t) { - len += count_utf8_lead_bytes_with_word(s); - s++; - } - p = (const char *)s; - } - while (p < e) { - if (is_utf8_lead_byte(*p)) len++; - p++; - } - return (long)len; - } -#endif n = mrb_enc_strlen_cr(mrb, p, e, enc, &cr); if (cr) { ENC_CODERANGE_SET(str, cr); @@ -5000,9 +4839,9 @@ mrb_str_buf_cat_escaped_char(mrb_state *mrb, mrb_value result, unsigned int c, i char buf[CHAR_ESC_LEN + 1]; int l; -#if SIZEOF_INT > 4 - c &= 0xffffffff; -#endif + if (sizeof(c) > 4) { + c &= 0xffffffff; + } if (unicode_p) { if (c < 0x7F && ISPRINT(c)) { snprintf(buf, CHAR_ESC_LEN, "%c", c); diff --git a/src/transcode.c b/src/transcode.c index 05d2b05b1..66a4d4e42 100644 --- a/src/transcode.c +++ b/src/transcode.c @@ -1770,9 +1770,9 @@ mrb_econv_putbackable(mrb_econv_t *ec) { if (ec->num_trans == 0) return 0; -#if SIZEOF_SIZE_T > SIZEOF_INT - if (ec->elems[0].tc->readagain_len > INT_MAX) return INT_MAX; -#endif + if (sizeof(size_t) > sizeof(int)) { + if (ec->elems[0].tc->readagain_len > INT_MAX) return INT_MAX; + } return (int)ec->elems[0].tc->readagain_len; } @@ -2625,7 +2625,7 @@ str_transcode0(mrb_state *mrb, int argc, mrb_value *argv, mrb_value *self, int e transcode_loop(mrb, &fromp, &bp, (sp+slen), (bp+blen), dest, str_transcoding_resize, sname, dname, ecflags, ecopts); if (fromp != sp+slen) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "not fully converted, %"PRIdPTRDIFF" bytes left", sp+slen-fromp); + mrb_raise(mrb, E_ARGUMENT_ERROR, "not fully converted, %td bytes left", sp+slen-fromp); } buf = (unsigned char *)RSTRING_PTR(dest); *bp = '\0'; diff --git a/src/variable.c b/src/variable.c index 5c3c493d8..9804ab2ef 100644 --- a/src/variable.c +++ b/src/variable.c @@ -4,6 +4,7 @@ ** See Copyright Notice in mruby.h */ +#include <stdio.h> #include "mruby.h" #include "mruby/class.h" #include "ritehash.h" diff --git a/src/variable.h b/src/variable.h index bd00b2f1f..9076fcf7c 100644 --- a/src/variable.h +++ b/src/variable.h @@ -36,7 +36,6 @@ int mrb_const_defined(mrb_state*, mrb_value, mrb_sym); mrb_value mrb_obj_iv_get(mrb_state*, struct RObject*, mrb_sym); void mrb_obj_iv_set(mrb_state*, struct RObject*, mrb_sym, mrb_value); const char * mrb_class2name(mrb_state *mrb, struct RClass* klass); -void mrb_define_variable(mrb_state *mrb, const char *name, mrb_value *var); mrb_value mrb_iv_get(mrb_state *mrb, mrb_value obj, mrb_sym sym); void mrb_iv_set(mrb_state *mrb, mrb_value obj, mrb_sym sym, mrb_value v); /* mrb_iv_set */ void mrb_copy_generic_ivar(mrb_value clone, mrb_value obj); |
