@@ -46,6 +46,12 @@ lazy_static! {
4646 Strategery :: new( r"[\p{CJK_N}\s)】」”’》][\-]" , r"[\p{CJK_N}“‘]" ) ,
4747 ] ;
4848
49+ static ref DOLLAR_STRATEGIES : Vec <Strategery > = vec![
50+ // Add space before and after dollar $ near the CJK
51+ Strategery :: new( r"\p{CJK}" , r"\$" ) ,
52+ Strategery :: new( r"\$" , r"\p{CJK}" ) ,
53+ ] ;
54+
4955 static ref NO_SPACE_FULLWIDTH_STRATEGIES : Vec <Strategery > = vec![
5056 // FullwidthPunctuation remove space case, Fullwidth can safe to remove spaces
5157 Strategery :: new( r"\w|\p{CJK}|`" , r"[,。、!?:;()「」《》【】]" ) . with_remove_space( ) . with_reverse( ) ,
@@ -102,6 +108,16 @@ pub fn format_space_backticks(input: &str) -> Cow<str> {
102108 } )
103109}
104110
111+ pub fn format_space_dollar ( input : & str ) -> Cow < str > {
112+ DOLLAR_STRATEGIES
113+ . iter ( )
114+ . fold ( Cow :: Borrowed ( input) , |text, strategy| match text {
115+ Cow :: Borrowed ( s) => strategy. format ( s) ,
116+ Cow :: Owned ( s) => Cow :: Owned ( strategy. format ( & s) . into_owned ( ) ) ,
117+ } ,
118+ )
119+ }
120+
105121pub fn format_no_space_fullwidth ( input : & str ) -> Cow < str > {
106122 if !CJK_RE . is_match ( input) {
107123 return Cow :: Borrowed ( input) ;
@@ -130,7 +146,7 @@ pub fn format_no_space_fullwidth_quote(input: &str) -> Cow<str> {
130146
131147#[ cfg( test) ]
132148mod tests {
133- use crate :: rule:: word:: { format_space_backticks, format_space_bracket, format_space_dash} ;
149+ use crate :: rule:: word:: { format_space_backticks, format_space_bracket, format_space_dash, format_space_dollar } ;
134150
135151 #[ test]
136152 fn test_format_space_dash ( ) {
@@ -167,4 +183,26 @@ mod tests {
167183 assert_eq ! ( format_space_backticks( "`代码第1行" ) , "`代码第1行" ) ;
168184 assert_eq ! ( format_space_backticks( "代码第2行`" ) , "代码第2行`" ) ;
169185 }
186+
187+ #[ test]
188+ fn test_format_space_dollar ( ) {
189+ assert_eq ! ( format_space_dollar( "你好$世界" ) , "你好 $ 世界" ) ;
190+ assert_eq ! ( format_space_dollar( "hello$世界" ) , "hello$ 世界" ) ;
191+ assert_eq ! ( format_space_dollar( "你好$world" ) , "你好 $world" ) ;
192+ assert_eq ! ( format_space_dollar( "你好$x$世界" ) , "你好 $x$ 世界" ) ;
193+ assert_eq ! ( format_space_dollar( "变量 $x$ 代表" ) , "变量 $x$ 代表" ) ;
194+ assert_eq ! ( format_space_dollar( "令$x^2+y^2=z^2$,可得" ) , "令 $x^2+y^2=z^2$,可得" ) ;
195+ assert_eq ! ( format_space_dollar( "这是一个例子:$E=mc^2$。" ) , "这是一个例子:$E=mc^2$。" ) ;
196+ assert_eq ! ( format_space_dollar( "$x+y$是方程" ) , "$x+y$ 是方程" ) ;
197+ assert_eq ! ( format_space_dollar( "方程为$x+y=1$" ) , "方程为 $x+y=1$" ) ;
198+ assert_eq ! ( format_space_dollar( "若$x>0$且$y<0$" ) , "若 $x>0$ 且 $y<0$" ) ;
199+ assert_eq ! ( format_space_dollar( "函数$f(x)$的极值" ) , "函数 $f(x)$ 的极值" ) ;
200+ assert_eq ! ( format_space_dollar( "变数$x$、$y$满足" ) , "变数 $x$、$y$ 满足" ) ;
201+ assert_eq ! ( format_space_dollar( "公式$$E=mc^2$$证明了" ) , "公式 $$E=mc^2$$ 证明了" ) ;
202+ assert_eq ! ( format_space_dollar( "矩阵$A=\\ begin{bmatrix}1&0\\ \\ 0&1\\ end{bmatrix}$满足" ) , "矩阵 $A=\\ begin{bmatrix}1&0\\ \\ 0&1\\ end{bmatrix}$ 满足" ) ;
203+ assert_eq ! ( format_space_dollar( "测试$x_1,x_2$以及$x_3$" ) , "测试 $x_1,x_2$ 以及 $x_3$" ) ;
204+ assert_eq ! ( format_space_dollar( "若$a>b$则有$c>d$" ) , "若 $a>b$ 则有 $c>d$" ) ;
205+ assert_eq ! ( format_space_dollar( "设$a,b∈\\ mathbb{R}$且$a>b$" ) , "设 $a,b∈\\ mathbb{R}$ 且 $a>b$" ) ;
206+ assert_eq ! ( format_space_dollar( "下式成立:$$f(x)=x^2$$" ) , "下式成立:$$f(x)=x^2$$" ) ;
207+ }
170208}
0 commit comments