major style changes for rest of the files

This commit is contained in:
Dmitry Shirokov 2013-11-22 15:37:41 +11:00
parent e5ba182d7f
commit 1a7da87ec4
2 changed files with 64 additions and 64 deletions

122
index.js
View File

@ -1,85 +1,85 @@
var fs = require('fs'); var fs = require('fs');
var utf8 = require('./encoding/utf8'), var utf8 = require('./encoding/utf8'),
unicode = require('./encoding/unicode'), unicode = require('./encoding/unicode'),
mbcs = require('./encoding/mbcs'), mbcs = require('./encoding/mbcs'),
sbcs = require('./encoding/sbcs'), sbcs = require('./encoding/sbcs'),
iso2022 = require('./encoding/iso2022'); iso2022 = require('./encoding/iso2022');
var self = this; var self = this;
var recognisers = [ var recognisers = [
new utf8, new utf8,
new unicode.UTF_16BE, new unicode.UTF_16BE,
new unicode.UTF_16LE, new unicode.UTF_16LE,
new unicode.UTF_32BE, new unicode.UTF_32BE,
new unicode.UTF_32LE, new unicode.UTF_32LE,
new mbcs.sjis, new mbcs.sjis,
new mbcs.big5, new mbcs.big5,
new mbcs.euc_jp, new mbcs.euc_jp,
new mbcs.euc_kr, new mbcs.euc_kr,
new mbcs.gb_18030, new mbcs.gb_18030,
new iso2022.ISO_2022_JP, new iso2022.ISO_2022_JP,
new iso2022.ISO_2022_KR, new iso2022.ISO_2022_KR,
new iso2022.ISO_2022_CN, new iso2022.ISO_2022_CN,
new sbcs.ISO_8859_1, new sbcs.ISO_8859_1,
new sbcs.ISO_8859_2, new sbcs.ISO_8859_2,
new sbcs.ISO_8859_5, new sbcs.ISO_8859_5,
new sbcs.ISO_8859_6, new sbcs.ISO_8859_6,
new sbcs.ISO_8859_7, new sbcs.ISO_8859_7,
new sbcs.ISO_8859_8, new sbcs.ISO_8859_8,
new sbcs.ISO_8859_9, new sbcs.ISO_8859_9,
new sbcs.windows_1251, new sbcs.windows_1251,
new sbcs.windows_1256, new sbcs.windows_1256,
new sbcs.KOI8_R new sbcs.KOI8_R
]; ];
module.exports.detect = function(buffer) { module.exports.detect = function(buffer) {
// Tally up the byte occurence statistics. // Tally up the byte occurence statistics.
var fByteStats = []; var fByteStats = [];
for (var i = 0; i < 256; i++) for (var i = 0; i < 256; i++)
fByteStats[i] = 0; fByteStats[i] = 0;
for (var i = buffer.length - 1; i >= 0; i--) for (var i = buffer.length - 1; i >= 0; i--)
fByteStats[buffer[i] & 0x00ff]++; fByteStats[buffer[i] & 0x00ff]++;
var fC1Bytes = false; var fC1Bytes = false;
for (var i = 0x80; i <= 0x9F; i += 1) { for (var i = 0x80; i <= 0x9F; i += 1) {
if (fByteStats[i] != 0) { if (fByteStats[i] != 0) {
fC1Bytes = true; fC1Bytes = true;
break; break;
}
} }
}
var context = { var context = {
fByteStats: fByteStats, fByteStats: fByteStats,
fC1Bytes: fC1Bytes, fC1Bytes: fC1Bytes,
fRawInput: buffer, fRawInput: buffer,
fRawLength: buffer.length, fRawLength: buffer.length,
fInputBytes: buffer, fInputBytes: buffer,
fInputLen: buffer.length fInputLen: buffer.length
}; };
var match = recognisers.map(function(rec) { var match = recognisers.map(function(rec) {
return rec.match(context); return rec.match(context);
}).filter(function(match) { }).filter(function(match) {
return !!match; return !!match;
}).sort(function(a, b) { }).sort(function(a, b) {
return a.confidence - b.confidence; return a.confidence - b.confidence;
}).pop(); }).pop();
return match ? match.name : null; return match ? match.name : null;
}; };
module.exports.detectFile = function(filepath, fn) { module.exports.detectFile = function(filepath, fn) {
fs.readFile(filepath, function(err, res) { fs.readFile(filepath, function(err, res) {
if (err) return fn(err, null); if (err) return fn(err, null);
fn(null, self.detect(res)); fn(null, self.detect(res));
}); });
}; };
module.exports.detectFileSync = function(filepath) { module.exports.detectFileSync = function(filepath) {
return self.detect(fs.readFileSync(filepath)); return self.detect(fs.readFileSync(filepath));
}; };

View File

@ -1,6 +1,6 @@
module.exports = function(det, rec, confidence, name, lang) { module.exports = function(det, rec, confidence, name, lang) {
this.confidence = confidence; this.confidence = confidence;
this.name = name || rec.name(det); this.name = name || rec.name(det);
this.lang = lang; this.lang = lang;
}; };