Skip to content

Instantly share code, notes, and snippets.

@bmeck
Created September 16, 2010 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bmeck/581949 to your computer and use it in GitHub Desktop.
Save bmeck/581949 to your computer and use it in GitHub Desktop.
//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"
}
}
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