Created
July 25, 2012 12:47
-
-
Save FGRibreau/3175997 to your computer and use it in GitHub Desktop.
Extract arguments from a string (quoted or not)
This file contains hidden or 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 extractArgs(str){ | |
var args = [], buff = '', idx = -1, insideQuote = false, lgth = str.length; | |
function next(){return str.charAt(++idx);} | |
function peak(){return str.charAt(idx+1);} | |
function resetState(){insideQuote = false;buff = '';} | |
while(idx < lgth){ | |
var c = next(); | |
if(c === ' ' && !insideQuote){ | |
if(buff.length === 0){continue;} | |
args.push(buff); | |
resetState(); | |
continue; | |
} | |
if(c === '\\' && insideQuote){ | |
buff += next(); | |
continue; | |
} | |
if(c === "'" || c === '"'){ | |
if(!insideQuote){// enter | |
insideQuote = true; | |
continue; | |
} else {// leave | |
insideQuote = false; | |
args.push(buff); | |
resetState(); | |
continue; | |
} | |
} | |
buff += c; | |
} | |
if(buff.length > 0){args.push(buff);} | |
return args; | |
} | |
console.log(extractArgs("bonjour ça 'va' \"hello world\" \"c\\\"est ça !\" \"hey hey\"")); | |
// [ 'bonjour', 'ça', 'va', 'hello world', 'c"est ça !', 'hey hey' ] |
WebReflection
commented
Jul 25, 2012
in case the order is not relevant ... what you wrote makes sense in C, not in JS, imho
the faster way is the use of regexp, here the tests http://jsperf.com/extract
for me and with chrome the fastest one is without regexp :)
In fact, I optimized my JavaScript code for speed ("v2") and as you can see it's faster than regexp http://jsperf.com/extract/4 while still holding the order of values (which was a requirement /cc @WebReflection)
up to 10% faster, and we are talking about 250.000 and more per second, against up to 80% more code ? As I have said, that is a good way in C, not in JS ... but of course if code size is not a concern, many regexps can be replaced by procedural code
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment