1
0
ソースを参照

Fast random strings usable as identifiers.

Herbert Vojčík 11 年 前
コミット
cdeecbc965
3 ファイル変更85 行追加2 行削除
  1. 31 1
      js/Kernel-Collections.deploy.js
  2. 41 1
      js/Kernel-Collections.js
  3. 13 0
      st/Kernel-Collections.st

+ 31 - 1
js/Kernel-Collections.deploy.js

@@ -1,4 +1,4 @@
-smalltalk.addPackage('Kernel-Collections', {});
+smalltalk.addPackage('Kernel-Collections');
 smalltalk.addClass('Association', smalltalk.Object, ['key', 'value'], 'Kernel-Collections');
 smalltalk.addMethod(
 "__eq",
@@ -3254,6 +3254,36 @@ return self}, function($ctx1) {$ctx1.fill(self,"lf",{}, smalltalk.String.klass)}
 messageSends: []}),
 smalltalk.String.klass);
 
+smalltalk.addMethod(
+"_random",
+smalltalk.method({
+selector: "random",
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);;
+return self}, function($ctx1) {$ctx1.fill(self,"random",{}, smalltalk.String.klass)})},
+messageSends: []}),
+smalltalk.String.klass);
+
+smalltalk.addMethod(
+"_randomNotIn_",
+smalltalk.method({
+selector: "randomNotIn:",
+fn: function (aString){
+var self=this;
+var result;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+_st((function(){
+return smalltalk.withContext(function($ctx2) {
result=_st(self)._random();
+result;
+return _st(aString)._includesSubString_(result);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))._whileTrue();
+$1=result;
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"randomNotIn:",{aString:aString,result:result}, smalltalk.String.klass)})},
+messageSends: ["whileTrue", "random", "includesSubString:"]}),
+smalltalk.String.klass);
+
 smalltalk.addMethod(
 "_space",
 smalltalk.method({

+ 41 - 1
js/Kernel-Collections.js

@@ -1,4 +1,4 @@
-smalltalk.addPackage('Kernel-Collections', {});
+smalltalk.addPackage('Kernel-Collections');
 smalltalk.addClass('Association', smalltalk.Object, ['key', 'value'], 'Kernel-Collections');
 smalltalk.Association.comment="I represent a pair of associated objects, a key and a value. My instances can serve as entries in a dictionary.\x0a\x0aInstances can be created with the class-side method `#key:value:`"
 smalltalk.addMethod(
@@ -4323,6 +4323,46 @@ referencedClasses: []
 }),
 smalltalk.String.klass);
 
+smalltalk.addMethod(
+"_random",
+smalltalk.method({
+selector: "random",
+category: 'random',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);;
+return self}, function($ctx1) {$ctx1.fill(self,"random",{}, smalltalk.String.klass)})},
+args: [],
+source: "random\x0a\x09\x22Returns random alphanumeric string beginning with letter\x22\x0a\x09<return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.String.klass);
+
+smalltalk.addMethod(
+"_randomNotIn_",
+smalltalk.method({
+selector: "randomNotIn:",
+category: 'random',
+fn: function (aString){
+var self=this;
+var result;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+_st((function(){
+return smalltalk.withContext(function($ctx2) {
result=_st(self)._random();
+result;
+return _st(aString)._includesSubString_(result);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))._whileTrue();
+$1=result;
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"randomNotIn:",{aString:aString,result:result}, smalltalk.String.klass)})},
+args: ["aString"],
+source: "randomNotIn: aString\x0a\x09| result |\x0a    [ result := self random. aString includesSubString: result ] whileTrue.\x0a    ^result",
+messageSends: ["whileTrue", "random", "includesSubString:"],
+referencedClasses: []
+}),
+smalltalk.String.klass);
+
 smalltalk.addMethod(
 "_space",
 smalltalk.method({

+ 13 - 0
st/Kernel-Collections.st

@@ -1400,6 +1400,19 @@ value: aUTFCharCode
 	<return String.fromCharCode(aUTFCharCode);>
 ! !
 
+!String class methodsFor: 'random'!
+
+random
+	"Returns random alphanumeric string beginning with letter"
+	<return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);>
+!
+
+randomNotIn: aString
+	| result |
+    [ result := self random. aString includesSubString: result ] whileTrue.
+    ^result
+! !
+
 CharacterArray subclass: #Symbol
 	instanceVariableNames: ''
 	package: 'Kernel-Collections'!