MCPcopy Index your code
hub / github.com/OneNoteDev/WebClipper / byteLength

Function byteLength

lib/sanitize-html.js:1001–1043  ·  view source on GitHub ↗
(string, encoding)

Source from the content-addressed store, hash-verified

999}
1000
1001function byteLength (string, encoding) {
1002 if (Buffer.isBuffer(string)) {
1003 return string.length
1004 }
1005 if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
1006 (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
1007 return string.byteLength
1008 }
1009 if (typeof string !== 'string') {
1010 string = '' + string
1011 }
1012
1013 var len = string.length
1014 if (len === 0) return 0
1015
1016 // Use a for loop to avoid recursion
1017 var loweredCase = false
1018 for (;;) {
1019 switch (encoding) {
1020 case 'ascii':
1021 case 'latin1':
1022 case 'binary':
1023 return len
1024 case 'utf8':
1025 case 'utf-8':
1026 case undefined:
1027 return utf8ToBytes(string).length
1028 case 'ucs2':
1029 case 'ucs-2':
1030 case 'utf16le':
1031 case 'utf-16le':
1032 return len * 2
1033 case 'hex':
1034 return len >>> 1
1035 case 'base64':
1036 return base64ToBytes(string).length
1037 default:
1038 if (loweredCase) return utf8ToBytes(string).length // assume utf8
1039 encoding = ('' + encoding).toLowerCase()
1040 loweredCase = true
1041 }
1042 }
1043}
1044Buffer.byteLength = byteLength
1045
1046function slowToString (encoding, start, end) {

Callers 1

fromStringFunction · 0.85

Calls 2

utf8ToBytesFunction · 0.85
base64ToBytesFunction · 0.85

Tested by

no test coverage detected