SmallInteger.st
branchjv
changeset 20143 c64b16f62536
parent 20079 8d884971c2ed
parent 20141 063019f72480
child 20342 219a5a47e8b1
--- a/SmallInteger.st	Tue Jul 12 06:40:09 2016 +0200
+++ b/SmallInteger.st	Wed Jul 13 07:01:16 2016 +0200
@@ -512,9 +512,7 @@
 # ifdef _SUB_IO_IO
 	RETURN ( _SUB_IO_IO(self, aNumber) );
 # else
-	REGISTER INT diff;
-
-	diff =  __intVal(self) - __intVal(aNumber);
+	REGISTER INT diff =  __intVal(self) - __intVal(aNumber);
 	if (__ISVALIDINTEGER(diff)) {
 	    RETURN ( __mkSmallInteger(diff) );
 	}
@@ -4278,15 +4276,16 @@
      (if showRadix is true, with initial XXr)
      The base argument should be between 2 and 36."
 
-    showRadix ifTrue:[
-	base printOn:aStream.
-	aStream nextPut:$r.
-    ].
-
-    (base isInteger and:[ base between:2 and:36 ]) ifTrue:[
-	aStream nextPutAll:(self printStringRadix:base)
+    |absBase|
+
+    (base isInteger and:[absBase := base abs. absBase between:2 and:36]) ifTrue:[
+        showRadix ifTrue:[
+            absBase printOn:aStream.
+            aStream nextPut:$r.
+        ].
+        aStream nextPutAll:(self printStringRadix:base)
     ] ifFalse:[
-	super printOn:aStream base:base showRadix:false.
+        super printOn:aStream base:base showRadix:true.
     ].
 
     "Created: / 07-09-2001 / 13:54:40 / cg"
@@ -4353,101 +4352,111 @@
 !
 
 printStringRadix:base
-    "return my printstring (optimized for bases 16, 10 and 8)"
+    "return my printstring (optimized for bases 16, 10 and 8).
+     Print digits > 0 as uppercase chars if base > 0,
+     as lowercase chars if base < 0."
 
     |s|
 
 %{
 #ifdef __SCHTEAM__
-    int __base = base.intValue();
+    int __base = base.intValue().abs();
     long myValue = self.longValue();
     java.lang.String __s;
 
     switch (__base) {
-	case 2:
-	    __s = java.lang.Long.toBinaryString(myValue);
-	    break;
-
-	case 8:
-	    __s = java.lang.Long.toOctalString(myValue);
-	    break;
-
-	case 10:
-	    __s = java.lang.Long.toString(myValue);
-	    break;
-
-	case 16:
-	    __s = java.lang.Long.toHexString(myValue);
-	    break;
-
-	default:
-	{
-	    boolean negative = false;
-	    __s = "";
-
-	    if ((__base > 36) || (__base < 2)) {
-		throw new SmalltalkError("invalid base: ", base);
-	    }
-	    if (myValue < 0) {
-		negative = true;
-		myValue = -myValue;
-	    }
-	    while (myValue != 0) {
-		int digit;
-		char ch;
-
-		digit = (int)(myValue % __base);
-		if (digit <= 9) {
-		    ch = (char)('0' + digit);
-		} else {
-		    ch = (char)('A' + digit - 10);
-		}
-		__s = ch + __s;
-		myValue = myValue / __base;
-	    }
-	    if (negative) {
-		__s = "-" + __s;
-	    }
-	    break;
-	}
+        case 2:
+            __s = java.lang.Long.toBinaryString(myValue);
+            break;
+
+        case 8:
+            __s = java.lang.Long.toOctalString(myValue);
+            break;
+
+        case 10:
+            __s = java.lang.Long.toString(myValue);
+            break;
+
+        case 16:
+            __s = java.lang.Long.toHexString(myValue);
+            break;
+
+        default:
+        {
+            boolean negative = false;
+            __s = "";
+
+            if ((__base > 36) || (__base < 2)) {
+                throw new SmalltalkError("invalid base: ", base);
+            }
+            if (myValue < 0) {
+                negative = true;
+                myValue = -myValue;
+            }
+            while (myValue != 0) {
+                int digit;
+                char ch;
+
+                digit = (int)(myValue % __base);
+                if (digit <= 9) {
+                    ch = (char)('0' + digit);
+                } else {
+                    ch = (char)('A' + digit - 10);
+                }
+                __s = ch + __s;
+                myValue = myValue / __base;
+            }
+            if (negative) {
+                __s = "-" + __s;
+            }
+            break;
+        }
     }
     return context._RETURN( new STString( __s ));
 #else
-    static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
-    INT __base;
+    static char ucDigits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+    static char lcDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
 
     if (__isSmallInteger(base)) {
-	if (self == __MKSMALLINT(0)) {
-	    RETURN (@global(ZeroString));
-    //        RETURN (__MKSTRING_L("0", 1));
-	}
-	__base = __intVal(base);
-
-	if ((__base < sizeof(digits)) && (__base > 1)) {
-	    char buffer[64+3];  /* for 64bit machines, base 2, plus sign, plus 0-byte */
-	    char *cp;
-	    OBJ newString;
-	    int negative = 0;
-	    INT myValue = __intVal(self);
-
-	    if (myValue < 0) {
-		negative = 1;
-		myValue = -myValue;
-	    }
-	    cp = buffer + sizeof(buffer) - 1;
-	    *cp-- = '\0';
-	    for (; myValue != 0; cp--) {
-		*cp = digits[myValue % __base];
-		myValue /= __base;
-	    }
-	    if (negative) {
-		*cp-- = '-';
-	    }
-	    newString = __MKSTRING_L(cp+1, (buffer + sizeof(buffer) - 2 - cp));
-	    if (newString != nil) {
-		RETURN (newString);
-	    }
-	}
+        char *digits;
+        INT __base;
+
+        if (self == __MKSMALLINT(0)) {
+            RETURN (@global(ZeroString));
+        }
+        __base = __intVal(base);
+        if (__base < 0) {
+            __base = - __base;
+            digits = lcDigits;
+        } else {
+            digits = ucDigits;
+        }
+                
+        if ((__base < sizeof(ucDigits)) && (__base > 1)) {
+            char buffer[64+3];  /* for 64bit machines, base 2, plus sign, plus 0-byte */
+            char *cp;
+            OBJ newString;
+            int negative = 0;
+            INT myValue = __intVal(self);
+
+            if (myValue < 0) {
+                negative = 1;
+                myValue = -myValue;
+            }
+            cp = buffer + sizeof(buffer) - 1;
+            *cp-- = '\0';
+            for (; myValue != 0; cp--) {
+                *cp = digits[myValue % __base];
+                myValue /= __base;
+            }
+            if (negative) {
+                *cp-- = '-';
+            }
+            newString = __MKSTRING_L(cp+1, (buffer + sizeof(buffer) - 2 - cp));
+            if (newString != nil) {
+                RETURN (newString);
+            }
+        }
     }
 #endif /* not __SCHTEAM__ */
 %}.
@@ -4466,6 +4475,7 @@
 
     "
       127 printStringRadix:16
+      127 printStringRadix:-16
       123 printStringRadix:12
       123 printStringRadix:10
       123 printStringRadix:8