* @return {string}
*/
trimToByteLength = function ( s, length ) {
- var byteLength, chopOffChars, chopOffBytes;
-
- // bytelength is always greater or equal to the length in characters
- s = s.substr( 0, length );
- while ( ( byteLength = $.byteLength( s ) ) > length ) {
- // Calculate how many characters can be safely removed
- // First, we need to know how many bytes the string exceeds the threshold
- chopOffBytes = byteLength - length;
- // A character in UTF-8 is at most 4 bytes
- // One character must be removed in any case because the
- // string is too long
- chopOffChars = Math.max( 1, Math.floor( chopOffBytes / 4 ) );
- s = s.substr( 0, s.length - chopOffChars );
- }
- return s;
+ return $.trimByteLength( '', s, length ).newVal;
},
/**