Skip to content

Instantly share code, notes, and snippets.

@torus
Created August 4, 2012 05:01
Show Gist options
  • Select an option

  • Save torus/3254639 to your computer and use it in GitHub Desktop.

Select an option

Save torus/3254639 to your computer and use it in GitHub Desktop.
function zero(str) {
var m
if (m = str.match(/^0(?!\d)(.*)/)) {
return m[1]
}
throw "zero"
}
function decimal(str) {
var m
try {
return zero(str)
} catch (e) {
if (m = str.match(/^[1-9]\d*(.*)$/)) {
return m[1]
} else {
throw "decimal"
}
}
}
function hex(str) {
var m
try {
return zero(str)
} catch (e) {
if (m = str.match(/^[1-9a-x][0-9a-x]*(.*)$/i)) {
return m[1]
} else {
throw "hex"
}
}
}
function machex(str) {
var m
if (m = str.match(/^\xhh(.*)$/)) {
return m[1]
} else {
throw "machex"
}
}
function dash(str) {
if (m = str.match(/^-(.*)/)) {
return m[1]
}
throw "dash"
}
function colon(str) {
if (m = str.match(/^:(.*)/)) {
return m[1]
}
throw "colon"
}
function dot(str) {
if (m = str.match(/^\.(.*)/)) {
return m[1]
}
return "dot"
}
try {
decimal("0123")
decimal("123:hoge")
decimal("0")
} catch (e) {
console.log("fail", e)
}
function ipv4(str) {
try {
decimal(dot(decimal(dot(decimal(dot(decimal(str)))))))
return true
} catch (e) {
throw "ipv4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment