Skip to content

Instantly share code, notes, and snippets.

@FGRibreau
Created July 25, 2012 12:47
Show Gist options
  • Save FGRibreau/3175997 to your computer and use it in GitHub Desktop.
Save FGRibreau/3175997 to your computer and use it in GitHub Desktop.
Extract arguments from a string (quoted or not)
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
Copy link

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