Merge "Disabling profiling in the debug toolbar for ProfilerSimple (for now)"
[lhc/web/wiklou.git] / resources / src / 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 * @author Timo Tijhof, 2012
8 * @author David Chan, 2013
9 */
10 jQuery.byteLength = function ( str ) {
11
12 // This basically figures out how many bytes a UTF-16 string (which is what js sees)
13 // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that.
14 // Note, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's two of them
15 // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in
16 // edge cases such as illegal sequences, but that should never happen.
17
18 // https://en.wikipedia.org/wiki/UTF-8#Description
19 // The mapping from UTF-16 code units to UTF-8 bytes is as follows:
20 // > Range 0000-007F: codepoints that become 1 byte of UTF-8
21 // > Range 0080-07FF: codepoints that become 2 bytes of UTF-8
22 // > Range 0800-D7FF: codepoints that become 3 bytes of UTF-8
23 // > Range D800-DFFF: Surrogates (each pair becomes 4 bytes of UTF-8)
24 // > Range E000-FFFF: codepoints that become 3 bytes of UTF-8 (continued)
25
26 return str
27 .replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' )
28 .replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' )
29 .length;
30
31 };