|
| 1 | +"use strict";; |
| 2 | +class SingleByteCodec { |
| 3 | + constructor(codes) { |
| 4 | + if (codes.length !== 128 && codes.length !== 256) { |
| 5 | + throw new Error("invalid codes passed to SingleByteCodec"); |
| 6 | + } |
| 7 | + |
| 8 | + if (codes.length === 128) { |
| 9 | + let asciiString = ""; |
| 10 | + for (var i = 0; i < 128; i++) |
| 11 | + asciiString += String.fromCharCode(i); |
| 12 | + codes = asciiString + codes; |
| 13 | + } |
| 14 | + |
| 15 | + this.encodeBuf = new Buffer(65536); |
| 16 | + this.encodeBuf.fill(0); |
| 17 | + |
| 18 | + // stored separately so that we can have a unmapped flag in encodeBuf |
| 19 | + this.zero = codes.charCodeAt(0); |
| 20 | + for (var i = 1; i < codes.length; i++) { |
| 21 | + //console.log(codes[i] + ": " + codes.charCodeAt(i) + ' -> ' + i); |
| 22 | + this.encodeBuf[codes.charCodeAt(i)] = i; |
| 23 | + } |
| 24 | + |
| 25 | + this.decodeBuf = Buffer.from(codes, 'ucs2'); |
| 26 | + } |
| 27 | + |
| 28 | + encode(str) { |
| 29 | + let buf = new Buffer(str.length); |
| 30 | + for (let i = 0; i < str.length; i++) { |
| 31 | + // TODO: surrogate check |
| 32 | + const code = str.charCodeAt(i); |
| 33 | + if (code === this.zero) { |
| 34 | + buf[i] = 0; |
| 35 | + } else { |
| 36 | + const encoded = this.encodeBuf[code]; |
| 37 | + if (encoded === 0) { |
| 38 | + console.log("unmapped character:", str[i]); |
| 39 | + throw new Error("unmapped character"); |
| 40 | + } else { |
| 41 | + buf[i] = encoded; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return buf; |
| 47 | + } |
| 48 | + |
| 49 | + decode(buf) { |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | + |
| 54 | +const windows1252 = new SingleByteCodec("€�‚ƒ„…†‡ˆ‰Š‹Œ�Ž��‘’“”•–—˜™š›œ�žŸ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"); |
| 55 | + |
| 56 | +//console.log(windows1252.encode("test")); |
| 57 | +//console.log(windows1252.encode("test: ☃")); |
| 58 | + |
| 59 | +module.exports.windows1252 = windows1252; |
0 commit comments