return buf
}
-Buffer.byteLength = function byteLength (str, encoding) {
- var ret
- str = str + ''
+Buffer.byteLength = function byteLength (string, encoding) {
+ if (typeof string !== 'string') string = String(string)
+
+ if (string.length === 0) return 0
+
switch (encoding || 'utf8') {
case 'ascii':
case 'binary':
case 'raw':
- ret = str.length
- break
+ return string.length
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
- ret = str.length * 2
- break
+ return string.length * 2
case 'hex':
- ret = str.length >>> 1
- break
+ return string.length >>> 1
case 'utf8':
case 'utf-8':
- ret = utf8ToBytes(str).length
- break
+ return utf8ToBytes(string).length
case 'base64':
- ret = base64ToBytes(str).length
- break
+ return base64ToBytes(string).length
default:
- ret = str.length
+ return string.length
}
- return ret
}
// pre-set for values that may exist in the future