summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'string.c')
-rw-r--r--string.c51
1 files changed, 25 insertions, 26 deletions
diff --git a/string.c b/string.c
index 8782e41bc2..d70ea21868 100644
--- a/string.c
+++ b/string.c
@@ -4380,35 +4380,35 @@ rb_str_sum(argc, argv, str)
ptr = p = RSTRING(str)->ptr;
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));
+ if (bits >= sizeof(long)*CHAR_BIT) {
+ 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);
}
}
@@ -4426,18 +4426,17 @@ rb_str_justify(argc, argv, str, jflag)
long n;
VALUE pad;
- if (rb_scan_args(argc, argv, "11", &w, &pad) == 2) {
- if (!NIL_P(pad)) {
- StringValue(pad);
- if (RSTRING(pad)->len > 0) {
- f = RSTRING(pad)->ptr;
- flen = RSTRING(pad)->len;
- }
- }
- }
+ rb_scan_args(argc, argv, "11", &w, &pad);
width = NUM2LONG(w);
if (width < 0 || RSTRING(str)->len >= width) return rb_str_dup(str);
res = rb_str_new5(str, 0, width);
+ if (argc == 0) {
+ StringValue(pad);
+ if (RSTRING(pad)->len > 0) {
+ f = RSTRING(pad)->ptr;
+ flen = RSTRING(pad)->len;
+ }
+ }
p = RSTRING(res)->ptr;
if (jflag != 'l') {
n = width - RSTRING(str)->len;