(string, encoding)
| 2261 | }; |
| 2262 | |
| 2263 | function 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 | } |
| 2306 | Buffer.byteLength = byteLength; |
| 2307 | |
| 2308 | function slowToString (encoding, start, end) { |
no test coverage detected