Merge "Fixed dependencies for jquery.collapsibleTabs"
[lhc/web/wiklou.git] / resources / jquery / jquery.byteLength.js
1 /**
2 * jQuery.byteLength
3 *
4 * Calculate the byte length of a string (accounting for UTF-8).
5 *
6 * @author Jan Paul Posma, 2011
7 */
8 jQuery.byteLength = function ( str ) {
9
10 // This basically figures out how many bytes a UTF-16 string (which is what js sees)
11 // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that.
12 // Note, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's two of them
13 // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in
14 // edge cases such as illegal sequences, but that should never happen.
15 return str
16 .replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' )
17 .replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' )
18 .length;
19 };