MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / byteLength

Function byteLength

lib/models/dav.js:2263–2305  ·  view source on GitHub ↗
(string, encoding)

Source from the content-addressed store, hash-verified

2261};
2262
2263function byteLength (string, encoding) {
2264 if (internalIsBuffer(string)) {
2265 return string.length
2266 }
2267 if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
2268 (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
2269 return string.byteLength
2270 }
2271 if (typeof string !== 'string') {
2272 string = '' + string;
2273 }
2274
2275 var len = string.length;
2276 if (len === 0) return 0
2277
2278 // Use a for loop to avoid recursion
2279 var loweredCase = false;
2280 for (;;) {
2281 switch (encoding) {
2282 case 'ascii':
2283 case 'latin1':
2284 case 'binary':
2285 return len
2286 case 'utf8':
2287 case 'utf-8':
2288 case undefined:
2289 return utf8ToBytes(string).length
2290 case 'ucs2':
2291 case 'ucs-2':
2292 case 'utf16le':
2293 case 'utf-16le':
2294 return len * 2
2295 case 'hex':
2296 return len >>> 1
2297 case 'base64':
2298 return base64ToBytes(string).length
2299 default:
2300 if (loweredCase) return utf8ToBytes(string).length // assume utf8
2301 encoding = ('' + encoding).toLowerCase();
2302 loweredCase = true;
2303 }
2304 }
2305}
2306Buffer.byteLength = byteLength;
2307
2308function slowToString (encoding, start, end) {

Callers 1

fromStringFunction · 0.85

Calls 3

internalIsBufferFunction · 0.85
utf8ToBytesFunction · 0.85
base64ToBytesFunction · 0.85

Tested by

no test coverage detected