MCPcopy Index your code
hub / github.com/dcodeIO/bcrypt.js / utf8Length

Function utf8Length

index.js:342–358  ·  view source on GitHub ↗

Calculates the byte length of a string encoded as UTF8.

(string)

Source from the content-addressed store, hash-verified

340
341/** Calculates the byte length of a string encoded as UTF8. */
342function 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. */
361function utf8Array(string) {

Callers 2

truncatesFunction · 0.85
utf8ArrayFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected