-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecoder.js
More file actions
27 lines (24 loc) · 775 Bytes
/
decoder.js
File metadata and controls
27 lines (24 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'use strict'
var getIconv = require('./lib/getIconv.js')
var reg = /%([0-9A-F]{2})/ig
module.exports = function decoder (codec, useIconv) {
var iconvImpl = getIconv(useIconv)
return function (string) {
if (string.length === 0) {
return ''
}
var buffers = []
var lastIndex = 0
reg.lastIndex = 0
while (reg.test(string)) {
var charPos = reg.lastIndex - 3
buffers.push(new Buffer(string.substring(lastIndex, charPos)))
buffers.push(new Buffer([parseInt(string.substr(charPos + 1, 2), 16)]))
lastIndex = reg.lastIndex
}
if (lastIndex < string.length) {
buffers.push(new Buffer(string.substring(lastIndex, string.length)))
}
return iconvImpl.decode(Buffer.concat(buffers), codec).toString()
}
}