Skip to content

Instantly share code, notes, and snippets.

@satyr
Created May 4, 2009 11:17
Show Gist options
  • Save satyr/106422 to your computer and use it in GitHub Desktop.
Save satyr/106422 to your computer and use it in GitHub Desktop.
diff --git a/ubiquity/modules/parser/new/parser.js b/ubiquity/modules/parser/new/parser.js
--- a/ubiquity/modules/parser/new/parser.js
+++ b/ubiquity/modules/parser/new/parser.js
@@ -423,43 +423,37 @@ Parser.prototype = {
// {{{words}}} and {{{delimiters}}} are just a copy of every other word in
// {{{all}}}.
//
// Used by {{{Parser.argFinder()}}}
splitWords: function(input) {
var returnObj = { words: [], delimiters: [], all: [],
beforeSpace: '', afterSpace: '' };
- // if it's all space, just return nothing. (note that \s includes \u200b)
- if (input.search(/^\s*$/) != -1)
+ // if there is no non-space character, just return nothing.
+ // (note that \S doesn't include \u200b)
+ if (!/\S/.test(input))
return returnObj;
// take all whitespace that is not of the very special no-width variety
// (\u200b) nor a East Asian full-width space (\u3000) and
// replace them with regular spaces.
- input = input.replace(/[^\S\u200b\u3000]/,' ');
+ input = input.replace(/[^\S\u200b\u3000]/g, ' ');
+
+ // this regexp with the () in it matches words but also non-words
+ // (delimiters). The odd numbered elements will be words and the even
+ // numbered ones are delimiters.
+ var splitWithWords = input.split(/(\S+)/);
- // strip whitespace from the beginning and end to ensure
- if (match = input.match(/^(\s*)(.*)(\s*)$/)) {
- input = match[2];
- returnObj.beforeSpace = match[1];
- returnObj.afterSpace = match[3];
- }
+ returnObj.afterSpace = splitWithWords.pop();
+ returnObj.beforeSpace = splitWithWords.shift();
+ returnObj.all = splitWithWords;
+ for (let i in splitWithWords)
+ returnObj[i % 2 ? 'delimiters' : 'words'].push(splitWithWords[i]);
- // this regexp with the () in it matches words but also non-words
- // (delimiters). The even numbered elements will be words and the odd
- // numbered ones are delimiters.
- let splitWithDelimiters = input.split(/(\s+)/g);
- for (let i in splitWithDelimiters) {
- returnObj.all.push(splitWithDelimiters[i]);
- if (i % 2)
- returnObj.delimiters.push(splitWithDelimiters[i]);
- else
- returnObj.words.push(splitWithDelimiters[i]);
- }
return returnObj;
},
// ** {{{Parser.hasDelimiter()}}} **
//
// Checks to see whether a certain delimiter is compatible with a certain
// verb, i.e., whether that verb has a role which takes that delimiter.
// This is done using the regex of delimiters of all roles of {{{verb}}} in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment