MCPcopy Create free account
hub / github.com/microsoft/SandDance / byteLength

Function byteLength

docs/app/js/sanddance-app.js:113119–113151  ·  view source on GitHub ↗
(string, encoding)

Source from the content-addressed store, hash-verified

113117 return buffer;
113118};
113119function byteLength(string, encoding) {
113120 if (Buffer.isBuffer(string)) return string.length;
113121 if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) return string.byteLength;
113122 if (typeof string !== "string") throw new TypeError('The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type ' + typeof string);
113123 var len = string.length;
113124 var mustMatch = arguments.length > 2 && arguments[2] === true;
113125 if (!mustMatch && len === 0) return 0;
113126 // Use a for loop to avoid recursion
113127 var loweredCase = false;
113128 for(;;)switch(encoding){
113129 case "ascii":
113130 case "latin1":
113131 case "binary":
113132 return len;
113133 case "utf8":
113134 case "utf-8":
113135 return utf8ToBytes(string).length;
113136 case "ucs2":
113137 case "ucs-2":
113138 case "utf16le":
113139 case "utf-16le":
113140 return len * 2;
113141 case "hex":
113142 return len >>> 1;
113143 case "base64":
113144 return base64ToBytes(string).length;
113145 default:
113146 if (loweredCase) return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8
113147 ;
113148 encoding = ("" + encoding).toLowerCase();
113149 loweredCase = true;
113150 }
113151}
113152Buffer.byteLength = byteLength;
113153function slowToString(encoding, start, end) {
113154 var loweredCase = false;

Callers 1

fromStringFunction · 0.70

Calls 4

isInstanceFunction · 0.70
utf8ToBytesFunction · 0.70
base64ToBytesFunction · 0.70
getLensFunction · 0.70

Tested by

no test coverage detected