10
10
11
11
/**
12
12
* Unicode を扱うクラス
13
+ *
14
+ * 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
15
+ * @deprecated
13
16
*/
14
17
class Unicode {
15
18
@@ -587,6 +590,9 @@ class Unicode {
587
590
588
591
/**
589
592
* Shift_JIS を扱うクラス
593
+ *
594
+ * 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
595
+ * @deprecated
590
596
*/
591
597
class SJIS {
592
598
@@ -595,6 +601,7 @@ class SJIS {
595
601
* @param {String } text - 変換したいテキスト
596
602
* @param {Object<number, number> } unicode_to_sjis - Unicode から Shift_JIS への変換マップ
597
603
* @returns {{encode : Array<number>, ng_count : number} } Shift_JIS のデータが入った配列
604
+ * @ignore
598
605
*/
599
606
static toSJISArray ( text , unicode_to_sjis ) {
600
607
const map = unicode_to_sjis ;
@@ -624,6 +631,7 @@ class SJIS {
624
631
* @param {String } text - 変換したいテキスト
625
632
* @param {Object<number, number> } unicode_to_sjis - Unicode から Shift_JIS への変換マップ
626
633
* @returns {Array<number> } Shift_JIS のデータが入ったバイナリ配列
634
+ * @ignore
627
635
*/
628
636
static toSJISBinary ( text , unicode_to_sjis ) {
629
637
const sjis = SJIS . toSJISArray ( text , unicode_to_sjis ) . encode ;
@@ -645,6 +653,7 @@ class SJIS {
645
653
* @param {Array<number> } sjis - 変換したいテキスト
646
654
* @param {Object<number, number|Array<number>> } sjis_to_unicode - Shift_JIS から Unicode への変換マップ
647
655
* @returns {{decode : String, ng_count : number} } 変換後のテキスト
656
+ * @ignore
648
657
*/
649
658
static fromSJISArray ( sjis , sjis_to_unicode ) {
650
659
const map = sjis_to_unicode ;
@@ -704,6 +713,7 @@ class SJIS {
704
713
* @param {String } text - カウントしたいテキスト
705
714
* @param {Object<number, number> } unicode_to_sjis - Unicode から Shift_JIS への変換マップ
706
715
* @returns {Number } 文字の横幅
716
+ * @ignore
707
717
*/
708
718
static getWidthForSJIS ( text , unicode_to_sjis ) {
709
719
return SJIS . toSJISBinary ( text , unicode_to_sjis ) . length ;
@@ -717,6 +727,7 @@ class SJIS {
717
727
* @param {Object<number, number> } unicode_to_sjis - Unicode から Shift_JIS への変換マップ
718
728
* @param {Object<number, number|Array<number>> } sjis_to_unicode - Shift_JIS から Unicode への変換マップ
719
729
* @returns {String } 切り出したテキスト
730
+ * @ignore
720
731
*/
721
732
static cutTextForSJIS ( text , offset , size , unicode_to_sjis , sjis_to_unicode ) {
722
733
const sjisbin = SJIS . toSJISBinary ( text , unicode_to_sjis ) ;
@@ -786,6 +797,7 @@ class SJIS {
786
797
* @param {Number } unicode_codepoint - Unicodeのコードポイント
787
798
* @param {Object<number, number> } unicode_to_sjis - Unicode から Shift_JIS への変換マップ
788
799
* @returns {Number } 符号化数値(変換できない場合はnullとなる)
800
+ * @ignore
789
801
*/
790
802
static toSJISCodeFromUnicode ( unicode_codepoint , unicode_to_sjis ) {
791
803
if ( ! unicode_to_sjis [ unicode_codepoint ] ) {
@@ -879,6 +891,7 @@ class SJIS {
879
891
* @param {Number } unicode_codepoint - Unicodeのコードポイント
880
892
* @param {Object<number, number> } unicode_to_sjis - Unicode から Shift_JIS-2004 への変換マップ
881
893
* @returns {MenKuTen } 面区点番号(存在しない場合(1バイトのJISコードなど)はnullを返す)
894
+ * @ignore
882
895
*/
883
896
static toMenKuTenFromUnicode ( unicode_codepoint , unicode_to_sjis ) {
884
897
if ( ! unicode_to_sjis [ unicode_codepoint ] ) {
@@ -975,6 +988,7 @@ class SJIS {
975
988
* @param {MenKuTen|string } menkuten - 面区点番号
976
989
* @param {Object<number, number|Array<number>> } sjis_to_unicode - Shift_JIS-2004 から Unicode への変換マップ
977
990
* @returns {Array<number> } UTF-32の配列(存在しない場合はnullを返す)
991
+ * @ignore
978
992
*/
979
993
static toUnicodeCodeFromMenKuTen ( menkuten , sjis_to_unicode ) {
980
994
const sjis_code = SJIS . toSJIS2004CodeFromMenKuTen ( menkuten ) ;
@@ -1055,6 +1069,7 @@ class SJIS {
1055
1069
* @param {Number } unicode_codepoint - Unicodeのコードポイント
1056
1070
* @param {Object<number, number> } unicode_to_sjis - Unicode から Shift_JIS への変換マップ
1057
1071
* @returns {Object } 面区点番号(存在しない場合(1バイトのJISコードなど)はnullを返す)
1072
+ * @ignore
1058
1073
*/
1059
1074
static toKuTenFromUnicode ( unicode_codepoint , unicode_to_sjis ) {
1060
1075
if ( ! unicode_to_sjis [ unicode_codepoint ] ) {
@@ -1081,6 +1096,7 @@ class SJIS {
1081
1096
* @param {MenKuTen|string } kuten - 区点番号
1082
1097
* @param {Object<number, number|Array<number>> } sjis_to_unicode - Shift_JIS-2004 から Unicode への変換マップ
1083
1098
* @returns {Array<number> } UTF-32の配列(存在しない場合はnullを返す)
1099
+ * @ignore
1084
1100
*/
1085
1101
static toUnicodeCodeFromKuTen ( kuten , sjis_to_unicode ) {
1086
1102
const sjis_code = SJIS . toSJISCodeFromKuTen ( kuten ) ;
@@ -1162,6 +1178,7 @@ class SJIS {
1162
1178
* @param {Number } unicode_codepoint - Unicodeのコードポイント
1163
1179
* @param {Object<number, number> } unicode_to_sjis - Unicode から Shift_JIS への変換マップ
1164
1180
* @returns {Number } -1...変換不可, 0...水準なし, 1...第1水準, ...
1181
+ * @ignore
1165
1182
*/
1166
1183
static toJISKanjiSuijunFromUnicode ( unicode_codepoint , unicode_to_sjis ) {
1167
1184
if ( ! unicode_to_sjis [ unicode_codepoint ] ) {
@@ -2610,6 +2627,9 @@ CP932MAP.unicode_to_cp932_map = null;
2610
2627
2611
2628
/**
2612
2629
* CP932, Windows-31J を扱うクラス
2630
+ *
2631
+ * 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
2632
+ * @deprecated
2613
2633
*/
2614
2634
class CP932 {
2615
2635
@@ -4256,6 +4276,9 @@ SJIS2004MAP.unicode_to_sjis2004_map = null;
4256
4276
4257
4277
/**
4258
4278
* Shift_JIS-2004 を扱うクラス
4279
+ *
4280
+ * 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
4281
+ * @deprecated
4259
4282
*/
4260
4283
class SJIS2004 {
4261
4284
@@ -4438,6 +4461,9 @@ class EUCJPTools {
4438
4461
4439
4462
/**
4440
4463
* EUC-JP を扱うクラス
4464
+ *
4465
+ * 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
4466
+ * @deprecated
4441
4467
*/
4442
4468
class EUCJP {
4443
4469
@@ -4533,6 +4559,49 @@ class EncodeTools {
4533
4559
return charset ;
4534
4560
}
4535
4561
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
+
4536
4605
}
4537
4606
4538
4607
/**
@@ -4605,28 +4674,46 @@ class Encode {
4605
4674
return Unicode . fromUTF32Array ( ret ) ;
4606
4675
}
4607
4676
}
4608
- // 有名な文字コードで試していく
4609
- // UTF-8
4677
+ // 有名な文字コードで試す
4678
+ let max_data = "" ;
4679
+ let max_count = - 1 ;
4680
+ // Shift_JIS
4610
4681
{
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 ;
4614
4687
}
4615
4688
}
4616
- // UTF-16LE
4689
+ // EUC-JP-2004
4617
4690
{
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 ;
4621
4696
}
4622
4697
}
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
4624
4708
{
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 ;
4628
4714
}
4629
4715
}
4716
+ return max_data ;
4630
4717
}
4631
4718
return null ;
4632
4719
}
@@ -6953,39 +7040,54 @@ class MojiJS {
6953
7040
6954
7041
/**
6955
7042
* Unicode専用の内部関数を利用する
7043
+ *
7044
+ * 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
6956
7045
* @returns {typeof Unicode }
7046
+ * @deprecated
6957
7047
*/
6958
7048
static get Unicode ( ) {
6959
7049
return Unicode ;
6960
7050
}
6961
7051
6962
7052
/**
6963
7053
* Shift_JIS専用の内部関数を利用する
7054
+ *
7055
+ * 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
6964
7056
* @returns {typeof SJIS }
7057
+ * @deprecated
6965
7058
*/
6966
7059
static get SJIS ( ) {
6967
7060
return SJIS ;
6968
7061
}
6969
7062
6970
7063
/**
6971
7064
* CP932専用の内部関数を利用する
7065
+ *
7066
+ * 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
6972
7067
* @returns {typeof CP932 }
7068
+ * @deprecated
6973
7069
*/
6974
7070
static get CP932 ( ) {
6975
7071
return CP932 ;
6976
7072
}
6977
7073
6978
7074
/**
6979
7075
* Shift_JIS-2004専用の内部関数を利用する
7076
+ *
7077
+ * 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
6980
7078
* @returns {typeof SJIS2004 }
7079
+ * @deprecated
6981
7080
*/
6982
7081
static get SJIS2004 ( ) {
6983
7082
return SJIS2004 ;
6984
7083
}
6985
7084
6986
7085
/**
6987
7086
* EUC-JP専用の内部関数を利用する
7087
+ *
7088
+ * 内部処理用の関数のため変更する可能性が高く、直接利用することをお勧めしません。
6988
7089
* @returns {typeof EUCJP }
7090
+ * @deprecated
6989
7091
*/
6990
7092
static get EUCJP ( ) {
6991
7093
return EUCJP ;
0 commit comments