Created
September 16, 2010 04:11
-
-
Save bmeck/581949 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//no whitespace whole number json array parsing | |
function SavedStateNumberArrayParser() { | |
this.state = "unopened" | |
this.value = undefined | |
} | |
SavedStateNumberArrayParser.prototype.arr = function() { | |
return this.state === "error" ? undefined : this.value | |
} | |
SavedStateNumberArrayParser.prototype.reset = function() { | |
this.state = this.state === "closed" || typeof this.value === "undefined" ? "unopened" : "inside" | |
if(this.state === "unopened") this.value = undefined | |
return this.state | |
} | |
SavedStateNumberArrayParser.prototype.parse = function(str) { | |
for(var i=0;i<str.length;i++) switch(this.state) { | |
case "unopened": | |
if(str.charAt(i)==="[") { | |
this.state = "inside" | |
} | |
else { | |
this.state = "error" | |
} | |
break | |
case "continueinside": | |
//shares all attributes with next except "," | |
if(str.charAt(i)===",") { | |
this.state = "error"; | |
break | |
} | |
case "inside": | |
if(str.charAt(i)===",") { | |
this.value.push(0) | |
} | |
else if(str.charAt(i)==="]") { | |
this.state = "closed" | |
} | |
else if(/\d/.test(str.charAt(i))) { | |
this.value = this.value || [0] | |
this.value[this.value.length] = this.value[this.value.length] * 10 + Number(str.charAt(i)) | |
break | |
} | |
case "closed": | |
default: this.state = "error" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Client() { | |
this.state = "loggedoff" | |
} | |
//attempt to say something, will attempt login every time and keep you at as far as you have gotten | |
Client.prototype.say = function(msg) { | |
switch(this.state) { | |
case "loggedoff": | |
this.queueLogin() | |
case "authenticated": | |
this.queueVoiceRequest() | |
case "voiced": | |
this.queueMessage(msg) | |
break | |
//banned etc. | |
default: | |
throw "not allowed" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment