Calculates the byte length of a string encoded as UTF8.
(string)
| 340 | |
| 341 | /** Calculates the byte length of a string encoded as UTF8. */ |
| 342 | function utf8Length(string) { |
| 343 | var len = 0, |
| 344 | c = 0; |
| 345 | for (var i = 0; i < string.length; ++i) { |
| 346 | c = string.charCodeAt(i); |
| 347 | if (c < 128) len += 1; |
| 348 | else if (c < 2048) len += 2; |
| 349 | else if ( |
| 350 | (c & 0xfc00) === 0xd800 && |
| 351 | (string.charCodeAt(i + 1) & 0xfc00) === 0xdc00 |
| 352 | ) { |
| 353 | ++i; |
| 354 | len += 4; |
| 355 | } else len += 3; |
| 356 | } |
| 357 | return len; |
| 358 | } |
| 359 | |
| 360 | /** Converts a string to an array of UTF8 bytes. */ |
| 361 | function utf8Array(string) { |