* string.c (rb_str_sum): don't call method for each byte.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26975 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
bd5e925cc4
commit
053d89ca2b
@ -1,3 +1,7 @@
|
|||||||
|
Fri Mar 19 05:26:31 2010 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* string.c (rb_str_sum): don't call method for each byte.
|
||||||
|
|
||||||
Thu Mar 18 21:24:21 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Thu Mar 18 21:24:21 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* Makefile.in (miniruby): link $(NORMALMAINOBJ).
|
* Makefile.in (miniruby): link $(NORMALMAINOBJ).
|
||||||
|
58
string.c
58
string.c
@ -6537,7 +6537,7 @@ rb_str_ord(VALUE s)
|
|||||||
* Returns a basic <em>n</em>-bit checksum of the characters in <i>str</i>,
|
* Returns a basic <em>n</em>-bit checksum of the characters in <i>str</i>,
|
||||||
* where <em>n</em> is the optional <code>Fixnum</code> parameter, defaulting
|
* where <em>n</em> is the optional <code>Fixnum</code> parameter, defaulting
|
||||||
* to 16. The result is simply the sum of the binary value of each character in
|
* to 16. The result is simply the sum of the binary value of each character in
|
||||||
* <i>str</i> modulo <code>2n - 1</code>. This is not a particularly good
|
* <i>str</i> modulo <code>2**n - 1</code>. This is not a particularly good
|
||||||
* checksum.
|
* checksum.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -6548,6 +6548,8 @@ rb_str_sum(int argc, VALUE *argv, VALUE str)
|
|||||||
int bits;
|
int bits;
|
||||||
char *ptr, *p, *pend;
|
char *ptr, *p, *pend;
|
||||||
long len;
|
long len;
|
||||||
|
VALUE sum = INT2FIX(0);
|
||||||
|
unsigned long sum0 = 0;
|
||||||
|
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
bits = 16;
|
bits = 16;
|
||||||
@ -6559,36 +6561,42 @@ rb_str_sum(int argc, VALUE *argv, VALUE str)
|
|||||||
ptr = p = RSTRING_PTR(str);
|
ptr = p = RSTRING_PTR(str);
|
||||||
len = RSTRING_LEN(str);
|
len = RSTRING_LEN(str);
|
||||||
pend = p + len;
|
pend = p + len;
|
||||||
if (bits >= (int)sizeof(long)*CHAR_BIT) {
|
|
||||||
VALUE sum = INT2FIX(0);
|
|
||||||
|
|
||||||
while (p < pend) {
|
while (p < pend) {
|
||||||
str_mod_check(str, ptr, len);
|
if (FIXNUM_MAX - 255 < sum0) {
|
||||||
sum = rb_funcall(sum, '+', 1, INT2FIX((unsigned char)*p));
|
sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
|
||||||
p++;
|
str_mod_check(str, ptr, len);
|
||||||
}
|
sum0 = 0;
|
||||||
if (bits != 0) {
|
}
|
||||||
VALUE mod;
|
sum0 += (unsigned char)*p;
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
|
||||||
mod = rb_funcall(INT2FIX(1), rb_intern("<<"), 1, INT2FIX(bits));
|
if (bits == 0) {
|
||||||
mod = rb_funcall(mod, '-', 1, INT2FIX(1));
|
if (sum0) {
|
||||||
sum = rb_funcall(sum, '&', 1, mod);
|
sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
|
||||||
}
|
}
|
||||||
return sum;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
unsigned long sum = 0;
|
if (sum == INT2FIX(0)) {
|
||||||
|
if (bits < (int)sizeof(long)*CHAR_BIT) {
|
||||||
|
sum0 &= (((unsigned long)1)<<bits)-1;
|
||||||
|
}
|
||||||
|
sum = LONG2FIX(sum0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
VALUE mod;
|
||||||
|
|
||||||
while (p < pend) {
|
if (sum0) {
|
||||||
str_mod_check(str, ptr, len);
|
sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
|
||||||
sum += (unsigned char)*p;
|
}
|
||||||
p++;
|
|
||||||
}
|
mod = rb_funcall(INT2FIX(1), rb_intern("<<"), 1, INT2FIX(bits));
|
||||||
if (bits != 0) {
|
mod = rb_funcall(mod, '-', 1, INT2FIX(1));
|
||||||
sum &= (((unsigned long)1)<<bits)-1;
|
sum = rb_funcall(sum, '&', 1, mod);
|
||||||
}
|
}
|
||||||
return rb_int2inum(sum);
|
|
||||||
}
|
}
|
||||||
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user