unicode-wordbreak.js revision 82a0ee460bbaf77a5d99be700ce8fffdf7173b66
/**
* Provides utility methods for splitting strings on word breaks and determining
* whether a character represents a word boundary, using the algorithm defined
* in the Unicode Text Segmentation guidelines
* (<a href="http://unicode.org/reports/tr29/#Word_Boundaries">Unicode Standard
* Annex #29</a>).
*
* @module unicode
* @submodule unicode-wordbreak
* @class Unicode.WordBreak
* @static
*/
// Constants representing code point classifications.
ALETTER = 0,
MIDNUMLET = 1,
MIDLETTER = 2,
MIDNUM = 3,
NUMERIC = 4,
CR = 5,
LF = 6,
NEWLINE = 7,
EXTEND = 8,
FORMAT = 9,
KATAKANA = 10,
EXTENDNUMLET = 11,
OTHER = 12,
// RegExp objects generated from code point data. Each regex matches a single
// character against a set of unicode code points. The index of each item in
// this array must match its corresponding code point constant value defined
// above.
SETS = [
],
EMPTY_STRING = '',
WHITESPACE = /\s/,
WordBreak = {
// -- Public Static Methods ------------------------------------------------
var i = 0,
word = [],
words = [],
chr,
if (!options) {
options = {};
}
if (options.ignoreCase) {
}
// Loop through each character in the classification map and determine
// whether it precedes a word boundary, building an array of distinct
// words as we go.
for (; i < len; ++i) {
// Append this character to the current word.
// If there's a word boundary between the current character and the
// next character, append the current word to the words array and
// start building a new word.
if (word &&
}
word = [];
}
}
return words;
},
},
},
// -- Protected Static Methods ---------------------------------------------
// TODO: come up with a way to memoize this function without leaking memory.
var chr,
map = [],
i = 0,
j,
set,
type;
for (; i < stringLength; ++i) {
for (j = 0; j < setsLength; ++j) {
type = j;
break;
}
}
}
return map;
},
var prevType,
// WB5. Don't break between most letters.
return false;
}
// WB6. Don't break letters across certain punctuation.
nextNextType === ALETTER) {
return false;
}
// WB7. Don't break letters across certain punctuation.
return false;
}
// adjacent to letters.
return false;
}
// WB11. Don't break inside numeric sequences like "3.2" or
// "3,456.789".
return false;
}
// WB12. Don't break inside numeric sequences like "3.2" or
// "3,456.789".
nextNextType === NUMERIC) {
return false;
}
// WB4. Ignore format and extend characters.
return false;
}
// WB3. Don't break inside CRLF.
return false;
}
// WB3a. Break before newlines (including CR and LF).
return true;
}
// WB3b. Break after newlines (including CR and LF).
return true;
}
// Break after any character not covered by the rules above.
return true;
}
};