-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b210ea8
commit 69fc62f
Showing
10 changed files
with
6,270 additions
and
2,692 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
export const args = (str: string): string[] => { | ||
const args = []; | ||
str = str.trim(); | ||
let currentArg = ''; | ||
for (let i = 0; i < str.length; i++) { | ||
switch (str[i]) { | ||
case '\'': | ||
const endQuoteIndex = str.indexOf('\'', i+1); | ||
if (endQuoteIndex < 0) { | ||
throw 'single quote not closed'; | ||
} | ||
currentArg = currentArg + str.substring(i+1, endQuoteIndex); | ||
i = endQuoteIndex; | ||
break; | ||
case '"': | ||
// Double quotes can contain escaped characters | ||
for (i++; i < str.length && str[i] !== '"'; i++) { | ||
if (str[i] === '\\' && (i+1) < str.length) { | ||
i++; | ||
switch (str[i]) { | ||
case 'n': | ||
currentArg += '\n'; | ||
break; | ||
case 'r': | ||
currentArg += '\r'; | ||
break; | ||
case 't': | ||
currentArg += '\t'; | ||
break; | ||
default: | ||
currentArg += str[i]; | ||
} | ||
} else { | ||
currentArg += str[i]; | ||
} | ||
} | ||
if (i >= str.length) { | ||
throw 'double quote not closed'; | ||
} | ||
break; | ||
case ' ': | ||
case '\t': | ||
args.push(currentArg); | ||
currentArg = ''; | ||
while (i < str.length && (str[i] === ' ' || str[i] === '\t')) { | ||
i++; | ||
} | ||
// We will have advanced to the next non-whitespace | ||
i--; | ||
break; | ||
case '\\': | ||
i++; | ||
if (i < str.length) { | ||
currentArg = currentArg + str[i]; | ||
} else { | ||
throw 'uncompleted escape character'; | ||
} | ||
break; | ||
default: | ||
currentArg = currentArg + str[i]; | ||
break; | ||
} | ||
} | ||
if (currentArg != '') { | ||
args.push(currentArg); | ||
} | ||
return args; | ||
} |
Oops, something went wrong.