1- import { parse , type Range } from '../utils/content-tag.js' ;
1+ import { getBuffer , parse , type Range } from '../utils/content-tag.js' ;
22
33export interface Template {
44 contents : string ;
@@ -10,19 +10,8 @@ export interface Template {
1010 } ;
1111}
1212
13- const BufferMap : Map < string , Buffer > = new Map ( ) ;
14-
1513const PLACEHOLDER = '~' ;
1614
17- function getBuffer ( s : string ) : Buffer {
18- let buf = BufferMap . get ( s ) ;
19- if ( ! buf ) {
20- buf = Buffer . from ( s ) ;
21- BufferMap . set ( s , buf ) ;
22- }
23- return buf ;
24- }
25-
2615/** Slice string using byte range */
2716function sliceByteRange ( s : string , a : number , b ?: number ) : string {
2817 const buf = getBuffer ( s ) ;
@@ -35,11 +24,6 @@ function byteToCharIndex(s: string, byteOffset: number): number {
3524 return buf . subarray ( 0 , byteOffset ) . toString ( ) . length ;
3625}
3726
38- /** Calculate byte length */
39- function byteLength ( s : string ) : number {
40- return getBuffer ( s ) . length ;
41- }
42-
4327function replaceRange (
4428 s : string ,
4529 start : number ,
@@ -84,7 +68,7 @@ export function preprocessTemplateRange(
8468
8569 const tplLength = template . range . end - template . range . start ;
8670 const spaces =
87- tplLength - byteLength ( content ) - prefix . length - suffix . length ;
71+ tplLength - getBuffer ( content ) . length - prefix . length - suffix . length ;
8872 const total = prefix + content + ' ' . repeat ( spaces ) + suffix ;
8973
9074 return replaceRange ( code , template . range . start , template . range . end , total ) ;
0 commit comments