Changed toStrBase() to accept a number of bits rather than a number of bytes, because bytes aren't always 8 bits (see PDP-10)

This commit is contained in:
Jeff Parsons 2017-02-24 10:58:56 -08:00 committed by Jeff Parsons
commit c0de732f76
14 changed files with 260 additions and 239 deletions

View file

@ -647,31 +647,42 @@ class Debugger extends Component {
}
/**
* toStrBase(n, nBytes, fStripLeadingZeros)
* toStrBase(n, nBits)
*
* Use this instead of Str.toHex() or Str.toOct() to convert bytes/words to the Debugger's default base.
* Use this instead of Str's toOct()/toDec()/toHex() to convert numbers to the Debugger's default base.
*
* TODO: The 32-bit limitation on n is imposed by the Str functions we call, not us. Consider modifying
* those functions to support a higher number of bits (eg, 36), using arithmetic operators instead of bit-wise
* operators, and increasing the maximum number of supported bits to at least 52, while still using JavaScript
* floating-point numbers as the underlying data type.
*
* For now, the only component that really cares about supporting more than 32 bits (eg, 36 bits) is the PDP-10
* Debugger, which currently uses its own functions (eg, toStrWord()) to divide quantities into smaller (eg, 18-bit)
* values.
*
* @this {Debugger}
* @param {number|null|undefined} n
* @param {number} [nBytes] is the number of bytes to display, which we translate into a number of characters
* @param {boolean} [fStripLeadingZeros]
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [nBits] (-1 to strip leading zeros, 0 to allow a variable number of digits)
* @return {string}
*/
toStrBase(n, nBytes = 0, fStripLeadingZeros = false) {
toStrBase(n, nBits = 0) {
var s;
switch(this.nBase) {
case 8:
s = Str.toOct(n, nBytes * 3 /* - (nBytes > 2? 1 : 0) */);
s = Str.toOct(n, nBits > 0? ((nBits + 2)/3)|0 : 0);
break;
case 10:
s = n.toString();
/*
* The multiplier is actually Math.log(2)/Math.log(10), but an approximation is more than adequate.
*/
s = Str.toDec(n, nBits > 0? Math.ceil(nBits * 0.3) : 0);
break;
case 16:
default:
s = Str.toHex(n, nBytes * 2);
s = Str.toHex(n, nBits > 0? ((nBits + 3) >> 2) : 0);
break;
}
return (fStripLeadingZeros? Str.stripLeadingZeros(s) : s);
return (nBits < 0? Str.stripLeadingZeros(s) : s);
}
}

View file

@ -143,7 +143,7 @@ class Str {
*
* Converts an integer to binary, with the specified number of digits (up to the default of 32).
*
* @param {number|null|undefined} n is a 32-bit value
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [cch] is the desired number of binary digits (32 is both the default and the maximum)
* @param {number} [grouping]
* @return {string} the binary representation of n
@ -171,7 +171,7 @@ class Str {
s = "," + s;
group = grouping;
}
s = (fInvalid ? '?' : ((n & 0x1) ? '1' : '0')) + s;
s = (fInvalid? '?' : ((n & 0x1)? '1' : '0')) + s;
n >>= 1;
group--;
}
@ -183,7 +183,7 @@ class Str {
*
* Converts an integer to binary, with the specified number of bytes (up to the default of 4).
*
* @param {number|null|undefined} n is a 32-bit value
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [cb] is the desired number of binary bytes (4 is both the default and the maximum)
* @param {boolean} [fPrefix]
* @return {string} the binary representation of n
@ -197,7 +197,7 @@ class Str {
s = Str.toBin(n & 0xff, 8) + s;
n >>= 8;
}
return (fPrefix ? "0b" : "") + s;
return (fPrefix? "0b" : "") + s;
}
/**
@ -209,7 +209,7 @@ class Str {
* doesn't properly convert negative values. Moreover, if n is undefined, n.toString() will throw
* an exception, whereas this function will return '?' characters.
*
* @param {number|null|undefined} n is a 32-bit value
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [cch] is the desired number of octal digits (0 or undefined for default of either 6 or 11)
* @param {boolean} [fPrefix]
* @return {string} the octal representation of n
@ -221,7 +221,7 @@ class Str {
if (cch) {
if (cch > 11) cch = 11;
} else {
cch = (n & ~0xffffff) ? 11 : ((n & ~0xffff) ? 8 : 6);
cch = (n & ~0xffffff)? 11 : ((n & ~0xffff)? 8 : 6);
}
/*
* An initial "falsey" check for null takes care of both null and undefined;
@ -240,7 +240,7 @@ class Str {
n >>= 3;
}
}
return (fPrefix ? "0o" : "") + s;
return (fPrefix? "0o" : "") + s;
}
/**
@ -252,7 +252,7 @@ class Str {
* doesn't properly convert negative values. Moreover, if n is undefined, n.toString() will throw
* an exception, whereas this function will return '?' characters.
*
* @param {number|null|undefined} n is a 32-bit value
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [cch] is the desired number of decimal digits (0 or undefined for default of either 5 or 10)
* @return {string} the octal representation of n
*/
@ -263,7 +263,7 @@ class Str {
if (cch) {
if (cch > 10) cch = 10;
} else {
cch = (n & ~0xffff) ? 10 : 5;
cch = (n & ~0xffff)? 10 : 5;
}
/*
* An initial "falsey" check for null takes care of both null and undefined;
@ -302,7 +302,7 @@ class Str {
* s = "00000000".substr(0, 8 - s.length) + s;
* s = s.substr(0, cch).toUpperCase();
*
* @param {number|null|undefined} n is a 32-bit value
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [cch] is the desired number of hex digits (0 or undefined for default of either 4 or 8)
* @param {boolean} [fPrefix]
* @return {string} the hex representation of n
@ -314,7 +314,7 @@ class Str {
if (cch) {
if (cch > 8) cch = 8;
} else {
cch = (n & ~0xffff) ? 8 : 4;
cch = (n & ~0xffff)? 8 : 4;
}
/*
* An initial "falsey" check for null takes care of both null and undefined;
@ -329,12 +329,12 @@ class Str {
} else {
while (cch-- > 0) {
var d = n & 0xf;
d += (d >= 0 && d <= 9 ? 0x30 : 0x41 - 10);
d += (d >= 0 && d <= 9? 0x30 : 0x41 - 10);
s = String.fromCharCode(d) + s;
n >>= 4;
}
}
return (fPrefix ? "0x" : "") + s;
return (fPrefix? "0x" : "") + s;
}
/**
@ -492,7 +492,7 @@ class Str {
* and the hyphen is last, you can avoid escaping those as well.
*/
k = k.replace(/([\\[\]*{}().+?])/g, "\\$1");
sMatch += (sMatch ? '|' : '') + k;
sMatch += (sMatch? '|' : '') + k;
}
return s.replace(new RegExp('(' + sMatch + ')', "g"), function(m)
{
@ -513,7 +513,7 @@ class Str {
static pad(s, cch, fPadLeft)
{
var sPadding = " ";
return fPadLeft ? (sPadding + s).slice(-cch) : (s + sPadding).slice(0, cch);
return fPadLeft? (sPadding + s).slice(-cch) : (s + sPadding).slice(0, cch);
}
/**