Skip to content

Commit 3a698cf

Browse files
committed
v2.1.0
- 文字コードの自動判定アルゴリズムを改善。
1 parent b0113ef commit 3a698cf

17 files changed

+627
-256
lines changed

HISTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# History
22

3+
## v2.1.0
4+
5+
### 機能改善
6+
- 文字コードの自動判定アルゴリズムを改善。
7+
38
## v2.0.0
49

510
### 仕様変更

build/mojijs.mjs.js build/esm/index.js

+115-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
/**
1212
* Unicode を扱うクラス
13+
*
14+
* 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
15+
* @deprecated
1316
*/
1417
class Unicode {
1518

@@ -587,6 +590,9 @@ class Unicode {
587590

588591
/**
589592
* Shift_JIS を扱うクラス
593+
*
594+
* 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
595+
* @deprecated
590596
*/
591597
class SJIS {
592598

@@ -595,6 +601,7 @@ class SJIS {
595601
* @param {String} text - 変換したいテキスト
596602
* @param {Object<number, number>} unicode_to_sjis - Unicode から Shift_JIS への変換マップ
597603
* @returns {{encode : Array<number>, ng_count : number}} Shift_JIS のデータが入った配列
604+
* @ignore
598605
*/
599606
static toSJISArray(text, unicode_to_sjis) {
600607
const map = unicode_to_sjis;
@@ -624,6 +631,7 @@ class SJIS {
624631
* @param {String} text - 変換したいテキスト
625632
* @param {Object<number, number>} unicode_to_sjis - Unicode から Shift_JIS への変換マップ
626633
* @returns {Array<number>} Shift_JIS のデータが入ったバイナリ配列
634+
* @ignore
627635
*/
628636
static toSJISBinary(text, unicode_to_sjis) {
629637
const sjis = SJIS.toSJISArray(text, unicode_to_sjis).encode;
@@ -645,6 +653,7 @@ class SJIS {
645653
* @param {Array<number>} sjis - 変換したいテキスト
646654
* @param {Object<number, number|Array<number>>} sjis_to_unicode - Shift_JIS から Unicode への変換マップ
647655
* @returns {{decode : String, ng_count : number}} 変換後のテキスト
656+
* @ignore
648657
*/
649658
static fromSJISArray(sjis, sjis_to_unicode) {
650659
const map = sjis_to_unicode;
@@ -704,6 +713,7 @@ class SJIS {
704713
* @param {String} text - カウントしたいテキスト
705714
* @param {Object<number, number>} unicode_to_sjis - Unicode から Shift_JIS への変換マップ
706715
* @returns {Number} 文字の横幅
716+
* @ignore
707717
*/
708718
static getWidthForSJIS(text, unicode_to_sjis) {
709719
return SJIS.toSJISBinary(text, unicode_to_sjis).length;
@@ -717,6 +727,7 @@ class SJIS {
717727
* @param {Object<number, number>} unicode_to_sjis - Unicode から Shift_JIS への変換マップ
718728
* @param {Object<number, number|Array<number>>} sjis_to_unicode - Shift_JIS から Unicode への変換マップ
719729
* @returns {String} 切り出したテキスト
730+
* @ignore
720731
*/
721732
static cutTextForSJIS(text, offset, size, unicode_to_sjis, sjis_to_unicode) {
722733
const sjisbin = SJIS.toSJISBinary(text, unicode_to_sjis);
@@ -786,6 +797,7 @@ class SJIS {
786797
* @param {Number} unicode_codepoint - Unicodeのコードポイント
787798
* @param {Object<number, number>} unicode_to_sjis - Unicode から Shift_JIS への変換マップ
788799
* @returns {Number} 符号化数値(変換できない場合はnullとなる)
800+
* @ignore
789801
*/
790802
static toSJISCodeFromUnicode(unicode_codepoint, unicode_to_sjis) {
791803
if(!unicode_to_sjis[unicode_codepoint]) {
@@ -879,6 +891,7 @@ class SJIS {
879891
* @param {Number} unicode_codepoint - Unicodeのコードポイント
880892
* @param {Object<number, number>} unicode_to_sjis - Unicode から Shift_JIS-2004 への変換マップ
881893
* @returns {MenKuTen} 面区点番号(存在しない場合(1バイトのJISコードなど)はnullを返す)
894+
* @ignore
882895
*/
883896
static toMenKuTenFromUnicode(unicode_codepoint, unicode_to_sjis) {
884897
if(!unicode_to_sjis[unicode_codepoint]) {
@@ -975,6 +988,7 @@ class SJIS {
975988
* @param {MenKuTen|string} menkuten - 面区点番号
976989
* @param {Object<number, number|Array<number>>} sjis_to_unicode - Shift_JIS-2004 から Unicode への変換マップ
977990
* @returns {Array<number>} UTF-32の配列(存在しない場合はnullを返す)
991+
* @ignore
978992
*/
979993
static toUnicodeCodeFromMenKuTen(menkuten, sjis_to_unicode) {
980994
const sjis_code = SJIS.toSJIS2004CodeFromMenKuTen(menkuten);
@@ -1055,6 +1069,7 @@ class SJIS {
10551069
* @param {Number} unicode_codepoint - Unicodeのコードポイント
10561070
* @param {Object<number, number>} unicode_to_sjis - Unicode から Shift_JIS への変換マップ
10571071
* @returns {Object} 面区点番号(存在しない場合(1バイトのJISコードなど)はnullを返す)
1072+
* @ignore
10581073
*/
10591074
static toKuTenFromUnicode(unicode_codepoint, unicode_to_sjis) {
10601075
if(!unicode_to_sjis[unicode_codepoint]) {
@@ -1081,6 +1096,7 @@ class SJIS {
10811096
* @param {MenKuTen|string} kuten - 区点番号
10821097
* @param {Object<number, number|Array<number>>} sjis_to_unicode - Shift_JIS-2004 から Unicode への変換マップ
10831098
* @returns {Array<number>} UTF-32の配列(存在しない場合はnullを返す)
1099+
* @ignore
10841100
*/
10851101
static toUnicodeCodeFromKuTen(kuten, sjis_to_unicode) {
10861102
const sjis_code = SJIS.toSJISCodeFromKuTen(kuten);
@@ -1162,6 +1178,7 @@ class SJIS {
11621178
* @param {Number} unicode_codepoint - Unicodeのコードポイント
11631179
* @param {Object<number, number>} unicode_to_sjis - Unicode から Shift_JIS への変換マップ
11641180
* @returns {Number} -1...変換不可, 0...水準なし, 1...第1水準, ...
1181+
* @ignore
11651182
*/
11661183
static toJISKanjiSuijunFromUnicode(unicode_codepoint, unicode_to_sjis) {
11671184
if(!unicode_to_sjis[unicode_codepoint]) {
@@ -2610,6 +2627,9 @@ CP932MAP.unicode_to_cp932_map = null;
26102627

26112628
/**
26122629
* CP932, Windows-31J を扱うクラス
2630+
*
2631+
* 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
2632+
* @deprecated
26132633
*/
26142634
class CP932 {
26152635

@@ -4256,6 +4276,9 @@ SJIS2004MAP.unicode_to_sjis2004_map = null;
42564276

42574277
/**
42584278
* Shift_JIS-2004 を扱うクラス
4279+
*
4280+
* 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
4281+
* @deprecated
42594282
*/
42604283
class SJIS2004 {
42614284

@@ -4438,6 +4461,9 @@ class EUCJPTools {
44384461

44394462
/**
44404463
* EUC-JP を扱うクラス
4464+
*
4465+
* 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
4466+
* @deprecated
44414467
*/
44424468
class EUCJP {
44434469

@@ -4533,6 +4559,49 @@ class EncodeTools {
45334559
return charset;
45344560
}
45354561

4562+
/**
4563+
* 同一の種別の文字列の重なりをカウントする
4564+
* @param {Array<number>} utf32_array
4565+
* @returns {number}
4566+
*/
4567+
static countWord(utf32_array) {
4568+
let count = 0;
4569+
let type = 0;
4570+
let old_type = -1;
4571+
for(let i = 0; i < utf32_array.length; i++) {
4572+
const ch = utf32_array[i];
4573+
// a-zA-Z
4574+
if(((0x41 <= ch) && (ch <= 0x5A)) || ((0x61 <= ch) && (ch <= 0x6A))) {
4575+
type = 1;
4576+
}
4577+
// 0-9
4578+
else if((0x30 <= ch) && (ch <= 0x39)) {
4579+
type = 2;
4580+
}
4581+
// ぁ-ん
4582+
else if((0x3041 <= ch) && (ch <= 0x3093)) {
4583+
type = 3;
4584+
}
4585+
// ァ-ン
4586+
else if((0x30A1 <= ch) && (ch <= 0x30F3)) {
4587+
type = 4;
4588+
}
4589+
// CJK統合漢字拡張A - CJK統合漢字, 追加漢字面
4590+
else if(((0x3400 <= ch) && (ch < 0xA000)) || ((0x20000 <= ch) && (ch < 0x2FA20))) {
4591+
type = 5;
4592+
}
4593+
else {
4594+
old_type = -1;
4595+
continue;
4596+
}
4597+
if(type === old_type) {
4598+
count++;
4599+
}
4600+
old_type = type;
4601+
}
4602+
return count;
4603+
}
4604+
45364605
}
45374606

45384607
/**
@@ -4605,28 +4674,46 @@ class Encode {
46054674
return Unicode.fromUTF32Array(ret);
46064675
}
46074676
}
4608-
// 有名な文字コードで試していく
4609-
// UTF-8
4677+
// 有名な文字コードで試す
4678+
let max_data = "";
4679+
let max_count = -1;
4680+
// Shift_JIS
46104681
{
4611-
const text = Unicode.fromUTF32Array(Unicode.toCodePointFromUTFBinary(binary, "utf-8"));
4612-
if(CP932.toCP932Array(text).ng_count === 0) {
4613-
return text;
4682+
const text = CP932.fromCP932Array(binary).decode;
4683+
const count = EncodeTools.countWord(Unicode.toUTF32Array(text));
4684+
if(max_count < count) {
4685+
max_data = text;
4686+
max_count = count;
46144687
}
46154688
}
4616-
// UTF-16LE
4689+
// EUC-JP-2004
46174690
{
4618-
const text = Unicode.fromUTF32Array(Unicode.toCodePointFromUTFBinary(binary, "utf-16"));
4619-
if(CP932.toCP932Array(text).ng_count === 0) {
4620-
return text;
4691+
const text = EUCJP.fromEUCJIS2004Binary(binary).decode;
4692+
const count = EncodeTools.countWord(Unicode.toUTF32Array(text));
4693+
if(max_count < count) {
4694+
max_data = text;
4695+
max_count = count;
46214696
}
46224697
}
4623-
// Shift_JIS
4698+
// UTF-8
4699+
{
4700+
const utf32 = Unicode.toCodePointFromUTFBinary(binary, "utf-8");
4701+
const count = EncodeTools.countWord(utf32);
4702+
if(max_count < count) {
4703+
max_data = Unicode.fromUTF32Array(utf32);
4704+
max_count = count;
4705+
}
4706+
}
4707+
// UTF-16LE
46244708
{
4625-
const text = CP932.fromCP932Array(binary);
4626-
if(text.ng_count === 0) {
4627-
return text.decode;
4709+
const utf32 = Unicode.toCodePointFromUTFBinary(binary, "utf-16");
4710+
const count = EncodeTools.countWord(utf32);
4711+
if(max_count < count) {
4712+
max_data = Unicode.fromUTF32Array(utf32);
4713+
max_count = count;
46284714
}
46294715
}
4716+
return max_data;
46304717
}
46314718
return null;
46324719
}
@@ -6953,39 +7040,54 @@ class MojiJS {
69537040

69547041
/**
69557042
* Unicode専用の内部関数を利用する
7043+
*
7044+
* 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
69567045
* @returns {typeof Unicode}
7046+
* @deprecated
69577047
*/
69587048
static get Unicode() {
69597049
return Unicode;
69607050
}
69617051

69627052
/**
69637053
* Shift_JIS専用の内部関数を利用する
7054+
*
7055+
* 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
69647056
* @returns {typeof SJIS}
7057+
* @deprecated
69657058
*/
69667059
static get SJIS() {
69677060
return SJIS;
69687061
}
69697062

69707063
/**
69717064
* CP932専用の内部関数を利用する
7065+
*
7066+
* 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
69727067
* @returns {typeof CP932}
7068+
* @deprecated
69737069
*/
69747070
static get CP932() {
69757071
return CP932;
69767072
}
69777073

69787074
/**
69797075
* Shift_JIS-2004専用の内部関数を利用する
7076+
*
7077+
* 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
69807078
* @returns {typeof SJIS2004}
7079+
* @deprecated
69817080
*/
69827081
static get SJIS2004() {
69837082
return SJIS2004;
69847083
}
69857084

69867085
/**
69877086
* EUC-JP専用の内部関数を利用する
7087+
*
7088+
* 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
69887089
* @returns {typeof EUCJP}
7090+
* @deprecated
69897091
*/
69907092
static get EUCJP() {
69917093
return EUCJP;

build/mojijs.d.ts build/index.d.ts

File renamed without changes.

build/mojijs.module.min.js build/mojijs.esm.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/mojijs.umd.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)