react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / node_modules / commoner / node_modules / iconv-lite / encodings / sbcs-codec.js
81169 views"use strict"12// Single-byte codec. Needs a 'chars' string parameter that contains 256 or 128 chars that3// correspond to encoded bytes (if 128 - then lower half is ASCII).45exports._sbcs = SBCSCodec;6function SBCSCodec(codecOptions, iconv) {7if (!codecOptions)8throw new Error("SBCS codec is called without the data.")910// Prepare char buffer for decoding.11if (!codecOptions.chars || (codecOptions.chars.length !== 128 && codecOptions.chars.length !== 256))12throw new Error("Encoding '"+codecOptions.type+"' has incorrect 'chars' (must be of len 128 or 256)");1314if (codecOptions.chars.length === 128) {15var asciiString = "";16for (var i = 0; i < 128; i++)17asciiString += String.fromCharCode(i);18codecOptions.chars = asciiString + codecOptions.chars;19}2021this.decodeBuf = new Buffer(codecOptions.chars, 'ucs2');2223// Encoding buffer.24var encodeBuf = new Buffer(65536);25encodeBuf.fill(iconv.defaultCharSingleByte.charCodeAt(0));2627for (var i = 0; i < codecOptions.chars.length; i++)28encodeBuf[codecOptions.chars.charCodeAt(i)] = i;2930this.encodeBuf = encodeBuf;31}3233SBCSCodec.prototype.encoder = SBCSEncoder;34SBCSCodec.prototype.decoder = SBCSDecoder;353637function SBCSEncoder(options, codec) {38this.encodeBuf = codec.encodeBuf;39}4041SBCSEncoder.prototype.write = function(str) {42var buf = new Buffer(str.length);43for (var i = 0; i < str.length; i++)44buf[i] = this.encodeBuf[str.charCodeAt(i)];4546return buf;47}4849SBCSEncoder.prototype.end = function() {50}515253function SBCSDecoder(options, codec) {54this.decodeBuf = codec.decodeBuf;55}5657SBCSDecoder.prototype.write = function(buf) {58// Strings are immutable in JS -> we use ucs2 buffer to speed up computations.59var decodeBuf = this.decodeBuf;60var newBuf = new Buffer(buf.length*2);61var idx1 = 0, idx2 = 0;62for (var i = 0; i < buf.length; i++) {63idx1 = buf[i]*2; idx2 = i*2;64newBuf[idx2] = decodeBuf[idx1];65newBuf[idx2+1] = decodeBuf[idx1+1];66}67return newBuf.toString('ucs2');68}6970SBCSDecoder.prototype.end = function() {71}727374