@@ -54,8 +54,8 @@ describe("estimateStringChars", () => {
5454 } ) ;
5555
5656 it ( "handles CJK punctuation and symbols in the extended range" , ( ) => {
57- // "⺀" (U+2E80) is in CJK Radicals Supplement range
58- expect ( estimateStringChars ( "⺀" ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE ) ;
57+ // "⺀" (U+2E80) is a rare radical that current tokenizers encode as 3 tokens.
58+ expect ( estimateStringChars ( "⺀" ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 3 ) ;
5959 } ) ;
6060
6161 it ( "does not inflate standard Latin characters" , ( ) => {
@@ -68,15 +68,56 @@ describe("estimateStringChars", () => {
6868 expect ( estimateStringChars ( text ) ) . toBe ( text . length ) ;
6969 } ) ;
7070
71- it ( "counts CJK Extension B characters as one code point " , ( ) => {
71+ it ( "weights CJK Extension B characters for their measured token cost " , ( ) => {
7272 // "𠀀" (U+20000) is represented as a surrogate pair in UTF-16.
73- // Result = 1 + 1 * 3 = 4 (exactly CHARS_PER_TOKEN_ESTIMATE)
74- expect ( estimateStringChars ( "𠀀" ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE ) ;
73+ expect ( estimateStringChars ( "𠀀" ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 4 ) ;
7574 } ) ;
7675
77- it ( "handles mixed BMP and Extension B CJK consistently" , ( ) => {
78- // 3 CJK code points total: 你 + 𠀀 + 好 => 3 * 4 = 12
79- expect ( estimateStringChars ( "你𠀀好" ) ) . toBe ( 12 ) ;
76+ it ( "handles mixed BMP and Extension B CJK weights" , ( ) => {
77+ expect ( estimateStringChars ( "你𠀀好" ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 6 ) ;
78+ } ) ;
79+
80+ it ( "weights halfwidth Japanese, halfwidth Hangul, and supplementary CJK" , ( ) => {
81+ expect ( estimateStringChars ( "コンニチハ" ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 10 ) ;
82+ expect ( estimateStringChars ( String . fromCodePoint ( 0xffa1 ) ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 2 ) ;
83+ expect ( estimateStringChars ( String . fromCodePoint ( 0x30000 ) ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 4 ) ;
84+ } ) ;
85+
86+ it ( "weights decomposed Hangul and compatibility forms" , ( ) => {
87+ const decomposedHangul = "안녕하세요" . normalize ( "NFD" ) ;
88+ expect ( estimateStringChars ( decomposedHangul ) ) . toBe (
89+ decomposedHangul . length * CHARS_PER_TOKEN_ESTIMATE * 3 ,
90+ ) ;
91+ expect ( estimateStringChars ( String . fromCodePoint ( 0xa960 ) ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 3 ) ;
92+ expect ( estimateStringChars ( String . fromCodePoint ( 0xd7b0 ) ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 3 ) ;
93+ expect ( estimateStringChars ( String . fromCodePoint ( 0xfe10 ) ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 2 ) ;
94+ expect ( estimateStringChars ( String . fromCodePoint ( 0xffe0 ) ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 2 ) ;
95+ } ) ;
96+
97+ it . each ( [ 0x2e80 , 0x3400 , 0x9fff , 0xa000 , 0xf900 ] ) (
98+ "weights rare BMP CJK U+%s conservatively" ,
99+ ( codePoint ) => {
100+ expect ( estimateStringChars ( String . fromCodePoint ( codePoint ) ) ) . toBe (
101+ CHARS_PER_TOKEN_ESTIMATE * 3 ,
102+ ) ;
103+ } ,
104+ ) ;
105+
106+ it . each ( [ 0x16fe3 , 0x1aff0 , 0x1b001 , 0x1b11f , 0x1b132 , 0x1f200 ] ) (
107+ "weights supplementary CJK U+%s conservatively" ,
108+ ( codePoint ) => {
109+ expect ( estimateStringChars ( String . fromCodePoint ( codePoint ) ) ) . toBe (
110+ CHARS_PER_TOKEN_ESTIMATE * 4 ,
111+ ) ;
112+ } ,
113+ ) ;
114+
115+ it ( "covers CJK script-extension marks with measured weights" , ( ) => {
116+ expect ( estimateStringChars ( String . fromCodePoint ( 0x00b7 ) ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE ) ;
117+ expect ( estimateStringChars ( "·" . repeat ( 32 ) ) ) . toBe ( 32 * CHARS_PER_TOKEN_ESTIMATE ) ;
118+ expect ( estimateStringChars ( String . fromCodePoint ( 0x02ca ) ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 2 ) ;
119+ expect ( estimateStringChars ( String . fromCodePoint ( 0xa700 ) ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 3 ) ;
120+ expect ( estimateStringChars ( String . fromCodePoint ( 0x1d360 ) ) ) . toBe ( CHARS_PER_TOKEN_ESTIMATE * 3 ) ;
80121 } ) ;
81122
82123 it ( "does not collapse non-CJK surrogate pairs like emoji" , ( ) => {
0 commit comments