Browse Source

Merge pull request #400 from herby/random-strings

Simple alphanum random strings.
Nicolas Petton 11 years ago
parent
commit
18060aefbe
3 changed files with 83 additions and 0 deletions
  1. 30 0
      js/Kernel-Collections.deploy.js
  2. 40 0
      js/Kernel-Collections.js
  3. 13 0
      st/Kernel-Collections.st

+ 30 - 0
js/Kernel-Collections.deploy.js

@@ -3034,6 +3034,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({

+ 40 - 0
js/Kernel-Collections.js

@@ -4103,6 +4103,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'!