APIdock / Ruby
/
method

sum

ruby latest stable - Class: String
sum(p1 = v1)
public

Returns a basic n-bit checksum of the characters in str, where n is the optional Integer parameter, defaulting to 16. The result is simply the sum of the binary value of each byte in str modulo 2**n - 1. This is not a particularly good checksum.

static VALUE
rb_str_sum(int argc, VALUE *argv, VALUE str)
{
 VALUE vbits;
 int bits;
 char *ptr, *p, *pend;
 long len;
 VALUE sum = INT2FIX(0);
 unsigned long sum0 = 0;
 if (argc == 0) {
 bits = 16;
 }
 else {
 rb_scan_args(argc, argv, "01", &vbits);
 bits = NUM2INT(vbits);
 if (bits < 0)
 bits = 0;
 }
 ptr = p = RSTRING_PTR(str);
 len = RSTRING_LEN(str);
 pend = p + len;
 while (p < pend) {
 if (FIXNUM_MAX - UCHAR_MAX < sum0) {
 sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
 str_mod_check(str, ptr, len);
 sum0 = 0;
 }
 sum0 += (unsigned char)*p;
 p++;
 }
 if (bits == 0) {
 if (sum0) {
 sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
 }
 }
 else {
 if (sum == INT2FIX(0)) {
 if (bits < (int)sizeof(long)*CHAR_BIT) {
 sum0 &= (((unsigned long)1)<<bits)-1;
 }
 sum = LONG2FIX(sum0);
 }
 else {
 VALUE mod;
 if (sum0) {
 sum = rb_funcall(sum, '+', 1, LONG2FIX(sum0));
 }
 mod = rb_funcall(INT2FIX(1), idLTLT, 1, INT2FIX(bits));
 mod = rb_funcall(mod, '-', 1, INT2FIX(1));
 sum = rb_funcall(sum, '&', 1, mod);
 }
 }
 return sum;
}

AltStyle によって変換されたページ (->オリジナル) /