Created
November 4, 2014 13:43
-
-
Save missett/de7f2bef10aa55544855 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
function pop(input) { | |
var i = input.indexOf('(') + 1, // Start of out outermost s-expression | |
j = -1, // Will be set to the index of the matching ) to close the outsermost s-expression | |
l = 1, // Nesting 'stack counter' to make sure we get the outermost expression only | |
k = 0; // Loop index | |
if(i < 0) | |
return input; | |
for(k=i; k<input.length; k++) { | |
if(input[k] === '(') { | |
l += 1; | |
} else if(input[k] === ')') { | |
l -= 1; | |
} | |
if(l === 0) { | |
j = k; | |
break; | |
} | |
} | |
if(j < 0) | |
return input; | |
//first s-expression, after first s-expression | |
return [input.slice(0,i-1), input.slice(i,j), input.slice(j+1)]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment