1
0
فهرست منبع

Add bitAnd: bitOr: bitXor: and printStringBase: to Number

Number supports & and | already, which are now synonyms of bitAnd: and
bitOr: respectively.

printStringBase: returns the number as a string in the given base.

All names lifted from Pharo.
Braden Shepherdson 6 سال پیش
والد
کامیت
c7be6bd581
4فایلهای تغییر یافته به همراه318 افزوده شده و 0 حذف شده
  1. 96 0
      src/Kernel-Objects.js
  2. 16 0
      src/Kernel-Objects.st
  3. 180 0
      src/Kernel-Tests.js
  4. 26 0
      src/Kernel-Tests.st

+ 96 - 0
src/Kernel-Objects.js

@@ -3803,6 +3803,78 @@ messageSends: ["and:", ">=", "<="]
 }),
 $globals.Number);
 
+$core.addMethod(
+$core.method({
+selector: "bitAnd:",
+protocol: "converting",
+fn: function (aNumber){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+return self & aNumber;
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"bitAnd:",{aNumber:aNumber},$globals.Number)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aNumber"],
+source: "bitAnd: aNumber\x0a\x09<inlineJS: 'return self & aNumber'>",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: []
+}),
+$globals.Number);
+
+$core.addMethod(
+$core.method({
+selector: "bitOr:",
+protocol: "converting",
+fn: function (aNumber){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+return self | aNumber;
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"bitOr:",{aNumber:aNumber},$globals.Number)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aNumber"],
+source: "bitOr: aNumber\x0a\x09<inlineJS: 'return self | aNumber'>",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: []
+}),
+$globals.Number);
+
+$core.addMethod(
+$core.method({
+selector: "bitXor:",
+protocol: "converting",
+fn: function (aNumber){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+return self ^ aNumber;
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"bitXor:",{aNumber:aNumber},$globals.Number)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aNumber"],
+source: "bitXor: aNumber\x0a\x09<inlineJS: 'return self ^ aNumber'>",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: []
+}),
+$globals.Number);
+
 $core.addMethod(
 $core.method({
 selector: "ceiling",
@@ -4304,6 +4376,30 @@ messageSends: []
 }),
 $globals.Number);
 
+$core.addMethod(
+$core.method({
+selector: "printStringBase:",
+protocol: "converting",
+fn: function (aBase){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+return self.toString(aBase);
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"printStringBase:",{aBase:aBase},$globals.Number)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aBase"],
+source: "printStringBase: aBase\x0a\x09<inlineJS: 'return self.toString(aBase)'>",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: []
+}),
+$globals.Number);
+
 $core.addMethod(
 $core.method({
 selector: "radiansToDegrees",

+ 16 - 0
src/Kernel-Objects.st

@@ -888,6 +888,18 @@ atRandom
 	^ (Random new next * self) truncated + 1
 !
 
+bitAnd: aNumber
+	<inlineJS: 'return self & aNumber'>
+!
+
+bitOr: aNumber
+	<inlineJS: 'return self | aNumber'>
+!
+
+bitXor: aNumber
+	<inlineJS: 'return self ^ aNumber'>
+!
+
 ceiling
 	<inlineJS: 'return Math.ceil(self);'>
 !
@@ -900,6 +912,10 @@ floor
 	<inlineJS: 'return Math.floor(self);'>
 !
 
+printStringBase: aBase
+	<inlineJS: 'return self.toString(aBase)'>
+!
+
 radiansToDegrees
 	^ self / Number radiansPerDegree
 !

+ 180 - 0
src/Kernel-Tests.js

@@ -12263,6 +12263,137 @@ messageSends: ["assert:", "between:and:", "not"]
 }),
 $globals.NumberTest);
 
+$core.addMethod(
+$core.method({
+selector: "testBitAnd",
+protocol: "tests",
+fn: function (){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1,$2;
+$1=(15)._bitAnd_((2));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["bitAnd:"]=1;
+//>>excludeEnd("ctx");
+$self._assert_equals_($1,(2));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=1;
+//>>excludeEnd("ctx");
+$2=(15)._bitAnd_((15));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["bitAnd:"]=2;
+//>>excludeEnd("ctx");
+$self._assert_equals_($2,(15));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=2;
+//>>excludeEnd("ctx");
+$self._assert_equals_((-1)._bitAnd_((1021)),(1021));
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"testBitAnd",{},$globals.NumberTest)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "testBitAnd\x0a\x09self assert: (15 bitAnd: 2) equals: 2.\x0a\x09self assert: (15 bitAnd: 15) equals: 15.\x0a\x09self assert: (-1 bitAnd: 1021) equals: 1021",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: ["assert:equals:", "bitAnd:"]
+}),
+$globals.NumberTest);
+
+$core.addMethod(
+$core.method({
+selector: "testBitOr",
+protocol: "tests",
+fn: function (){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1,$2;
+$1=(2)._bitOr_((4));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["bitOr:"]=1;
+//>>excludeEnd("ctx");
+$self._assert_equals_($1,(6));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=1;
+//>>excludeEnd("ctx");
+$2=(7)._bitOr_((2));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["bitOr:"]=2;
+//>>excludeEnd("ctx");
+$self._assert_equals_($2,(7));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=2;
+//>>excludeEnd("ctx");
+$self._assert_equals_((-1)._bitOr_((1021)),(-1));
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"testBitOr",{},$globals.NumberTest)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "testBitOr\x0a\x09self assert: (2 bitOr: 4) equals: 6.\x0a\x09self assert: (7 bitOr: 2) equals: 7.\x0a\x09self assert: (-1 bitOr: 1021) equals: -1",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: ["assert:equals:", "bitOr:"]
+}),
+$globals.NumberTest);
+
+$core.addMethod(
+$core.method({
+selector: "testBitXor",
+protocol: "tests",
+fn: function (){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1,$2,$3;
+$1=(2)._bitXor_((4));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["bitXor:"]=1;
+//>>excludeEnd("ctx");
+$self._assert_equals_($1,(6));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=1;
+//>>excludeEnd("ctx");
+$2=(7)._bitXor_((2));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["bitXor:"]=2;
+//>>excludeEnd("ctx");
+$self._assert_equals_($2,(5));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=2;
+//>>excludeEnd("ctx");
+$3=(-1)._bitXor_((1021));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["bitXor:"]=3;
+//>>excludeEnd("ctx");
+$self._assert_equals_($3,(-1022));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=3;
+//>>excludeEnd("ctx");
+$self._assert_equals_((91)._bitXor_((91)),(0));
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"testBitXor",{},$globals.NumberTest)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "testBitXor\x0a\x09self assert: (2 bitXor: 4) equals: 6.\x0a\x09self assert: (7 bitXor: 2) equals: 5.\x0a\x09self assert: (-1 bitXor: 1021) equals: -1022.\x0a\x09self assert: (91 bitXor: 91) equals: 0",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: ["assert:equals:", "bitXor:"]
+}),
+$globals.NumberTest);
+
 $core.addMethod(
 $core.method({
 selector: "testCeiling",
@@ -13484,6 +13615,55 @@ messageSends: ["assert:equals:", "printShowingDecimalPlaces:", "negated"]
 }),
 $globals.NumberTest);
 
+$core.addMethod(
+$core.method({
+selector: "testPrintStringBase",
+protocol: "tests",
+fn: function (){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1,$2,$3;
+$1=(15)._printStringBase_((2));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["printStringBase:"]=1;
+//>>excludeEnd("ctx");
+$self._assert_equals_($1,"1111");
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=1;
+//>>excludeEnd("ctx");
+$2=(15)._printStringBase_((16));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["printStringBase:"]=2;
+//>>excludeEnd("ctx");
+$self._assert_equals_($2,"f");
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=2;
+//>>excludeEnd("ctx");
+$3=(256)._printStringBase_((16));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["printStringBase:"]=3;
+//>>excludeEnd("ctx");
+$self._assert_equals_($3,"100");
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=3;
+//>>excludeEnd("ctx");
+$self._assert_equals_((256)._printStringBase_((2)),"100000000");
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"testPrintStringBase",{},$globals.NumberTest)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "testPrintStringBase\x0a\x09self assert: (15 printStringBase: 2) equals: '1111'.\x0a\x09self assert: (15 printStringBase: 16) equals: 'f'.\x0a\x09self assert: (256 printStringBase: 16) equals: '100'.\x0a\x09self assert: (256 printStringBase: 2) equals: '100000000'",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: ["assert:equals:", "printStringBase:"]
+}),
+$globals.NumberTest);
+
 $core.addMethod(
 $core.method({
 selector: "testRadiansToDegrees",

+ 26 - 0
src/Kernel-Tests.st

@@ -2278,6 +2278,25 @@ testBetweenAnd
 	self assert: (1 between: 1 and: 1).
 !
 
+testBitAnd
+	self assert: (15 bitAnd: 2) equals: 2.
+	self assert: (15 bitAnd: 15) equals: 15.
+	self assert: (-1 bitAnd: 1021) equals: 1021
+!
+
+testBitOr
+	self assert: (2 bitOr: 4) equals: 6.
+	self assert: (7 bitOr: 2) equals: 7.
+	self assert: (-1 bitOr: 1021) equals: -1
+!
+
+testBitXor
+	self assert: (2 bitXor: 4) equals: 6.
+	self assert: (7 bitXor: 2) equals: 5.
+	self assert: (-1 bitXor: 1021) equals: -1022.
+	self assert: (91 bitXor: 91) equals: 0
+!
+
 testCeiling
 	self assert: 1.2 ceiling equals: 2.
 	self assert: -1.2 ceiling equals: -1.
@@ -2432,6 +2451,13 @@ testPrintShowingDecimalPlaces
 	self assert: (0 printShowingDecimalPlaces: 2) equals: '0.00'.
 !
 
+testPrintStringBase
+	self assert: (15 printStringBase: 2) equals: '1111'.
+	self assert: (15 printStringBase: 16) equals: 'f'.
+	self assert: (256 printStringBase: 16) equals: '100'.
+	self assert: (256 printStringBase: 2) equals: '100000000'
+!
+
 testRadiansToDegrees
 	self assert: (Number pi radiansToDegrees - 180) abs <= 0.01.
 !