diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2004-10-02 07:57:32 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2004-10-02 07:57:32 +0000 |
commit | 779f3d6d11146c5c4e6acfff3c1c05f404387cf0 (patch) | |
tree | 8121d6d9a126bdbb1d37228c873ea1849990d082 /string.c | |
parent | 9d923be60446cd599094d12f259b7fee173ec875 (diff) |
* string.c (rb_str_sum): wrong cast caused wrong result.
[ruby-dev:24385]
* enum.c (enum_sort_by): hide temporary array from
ObjectSpace.each_object. [ruby-dev:24386]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6981 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 32 |
1 files changed, 16 insertions, 16 deletions
@@ -4417,34 +4417,34 @@ rb_str_sum(argc, argv, str) len = RSTRING(str)->len; pend = p + len; if (bits > sizeof(long)*CHAR_BIT) { - VALUE res = INT2FIX(0); - VALUE mod; - - mod = rb_funcall(INT2FIX(1), rb_intern("<<"), 1, INT2FIX(bits)); - mod = rb_funcall(mod, '-', 1, INT2FIX(1)); + VALUE sum = INT2FIX(0); while (p < pend) { str_mod_check(str, ptr, len); - res = rb_funcall(res, '+', 1, INT2FIX((unsigned int)*p)); + sum = rb_funcall(sum, '+', 1, INT2FIX((unsigned char)*p)); p++; } - res = rb_funcall(res, '&', 1, mod); - return res; + if (bits != 0) { + VALUE mod; + + mod = rb_funcall(INT2FIX(1), rb_intern("<<"), 1, INT2FIX(bits)); + mod = rb_funcall(mod, '-', 1, INT2FIX(1)); + sum = rb_funcall(sum, '&', 1, mod); + } + return sum; } else { - unsigned int res = 0; - unsigned int mod = (1<<bits)-1; + unsigned int sum = 0; - if (mod == 0) { - mod = -1; - } while (p < pend) { str_mod_check(str, ptr, len); - res += (unsigned int)*p; + sum += (unsigned char)*p; p++; } - res &= mod; - return rb_int2inum(res); + if (bits != 0) { + sum &= (1<<bits)-1; + } + return rb_int2inum(sum); } } |